Laser Sensors

Given N blue points and 2N red points in general position, build the particular non-crossing perfect matching prescribed by the paper's recursive angular-sweep Solve/Attach procedure.

Hard9Divide and conquerGeometrySortingRecursionNo attempts yetTime limit2sMemory limit512 MB

Problem

A precision measurement company bought several devices that measure the movement of a distant object with a laser and installed them at different places. Every device carries two laser sensors and one sensor measures one object, so a single device measures two objects at the same time. Laser beams sent from two different devices interfere with each other where they meet, and that interference produces measurement errors, so the beams are arranged so that they never cross.

Draw every installed device as a blue point of the plane and every measured object as a red point. The task becomes the following.

The plane holds NN blue points and 2N2N red points. Call a set of segments a linking when it satisfies all three conditions.

  • Every blue point is joined to exactly two red points.
  • Every red point is joined to exactly one blue point.
  • No two segments cross.

Part (A) of the figure holds three blue points and six red points. Blue point 1 is joined to red points 1 and 4, blue point 2 to red points 2 and 5, blue point 3 to red points 3 and 6, and no two segments cross, so (A) is a linking. Part (B) is another linking of the same points, so one input can admit several linkings. In part (C) the segment from blue point 1 to red point 3 crosses the segment from blue point 3 to red point 2, so (C) is not a linking.

One input can admit several linkings, so print the one that the procedure in the output section builds.

Input

The first line holds the number of blue points NN (1N1,0001 \le N \le 1{,}000). The ii-th of the next NN lines holds the x coordinate and the y coordinate of blue point ii. The jj-th of the following 2N2N lines holds the x coordinate and the y coordinate of red point jj. Every coordinate is an integer between 108-10^8 and 10810^8. No three of the given points lie on one line.

Output

Print NN lines. Line ii holds the numbers of the two red points joined to blue point ii, the smaller number first, separated by one space.

Several linkings can exist, so print the one that the procedure below builds. The procedure always finds the index it asks for, and the linking it builds always satisfies the three conditions.

Give every blue point the value 22 and every red point the value 1-1, and let the value of a set of points be the sum of the values of its points. The 3N3N given points have value 00. Run Solve on all 3N3N given points.

Solve(SS), where the value of SS is 00:

  1. If SS is empty, stop.
  2. Let pp be the point of SS with the smallest y coordinate, and among such points the one with the smallest x coordinate. List the other points of SS as q1,q2,,qmq_1, q_2, \dots, q_m in increasing order of the angle of the vector that runs from pp to the point, measured counterclockwise from the positive direction of the x axis. Every one of these angles is at least 00 and smaller than π\pi, and no two of them are equal.
  3. Let W0=0W_0 = 0 and Wi=Wi1+viW_i = W_{i-1} + v_i, where viv_i is the value of qiq_i.
  4. If pp is blue, let kk be the smallest index with Wk=1W_k = -1 and let ll be the smallest index with Wl=2W_l = -2. Join pp to qkq_k and to qlq_l. Then run Solve on q1,,qk1q_1, \dots, q_{k-1}, on qk+1,,ql1q_{k+1}, \dots, q_{l-1}, and on ql+1,,qmq_{l+1}, \dots, q_m.
  5. If pp is red, let kk be the largest index with Wk10W_{k-1} \le 0. The point qkq_k is blue. Join pp to qkq_k. If Wk1=1W_{k-1} = -1, run Attach(q1,,qk1q_1, \dots, q_{k-1}; qkq_k; pp) and Solve(qk+1,,qmq_{k+1}, \dots, q_m). Otherwise run Solve(q1,,qk1q_1, \dots, q_{k-1}) and Attach(qk+1,,qmq_{k+1}, \dots, q_m; qkq_k; pp).

Attach(TT; cc; pp), where TT is not empty and has value 1-1, the blue point cc still needs one red point, and pp is a point outside TT:

  1. List the points of TT as s1,s2,,sts_1, s_2, \dots, s_t in increasing order of the angle at cc between the ray from cc to pp and the ray from cc to the point. Every one of these angles is greater than 00 and smaller than π\pi, and no two of them are equal.
  2. Let V0=0V_0 = 0 and Vi=Vi1+uiV_i = V_{i-1} + u_i, where uiu_i is the value of sis_i.
  3. Let jj be the largest index with Vj10V_{j-1} \ge 0. The point sjs_j is red. Join cc to sjs_j. Then run Solve on s1,,sj1s_1, \dots, s_{j-1} and on sj+1,,sts_{j+1}, \dots, s_t.