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 Rust (1.42.0) (judge verdict: Wrong Answer)

#![allow(unused, non_snake_case, dead_code, non_upper_case_globals)]
use {
    proconio::{marker::*, *},
    std::{cmp::*, collections::*, mem::*},
};

#[fastout]
fn main() {
    input! {//
        n:usize,
        xyu:[(i64,i64,char);n],
    }

    let mut xs = BTreeSet::new();
    let mut ys = BTreeSet::new();
    let mut xsv = vec![];
    let mut ysv = vec![];
    let mut xxs = vec![vec![]; 222222];
    let mut yys = vec![vec![]; 222222];
    for &(x, y, u) in xyu.iter() {
        if u == 'L' || u == 'R' {
            ys.insert((y, x));
            ysv.push((y, x, u));
            yys[y as usize].push((x, u));
        }
        if u == 'D' || u == 'U' {
            xs.insert((x, y));
            xsv.push((x, y, u));
            xxs[x as usize].push((y, u));
        }
    }
    xsv.sort();
    ysv.sort();
    let mut ans = INF;
    for (i, x) in xxs.clone().iter().enumerate() {
        if x.len() > 1 {
            let mut xs = x.clone();
            xs.sort();
            let mut ds = vec![];
            let mut us = vec![];
            for &(y, u) in xs.iter() {
                if u == 'D' {
                    ds.push(y);
                } else {
                    us.push(y);
                }
            }
            for d in us.iter() {
                let mut uu = ds.upper_bound(&d);
                if uu == ds.len() {
                    break;
                } else {
                    let sa = ds[uu] - d;
                    ans = min(ans, sa * 5);
                }
            }
        }
    }
    for (i, x) in yys.clone().iter().enumerate() {
        if x.len() > 1 {
            let mut xs = x.clone();
            xs.sort();
            let mut ds = vec![];
            let mut us = vec![];
            for &(y, u) in xs.iter() {
                if u == 'L' {
                    ds.push(y);
                } else {
                    us.push(y);
                }
            }
            for d in us.iter() {
                let mut uu = ds.upper_bound(&d);
                if uu == ds.len() {
                    break;
                } else {
                    let sa = ds[uu] - d;
                    ans = min(ans, sa * 5);
                }
            }
        }
    }

    for &(x, y, u) in xyu.iter() {
        if u == 'L' {
            // xが xいないのやつ
            let ind = xsv.lower_bound(&(x, 0, 'A'));
            for l in 0..ind {
                // majiwaruten: 高さy, xsv[l].1
                let sa = x - xsv[l].0;
                let say = if xsv[l].2 == 'D' {
                    xsv[l].1 - y
                } else {
                    y - xsv[l].1
                };
                if sa == say {
                    ans = min(ans, sa * 10);
                }
            }
        }
        if u == 'R' {
            // xが xいないのやつら
            let ind = xsv.lower_bound(&(x, 0, 'A'));
            for l in ind..xsv.len() {
                // majiwaruten: 高さy, xsv[l].1
                let sa = xsv[l].0 - x;
                let say = if xsv[l].2 == 'D' {
                    xsv[l].1 - y
                } else {
                    y - xsv[l].1
                };
                if sa == say {
                    ans = min(ans, sa * 10);
                }
            }
        }
        if u == 'U' {
            // xが xいないのやつら
            let ind = ysv.lower_bound(&(0, y, 'A'));
            for l in 0..ind {
                // majiwaruten: 高さy, xsv[l].1
                let sa = ysv[l].0 - y;
                let say = if ysv[l].2 == 'L' {
                    ysv[l].1 - y
                } else {
                    y - ysv[l].1
                };
                if sa == say {
                    ans = min(ans, sa * 10);
                }
            }
        }
        if u == 'D' {
            // xが xいないのやつら
            let ind = ysv.lower_bound(&(0, y, 'A'));
            for l in ind..ysv.len() {
                // majiwaruten: 高さy, xsv[l].1
                let sa = y - ysv[l].0;
                let say = if ysv[l].2 == 'L' {
                    ysv[l].1 - y
                } else {
                    y - ysv[l].1
                };
                if sa == say {
                    ans = min(ans, sa * 10);
                }
            }
        }
    }
    if ans == INF {
        println!("SAFE");
    } else {
        println!("{}", ans);
    }

    // let mut r = 4000000;
    // let mut l = 0;
    // while r - l > 1 {
    //     let mut st = BTreeSet::new();
    //     let mid = (r + l) / 2;
    //     let mut found = false;
    //     for &(x, y, u) in xyu.iter() {
    //         if u == 'U' {
    //             if st.contains(&(x + mid, y)) {
    //                 found = true;
    //             }
    //             st.insert((x + mid, y));
    //         }
    //         if u == 'D' {
    //             if st.contains(&(x - mid, y)) {
    //                 found = true;
    //             }
    //             st.insert((x - mid, y));
    //         }
    //         if u == 'R' {
    //             if st.contains(&(x, y + mid)) {
    //                 found = true;
    //             }
    //             st.insert((x, y + mid));
    //         }
    //         if u == 'L' {
    //             if st.contains(&(x, y - mid)) {
    //                 found = true;
    //             }
    //             st.insert((x, y - mid));
    //         }
    //     }
    // }
}
pub trait BinarySearch<T> {
    fn lower_bound_by<F: Fn(&T) -> bool>(&self, f: F) -> usize;
    fn lower_bound(&self, x: &T) -> usize;
    fn upper_bound(&self, x: &T) -> usize;
}
impl<T: Ord> BinarySearch<T> for [T] {
    fn lower_bound_by<F: Fn(&T) -> bool>(&self, f: F) -> usize {
        let mut ng = -1;
        let mut ok = self.len() as i64;
        while (ok as i32 - ng as i32).abs() > 1 {
            let mid = (ok + ng) / 2;
            if f(&self[mid as usize]) {
                ok = mid;
            } else {
                ng = mid;
            }
        }
        ok as usize
    }
    fn lower_bound(&self, x: &T) -> usize {
        self.lower_bound_by(|y| y >= x)
    }
    fn upper_bound(&self, x: &T) -> usize {
        self.lower_bound_by(|y| y > x)
    }
}
const INF: i64 = 1 << 60;