# Codeforces 849 D. Rooter’s Song （技巧）

## 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 × 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 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.

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

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

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


## Examples output

4 8
10 5
8 8
10 6
10 2
1 8
7 8
10 6


## 思路

• 若为空，则说明没有任何一个点会影响该点的运动
• 否则将当前点的移动状态转移给队列尾部的那一个点，然后将自身加入该队列的首部。（因为最后只有自身与队尾改变了移动方向）

## AC 代码

#include<bits/stdc++.h>
using namespace std;

const int maxn = 1e5+10;
typedef __int64 LL;
typedef map<int,deque<int> >::iterator MPIT;

map<int,deque<int> > mapp;

int n,w,h;
int ax[maxn],ay[maxn],tmp[maxn];

struct node
{
int gg,pp,tt,id;
bool operator<(const node &x)const
{
return pp<x.pp;
}
} a[maxn];

void call(int cal,int o)
{
for(int i=1; i<=n; ++i)
if(a[i].gg==cal)
mapp[a[i].tt-a[i].pp].push_back(i);
for(int i=1; i<=n; ++i)
if(a[i].gg==o)
{
MPIT it=mapp.find(a[i].tt-a[i].pp);
if(it==mapp.end() || it->second.empty())
tmp[i]=i;
else
{
tmp[i]=it->second.back();
it->second.pop_back();
it->second.push_front(i);
}
}
mapp.clear();
}

void solve()
{
sort(a+1,a+n+1);
call(1,2);
call(2,1);
for(int i=1; i<=n; i++)
if(a[i].gg==1)
{
ax[a[tmp[i]].id]=a[i].pp;
ay[a[tmp[i]].id]=h;
}
else
{
ax[a[tmp[i]].id]=w;
ay[a[tmp[i]].id]=a[i].pp;
}
for(int i=1; i<=n; i++)
cout<<ax[i]<<" "<<ay[i]<<endl;
}

int main()
{
ios::sync_with_stdio(false);
cin>>n>>w>>h;
for(int i=1; i<=n; i++)
cin>>a[i].gg>>a[i].pp>>a[i].tt,a[i].id=i;
solve();
return 0;
}