D. Rooter's Song
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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).

According to choreography, the i-th dancer should stand still for the first ti milliseconds, and then start moving in the specified direction at 1unit 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.

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 nw and h (1 ≤ n ≤ 100 0002 ≤ 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 gipi, and ti (1 ≤ gi ≤ 21 ≤ pi ≤ 99 9990 ≤ 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

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.

Examples
input
Copy
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
Copy
4 8
10 5
8 8
10 6
10 2
1 8
7 8
10 6
input
Copy
3 2 3
1 1 2
2 1 1
1 1 5
output
Copy
1 3
2 1
1 3
Note

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.

In the second example, no dancers collide.


题意:在一个h*w的矩阵中有n个舞者,每秒运动1个单位,每个舞者有出发的时间t[i],和初始位置p[i](都在x,y轴).如果两个舞者相撞,那么交换方向. 问  最终每个舞者的坐标是什么


思路:

1.d==p[i]-t[i]的舞者们在同一个group中,只有这个group才会互相碰撞

证明:假设x(t1)和y(t2)在时间为t时,向碰撞

那么有t-t2=x,t-t1=y . 两式做差消去t   得到  t-t1=y-t2 . 证明完毕

2.对于确定的d . 有几个x就会有x个舞者跑到上边界.  这x个舞者是将所有人按往右跑为优先,Pos越大越好排序所得到的.pos越大,x越小.因此有一个对应关系,用排序可以得到.

#include<bits/stdc++.h>
#define PI acos(-1.0)
using namespace std;
typedef long long ll;

const int N=2e5+6;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;

int g[N],p[N],t[N];
vector <int> vec[N];
pair <int,int> ans[N];

bool cmp(int u,int v){
    if(g[u]!=g[v])  return g[u]==2;
    else if(g[u]==g[v]){
        if(g[u]==2) return p[u]>p[v];
        else if(g[u]==1)    return p[u]<p[v];
    }
}

int main(void){
    int n,w,h;
    cin >>n >> w>>h;
    for(int i=1;i<=n;i++){
        scanf("%d%d%d",&g[i],&p[i],&t[i]);
        int te=p[i]-t[i]+1e5;
        vec[te].push_back(i);
    }
    ///The talk with the group
    for(int d=0;d<=2e5;++d){
        vector <int> x,y;
        for(auto te : vec[d]){
            if(g[te]==1)    x.push_back(p[te]);
            if(g[te]==2)    y.push_back(p[te]);
        }
        sort(x.begin(),x.end());
        sort(y.begin(),y.end());
        sort(vec[d].begin(),vec[d].end(),cmp);
        // 手动模拟一下可以发现有这样的规律存在
        for(int i=0;i<x.size();++i){
            ans[vec[d][i]]=make_pair(x[i],h);
        }

        for(int j=0;j<y.size();j++){
            ans[vec[d][j+x.size()]]=make_pair(w,y[y.size()-1-j]);
        }

        x.clear(),y.clear();
    }
    for(int i=1;i<=n;i++)
        printf("%d %d\n",ans[i].first,ans[i].second);
    return 0;
}
/*********

*********/