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)

// ---------- begin SegmentTree Point update Range query ----------
mod segment_tree {
    pub struct PURQ<T: Clone, F: Fn(T, T) -> T> {
        n: usize,
        a: Vec<T>,
        id: T,
        op: F,
    }
    #[allow(dead_code)]
    impl<T: Clone, F: Fn(T, T) -> T> PURQ<T, F> {
        pub fn new(n: usize, id: T, op: F) -> PURQ<T, F> {
            let mut k = 1;
            while k < n {
                k *= 2;
            }
            PURQ {
                n: k,
                a: vec![id.clone(); 2 * k],
                id: id,
                op: op,
            }
        }
        pub fn update(&mut self, x: usize, v: T) {
            let mut k = self.n + x;
            let a = &mut self.a;
            a[k] = v;
            k >>= 1;
            while k > 0 {
                a[k] = (self.op)(a[2 * k].clone(), a[2 * k + 1].clone());
                k >>= 1;
            }
        }
        pub fn update_tmp(&mut self, x: usize, v: T) {
            self.a[x + self.n] = v;
        }
        pub fn update_all(&mut self) {
            for k in (1..(self.n)).rev() {
                self.a[k] = (self.op)(self.a[2 * k].clone(), self.a[2 * k + 1].clone());
            }
        }
        pub fn find(&self, mut l: usize, mut r: usize) -> T {
            let mut p = self.id.clone();
            let mut q = self.id.clone();
            l += self.n;
            r += self.n;
            while l < r {
                if (l & 1) == 1 {
                    p = (self.op)(p, self.a[l].clone());
                    l += 1;
                }
                if (r & 1) == 1 {
                    r -= 1;
                    q = (self.op)(self.a[r].clone(), q);
                }
                l >>= 1;
                r >>= 1;
            }
            (self.op)(p, q)
        }
    }
}
// ---------- end SegmentTree Point update Range query ----------

use proconio::*;
use proconio::marker::*;

fn safe(m: i64, p: &[(i64, i64, u8)]) -> bool {
    let mut row = vec![];
    let mut col = vec![];
    for &(x, y, c) in p.iter() {
        match c {
            b'U' => col.push((x, y, y + m)),
            b'D' => col.push((x, y - m, y)),
            b'L' => row.push((y, x - m, x)),
            b'R' => row.push((y, x, x + m)),
            _ => unreachable!(),
        }
    }
    row.sort();
    col.sort();
    for p in row.windows(2) {
        if p[0].0 == p[1].0 && p[0].2 >= p[1].1 {
            return false;
        }
    }
    for p in col.windows(2) {
        if p[0].0 == p[1].0 && p[0].2 >= p[1].1 {
            return false;
        }
    }
    let mut z = vec![];
    let mut event = vec![];
    for &(x, d, u) in col.iter() {
        z.push(x);
        event.push((d, 0, x, 1));
        event.push((u, 2, x, -1));
    }
    for &(y, l, r) in row.iter() {
        z.push(l);
        z.push(r + 1);
        event.push((y, 1, l, r));
    }
    z.sort();
    z.dedup();
    let mut cnt = vec![0; z.len()];
    event.sort();
    let mut seg = segment_tree::PURQ::new(z.len(), 0, |a, b| a + b);
    for (_, op, l, r) in event {
        if op == 1 {
            let l = z.binary_search(&l).unwrap();
            let r = z.binary_search(&(r + 1)).unwrap();
            if seg.find(l, r) > 0 {
                return false;
            }
        } else {
            let x = z.binary_search(&l).unwrap();
            cnt[x] += r;
            seg.update(x, cnt[x]);
        }
    }
    true
}

fn run() {
    input! {
        n: usize,
        p: [(i64, i64, Bytes); n],
    }
    let p = p.into_iter().map(|(x, y, s)| (x, y, s[0])).collect::<Vec<_>>();
    let inf = 400000;
    let mut ok = 0;
    let mut ng = inf;
    while ng - ok > 1 {
        let mid = (ok + ng) / 2;
        if safe(mid, &p) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    if ng == inf {
        println!("SAFE");
    } else {
        println!("{}", ng * 10);
    }
}

fn main() {
    run();
}