PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000220. Tìm búp bê thiên thần gần nhất (Angel Doll / SJY Placing Chess Pieces)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tìm búp bê thiên thần gần nhất (Angel Doll / SJY Placing Chess Pieces)

Angel Doll / SJY Placing Chess Pieces

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

Seven years ago, Ayu received an angel doll and buried it as a time capsule. She can no longer remember its exact location, but remembers some possible locations in her town.

Model the town as a Cartesian plane, and represent each location by nonnegative integer coordinates (x,y)(x,y). Initially Ayu remembers nn possible locations. She then performs mm operations in order:

  • 1 x y: remember another possible location (x,y)(x,y). It is added to the known locations and remains available for every later operation.
  • 2 x y: at location (x,y)(x,y), ask for the minimum distance to any location currently known.

Ayu moves only parallel to the coordinate axes, so the distance between (x1,y1)(x_1,y_1) and (x2,y2)(x_2,y_2) is ∣x1−x2∣+∣y1−y2∣|x_1-x_2|+|y_1-y_2| (Manhattan distance).

Answer all type 2 queries in order. Duplicate point coordinates are allowed and do not change the nearest distance.

Input

  • The first line contains two integers n,mn,m: the number of initially known locations and the number of operations.
  • Each of the next nn lines contains two integers xi,yix_i,y_i, the coordinates of one initially known location.
  • Each of the following mm lines contains three integers t,x,yt,x,y: operation type 1 adds point (x,y)(x,y), and operation type 2 asks for the smallest distance from (x,y)(x,y) to any currently known point.

Output

For every type 2 operation, print one nonnegative integer: the minimum Manhattan distance to a point known at the time of that operation. Print nothing for type 1 operations.

Subtasks

  • Subtask 1 (20 points): 1≤n,m≤121\le n,m\le 12; 0≤xi,yi,x,y≤1060\le x_i,y_i,x,y\le10^6 for every initial point and operation; t∈{1,2}t\in\{1,2\}; inserted points remain forever.
  • Subtask 2 (30 points): 1≤n,m≤15001\le n,m\le 1500; 0≤xi,yi,x,y≤1060\le x_i,y_i,x,y\le10^6 for every initial point and operation; t∈{1,2}t\in\{1,2\}; inserted points remain forever.
  • Subtask 3 (50 points): 1≤n,m≤3⋅1051\le n,m\le3\cdot10^5; 0≤xi,yi,x,y≤1060\le x_i,y_i,x,y\le10^6 for every initial point and operation; t∈{1,2}t\in\{1,2\}; inserted points remain forever.

Examples

Input

2 4
0 0
5 5
2 3 3
1 3 3
2 3 3
2 2 5

Output

4
0
3

Explanation

Initially Ayu knows locations (0,0)(0,0) and (5,5)(5,5).

  1. Query 2 3 3: distances from (3,3)(3,3) to those two points are 66 and 44, so print 4.
  2. Operation 1 3 3 adds location (3,3)(3,3) without producing output.
  3. Query 2 3 3: the query point now matches a known location, giving distance 00.
  4. Query 2 2 5: distances to (0,0)(0,0), (5,5)(5,5), and (3,3)(3,3) are respectively 77, 33, and 33. Print 3.

The output lines are therefore 4, 0, and 3.