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 × h, represented by a rectangle with corners (0, 0), (w, 0), (w, h) and (0, h). It can be seen that no collisions will 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 (xi, 0), moves in positive y direction (upwards);
- Horizontal: stands at (0, yi), moves in positive x direction (rightwards).
![](http://codeforces.com/predownloaded/c8/ef/c8ef4febb53df4c92041d2cc722683350ca6d14e.png)
According to choreography, the i-th dancer should stand still for the first ti milliseconds, and then start moving in the specified direction at 1 unit per millisecond, until another border is reached. It is guaranteed that no two dancers have the same group, position and waiting time at the same time.
When two dancers collide (i.e. are on the same point at some time when both of them are moving), they immediately exchange their moving directions and go on.
![](http://codeforces.com/predownloaded/9b/9f/9b9fb73178590e383ca6e5925d5dcf60501e28ef.png)
Dancers stop when a border of the stage is reached. Find out every dancer's stopping position.
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 gi, pi, and ti (1 ≤ gi ≤ 2, 1 ≤ pi ≤ 99 999, 0 ≤ ti ≤ 100 000), describing a dancer's group gi(gi = 1 — vertical, gi = 2 — horizontal), position, and waiting time. If gi = 1 then pi = xi; otherwise pi = yi. It's guaranteed that 1 ≤ xi ≤ w - 1 and 1 ≤ yi ≤ h - 1. It is guaranteed that no two dancers have the same group, position and waiting time at the same time.
Output n lines, the i-th of which contains two space-separated integers (xi, yi) — the stopping position of the i-th dancer in the 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
4 8 10 5 8 8 10 6 10 2 1 8 7 8 10 6
3 2 3 1 1 2 2 1 1 1 1 5
1 3 2 1 1 3
The first example corresponds to the initial setup in the legend, and the tracks of dancers are marked with different colours in the following figure.
![](http://codeforces.com/predownloaded/64/1e/641e1661ff9a4907ac92affb7e75a1c67422a16f.png)
In the second example, no dancers collide.
若干个点在不同的出发时间从坐标轴向坐标正向移动,两个点相遇时交换方向,保证没有多于两个点在同一时刻相遇,求每个点最后的位置。
根据图列个式子,发现两个点相遇的条件是:t1+p2=t2+p1,即t1-p1=t2-p2.
那么根据每个点的t-p排序,这样相邻一段区间内的点都可以相遇。
再次画图,通过观察求得,在所有能够相遇的点当中,底下的点的轨迹从左到右被左边从上到下的点代替,如果点不够就从底下的第一个点开始取点。对于左边的点,从下至上的每个点轨迹依次被最下方从右向左的每个点取代,点数不够亦然。
就这样XJB画图猜出了结论,还以为是什么高端的数据结构题呢。。。
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=100005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
int x[maxn],y[maxn],b[maxn],c[maxn];
struct Dancer {
int g,p,t,id;
};
Dancer a[maxn];
bool cmp(Dancer a,Dancer b) {
return a.t-a.p<b.t-b.p || (a.t-a.p==b.t-b.p&&a.p<b.p);
}
int main() {
int n,m,k,i,j;
scanf("%d%d%d",&k,&n,&m);
for (i=1;i<=k;i++) {
scanf("%d%d%d",&a[i].g,&a[i].p,&a[i].t);
a[i].id=i;
}
sort(a+1,a+k+1,cmp);
for (i=1;i<=k;i++) {
int t1,t2,cnt;
t1=t2=0;
if (a[i].g==1) b[++t1]=i; else c[++t2]=i;
while (a[i].t-a[i].p==a[i+1].t-a[i+1].p&&i<k) {
i++;
if (a[i].g==1) b[++t1]=i; else c[++t2]=i;
}
cnt=t2+1;int flag=0;
for (j=1;j<=t1;j++) {
if (!flag) cnt--; else cnt++;
if (cnt==0) cnt++,flag=1;
if (!flag) x[a[c[cnt]].id]=a[b[j]].p,y[a[c[cnt]].id]=m;
else x[a[b[cnt]].id]=a[b[j]].p,y[a[b[cnt]].id]=m;
}
cnt=t1+1;flag=0;
for (j=1;j<=t2;j++) {
if (!flag) cnt--; else cnt++;
if (cnt==0) cnt++,flag=1;
if (!flag) x[a[b[cnt]].id]=n,y[a[b[cnt]].id]=a[c[j]].p;
else x[a[c[cnt]].id]=n,y[a[c[cnt]].id]=a[c[j]].p;
}
}
for (i=1;i<=k;i++)
printf("%d %d\n",x[i],y[i]);
return 0;
}