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 Go (1.14.1) (judge verdict: Accepted)

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func Scan() string {
	sc.Scan()
	return sc.Text()
}
func iScan() int {
	n, _ := strconv.Atoi(Scan())
	return n
}
func fScan() float64 {
	n, _ := strconv.ParseFloat(Scan(), 64)
	return n
}
func stringToInt(s string) int {
	n, _ := strconv.Atoi(s)
	return n
}
func SScan(n int) []string {
	a := make([]string, n)
	for i := 0; i < n; i++ {
		a[i] = Scan()
	}
	return a
}
func iSScan(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = iScan()
	}
	return a
}
func abs(x int) int {
	if x < 0 {
		x = -x
	}
	return x
}
func larger(a, b int) int {
	if a < b {
		return b
	} else {
		return a
	}
}
func smaller(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}
func max(a []int) int {
	x := a[0]
	for i := 0; i < len(a); i++ {
		if x < a[i] {
			x = a[i]
		}
	}
	return x
}
func min(a []int) int {
	x := a[0]
	for i := 0; i < len(a); i++ {
		if x > a[i] {
			x = a[i]
		}
	}
	return x
}
func sum(a []int) int {
	x := 0
	for _, v := range a {
		x += v
	}
	return x
}

var mod int = 1000000007

func main() {
	buf := make([]byte, 0)
	sc.Buffer(buf, mod)
	sc.Split(bufio.ScanWords)
	n := iScan()
	pd, pu, pr, pl := make(map[int][]int), make(map[int][]int), make(map[int][]int), make(map[int][]int)
	md, mu, mr, ml := make(map[int][]int), make(map[int][]int), make(map[int][]int), make(map[int][]int)
	yd, yu, yr, yl := make(map[int][]int), make(map[int][]int), make(map[int][]int), make(map[int][]int)
	for i := 0; i < n; i++ {
		x, y := iScan(), iScan()
		u := Scan()
		if u == "D" {
			pd[y-x] = append(pd[y-x], x)
			md[y+x] = append(md[y+x], x)
			yd[x] = append(yd[x], y)
		} else if u == "U" {
			pu[y-x] = append(pu[y-x], x)
			mu[y+x] = append(mu[y+x], x)
			yu[x] = append(yu[x], y)
		} else if u == "R" {
			pr[y-x] = append(pr[y-x], x)
			mr[y+x] = append(mr[y+x], x)
			yr[y] = append(yr[y], x)
		} else if u == "L" {
			pl[y-x] = append(pl[y-x], x)
			ml[y+x] = append(ml[y+x], x)
			yl[y] = append(yl[y], x)
		}
	}
	for k, _ := range pd {
		sort.Ints(pd[k])
	}
	for k, _ := range pu {
		sort.Ints(pu[k])
	}
	for k, _ := range pr {
		sort.Ints(pr[k])
	}
	for k, _ := range pl {
		sort.Ints(pl[k])
	}
	for k, _ := range md {
		sort.Ints(md[k])
	}
	for k, _ := range mu {
		sort.Ints(mu[k])
	}
	for k, _ := range mr {
		sort.Ints(mr[k])
	}
	for k, _ := range ml {
		sort.Ints(ml[k])
	}
	for k, _ := range yd {
		sort.Ints(yd[k])
	}
	for k, _ := range yu {
		sort.Ints(yu[k])
	}
	for k, _ := range yr {
		sort.Ints(yr[k])
	}
	for k, _ := range yl {
		sort.Ints(yl[k])
	}
	ans := mod * mod
	for k, list := range pd {
		z := pr[k]
		for _, v := range list {
			if len(z) != 0 {
				idx := sort.SearchInts(z, v)
				if idx == 0 {
					continue
				} else {
					ans = smaller(ans, abs(v-z[idx-1])*10)
				}
			}
		}
	}
	for k, list := range ml {
		z := md[k]
		for _, v := range list {
			if len(z) != 0 {
				idx := sort.SearchInts(z, v)
				if idx == 0 {
					continue
				} else {
					ans = smaller(ans, abs(v-z[idx-1])*10)
				}
			}
		}
	}
	for k, list := range pl {
		z := pu[k]
		for _, v := range list {
			if len(z) != 0 {
				idx := sort.SearchInts(z, v)
				if idx == 0 {
					continue
				} else {
					ans = smaller(ans, abs(v-z[idx-1])*10)
				}
			}
		}
	}
	for k, list := range mu {
		z := mr[k]
		for _, v := range list {
			if len(z) != 0 {
				idx := sort.SearchInts(z, v)
				if idx == 0 {
					continue
				} else {
					ans = smaller(ans, abs(v-z[idx-1])*10)
				}
			}
		}
	}
	for k, list := range yd {
		z := yu[k]
		for _, v := range list {
			if len(z) != 0 {
				idx := sort.SearchInts(z, v)
				if idx == 0 {
					continue
				} else {
					ans = smaller(ans, abs(v-z[idx-1])*5)
				}
			}
		}
	}
	for k, list := range yl {
		z := yr[k]
		for _, v := range list {
			if len(z) != 0 {
				idx := sort.SearchInts(z, v)
				if idx == 0 {
					continue
				} else {
					ans = smaller(ans, abs(v-z[idx-1])*5)
				}
			}
		}
	}
	if ans == mod*mod {
		fmt.Println("SAFE")
	} else {
		fmt.Println(ans)
	}
}