Problem:
Score: 600 points

Problem Statement

M-kun is a brilliant air traffic controller.

On the display of his radar, there are N airplanes numbered 1, 2, ..., N, all flying at the same altitude.

Each of the airplanes flies at a constant speed of 0.1 per second in a constant direction. The current coordinates of the airplane numbered i are (X_i, Y_i), and the direction of the airplane is as follows:

if U_i is U, it flies in the positive y direction;

if U_i is R, it flies in the positive x direction;

if U_i is D, it flies in the negative y direction;

if U_i is L, it flies in the negative x direction.

To help M-kun in his work, determine whether there is a pair of airplanes that will collide with each other if they keep flying as they are now.

If there is such a pair, find the number of seconds after which the first collision will happen.

We assume that the airplanes are negligibly small so that two airplanes only collide when they reach the same coordinates simultaneously.

Constraints

1 \leq N \leq 200000

0 \leq X_i, Y_i \leq 200000

U_i is U, R, D, or L.

The current positions of the N airplanes, (X_i, Y_i), are all distinct.

N, X_i, and Y_i are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1 U_1
X_2 Y_2 U_2
X_3 Y_3 U_3
:
X_N Y_N U_N

Output

If there is a pair of airplanes that will collide with each other if they keep flying as they are now, print an integer representing the number of seconds after which the first collision will happen.

If there is no such pair, print SAFE.

Sample Input 1

2
11 1 U
11 47 D

Sample Output 1

230

If the airplanes keep flying as they are now, two airplanes numbered 1 and 2 will reach the coordinates (11, 24) simultaneously and collide.

Sample Input 2

4
20 30 U
30 20 R
20 10 D
10 20 L

Sample Output 2

SAFE

No pair of airplanes will collide.

Sample Input 3

8
168 224 U
130 175 R
111 198 D
121 188 L
201 116 U
112 121 R
145 239 D
185 107 L

Sample Output 3

100

Solution in C (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define repp(i,l,r,k)for(ll i=(l);i<(r);i+=(k))
#define INF ((1LL<<62)-(1LL<<31))
#define max(p,q)((p)>(q)?(p):(q))
#define min(p,q)((p)<(q)?(p):(q))
#define bit(n,m)(((n)>>(m))&1)
int upll(const void*a, const void*b){return*(ll*)a<*(ll*)b?-1:*(ll*)a>*(ll*)b?1:0;}
int downll(const void*a, const void*b){return*(ll*)a<*(ll*)b?1:*(ll*)a>*(ll*)b?-1:0;}
void sortup(ll*a,int n){qsort(a,n,sizeof(ll),upll);}
void sortdown(ll*a,int n){qsort(a,n,sizeof(ll),downll);}
ll pom(ll a,ll n,int m){ll x=1;for(a%=m;n;n/=2)n&1?x=x*a%m:0,a=a*a%m;return x;}
//#define MOD 998244353
#define MOD 1000000007
#define invp(a,p)pom(a,p-2,p)

typedef struct tuple{int x,y;}T;

int xcmp(const void*p,const void*q){
	T *pp=(T*)p,*qq=(T*)q;
	if(pp->x<qq->x)return-1;
	if(pp->x>qq->x)return 1;
	return 0;
}
int xcmp2(const void*p,const void*q){
	T *pp=(T*)p,*qq=(T*)q;
	if(pp->x<qq->x)return 1;
	if(pp->x>qq->x)return-1;
	return 0;
}
int ycmp(const void*p,const void*q){
	T *pp=(T*)p,*qq=(T*)q;
	if(pp->y<qq->y)return-1;
	if(pp->y>qq->y)return 1;
	return 0;
}

int cnt[4];
T temp[4][200010];

int hoge[800010],*aaa=hoge+400005;
int hoge2[800010],*bbb=hoge2+400005;

int main(){
	int n;
	scanf("%d",&n);
	rep(i,0,n){
		int x,y;
		char c;
		scanf("%d%d %c",&x,&y,&c);
		T*t;
		if(c=='U')t=temp[0]+cnt[0]++;
		if(c=='D')t=temp[1]+cnt[1]++;
		if(c=='R')t=temp[2]+cnt[2]++;
		if(c=='L')t=temp[3]+cnt[3]++;
		t->x=x;
		t->y=y;
	}
	int ans=1e9;
	//正面衝突1
	qsort(temp[0],cnt[0],sizeof(T),xcmp);
	qsort(temp[1],cnt[1],sizeof(T),xcmp);
	for(int l0=0,l1=0,x=0;x<=200000;x++){
		int r0=l0;
		while(r0<cnt[0]&&temp[0][r0].x==x)r0++;
		int r1=l1;
		while(r1<cnt[1]&&temp[1][r1].x==x)r1++;
		rep(i,l0,r0)rep(j,l1,r1)if(temp[0][i].y<temp[1][j].y)ans=min(ans,(temp[1][j].y-temp[0][i].y)*5);
		l0=r0,l1=r1;
	}
	//正面衝突2
	qsort(temp[2],cnt[2],sizeof(T),ycmp);
	qsort(temp[3],cnt[3],sizeof(T),ycmp);
	for(int l0=0,l1=0,y=0;y<=200000;y++){
		int r0=l0;
		while(r0<cnt[2]&&temp[2][r0].y==y)r0++;
		int r1=l1;
		while(r1<cnt[3]&&temp[3][r1].y==y)r1++;
		rep(i,l0,r0)rep(j,l1,r1)if(temp[2][i].x<temp[3][j].x)ans=min(ans,(temp[3][j].x-temp[2][i].x)*5);
		l0=r0,l1=r1;
	}
	//左から
	//aaa[i]=時刻0に(x,i)にいると下から衝突
	//bbb[i]=時刻0に(x,i)にいると上から衝突
	qsort(temp[0],cnt[0],sizeof(T),ycmp);
	qsort(temp[1],cnt[1],sizeof(T),ycmp);
	qsort(temp[2],cnt[2],sizeof(T),xcmp);
	for(int l0=0,l1=0,l2=0,x=0;x<=200000;x++){
		while(l2<cnt[2]&&temp[2][l2].x==x){
			aaa[temp[2][l2].y]=x+1;
			bbb[temp[2][l2].y]=x+1;
			l2++;
		}
		while(l0<cnt[0]&&temp[0][l0].x==x){
			if(aaa[temp[0][l0].y])ans=min(ans,(x+1-aaa[temp[0][l0].y])*10);
			l0++;
		}
		while(l1<cnt[1]&&temp[1][l1].x==x){
			if(bbb[temp[1][l1].y])ans=min(ans,(x+1-bbb[temp[1][l1].y])*10);
			l1++;
		}
		aaa++;
		bbb--;
	}
	
	//右から
	//aaa[i]=時刻0に(x,i)にいると下から衝突
	//bbb[i]=時刻0に(x,i)にいると上から衝突
	rep(i,0,800005)hoge[i]=hoge2[i]=0;
	aaa=hoge+400005,bbb=hoge2+400005;
	
	qsort(temp[3],cnt[3],sizeof(T),xcmp2);
	for(int l0=0,l1=0,l2=0,x=200000;x>=0;x--){
		while(l2<cnt[3]&&temp[3][l2].x==x){
			aaa[temp[3][l2].y]=x+1;
			bbb[temp[3][l2].y]=x+1;
			l2++;
		}
		while(l0<cnt[0]&&temp[0][l0].x==x){
			if(aaa[temp[0][l0].y])ans=min(ans,-(x+1-aaa[temp[0][l0].y])*10);
			l0++;
		}
		while(l1<cnt[1]&&temp[1][l1].x==x){
			if(bbb[temp[1][l1].y])ans=min(ans,-(x+1-bbb[temp[1][l1].y])*10);
			l1++;
		}
		aaa++;
		bbb--;
	}
	
	
	if(ans==1e9)puts("SAFE");
	else printf("%d",ans);
}