Problem:
蟻
大きなチェス盤上に、それぞれ１から
N
までの番号が割り振られた
N
匹の蟻がいます。図のように、チェス盤は H×W のマスからなる長方形で、北西角を白として、白マスと黒マスが交互に並んでいます。
最初、どの蟻もチェス盤のマスの中にいて、東または南を向いています。１つのマスの中に２匹以上の蟻がいることはありません。
今、蟻たちが一斉に動き出します。すべての蟻は 1 単位時間に向いている方向のマスに１つ移動します。ただし、移動先がチェス盤の外の場合、落下してチェス盤から姿を消します。
チェス盤上で２匹の蟻が同じマスに入ると、それらの蟻は以下のような行動をとります：
そのマスの色が白ならば、東方向に進んでいた蟻は南方向へ、南方向に進んでいた蟻は東方向へ向きを変える。
そのマスの色が黒ならば、それぞれの蟻は進む方向を維持する。
チェス盤の大きさと蟻の情報が与えられたとき、落下する順番に蟻の番号を報告するプログラムを作成せよ。ただし、同じ時刻に複数の蟻が落下する場合は、番号がより小さい方を先に報告する。
Input
入力は以下の形式で与えられる。
W
H
N
x
1
y
1
d
1
x
2
y
2
d
2
:
x
N
y
N
d
N
１行目にチェス盤の東西方向のマスの数
W
と南北方向のマスの数
H
(2 ≤
W
,
H
≤ 10
9
) と蟻の数
N
(1 ≤
N
≤ 200000) が与えられる。続く
N
行に
i
番目の蟻の東西方向の位置
x
i
(1 ≤
x
i
≤
W
)、南北方向の位置
y
i
(1 ≤
y
i
≤
H
)、向きを表す文字
d
i
（東向きの場合「E」、南向きの場合「S」）が与えられる。ここで、チェス盤の北西角のマスを (1,1)、
x
が増加する方向を東、
y
が増加する方向を南とする。
Output
落下する順番に、蟻の番号を１行ずつ出力する。
Sample Input 1
3 3 3
2 1 S
1 2 E
2 2 E
Sample Output 1
3
1
2
Sample Input 2
5 4 3
3 1 S
2 2 E
1 3 E
Sample Output 2
2
3
1

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
#define int long long
#define INF 2e+9
#define mp make_pair
using namespace std;

typedef pair<int,int> P;
typedef pair<int,P> PP;

int w,h,n,x[200000],y[200000],ans[200000];
string d[200000];
vector<int> vec[200000],xys;
vector<P> tim;

void solve(vector<int> vecvec,int add){
	vector<int> tmp;
	vector<PP> ord;
	map<int,int> cnt;
	for(int v : vecvec){
		cnt[x[v]]++;
	}
	for(int v : vecvec){
		if(cnt[x[v]] >= 2){
			if(d[v] == "S") d[v] = "E";
			else d[v] = "S";
		}
		if(d[v] == "S") ord.push_back(mp(x[v],mp(0,v)));
		else ord.push_back(mp(x[v],mp(1,v)));
	}
	sort(ord.begin(),ord.end());
	for(int i = 0;i < vecvec.size();i++) vecvec[i] = ord[i].second.second;
	map<int,int> mp[2];
	for(int v : vecvec){
		int pos = x[v] - y[v];
		if(d[v] == "S"){
			mp[1][pos] = h - y[v];
			pos -= INF;
		}
		else{
			mp[0][pos] = w - x[v];
			pos += INF;
		}
		tmp.push_back(pos);
	}
	sort(tmp.begin(),tmp.end());
	for(int i = 0;i < vecvec.size();i++){
		if(tmp[i] < 0){
			tmp[i] += INF;
			ans[vecvec[i]] = mp[1][tmp[i]] + add;
		}
		else{
			tmp[i] -= INF;
			ans[vecvec[i]] = mp[0][tmp[i]] + add;
		}
	}
}

signed main(){
	cin >> w >> h >> n;
	for(int i = 0;i < n;i++){
		cin >> x[i] >> y[i] >> d[i];x[i]--;y[i]--;
		xys.push_back(x[i] + y[i]);
	}
	sort(xys.begin(),xys.end());
	xys.erase(unique(xys.begin(),xys.end()),xys.end());
	for(int i = 0;i < n;i++){
		int ind = lower_bound(xys.begin(),xys.end(),x[i] + y[i]) - xys.begin();
		vec[ind].push_back(i);
	}
	for(int i = 0;i < xys.size();i++){
		if(xys[i] % 2 == 0){
			vector<int> vs1,vs2;
			for(int v : vec[i]){
				if(x[v] % 2) vs1.push_back(v);
				else vs2.push_back(v);
			}
			solve(vs1,0);
			solve(vs2,0);
		}else{
			vector<int> vs1,vs2;
			for(int v : vec[i]){
				if(d[v] == "E") x[v]++;
				else y[v]++;
				if(x[v] == w) ans[v] = 1;
				else if(y[v] == h) ans[v] = 1;
				else if(x[v] % 2) vs1.push_back(v);
				else vs2.push_back(v);
			}
			solve(vs1,1);
			solve(vs2,1);
		}
	}
	for(int i = 0;i < n;i++) {
		tim.push_back(P(ans[i],i));
	}
	sort(tim.begin(),tim.end());
	for(int i = 0;i < n;i++) cout << tim[i].second + 1 << endl;
	return 0;
}
accuracy: 1
time_ms: 370
memory_kb: 57548
final_rank: 15

code_snippet: #include <cmath>
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <stdlib.h>
using namespace std;
struct E{
	int x,y,no,p1;
	string str;
};
struct E2{
	int no,t;
	bool operator<(const E2& e2){
		if(t!=e2.t)return t<e2.t;
		return no<e2.no;
	}
};

map<int,map<int,int> > msDki,msRki,msDgu,msRgu,DKitoNo,DGutoNo,RKitoNo,RgutoNo;
vector<E> vs;
set<int> ssD,ssR;
int w,h,n;
int f3(map<int,int>& mr,map<int,int>& md,map<int,int>& toR,map<int,int>& toD,E& e1){
	int ans=0;
	map<int,int>::iterator it,it2;
	if(e1.str=="S"){
		//下向き
		it=mr.lower_bound(e1.x);
		if(it==mr.begin()){
			//cout<<"D1= "<<e1.no<<endl;
			return h-e1.y+1;
		}
		it--;
		ans=abs(e1.y-abs(e1.p1-(*it).first));
		
		it2=md.upper_bound(e1.x);
		if(it2==md.end()){
			//cout<<"D2= "<<e1.no<<endl;
			return ans+w-e1.x+1;
		}
		int nor=(*it).second;
		int nod=(*it2).second;
		if(nor<=nod){
			//あと少しで完成だがこんな巨大なコードになるの多分俺の設計ミス
			//これがうまく動くまで修正してもあまり褒められたことではないな。
			int nor2=nod-nor+1;
			ans=abs(h-e1.y+1);
			ans+=abs(e1.x-toD[nor2]);
		//	cout<<"D3= "<<e1.no<<endl;
		}else{
			int nor2=nor-nod;
			ans=w-e1.x+1;
			ans+=abs(e1.y-abs(e1.p1-toR[nor2]));
		//	cout<<"D4= "<<e1.no<<endl;
		}
	}else{
		//右向き
		it=md.upper_bound(e1.x);
		if(it==md.end()){
			//cout<<"R1= "<<e1.no<<endl;
			return w-e1.x+1;
		}
		ans=abs((*it).first-e1.x);
		it2=mr.lower_bound(e1.x);
		
		if(it2==mr.begin()){
			ans=ans+h-e1.y+1;
			//cout<<"R2= "<<e1.no<<endl;
			return ans;
		}
		it2--;
		int nod=(*it).second;
		int nor=(*it2).second;
		if(nod<=nor){
			ans=w-e1.x+1;
			int nod2=nor-nod+1;
			ans+=abs(e1.y-abs(e1.p1-toR[nod2]));
			//if(e1.no==3){
			//	cout<<"x "<<e1.x<<" y"<<e1.y<<" p1"<<e1.p1<<" nod"<<nod<<" nor"<<nor<<" toD"<<toR[nod2];
			//	cout<<" no="<<e1.no<<" ans="<<ans<<endl;
			//	for(map<int,int>::iterator it=mr.begin();it!=mr.end();it++){
			//		cout<<"x="<<(*it).first<<" no"<<(*it).second<<endl;
			//	}
			//	cout<<endl;
			//}
			//cout<<"R3= "<<e1.no<<endl;
		}else{
			ans=h-e1.y+1;
			int nor2=nod-nor;
			ans+=abs(e1.x-abs(toD[nor2]));
			//cout<<"R4= "<<e1.no<<endl;
		}
	}
	//cout<<ans<<endl;
	return ans;
}
void f2p(map<int,map<int,int> >& ms,set<int>& ks){
	set<int>::iterator it;
	map<int,int>::iterator it2;
	for(it=ks.begin();it!=ks.end();it++){
		int t=(*it);
		int no=0;
		for(it2=ms[t].begin();it2!=ms[t].end();it2++){
			cout<<(*it2).first<<" "<<(*it2).second<<endl;
		}
	}
}
void f2(map<int,map<int,int> >& ms,map<int,map<int,int> >& toNo,set<int>& ks){
	set<int>::iterator it;
	map<int,int>::iterator it2;
	for(it=ks.begin();it!=ks.end();it++){
		int t=(*it);
		int no=0;
		for(it2=ms[t].begin();it2!=ms[t].end();it2++){
			no++;
			(*it2).second=no;
			toNo[t][no]=(*it2).first;
		}
	}
}
void f2r(map<int,map<int,int> >& ms,map<int,map<int,int> >& toNo,set<int>& ks){
	set<int>::iterator it;
	map<int,int>::reverse_iterator it2;
	for(it=ks.begin();it!=ks.end();it++){
		int t=(*it);
		int no=0;
		for(it2=ms[t].rbegin();it2!=ms[t].rend();it2++){
			no++;
			(*it2).second=no;
			toNo[t][no]=(*it2).first;
		}
	}
}


void f(){
	map<int,int> m1;
	for(int i=0;i<n;i++){
		E e1;
		string str;
		cin>>e1.x>>e1.y>>e1.str;
		e1.no=i+1;
		int p1=e1.x+e1.y;
		e1.p1=p1;
		vs.push_back(e1);
		if(e1.str=="S"){
			ssD.insert(p1);
		}else{
			ssR.insert(p1);
		}
	}
	set<int>::iterator it;
	for(it=ssD.begin();it!=ssD.end();it++){
		msDki[(*it)]=m1;
		msDgu[(*it)]=m1;
		DKitoNo[(*it)]=m1;
		DGutoNo[(*it)]=m1;
		
	}
	for(it=ssR.begin();it!=ssR.end();it++){
		msRki[(*it)]=m1;
		msRgu[(*it)]=m1;
		RKitoNo[(*it)]=m1;
		RgutoNo[(*it)]=m1;
	}
	for(int i=0;i<n;i++){
		E e1=vs[i];
		if(e1.str=="S"){
			if((e1.x%2)==0){
				msDgu[e1.p1][e1.x]=0;
			}else{
				msDki[e1.p1][e1.x]=0;
			}
		}else{
			if((e1.x%2)==0){
				msRgu[e1.p1][e1.x]=0;
			}else{
				msRki[e1.p1][e1.x]=0;
			}
		}
	}
	
	f2(msRki,RKitoNo,ssR);
	f2(msRgu,RgutoNo,ssR);
	f2r(msDgu,DGutoNo,ssD);
	f2r(msDki,DKitoNo,ssD);
	vector<E2> ans;
	for(int i=0;i<n;i++){
		E e1=vs[i];
		int p1=e1.p1;
		E2 e2;
		e2.no=e1.no;
		//8パターンの場合分けがあってめんどくさいなこれ。ちょっと休憩しよう。多分設計をちょっと間違えた
		if(e1.str=="E"){
			if(e1.p1%2==0){
				//msDki,msRki,msDgu,msRgu,DKitoNo,DGutoNo,RKitoNo,RgutoNo
				if(e1.x%2==0){
					//東白偶数
					e2.t=f3(msRgu[p1],msDgu[p1],RgutoNo[p1],DGutoNo[p1],e1);
				}else{
					//東白奇数
					e2.t=f3(msRki[p1],msDki[p1],RKitoNo[p1],DKitoNo[p1],e1);
				}
			}else{
				
				if(e1.x%2==0){
					//東黒偶数
					e2.t=f3(msRgu[p1],msDki[p1],RgutoNo[p1],DKitoNo[p1],e1);
				}else{
					//東黒奇数
					e2.t=f3(msRki[p1],msDgu[p1],RKitoNo[p1],DGutoNo[p1],e1);
				}
			}
		}else{
			if(e1.p1%2==0){
				if(e1.x%2==0){
					//南白偶数
					e2.t=f3(msRgu[p1],msDgu[p1],RgutoNo[p1],DGutoNo[p1],e1);
				}else{
					//南白奇数
					e2.t=f3(msRki[p1],msDki[p1],RKitoNo[p1],DKitoNo[p1],e1);
				}
			}else{
				
				if(e1.x%2==0){
					//南黒偶数
					e2.t=f3(msRki[p1],msDgu[p1],RKitoNo[p1],DGutoNo[p1],e1);
				}else{
					//南黒奇数
					e2.t=f3(msRgu[p1],msDki[p1],RgutoNo[p1],DKitoNo[p1],e1);
				}
			}
		}
		//cout<<e2.no<<" "<<e2.t<<endl;
		ans.push_back(e2);
	}
	sort(ans.begin(),ans.end());
	for(int i=0;i<n;i++){
		cout<<ans[i].no<<endl;
	}
}

int main() {
	cin>>w>>h>>n;
	f();
	return 0;
}
accuracy: 1
time_ms: 1490
memory_kb: 158964
final_rank: 17

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
int H,W,N;
map<int,vector<pair<pair<int,int>,pair<bool,int> > > >mp;
main()
{
	cin>>W>>H>>N;
	for(int i=0;i<N;i++)
	{
		int x,y;char d;
		cin>>x>>y>>d;
		x--,y--;
		mp[x+y].push_back(make_pair(make_pair(x,y),make_pair(d=='S',i+1)));
	}
	vector<pair<int,int> >ans;
	ans.reserve(N);
	for(auto it=mp.begin();it!=mp.end();it++)
	{
		vector<pair<pair<int,int>,pair<bool,int> > >A=it->second;
		sort(A.begin(),A.end());
		int off=0;
		if(it->first%2==1)
		{
			off=1;
			for(int i=0;i<A.size();i++)
			{
				if(A[i].second.first)A[i].first.second++;
				else
				{
					A[i].first.first++;
					if(i+1<A.size()&&A[i+1].first.first==A[i].first.first&&A[i+1].second.first)
					{
						A[i].second.first=true;
						A[i+1].second.first=false;
						A[i+1].first.second++;
						i++;
					}
				}
			}
		}
		vector<pair<pair<int,int>,pair<bool,int> > >B[2];
		for(int i=0;i<A.size();i++)
		{
			B[A[i].first.first%2].push_back(A[i]);
		}
		for(int i=0;i<2;i++)
		{
			auto A=B[i];
			int id=0;
			for(int i=0;i<A.size();i++)if(A[i].second.first)
			{
				int dx=A[i].first.first-A[id].first.first;
				int dy=H-A[id].first.second;
				ans.push_back(make_pair(dx+dy+off,A[id++].second.second));
			}
			for(int i=0;i<A.size();i++)if(!A[i].second.first)
			{
				int dx=W-A[id].first.first;
				int dy=A[i].first.second-A[id].first.second;
				ans.push_back(make_pair(dx+dy+off,A[id++].second.second));
			}
		}
	}
	sort(ans.begin(),ans.end());
	for(int i=0;i<N;i++)cout<<ans[i].second<<endl;
}
accuracy: 1
time_ms: 430
memory_kb: 26224
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

struct Ant{
 char ty; int x,y; int i;
 bool operator<(Ant r)const{ return x-y < r.x-r.y; }
};

int H,W,N;
map<pair<int,int>,vector<Ant>> A;
vector<pair<int,int>> ans;

int main(){
 cin>>W>>H>>N;
 rep(i,N){
  char ty; int x,y; cin>>x>>y>>ty; x--; y--;
  int mat = 0;
  mat = (x+H-y)/2;
  if((x+y)%2==1) if(ty=='E') mat++;
  A[{mat%2,x+y}].push_back({ty,x,y,i});
 }

 ans.resize(N);

 for(auto& Axitr : A){
  auto& Ax = Axitr.second;
  if(Ax.size()==0) continue;

  vector<Ant> fall;
  for(Ant& a : Ax){
   Ant falltmp;
   if(a.ty=='E') falltmp = {'E',W,a.y,0};
   if(a.ty=='S') falltmp = {'S',a.x,H,0};
   fall.push_back(falltmp);
  }
  sort(fall.begin(),fall.end());
  sort(Ax.begin(),Ax.end());

  rep(x,Ax.size()){
   Ant a = Ax[x];
   Ant f = fall[x];
   ans[a.i] = {(f.y-a.y) + (f.x-a.x), a.i+1};
  }
 }

 sort(ans.begin(),ans.end());
 rep(i,N) cout<<ans[i].second<<endl;

 return 0;
}
accuracy: 1
time_ms: 420
memory_kb: 26272
final_rank: 10

code_snippet: #pragma GCC target ("avx2")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include <unordered_set>
#include <unordered_map>
#include <random>
using namespace std;
typedef long long ll;
const ll MOD = 1'000'000'007LL; /*998'244'353LL;*/
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
const int dx[4]={ 1,0,-1,0 };
const int dy[4]={ 0,1,0,-1 };

ll W, H;
int N;
ll x[400000], y[400000];
char d[400000];

vector<ll> cand;
unordered_map<ll,vector<pair<ll, int>>> v[2][2]; // →:1，↓:0
vector<pair<ll, int>> ans;

signed main(){
    cin >> W >> H >> N;
    rep(i, N){
        cin >> x[i] >> y[i] >> d[i];
        cand.pb(x[i]+y[i]);
        v[d[i] == 'E'][x[i]%2][x[i]+y[i]].pb({ x[i], i });
    }
    sort(all(cand));
    cand.erase(unique(all(cand)), cand.end());

    rep(t, cand.size()){
        ll i = cand[t];
        rep(j, 2) rep(k, 2) sort(all(v[j][k][i]));
        if(i%2 == 0){ // 白
            rep(k, 2){
                vector<pair<ll, ll>> tmp;
                vector<pair<ll, int>> marge;
                rep(j, v[0][k][i].size()){
                    marge.pb(v[0][k][i][j]);
                    int idx = v[0][k][i][j].second;
                    tmp.pb({ x[idx],H });
                }
                rep(j, v[1][k][i].size()){
                    marge.pb(v[1][k][i][j]);
                    int idx = v[1][k][i][j].second;
                    tmp.pb({ W,y[idx] });
                }
                sort(all(marge));
                rep(j, marge.size()){
                    int idx = marge[j].second;
                    ans.pb({ tmp[j].first-x[idx]+tmp[j].second-y[idx], idx });
                }
            }
        }
        else{ // 黒
            rep(k, 2){
                vector<pair<ll, ll>> tmp;
                vector<pair<ll, int>> marge;
                rep(j, v[0][k][i].size()){
                    marge.pb(v[0][k][i][j]);
                    int idx = v[0][k][i][j].second;
                    tmp.pb({ x[idx],H });
                }
                rep(j, v[1][k^1][i].size()){
                    marge.pb(v[1][k^1][i][j]);
                    int idx = v[1][k^1][i][j].second;
                    tmp.pb({ W,y[idx] });
                }
                sort(all(marge));
                rep(j, marge.size()){
                    int idx = marge[j].second;
                    ans.pb({ tmp[j].first-x[idx]+tmp[j].second-y[idx], idx });
                }
            }
        }
    }
    sort(all(ans));
    rep(i, N) cout << ans[i].second+1 << endl;
}
accuracy: 1
time_ms: 670
memory_kb: 62536
final_rank: 16

code_snippet: #line 2 "lib/template.cpp"

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using VI = vector<int>;
using VVI = vector<vector<int>>;
using VLL = vector<ll>;
using VVLL = vector<vector<ll>>;
using VB = vector<bool>;
using PII = pair<int, int>;
using PLL = pair<ll, ll>;
constexpr int INF = 1000000007;
constexpr ll INF_LL = 1'000'000'000'000'000'007;
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define newl '\n'

// loops rep(until) / rep(var, until) / rep(var, from, until) / repr (reversed order)
#define OVERLOAD3(_1, _2, _3, name, ...) name
#define rep(...) OVERLOAD3(__VA_ARGS__, REPEAT_FROM_UNTIL, REPEAT_UNTIL, REPEAT)(__VA_ARGS__)
#define REPEAT(times) REPEAT_CNT(_repeat, __COUNTER__, times)
#define REPEAT_CNT(_repeat, cnt, times) REPEAT_CNT_CAT(_repeat, cnt, times)
#define REPEAT_CNT_CAT(_repeat, cnt, times) REPEAT_FROM_UNTIL(_repeat ## cnt, 0, times)
#define REPEAT_UNTIL(name, times) REPEAT_FROM_UNTIL(name, 0, times)
#define REPEAT_FROM_UNTIL(name, from, until) for (int name = from, name ## __until = (until); name < name ## __until; name++)
#define repr(...) OVERLOAD3(__VA_ARGS__, REPR_FROM_UNTIL, REPR_UNTIL, REPEAT)(__VA_ARGS__)
#define REPR_UNTIL(name, times) REPR_FROM_UNTIL(name, 0, times)
#define REPR_FROM_UNTIL(name, from, until) for (int name = (until)-1, name ## __from = (from); name >= name ## __from; name--)

template <typename T>
bool chmin(T& var, T x) { if (var > x) { var = x; return true; } else return false; }
template <typename T>
bool chmax(T& var, T x) { if (var < x) { var = x; return true; } else return false; }
ll power(ll e, ll t, ll mod = INF_LL) {
  ll res = 1; for (; t; t >>= 1, (e *= e) %= mod) if (t & 1) (res *= e) %= mod; return res;
}
template <typename T> T divceil(T m, T d) { return (m + d - 1) / d; }
template <typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}
struct desync { desync() { cin.tie(nullptr); ios_base::sync_with_stdio(false); }} desync_;

// dump(...) for debug
#define repi(it, ds) for (auto it = ds.begin(); it != ds.end(); it++)
class DebugPrint { public: template <typename T> DebugPrint& operator <<(const T& v) {
#ifdef LOCAL
    cerr << v;
#endif
return *this; } } debugos; template <typename T> DebugPrint& operator<<(DebugPrint& os, const
vector<T>& vec) { os << "{"; for (int i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 ==
vec.size() ? "" : ", "); os << "}"; return os; } template <typename T, typename U> DebugPrint&
operator<<(DebugPrint& os, const map<T, U>& map_var) { os << "{"; repi(itr, map_var) { os << *
itr; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } template <
typename T> DebugPrint& operator<<(DebugPrint& os, const set<T>& set_var) { os << "{"; repi(
itr, set_var) { os << *itr; itr++; if (itr != set_var.end()) os << ", "; itr--; } os << "}";
return os; } template <typename T, typename U> DebugPrint& operator<<(DebugPrint& os, const
pair<T, U>& p) { os << "(" << p.first << ", " << p.second << ")"; return os; } void dump_func(
) { debugos << newl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail
&&... tail) { debugos << head; if (sizeof...(Tail) > 0) { debugos << ", "; } dump_func(forward
<Tail>(tail)...); }
#ifdef LOCAL
#define dump(...) debugos << "  " << #__VA_ARGS__ << ": " << "[" << __LINE__ \
<< ":" << __FUNCTION__ << "]" << newl << "    ", dump_func(__VA_ARGS__)
#else
#define dump(...) ((void)0)
#endif


#line 2 "main.cpp"

int h, w, n;
vector<pair<PII, int>> ps; string d;
vector<int> fall;

int diag(int r, int c) {
  return r + c;
}

// black -> 1
bool col(int r, int c) {
  return (r + c) % 2;
}

int di_left_time(int i, int di) {
  return h - ps[i].first.first;
}

int di_right_time(int i, int di) {
  return w - ps[i].first.second;
}

void proc(VI& black, VI& white, int di) {
  auto key = [](auto i){return ps[i].first.second;};
  sort(all(black), [&](auto& a, auto& b){return key(a) < key(b); });
  sort(all(white), [&](auto& a, auto& b){return key(a) < key(b); });
  VI total; merge(all(black), all(white), back_inserter(total), [&](auto& a, auto& b){return key(a) < key(b); });
  dump(black, white, total);
  rep(ii, white.size()) {
    chmin(fall[ps[total[ii]].second], di_left_time(white[ii], di));
  }
  reverse(all(total)); reverse(all(black));
  rep(ii, black.size()) {
    chmin(fall[ps[total[ii]].second], di_right_time(black[ii], di));
  }
}

int main() {
  cin >> w >> h >> n;
  ps.resize(n); d = string(n, '%');
  rep(i, n) cin >> ps[i].first.second >> ps[i].first.first >> d[i];
  rep(i, n) ps[i].first.first--, ps[i].first.second--, ps[i].second = i;
  sort(all(ps), [&](auto a, auto b){
    return diag(a.first.first, a.first.second) < diag(b.first.first, b.first.second);
  });
  fall.assign(n, numeric_limits<int>::max());

  for (auto iter = ps.begin(); iter != ps.end();) {
    int di = diag(iter->first.first, iter->first.second);
    int color = col(iter->first.first, iter->first.second);
    dump(di, color);
    VI black[2], white[2];
    for (; iter != ps.end() && di == diag(iter->first.first, iter->first.second); iter++) {
      if (d[iter->second] == 'E') {
        black[iter->first.second & 1].push_back(iter-ps.begin());
      } else {
        white[iter->first.second & 1].push_back(iter-ps.begin());
      }
    }
    if (color) {
      proc(black[0], white[1], di);
      proc(black[1], white[0], di);
    } else {
      proc(black[0], white[0], di);
      proc(black[1], white[1], di);
    }
  }
  dump(ps);
  dump(fall);

  vector<PII> ti(n); rep(i, n) ti[i].first = fall[i], ti[i].second = i;
  sort(all(ti));

  rep(i, n) cout << ti[i].second+1 << "\n";
}
accuracy: 1
time_ms: 70
memory_kb: 7976
final_rank: 1

code_snippet: #pragma GCC target ("avx2")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include <unordered_set>
#include <unordered_map>
#include <random>
using namespace std;
typedef long long ll;
const ll MOD = 1'000'000'007LL; /*998'244'353LL;*/
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
const int dx[4]={ 1,0,-1,0 };
const int dy[4]={ 0,1,0,-1 };

ll W, H;
int N;
ll x[400000], y[400000];
char d[400000];

vector<pair<ll, int>> v[800005][2][2]; // →:1，↓:0
vector<pair<ll, int>> ans;

signed main(){
    cin >> W >> H >> N;
    rep(i, N){
        cin >> x[i] >> y[i] >> d[i];
        v[x[i]+y[i]][d[i] == 'E'][x[i]%2].pb({ x[i], i });
    }

    rep(i, H+W+3){
        rep(j, 2) rep(k, 2) sort(all(v[i][j][k]));
        if(i%2 == 0){ // 白
            rep(k, 2){
                vector<pair<ll, ll>> tmp;
                vector<pair<ll, int>> marge;
                rep(j, v[i][0][k].size()){
                    marge.pb(v[i][0][k][j]);
                    int idx = v[i][0][k][j].second;
                    tmp.pb({ x[idx],H });
                }
                rep(j, v[i][1][k].size()){
                    marge.pb(v[i][1][k][j]);
                    int idx = v[i][1][k][j].second;
                    tmp.pb({ W,y[idx] });
                }
                sort(all(marge));
                rep(j, marge.size()){
                    int idx = marge[j].second;
                    ans.pb({ tmp[j].first-x[idx]+tmp[j].second-y[idx], idx });
                }
            }
        }
        else{ // 黒
            rep(k, 2){
                vector<pair<ll, ll>> tmp;
                vector<pair<ll, int>> marge;
                rep(j, v[i][0][k].size()){
                    marge.pb(v[i][0][k][j]);
                    int idx = v[i][0][k][j].second;
                    tmp.pb({ x[idx],H });
                }
                rep(j, v[i][1][k^1].size()){
                    marge.pb(v[i][1][k^1][j]);
                    int idx = v[i][1][k^1][j].second;
                    tmp.pb({ W,y[idx] });
                }
                sort(all(marge));
                rep(j, marge.size()){
                    int idx = marge[j].second;
                    ans.pb({ tmp[j].first-x[idx]+tmp[j].second-y[idx], idx });
                }
            }
        }
    }
    sort(all(ans));
    rep(i, N) cout << ans[i].second+1 << endl;
}
accuracy: 0.6666666666666666
time_ms: 20
memory_kb: 78228
final_rank: 20

code_snippet: #pragma GCC target ("avx2")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include <unordered_set>
#include <unordered_map>
#include <random>
using namespace std;
typedef long long ll;
const ll MOD = 1'000'000'007LL; /*998'244'353LL;*/
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
const int dx[4]={ 1,0,-1,0 };
const int dy[4]={ 0,1,0,-1 };

ll W, H;
int N;
int x[200000], y[200000];
char d[200000];

vector<pair<int, int>> v[400005][2][2]; // →:1，↓:0
vector<pair<ll, int>> ans;

signed main(){
    cin >> W >> H >> N;
    rep(i, N){
        cin >> x[i] >> y[i] >> d[i];
        v[x[i]+y[i]][d[i] == 'E'][x[i]%2].pb({ x[i], i });
    }

    rep(i, H+W+3){
        rep(j, 2) rep(k, 2) sort(all(v[i][j][k]));
        if(i%2 == 0){ // 白
            rep(k, 2){
                vector<pair<int, int>> tmp;
                vector<pair<int, int>> marge;
                rep(j, v[i][0][k].size()){
                    marge.pb(v[i][0][k][j]);
                    int idx = v[i][0][k][j].second;
                    tmp.pb({ x[idx],H });
                }
                rep(j, v[i][1][k].size()){
                    marge.pb(v[i][1][k][j]);
                    int idx = v[i][1][k][j].second;
                    tmp.pb({ W,y[idx] });
                }
                sort(all(marge));
                rep(j, marge.size()){
                    int idx = marge[j].second;
                    ans.pb({ tmp[j].first-x[idx]+tmp[j].second-y[idx], idx });
                }
            }
        }
        else{ // 黒
            rep(k, 2){
                vector<pair<int, int>> tmp;
                vector<pair<int, int>> marge;
                rep(j, v[i][0][k].size()){
                    marge.pb(v[i][0][k][j]);
                    int idx = v[i][0][k][j].second;
                    tmp.pb({ x[idx],H });
                }
                rep(j, v[i][1][k^1].size()){
                    marge.pb(v[i][1][k^1][j]);
                    int idx = v[i][1][k^1][j].second;
                    tmp.pb({ W,y[idx] });
                }
                sort(all(marge));
                rep(j, marge.size()){
                    int idx = marge[j].second;
                    ans.pb({ tmp[j].first-x[idx]+tmp[j].second-y[idx], idx });
                }
            }
        }
    }
    sort(all(ans));
    rep(i, N) cout << ans[i].second+1 << endl;
}
accuracy: 0.6666666666666666
time_ms: 10
memory_kb: 40636
final_rank: 19

code_snippet: #include <map>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	int H, W, N;
	cin >> W >> H >> N;
	vector<int> X(N), Y(N); vector<char> D(N);
	for (int i = 0; i < N; ++i) {
		cin >> Y[i] >> X[i] >> D[i]; --Y[i], --X[i];
	}
	map<int, vector<int> > g;
	for (int i = 0; i < N; ++i) {
		g[X[i] + Y[i]].push_back(i);
	}
	vector<int> ans(N);
	for (pair<int, vector<int> > i : g) {
		vector<int> ants = i.second;
		sort(ants.begin(), ants.end(), [&](int va, int vb) { return Y[va] < Y[vb]; });
		queue<int> q_even, q_odd;
		for (int j : ants) {
			if (D[j] == 'E') {
				if (X[j] % 2 == 0) q_even.push(j);
				else q_odd.push(j);
			}
			else if (Y[j] % 2 == 0) {
				if (q_even.empty()) {
					ans[j] = H - X[j];
				}
				else {
					int u = q_even.front(); q_even.pop();
					ans[u] = (H - X[u]) + (Y[j] - Y[u]);
					q_even.push(j);
				}
			}
			else {
				if (q_odd.empty()) {
					ans[j] = H - X[j];
				}
				else {
					int u = q_odd.front(); q_odd.pop();
					ans[u] = (H - X[u]) + (Y[j] - Y[u]);
					q_odd.push(j);
				}
			}
		}
		for (int j : ants) {
			if (D[j] == 'E') {
				if (X[j] % 2 == 0) {
					int u = q_even.front(); q_even.pop();
					ans[u] = (W - Y[u]) + (X[j] - X[u]);
				}
				else {
					int u = q_odd.front(); q_odd.pop();
					ans[u] = (W - Y[u]) + (X[j] - X[u]);
				}
			}
		}
	}
	vector<int> perm(N);
	for (int i = 0; i < N; ++i) {
		perm[i] = i;
	}
	sort(perm.begin(), perm.end(), [&](int i, int j) { return ans[i] != ans[j] ? ans[i] < ans[j] : i < j; });
	for (int i = 0; i < N; ++i) {
		cout << perm[i] + 1 << '\n';
	}
	return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 28152
final_rank: 8

code_snippet: #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

struct Ant {
	int x_y; // x - y
	bool dir;
	int dtime; // estimated
	int id;
};

int div2(int n) {
	if (n % 2 == 0) return n / 2;
	return (n - 1) / 2;
}

int main() {
	int h = ri();
	int w = ri();
	int n = ri();
	std::map<int, std::vector<Ant> > all;
	for (int i = 0; i < n; i++) {
		int x = ri() - 1;
		int y = ri() - 1;
		std::string s;
		std::cin >> s;
		all[x + y].push_back({div2(x - y), s[0] == 'S', s[0] == 'S' ? w - y : h - x, i});
	}
	
	std::map<int, std::vector<int> > res;
	auto process = [&] (std::vector<Ant> &a, std::vector<Ant> &b) {
		/*
		std::cerr << "process" << std::endl;
		std::cerr << "  ";
		for (auto i : a) std::cerr << i.x_y << "," << i.dir << "," << i.dtime << "," << i.id << "     ";
		std::cerr << std::endl;
		std::cerr << "  ";
		for (auto i : b) std::cerr << i.x_y << "," << i.dir << "," << i.dtime << "," << i.id << "     ";
		std::cerr << std::endl;
		*/
		
		std::vector<int> dtimes;
		std::sort(a.begin(), a.end(), [] (auto i, auto j) { return i.x_y < j.x_y; });
		std::sort(b.begin(), b.end(), [] (auto i, auto j) { return i.x_y < j.x_y; });
		for (auto i : b) dtimes.push_back(i.dtime);
		for (auto i : a) dtimes.push_back(i.dtime);
		
		std::vector<Ant> all = a;
		for (auto i : b) all.push_back(i);
		std::sort(all.begin(), all.end(), [] (auto i, auto j) { return i.x_y < j.x_y; });
		
		for (int i = 0; i < (int) all.size(); i++) res[dtimes[i]].push_back(all[i].id);
	};
	
	for (auto &i : all) {
		std::vector<Ant> ants[2][2];
		for (auto j : i.second) {
			ants[j.dir][(j.x_y % 2 + 2) % 2].push_back(j);
		}
		if (i.first & 1) { // different -> collide
			process(ants[0][0], ants[1][1]);
			process(ants[0][1], ants[1][0]);
		} else { // same -> collide
			process(ants[0][0], ants[1][0]);
			process(ants[0][1], ants[1][1]);
		}
	}
	for (auto &i : res) {
		std::sort(i.second.begin(), i.second.end());
		for (auto j : i.second) printf("%d\n", j + 1);
	}
	return 0;
}
accuracy: 1
time_ms: 330
memory_kb: 47008
final_rank: 13

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
#include<unordered_map>
#include<functional>
#include<map>
#include<iomanip>
#include<limits>
#include<unordered_set>
#include<cmath>
#include <numeric>
#include <array>
#include<utility>
#include <complex>
using namespace std;
#pragma region define
//#define M_PI 3.141592653589793238
#define upperbound(v,val) upper_bound(v.begin(),v.end(),val)-v.begin()
#define lowerbound(v,val) lower_bound(v.begin(),v.end(),val)-v.begin()
#define int long long
#define ll long long
#define vel vector<ll>
#define vvel vector<vel>
#define rep(i,n) for(int i=0;i<n;i++)
#define sor(v) sort(v.begin(),v.end())
#define mmax(a,b) a=max(a,b)
#define mmin(a,b) a=min(a,b)
#define mkp(a,b) make_pair(a,b)
#define pin pair<ll,ll>
#define qin pair<pin,ll>
#define V vector
#define Endl endl
#define veb vector<bool>
#define fcout cout << fixed << setprecision(15)
#define rev(s) reverse(s.begin(),s.end())
#define lower(h,val) (lower_bound(h.begin(),h.end(),val)-h.begin())
#define upper(h,val) (upper_bound(h.begin(),h.end(),val)-h.begin())
#define vveb V<veb>
#define omajinai cin.tie(0);ios::sync_with_stdio(false);
#define endl "\n"
#define pb push_back
#pragma endregion
#pragma region Inner Class
int root(int x, vel& pa) {
	if (pa[x] == -1) { return x; }
	int ans = root(pa[x], pa); pa[x] = ans;
	return ans;
}
bool mar(int x, int y, vel& pa) {
	x = root(x, pa);
	y = root(y, pa);
	if (x != y) { pa[x] = y; }
	return (x != y);
}
int gcd(int x, int y) {
	if (x < y) { return gcd(y, x); }
	if (y == 0) { return x; }
	return gcd(y, x % y);
}
int lcm(int x, int y) {
	x = abs(x); y = abs(y);
	return x * (y / gcd(x, y));
}
V<ll> dijk(V<V<pin>> way, int st, ll inf) {
	int n = way.size();
	V<ll> dist(n, inf); dist[st] = 0;
	priority_queue<pin, vector<pin>, greater<pin>> pq;
	pq.push(mkp(0, st));
	veb is_checked(n, false);
	while (!pq.empty()) {
		pin x = pq.top(); pq.pop();
		int pot = x.second;
		if (!is_checked[pot]) {
			is_checked[pot] = true;
			for (auto y : way[pot]) {
				ll nex_dist = x.first + y.second;
				int nex_pot = y.first;
				if (dist[nex_pot] > nex_dist) {
					dist[nex_pot] = nex_dist;
					pq.push(mkp(nex_dist, y.first));
				}
			}
		}
	}
	return dist;
}
int inf = 1234567890;
V<V<pin>> make_w(vvel v) {
	int n = v.size();
	V<V<pin>> ret(n);
	rep(i, n) {
		for (int x : v[i]) {
			ret[i].push_back(mkp(x, 1));
		}
	}
	return ret;
}
void make_tree(vvel& chi, V<pin>& par, int n, V<V<pin>>& way) {
	V<ll> dist = dijk(way, 0, inf * inf);
	par = V<pin>(n, mkp(0, 0));
	chi = vvel(n);
	rep(i, n) {
		for (auto nex : way[i]) {
			int pot = nex.first;
			if (dist[pot] > dist[i]) { chi[i].push_back(pot); }
			else { par[i] = mkp(pot, dist[i] - dist[pot]); }
		}
	}
}
void pri(vel& v) {
	if (v.size() == 0) { return; }
	cout << v[0];
	rep(i, v.size() - 1) { cout << " " << v[i + 1]; }
	cout << endl;
	return;
}
vvel disj_min(vel& v) {
	int n = v.size();
	vvel ret(22, vel(n));
	ret[0] = v;
	rep(i, 21) {
		rep(j, n) {
			int nex = j + (1 << i);
			if (nex < n) {
				ret[i + 1][j] = min(ret[i][j], ret[i][nex]);
			}
			else {
				ret[i + 1][j] = ret[i][j];
			}
		}
	}
	return ret;
}
int find_min(vvel& dv, int l, int r) {
	int i = 21;
	while (l + (1 << i) > r) {
		i--;
	}
	while (i >= 0) {
		if (dv[i][l] > dv[i][r - (1 << i)]) {
			l = r - (1 << i);
		}
		else {
			r = l + (1 << i);
		}
		i--;
	}
	return l;
}
V<V<pin>> dbl(V<pin>& v) {
	V<V<pin>> ans(20, V<pin>(v));
	int n = v.size();
	rep(i, 19) {
		rep(j, n) {
			ans[i + 1][j].first = ans[i][ans[i][j].first].first;
			ans[i + 1][j].second = max(ans[i][j].second, ans[i][ans[i][j].first].second);
		}
	}
	return ans;
}
pin lca(int s, int t, int diff, V<V<pin>>& pa) {
	if (diff < 0) { return lca(t, s, -diff, pa); }
	int ans = 0;
	rep(i, 19) {
		if ((diff & (1 << i)) != 0) {
			mmax(ans, pa[i][s].second);
			s = pa[i][s].first;
		}
	}
	for (int i = 19; i >= 0; i--) {
		if (pa[i][s] != pa[i][t]) {
			mmax(ans, pa[i][s].second);
			s = pa[i][s].first;
			mmax(ans, pa[i][t].second);
			t = pa[i][t].first;
		}
	}
	if (s != t) {
		mmax(ans, pa[0][s].second);
		mmax(ans, pa[0][t].second);
		s = pa[0][s].first;
	}
	return mkp(s, ans);
}

vel dx = { 0,0,-1,1,1,-1 };
vel dy = { 1,-1,0,0,1,1 };
#define all(a) a.begin(),a.end()
template<typename T>
void mk_uni(V<T>& a) {
	std::sort(a.begin(), a.end());
	a.erase(std::unique(a.begin(), a.end()), a.end());
}
#pragma endregion



signed main() {
	int h, w, n; cin >> w >> h >> n;
	vel x(n), y(n); veb d(n,true);
	V<pin> lis(n);
	rep(i, n) { 
		cin >> x[i] >> y[i];
		char a; cin >> a;
		int qx = x[i];
		if (a == 'E') { 
			d[i] = false;
			if ((x[i] + y[i]) % 2 == 1) { qx++; }
		}
		if (qx % 2 == 0) {lis[i] = mkp(x[i] + y[i], i);}
		else {lis[i] = mkp(x[i] + y[i] + 2 * (h + w), i);}
	}
	sor(lis);
	int pot = 0;
	V<pin> ans;
	while (pot < n) {
		int now_pl = lis[pot].first;
		V<pin> al_x;
		vel al_e;
		vel al_s;
		while (pot < n) {
			if (now_pl != lis[pot].first) {
				break;
			}
			int num = lis[pot].second;
			al_x.push_back(mkp(x[num],num));
			if (d[num]) { al_s.push_back(h - y[num]); }
			else { al_e.push_back(w - x[num]); }
			pot++;
		}
		sor(al_x); sor(al_e); sor(al_s);
		int sz = al_x.size();
		int szs = al_s.size();
		rep(i,sz) {
			int num = al_x[i].second;
			if (i < szs) {
				ans.push_back(mkp(al_s[i], num));
			}
			else {
				ans.push_back(mkp(al_e[sz - 1 - i], num));
			}
		}
	}
	sor(ans);
	for (auto b : ans) { cout << b.second + 1 << endl; }
	return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 20972
final_rank: 6

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
#include<unordered_map>
#include<functional>
#include<map>
#include<iomanip>
#include<limits>
#include<unordered_set>
#include<cmath>
#include <numeric>
#include <array>
#include<utility>
#include <complex>
using namespace std;
#pragma region define
//#define M_PI 3.141592653589793238
#define upperbound(v,val) upper_bound(v.begin(),v.end(),val)-v.begin()
#define lowerbound(v,val) lower_bound(v.begin(),v.end(),val)-v.begin()
#define ll long long
#define vel vector<int>
#define vvel vector<vel>
#define rep(i,n) for(int i=0;i<n;i++)
#define sor(v) sort(v.begin(),v.end())
#define mmax(a,b) a=max(a,b)
#define mmin(a,b) a=min(a,b)
#define mkp(a,b) make_pair(a,b)
#define pin pair<int,int>
#define qin pair<pin,int>
#define V vector
#define Endl endl
#define veb vector<bool>
#define fcout cout << fixed << setprecision(15)
#define rev(s) reverse(s.begin(),s.end())
#define lower(h,val) (lower_bound(h.begin(),h.end(),val)-h.begin())
#define upper(h,val) (upper_bound(h.begin(),h.end(),val)-h.begin())
#define vveb V<veb>
#define omajinai cin.tie(0);ios::sync_with_stdio(false);
#define endl "\n"
#define pb push_back
#pragma endregion
#pragma region Inner Class
int root(int x, vel& pa) {
	if (pa[x] == -1) { return x; }
	int ans = root(pa[x], pa); pa[x] = ans;
	return ans;
}
bool mar(int x, int y, vel& pa) {
	x = root(x, pa);
	y = root(y, pa);
	if (x != y) { pa[x] = y; }
	return (x != y);
}
int gcd(int x, int y) {
	if (x < y) { return gcd(y, x); }
	if (y == 0) { return x; }
	return gcd(y, x % y);
}
int lcm(int x, int y) {
	x = abs(x); y = abs(y);
	return x * (y / gcd(x, y));
}
V<ll> dijk(V<V<pin>> way, int st, ll inf) {
	int n = way.size();
	V<ll> dist(n, inf); dist[st] = 0;
	priority_queue<pin, vector<pin>, greater<pin>> pq;
	pq.push(mkp(0, st));
	veb is_checked(n, false);
	while (!pq.empty()) {
		pin x = pq.top(); pq.pop();
		int pot = x.second;
		if (!is_checked[pot]) {
			is_checked[pot] = true;
			for (auto y : way[pot]) {
				ll nex_dist = x.first + y.second;
				int nex_pot = y.first;
				if (dist[nex_pot] > nex_dist) {
					dist[nex_pot] = nex_dist;
					pq.push(mkp(nex_dist, y.first));
				}
			}
		}
	}
	return dist;
}
int inf = 1234567890;
V<V<pin>> make_w(vvel v) {
	int n = v.size();
	V<V<pin>> ret(n);
	rep(i, n) {
		for (int x : v[i]) {
			ret[i].push_back(mkp(x, 1));
		}
	}
	return ret;
}
void make_tree(vvel& chi, V<pin>& par, int n, V<V<pin>>& way) {
	V<ll> dist = dijk(way, 0, inf * inf);
	par = V<pin>(n, mkp(0, 0));
	chi = vvel(n);
	rep(i, n) {
		for (auto nex : way[i]) {
			int pot = nex.first;
			if (dist[pot] > dist[i]) { chi[i].push_back(pot); }
			else { par[i] = mkp(pot, dist[i] - dist[pot]); }
		}
	}
}
void pri(vel& v) {
	if (v.size() == 0) { return; }
	cout << v[0];
	rep(i, v.size() - 1) { cout << " " << v[i + 1]; }
	cout << endl;
	return;
}
vvel disj_min(vel& v) {
	int n = v.size();
	vvel ret(22, vel(n));
	ret[0] = v;
	rep(i, 21) {
		rep(j, n) {
			int nex = j + (1 << i);
			if (nex < n) {
				ret[i + 1][j] = min(ret[i][j], ret[i][nex]);
			}
			else {
				ret[i + 1][j] = ret[i][j];
			}
		}
	}
	return ret;
}
int find_min(vvel& dv, int l, int r) {
	int i = 21;
	while (l + (1 << i) > r) {
		i--;
	}
	while (i >= 0) {
		if (dv[i][l] > dv[i][r - (1 << i)]) {
			l = r - (1 << i);
		}
		else {
			r = l + (1 << i);
		}
		i--;
	}
	return l;
}
V<V<pin>> dbl(V<pin>& v) {
	V<V<pin>> ans(20, V<pin>(v));
	int n = v.size();
	rep(i, 19) {
		rep(j, n) {
			ans[i + 1][j].first = ans[i][ans[i][j].first].first;
			ans[i + 1][j].second = max(ans[i][j].second, ans[i][ans[i][j].first].second);
		}
	}
	return ans;
}
pin lca(int s, int t, int diff, V<V<pin>>& pa) {
	if (diff < 0) { return lca(t, s, -diff, pa); }
	int ans = 0;
	rep(i, 19) {
		if ((diff & (1 << i)) != 0) {
			mmax(ans, pa[i][s].second);
			s = pa[i][s].first;
		}
	}
	for (int i = 19; i >= 0; i--) {
		if (pa[i][s] != pa[i][t]) {
			mmax(ans, pa[i][s].second);
			s = pa[i][s].first;
			mmax(ans, pa[i][t].second);
			t = pa[i][t].first;
		}
	}
	if (s != t) {
		mmax(ans, pa[0][s].second);
		mmax(ans, pa[0][t].second);
		s = pa[0][s].first;
	}
	return mkp(s, ans);
}

vel dx = { 0,0,-1,1,1,-1 };
vel dy = { 1,-1,0,0,1,1 };
#define all(a) a.begin(),a.end()
template<typename T>
void mk_uni(V<T>& a) {
	std::sort(a.begin(), a.end());
	a.erase(std::unique(a.begin(), a.end()), a.end());
}
#pragma endregion



signed main() {
	int h, w, n; cin >> w >> h >> n;
	vel x(n), y(n); veb d(n,true);
	V<pin> lis(n);
	rep(i, n) { 
		cin >> x[i] >> y[i];
		char a; cin >> a;
		int qx = x[i];
		if (a == 'E') { 
			d[i] = false;
			if ((x[i] + y[i]) % 2 == 1) { qx++; }
		}
		if (qx % 2 == 0) {lis[i] = mkp(x[i] + y[i], i);}
		else {lis[i] = mkp(x[i] + y[i] + 2 * (h + w), i);}
	}
	sor(lis);
	int pot = 0;
	V<pin> ans;
	while (pot < n) {
		int now_pl = lis[pot].first;
		V<pin> al_x;
		vel al_e;
		vel al_s;
		while (pot < n) {
			if (now_pl != lis[pot].first) {
				break;
			}
			int num = lis[pot].second;
			al_x.push_back(mkp(x[num],num));
			if (d[num]) { al_s.push_back(h - y[num]); }
			else { al_e.push_back(w - x[num]); }
			pot++;
		}
		sor(al_x); sor(al_e); sor(al_s);
		int sz = al_x.size();
		int szs = al_s.size();
		rep(i,sz) {
			int num = al_x[i].second;
			if (i < szs) {
				ans.push_back(mkp(al_s[i], num));
			}
			else {
				ans.push_back(mkp(al_e[sz - 1 - i], num));
			}
		}
	}
	sor(ans);
	for (auto b : ans) { cout << b.second + 1 << endl; }
	return 0;
}
accuracy: 0.7916666666666666
time_ms: 160
memory_kb: 8072
final_rank: 18

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    int w, h, n;
    cin >> w >> h >> n;
    vector<int> zxy(n);
    vector<array<int, 4>> ant(n);
    for (int i = 0; i < n; ++i) {
        int x, y; char c;
        cin >> x >> y >> c;
        zxy.emplace_back(x+y);
        ant[i] = {x, y, c == 'S', i};
    }
    vector<int> ans(n);
    sort(zxy.begin(), zxy.end());
    zxy.erase(unique(zxy.begin(), zxy.end()), zxy.end());
    vector<vector<array<int, 4>>> ants(zxy.size());
    for (int i = 0; i < n; ++i) {
        int x = lower_bound(zxy.begin(),zxy.end(), ant[i][0]+ant[i][1]) - zxy.begin();
        ants[x].emplace_back(ant[i]);
    }
    for (int i = 0; i < zxy.size(); ++i) {
        int c = zxy[i]&1;
        sort(ants[i].begin(),ants[i].end());
        vector<queue<int>> a(2);
        for (auto &&e : ants[i]) {
            if(e[2]){
                int k = (e[1]&1)^c;
                a[k].emplace(e[3]);
                int tmp = a[k].front(); a[k].pop();
                ans[tmp] = h-e[1];
            }else {
                a[e[1]&1].emplace(e[3]);
            }
        }
        for (auto &&e : ants[i]) {
            if(!e[2]){
                int k = (e[1]&1);
                a[k].emplace(e[3]);
                int tmp = a[k].front(); a[k].pop();
                ans[tmp] = w-e[0];
            }
        }
    }
    vector<int> perm(n);
    iota(perm.begin(),perm.end(), 0);
    sort(perm.begin(),perm.end(), [&](int x, int y){
        if(ans[x] != ans[y]) return ans[x] < ans[y];
        else return x < y;
    });
    for (auto &&j : perm) {
        printf("%d\n", j+1);
    }
    return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 20052
final_rank: 7

code_snippet: #include <bits/stdc++.h>
#define _ pair<pair<int,int>,int>
using namespace std;
vector<_> A,B,C,D,v1,v2;
pair<int,int> a[200005];
_ b[200005],c[200005];
int cnt1,cnt2,n,i,j,x,y,m,k;
char kitu;
int T(_ x)
{
    return x.first.first+x.first.second;
}
bool cmp(_ x,_ y)
{
    if (T(x)==T(y)) return x.first.first>y.first.first;
    return T(x)<T(y);
}
void xuly()
{
    //if (A.empty() || B.empty()) return;
    //cout<<A.size()<<' '<<B.size()<<endl;
    C.clear();
    int i,j;i=j=0;
    while (i<A.size() && j<B.size())
    {
        if (A[i].first.first>B[j].first.first) {C.push_back(A[i]);i++;}
        else {C.push_back(B[j]);j++;}
    }
    while (i<A.size()) {C.push_back(A[i]);i++;}
    while (j<B.size()) {C.push_back(B[j]);j++;}
    D.clear();
    for (int i=0;i<A.size();++i) D.push_back(A[i]);
    for (int j=0;j<B.size();++j) D.push_back(B[j]);
    for (int i=0;i<C.size();++i) a[D[i].second].second=C[i].second;
}
void tach()
{

    if (x%2==0)
    {

        A.clear();B.clear();
        for (int i=0;i<v1.size();++i) if (v1[i].first.first%2==0) A.push_back(v1[i]);
        for (int i=0;i<v2.size();++i) if (v2[i].first.first%2==0) B.push_back(v2[i]);
        xuly();
        A.clear();B.clear();
        for (int i=0;i<v1.size();++i) if (v1[i].first.first%2) A.push_back(v1[i]);
        for (int i=0;i<v2.size();++i) if (v2[i].first.first%2) B.push_back(v2[i]);
        xuly();
    }
    else
    {
        A.clear();B.clear();
        for (int i=0;i<v1.size();++i) if (v1[i].first.first%2==0) A.push_back(v1[i]);
        for (int i=0;i<v2.size();++i) if (v2[i].first.first%2) B.push_back(v2[i]);
        xuly();
        A.clear();B.clear();
        for (int i=0;i<v1.size();++i) if (v1[i].first.first%2) A.push_back(v1[i]);
        for (int i=0;i<v2.size();++i) if (v2[i].first.first%2==0) B.push_back(v2[i]);
        xuly();
    }

}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n>>m>>k;
    for (int i=1;i<=k;++i)
    {
        cin>>y>>x;
        cin>>kitu;
        if (kitu=='S')
        {
            a[i].first=m-x;
            b[++cnt1]={{x,y},i};
        }
        else
        {
            a[i].first=n-y;
            c[++cnt2]={{x,y},i};
        }
        a[i].second=i;
    }
    sort(b+1,b+cnt1+1,cmp);
    sort(c+1,c+cnt2+1,cmp);
    i=1;j=1;
    while (i<=cnt1)
    {
        v1.clear();
        v2.clear();
        v1.push_back(b[i]);x=T(b[i]);
        i++;
        while (i<=cnt1 && T(b[i])==x) {v1.push_back(b[i]);i++;}
        while (j<=cnt2 && T(c[j])<x) j++;
        while (j<=cnt2 && T(c[j])==x)
        {
            v2.push_back(c[j]);j++;
        }

    tach();
    }
   // for (int i=1;i<=k;++i) cout<<a[i].first<<' '<<a[i].second<<endl;
    sort(a+1,a+k+1);
    for (int i=1;i<=k;++i) cout<<a[i].second<<'\n';
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 20680
final_rank: 4

code_snippet: #include <iostream>
#include <vector>
#include <array>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
#include <random>
enum class Direction: char {
	East, South
};
std::istream& operator>>(std::istream& is, Direction& dir) {
	char c; is >> c;
	switch (c) {
	case 'E': dir = Direction::East; break;
	case 'S': dir = Direction::South; break;
	default: throw 0;
	}
	return is;
}
struct Ant {
	int x, y, id;
	Direction dir;
	int time_until_drop(int w, int h) const {
		switch (dir) {
		case Direction::East: return w - x + 1;
		case Direction::South: return h - y + 1;
		default: throw 0;
		}
	}
};
int main() {
	int w, h, n; std::cin >> w >> h >> n;
	std::vector<Ant> ants(n);
	for (auto i = 0; i < n; ++i) {
		ants[i].id = i + 1;
		std::cin >> ants[i].x >> ants[i].y >> ants[i].dir;
	}
	std::sort(ants.begin(), ants.end(), [](const Ant& a, const Ant& b) {return (a.y + a.x == b.y + b.x) ? a.x < b.x : a.y + a.x < b.y + b.x; });
	std::vector<std::queue<int>> queue(2);
	for (auto current = ants.begin(); current != ants.end(); ) {
		auto last = std::upper_bound(current, ants.end(), current->y + current->x, [](const int val, const Ant& a) {return val < a.y + a.x; });
		for (auto itr = current; itr != last; ++itr) {
			switch (itr->dir) {
			case Direction::South:
				if (!queue[itr->y % 2].empty()) {
					queue[itr->y % 2].push(itr->id);
					itr->id = queue[itr->y % 2].front(); queue[itr->y % 2].pop();
				}
				break;
			case Direction::East: queue[itr->x % 2].push(itr->id); break;
			default: throw 0;
			}
		}
		for (; current != last; ++current) {
			switch (current->dir) {
			case Direction::South: break;
			case Direction::East: current->id = queue[current->x % 2].front(); queue[current->x % 2].pop(); break;
			default: throw 0;
			}
		}
	}
	std::sort(ants.begin(), ants.end(), [w, h](const Ant& a, const Ant& b) {return (a.time_until_drop(w, h) == b.time_until_drop(w, h)) ? a.id < b.id : a.time_until_drop(w, h) < b.time_until_drop(w, h); });
	for (const auto& ant : ants) std::cout << ant.id << '\n';
}
accuracy: 1
time_ms: 170
memory_kb: 6492
final_rank: 3

code_snippet: #include <iostream>
#include <vector>
#include <array>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
#include <random>

enum class Direction: char {
	East, South
};
std::istream& operator>>(std::istream& is, Direction& dir) {
	char c; is >> c;
	switch (c) {
	case 'E': dir = Direction::East; break;
	case 'S': dir = Direction::South; break;
	default: throw 0;
	}
	return is;
}
struct Ant {
	int x, y, id;
	Direction dir;
	int time_until_drop(int w, int h) const {
		switch (dir) {
		case Direction::East: return w - x + 1;
		case Direction::South: return h - y + 1;
		default: throw 0;
		}
	}
};
int main() {
	int w, h, n; std::cin >> w >> h >> n;
	std::vector<Ant> ants(n);
	for (auto i = 0; i < n; ++i) {
		ants[i].id = i + 1;
		std::cin >> ants[i].x >> ants[i].y >> ants[i].dir;
	}
	std::sort(ants.begin(), ants.end(), [](const Ant& a, const Ant& b) {return (a.y + a.x == b.y + b.x) ? a.x < b.x : a.y + a.x < b.y + b.x; });
	std::vector<std::queue<int>> queue(2);
	for (auto current = ants.begin(); current != ants.end(); ) {
		auto last = std::upper_bound(current, ants.end(), current->y + current->x, [](const int val, const Ant& a) {return val < a.y + a.x; });
		const auto parity = (current->x + current->y) % 2;
		for (auto itr = current; itr != last; ++itr) {
			switch (itr->dir) {
			case Direction::South:
				if (!queue[(itr->x + parity) % 2].empty()) {
					queue[(itr->x + parity) % 2].push(itr->id);
					itr->id = queue[(itr->x + parity) % 2].front(); queue[(itr->x + parity) % 2].pop();
				}
				break;
			case Direction::East: queue[itr->x % 2].push(itr->id); break;
			default: throw 0;
			}
		}
		for (; current != last; ++current) {
			switch (current->dir) {
			case Direction::South: break;
			case Direction::East: current->id = queue[current->x % 2].front(); queue[current->x % 2].pop(); break;
			default: throw 0;
			}
		}
	}
	std::sort(ants.begin(), ants.end(), [w, h](const Ant& a, const Ant& b) {return (a.time_until_drop(w, h) == b.time_until_drop(w, h)) ? a.id < b.id : a.time_until_drop(w, h) < b.time_until_drop(w, h); });
	for (const auto& ant : ants) std::cout << ant.id << '\n';
}
accuracy: 1
time_ms: 170
memory_kb: 6420
final_rank: 2

code_snippet: #include <iostream>
#include <vector>
#include <array>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
#include <random>



enum class Direction {
	East, South
};
std::istream& operator>>(std::istream& is, Direction& dir) {
	char c; is >> c;
	switch (c) {
	case 'E': dir = Direction::East; break;
	case 'S': dir = Direction::South; break;
	default: throw 0;
	}
	return is;
}
struct Ant {
	int x, y, id;
	Direction dir;
	int time_until_drop(int w, int h) const {
		switch (dir) {
		case Direction::East: return w - x + 1;
		case Direction::South: return h - y + 1;
		default: throw 0;
		}
	}
	bool is_on_black() const { return (x + y) % 2 == 1; }
};
int main() {
	int w, h, n; std::cin >> w >> h >> n;
	std::vector<Ant> ants(n);
	for (auto i = 0; i < n; ++i) {
		ants[i].id = i + 1;
		std::cin >> ants[i].x >> ants[i].y >> ants[i].dir;
	}
	std::sort(ants.begin(), ants.end(), [](const Ant& a, const Ant& b) {return (a.y + a.x == b.y + b.x) ? a.x < b.x : a.y + a.x < b.y + b.x; });
	for (auto current = ants.begin(); current != ants.end(); ) {
		auto last = std::upper_bound(current, ants.end(), current->y + current->x, [](const int val, const Ant& a) {return val < a.y + a.x; });
		const auto parity = (current->x + current->y) % 2;
		std::vector<std::queue<int>> queue(2);
		for (auto itr = current; itr != last; ++itr) {
			switch (itr->dir) {
			case Direction::South:
				if (!queue[(itr->x + parity) % 2].empty()) {
					queue[(itr->x + parity) % 2].push(itr->id);
					itr->id = queue[(itr->x + parity) % 2].front(); queue[(itr->x + parity) % 2].pop();
				}
				break;
			case Direction::East: queue[itr->x % 2].push(itr->id); break;
			default: throw 0;
			}
		}
		for (; current != last; ++current) {
			switch (current->dir) {
			case Direction::South: break;
			case Direction::East: current->id = queue[current->x % 2].front(); queue[current->x % 2].pop(); break;
			default: throw 0;
			}
		}
	}
	std::sort(ants.begin(), ants.end(), [w, h](const Ant& a, const Ant& b) {return (a.time_until_drop(w, h) == b.time_until_drop(w, h)) ? a.id < b.id : a.time_until_drop(w, h) < b.time_until_drop(w, h); });
	for (const auto& ant : ants) std::cout << ant.id << '\n';
}
accuracy: 1
time_ms: 230
memory_kb: 6408
final_rank: 5

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

#define int long long

int w, h, n;
int x[200010];
int y[200010];
char d[200010];

map<int, int> zip;
vector<pair<int, int> > member[200010];

int id[200010];

signed main(){
    cin >> w >> h >> n;
    vector<int> vec;

    {
        for(int i = 0;i < n;i++){
            cin >> x[i] >> y[i] >> d[i];
            vec.push_back(y[i]+x[i]);
        }

        sort(vec.begin(), vec.end());
        vec.erase(unique(vec.begin(), vec.end()), vec.end());

        for(int i = 0;i < vec.size();i++){
            zip[vec[i]] = i;
        }
    }

    for(int i = 0;i < n;i++){
        member[zip[y[i]+x[i]]].push_back(make_pair(x[i], i));
    }

    for(int i = 0;i < vec.size();i++){
        sort(member[i].begin(), member[i].end(), greater<pair<int, int> >());

        queue<int> que1;
        queue<int> que2;

        queue<int> ans;

        for(int j = 0;j < member[i].size();j++){
            int idx = member[i][j].second;
            if(d[idx] == 'S'){
                if(x[idx] % 2 == 0){
                    que1.push(idx);
                }else{
                    que2.push(idx);
                }
            }else{
                if((y[idx]+x[idx])%2 == 0){
                    if(x[idx]%2 == 0){
                        que1.push(idx);
                        ans.push(que1.front());
                        que1.pop();
                    }else{
                        que2.push(idx);
                        ans.push(que2.front());
                        que2.pop();
                    }
                }else{
                    if(x[idx]%2 == 1){
                        que1.push(idx);
                        ans.push(que1.front());
                        que1.pop();
                    }else{
                        que2.push(idx);
                        ans.push(que2.front());
                        que2.pop();
                    }
                }
            }
        }

        for(int j = 0;j < member[i].size();j++){
            int idx = member[i][j].second;
            if(d[idx] == 'S'){
                if(x[idx] % 2 == 0){
                    id[idx] = que1.front();
                    que1.pop();
                }else{
                    id[idx] = que2.front();
                    que2.pop();
                }
            }else{
                id[idx] = ans.front();
                ans.pop();
            }
        }
    }

    priority_queue<pair<int, int> > ans;

    for(int i = 0;i < n;i++){
        if(d[i] == 'S'){
            ans.push(make_pair(-(h-y[i]), -id[i]));
        }else{
            ans.push(make_pair(-(w-x[i]), -id[i]));
        }
    }

    while(!ans.empty()){
        cout << -ans.top().second+1 << endl;
        ans.pop();
    }

    return 0;
}
accuracy: 1
time_ms: 570
memory_kb: 36912
final_rank: 14

code_snippet: #include <iostream>
#include <vector>
#include <map>
#include <tuple>
#include <algorithm>
using namespace std;
#pragma warning (disable: 4996)

int W, H, N, x[200009], y[200009], ans[200009]; char c[200009]; bool used[200009];
map<int, vector<tuple<int, char, int>>>vec; vector<int>K;

void solve(int pos) {
	vector<tuple<int, char, int>>tup = vec[pos];
	sort(tup.begin(), tup.end());

	vector<pair<int, int>> GX, GY;
	for (int i = 0; i < tup.size(); i++) {
		if (get<1>(tup[i]) == 'E') GX.push_back(make_pair(i, get<2>(tup[i])));
		if (get<1>(tup[i]) == 'A') GY.push_back(make_pair(i, get<2>(tup[i])));
	}

	for (int i = 0; i < GX.size(); i++) {
		int T1 = i, T2 = lower_bound(GY.begin(), GY.end(), make_pair(GX[i].first, 0)) - GY.begin(); int KOSUU = GY.size() - T2;
		if (T1 < KOSUU) {
			ans[GY[T2 + T1].second] = GX[i].second;
		}
		else {
			ans[GX[T1 - KOSUU].second] = GX[i].second;
		}
	}
	for (int i = 0; i < GY.size(); i++) {
		int T1 = i, T2 = lower_bound(GX.begin(), GX.end(), make_pair(GY[i].first, 0)) - GX.begin(); int KOSUU = GY.size() - T1 - 1;
		if (KOSUU < T2) {
			ans[GX[(T2 - 1) - KOSUU].second] = GY[i].second;
		}
		else {
			ans[GY[T1 + T2].second] = GY[i].second;
		}
	}
}

int main() {
	cin >> W >> H >> N;
	for (int i = 1; i <= N; i++) {
		cin >> x[i] >> y[i] >> c[i]; if (c[i] == 'S') c[i] = 'A';
		int ID = x[i] + y[i];
		if ((x[i] + y[i]) % 2 == 1) {
			if (c[i] == 'E') x[i]--;
			if (c[i] == 'A') y[i]--;
			used[i] = true;
		}
		if (x[i] % 2 == 1) ID += (H + W);
		vec[ID].push_back(make_tuple(x[i], c[i], i));
		K.push_back(ID);
	}
	sort(K.begin(), K.end()); K.erase(unique(K.begin(), K.end()), K.end());

	for (int i = 0; i < K.size(); i++) solve(K[i]);
	for (int i = 1; i <= N; i++) { if (ans[i] == 0) ans[i] = i; } // i 番ルートを通る蟻の番号

	vector<pair<int, int>>ret;
	for (int i = 1; i <= N; i++) {
		int G = 1; if (used[i] == true) G = 0;
		if (c[i] == 'E') ret.push_back(make_pair(W - x[i] + G, ans[i]));
		if (c[i] == 'A') ret.push_back(make_pair(H - y[i] + G, ans[i]));
	}
	sort(ret.begin(), ret.end());
	for (int i = 0; i < N; i++) cout << ret[i].second << endl;
	return 0;
}
accuracy: 1
time_ms: 440
memory_kb: 30268
final_rank: 12

code_snippet: #include <stdio.h>
#include <algorithm>
#include <utility>
#include <map>
#include <deque>
#include <vector>
using namespace std;
typedef pair<int,int> pi;
#define F first
#define S second
#define PB push_back
const int N=2e5+10;
struct ant{
    int x;
    int y;
    bool face;
    void in(){
        char c[5];
        scanf("%d%d",&x,&y);
        scanf("%s",c);
        face=c[0]=='E';
        return ;
    }
    int pos(){return x+y;}
    bool black(){return (x+y)%2;}
};

int main(){
    vector<pi> ans,temp;
    vector<int> emp,l[2],r[2];
    map<int,vector<int>>m;
    deque<int>dq[2];
    int n,w,h,p[N];
    ant a[N];
    scanf("%d%d%d",&w,&h,&n);
    for(int i=1;i<=n;i++){
        a[i].in();
        if(m.find(a[i].pos())==m.end())m.insert({a[i].pos(),emp});
        m[a[i].pos()].PB(i);
    }
    for(pair<int,vector<int>> i:m){
        l[0].clear();
        l[1].clear();
        r[0].clear();
        r[1].clear();
        temp.clear();
        for(int j:i.S)temp.PB({a[j].x,j});
        sort(temp.begin(),temp.end());
        reverse(temp.begin(),temp.end());
        if(a[temp.front().S].black()){
            for(pi j:temp){
                if(a[j.S].face){
                    if(dq[(j.F+1)%2].empty())p[j.S]=j.S;
                    else{
                        p[j.S]=dq[(j.F+1)%2].front();
                        dq[(j.F+1)%2].pop_front();
                        dq[(j.F+1)%2].PB(j.S);
                    }
                }
                else {
                    l[j.F%2].PB(j.S);
                    dq[j.F%2].PB(j.S);
                }
            }
        }
        else{
            for(pi j:temp){
                if(a[j.S].face){
                    if(dq[j.F%2].empty())p[j.S]=j.S;
                    else{
                        p[j.S]=dq[j.F%2].front();
                        dq[j.F%2].pop_front();
                        dq[j.F%2].PB(j.S);
                    }
                }
                else {
                    l[j.F%2].PB(j.S);
                    dq[j.F%2].PB(j.S);
                }
            }
        }
        while(!dq[0].empty()){
            r[0].PB(dq[0].front());
            dq[0].pop_front();
        }
        while(!dq[1].empty()){
            r[1].PB(dq[1].front());
            dq[1].pop_front();
        }
        for(int j=0;j<r[0].size();j++)p[l[0][j]]=r[0][j];
        for(int j=0;j<r[1].size();j++)p[l[1][j]]=r[1][j];
    }
    for(int i=1;i<=n;i++)if(a[i].face)ans.PB({w-a[i].x,p[i]});
    else ans.PB({h-a[i].y,p[i]});
    sort(ans.begin(),ans.end());
    for(pi i:ans)printf("%d\n",i.S);
}
accuracy: 1
time_ms: 220
memory_kb: 29428
final_rank: 9