D. Dzy Loves FFT

**Source**

Http://codeforces.com/contest/445/problem/D

**Description**
*Wherever the destination is, whoever we meet, let's render this song together.*

On a Cartesian coordinate plane lies a rectangular stage of size *w*? x? * H*, represented by a rectangle with corners (0,?0), (*w*,? 0), (*w*, *? H*) and (0,? * H*). It can seen that no collisions would happen before one enters the stage.

On the sides of the stage stand *n* dancers. The *i*-th of them falls into one of the following groups:

- Vertical:stands at (
*x*_{-i,? 0), moves in positive y direction (upwards); }
- Horizontal:stands at (0,?
* Y*_{i), moves in positive x Direction (rightwards). }

According to choreography, the *i*-th dancer should stand still for the first *T*_{i Mill Iseconds, and then start moving in the specified direction at 1 unit per millisecond, until another border is reached. It is guaranteed that no dancers has the same group, position and waiting time at the same time. }

When the dancers collide (i.e. is on the same point at some time time both of them is moving), they immediately exchange Their moving directions and go on.

Dancers stop when a border of the stage is reached. Find out every dancer ' s stopping position.

**Input**
The first line of input contains three space-separated positive integers *n*, *w* and h ( 1?≤? * N*. ≤?100?000, 2?≤? * W*,? *h*. ≤?100?000)-the number of dancers and the width and height of the stage, respectively.

The following*N*Lines each describes a dancer:the*I*-th among them contains three space-separated integers*g*_{I,PI, andTI(1?≤?gI? ≤?2,1?≤?PI? ≤?99?999,0?≤?TI? ≤?100?000), describing a dancer ' s groupgI(gI? =?1-vertical,gI? =?2-horizontal), position, and waiting time. If g i ? =?1 Then p i ?=? x i ; otherwise P i ? =? Y i . It ' s guaranteed that 1?≤? X i ? ≤? W ?-? 1 and 1?≤? Y i ? ≤? H ?-? 1. It is guaranteed that no dancers has the same group, position and waiting time at the same time. }

**Output**
Output *n* lines, the *i*-th of which contains II space-separated integers (*x*_{ C12>i, huh? y i)-the stopping position of the I-th Dancer in the input. }

**Examples**Input

8 10 8

1 1 10

1 4 13

1 7 1

1 8 2

2 2 0

2 5 14

2 6 0

2 6 1

Output

4 8

10 5

8 8

10 6

10 2

1 8

7 8

10 6

Input

3 2 3

1 1 2

2 1 1

1 1 5

Output

1 3

2 1

1 3

**Note**
The first example corresponds to the initial setup in the legend, and the tracks of dancers is marked with different Colo Urs in the following figure.

In the second example, no dancers collide.

**Solution**

Very good, the official problem is also very detailed:

**How to deal with "waiting time"?**

Move every dancer *T*_{I units backwards in the first place, that's to (xi?-? T i,? 0) for the vertical-moving group, and (0,? Yi?-? Ti) for the horizontal-moving group. Then start the time, making everyone start moving immediately. }

**When does dancers collide? What changes and what keeps the same?**

Notice that if and dancers collide before any other collision happens and then they has the same *x*? +? * Y* values for their initial positions. Furthermore, after a collision, the dancers keep have the same *x*? +? * Y*, and also with the same relative orders of *x* and *y*. Also, after a collision, the union of all dancers ' tracks would be is the same as if they "went through" each other and no Col Lision happened at all (see the figure for sample 1 to get a general idea on this).

Therefore, divide dancers to groups by*P*_{I?-?TI, and collisions'll happen within groups only. Dancers in the same group would move on the samex?+?yLine (a line of slope?-? 1), and however collisions take place, they would keep current relative order of x and y . It's proved before so in each group, dancers ' exiting positions are the same as if no collision happened at all (namely, ( x i ,? H ) for initially-vertical dancers, and ( w ,? Y i ) for initially-horizontal ones). For each group, the find out all such positions. Sort all dancers according to their initial x values, and sort these positions in the dire Ction of (0,? H ) to ( w ,? H ) then ( w ,? 0). Match the sorted dancers to these sorted positions and obtain the answers for all dancers. This solution works in. }

Note the order of the sort, attaching your own code:

1#include <bits/stdc++.h>2 using namespacestd;3 4 Const intMAXN =100100;5 6 structnode{7 intx, y;8 }ANS[MAXN];9 Ten intN,W,H,G[MAXN],P[MAXN],T[MAXN]; Onevector<vector<int>>GRP (2*MAXN); AVector<vector<node>>res (2*MAXN); - - BOOLCMP1 (intAintb) { the if(G[a]! = G[b])returnG[a] >G[b]; - if(G[a] = =2)returnP[a] >P[b]; - returnP[a] <P[b]; - } + - BOOLCMP2 (Node a,node b) { + if(A.y = = h && b.x = = W)return 1; A if(a.x = = W && b.y = = h)return 0; at if(A.Y = = h)returnA.x <b.x; - returnA.y >b.y; - } - - intMain () { -CIN >> N >> W >>h; in for(inti =1; I <= n;++i) { -scanf"%d%d%d",&g[i],&p[i],&t[i]); to if(G[i] = =1){ +grp[p[i]-t[i]+100000].push_back (i); -res[p[i]-t[i]+100000].push_back ({p[i],h}); the } * Else{ $grp[p[i]-t[i]+100000].push_back (i);Panax Notoginsengres[p[i]-t[i]+100000].push_back ({w,p[i]}); - } the } + for(inti =0; I <200000;++i) { A if(Grp[i].empty ())Continue; the sort (Grp[i].begin (), Grp[i].end (), CMP1); + sort (Res[i].begin (), Res[i].end (), CMP2); - for(intj =0; J < Grp[i].size (); ++j) Ans[grp[i][j]] =Res[i][j]; $ } $ for(inti =1; I <= n;++i) printf ("%d%d\n", ans[i].x,ans[i].y); - - return 0; the}

"Sort" "law" codeforces Round #254 (Div. 2)-d. Rooter's Song