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



Sample input:
2
11 1 U
11 47 D


Sample output:
230

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque

INF = 1 << 50

def calc(Asum, Bsum, A, B, ans, ope = 1):
    if not len(A) or not len(B):
        return ans

    #print(Asum, Bsum)
    B_ = [val for val, _, _ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        if bi >= len(B_): continue
        x1, y1 = A[ai]
        val, _, bi2 = Bsum[bi]
        #print(bi, val, asum)
        if val == asum:
            x2, y2 = B[bi2]
            time = (x2 - x1) * ope
            if time < 0:
                continue
            ans = min(ans, time*10)
        else:
            continue
    return ans

def calc_line(Asum, Bsum, A, B, ans):
    if not len(A) or not len(B):
        return ans

    # print(Asum, Bsum)
    B_ = [val for val, _,_ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        if bi >= len(B_):continue
        x1, y1 = A[ai]
        val, _, bi2 = Bsum[bi]
        if val == asum:
            bi2 = Bsum[bi][2]
            x2, y2 = B[bi2]
            time = y2 - y1 if x1 == x2 else x2 - x1
            if time < 0:
                continue
            ans = min(ans, time*5)
        else:
            continue
    return ans

def run():
    U, D, R, L = [],[],[],[]
    N = int(sysread())
    for i in range(N):
        x,y,u = input().split()
        #print(x, y, u)
        x,y = int(x), int(y)
        if u == 'U':
            U.append((x,y))
        elif u == 'D':
            D.append((x,y))
        elif u == 'R':
            R.append((x,y))
        elif u == 'L':
            L.append((x,y))

    #print(U, D, R, L)
    ans = INF
    # U vs R
    Usum = sorted([(x+y, i) for i, (x,y) in enumerate(U)])
    Rsum = sorted([(x+y,-x, i) for i, (x,y) in enumerate(R)], key = lambda x : (x[0], x[1]))
    ans = calc(Usum, Rsum, U, R, ans, ope=-1)

    # D vs L
    Dsum = sorted([(x + y, i) for i, (x, y) in enumerate(D)])
    Lsum = sorted([(x + y,-x, i) for i, (x, y) in enumerate(L)], key = lambda x : (x[0], x[1]))
    ans = calc(Dsum, Lsum, D, L, ans, ope=1)

    # U vs L
    Usub = sorted([(x - y, i) for i, (x, y) in enumerate(U)])
    Lsub = sorted([(x - y,-x, i) for i, (x, y) in enumerate(L)], key = lambda x : (x[0], x[1]))
    ans = calc(Usub, Lsub, U, L, ans, ope=1)

    # D vs R
    Dsub = sorted([(x - y, i) for i, (x, y) in enumerate(D)])
    Rsub = sorted([(x - y,-x, i) for i, (x, y) in enumerate(R)], key = lambda x : (x[0], x[1]))
    ans = calc(Dsub, Rsub, D, R, ans, ope=-1)

    # U vs D
    UU = sorted([(x, i) for i, (x,y) in enumerate(U)])
    DD = sorted([(x, -y, i) for i, (x, y) in enumerate(D)], key = lambda x : (x[0], x[1]))
    ans = calc_line(UU, DD, U, D, ans)

    # R vs L
    RR = sorted([(y, i) for i, (x, y) in enumerate(R)])
    LL = sorted([(y, -x, i) for i, (x, y) in enumerate(L)], key = lambda x : (x[0], x[1]))
    ans = calc_line(RR, LL, R, L, ans)

    if ans == INF:
        print('SAFE')
    else:
        print(int(ans))



if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque

INF = 1 << 50

def calc(Asum, Bsum, A, B, ans, ope = 1):
    if not len(A) or not len(B):
        return ans

    #print(Asum, Bsum)
    B_ = [val for val, _, _ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        if bi >= len(B_): continue
        x1, y1 = A[ai]

        val, _, bi2 = Bsum[bi]
        #print(bi, val, asum)
        if val == asum:
            x2, y2 = B[bi2]
            time = (x2 - x1) * ope
            if time < 0:
                continue
            ans = min(ans, time*10)
        else:
            continue
    return ans

def calc_line(Asum, Bsum, A, B, ans):
    if not len(A) or not len(B):
        return ans

    # print(Asum, Bsum)
    B_ = [val for val, _,_ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        if bi >= len(B_):continue
        x1, y1 = A[ai]
        val, _, bi2 = Bsum[bi]
        if val == asum:
            bi2 = Bsum[bi][2]
            x2, y2 = B[bi2]
            time = y2 - y1 if x1 == x2 else x2 - x1
            if time < 0:
                continue
            ans = min(ans, time*5)
        else:
            continue
    return ans

def run():
    U, D, R, L = [],[],[],[]
    N = int(sysread())
    for i in range(N):
        x,y,u = input().split()
        #print(x, y, u)
        x,y = int(x), int(y)
        if u == 'U':
            U.append((x,y))
        elif u == 'D':
            D.append((x,y))
        elif u == 'R':
            R.append((x,y))
        elif u == 'L':
            L.append((x,y))

    #print(U, D, R, L)
    ans = INF
    # U vs R
    Usum = sorted([(x+y, i) for i, (x,y) in enumerate(U)])
    Rsum = sorted([(x+y,x, i) for i, (x,y) in enumerate(R)], key = lambda x : (x[0], x[1]))
    ans = calc(Usum, Rsum, U, R, ans, ope=-1)

    # D vs L
    Dsum = sorted([(x + y, i) for i, (x, y) in enumerate(D)])
    Lsum = sorted([(x + y,-x, i) for i, (x, y) in enumerate(L)], key = lambda x : (x[0], x[1]))
    ans = calc(Dsum, Lsum, D, L, ans, ope=1)

    # U vs L
    Usub = sorted([(x - y, i) for i, (x, y) in enumerate(U)])
    Lsub = sorted([(x - y,-x, i) for i, (x, y) in enumerate(L)], key = lambda x : (x[0], x[1]))
    ans = calc(Usub, Lsub, U, L, ans, ope=1)

    # D vs R
    Dsub = sorted([(x - y, i) for i, (x, y) in enumerate(D)])
    Rsub = sorted([(x - y,x, i) for i, (x, y) in enumerate(R)], key = lambda x : (x[0], x[1]))
    ans = calc(Dsub, Rsub, D, R, ans, ope=-1)

    # U vs D
    UU = sorted([(x, i) for i, (x,y) in enumerate(U)])
    DD = sorted([(x, -y, i) for i, (x, y) in enumerate(D)], key = lambda x : (x[0], x[1]))
    ans = calc_line(UU, DD, U, D, ans)

    # R vs L
    RR = sorted([(y, i) for i, (x, y) in enumerate(R)])
    LL = sorted([(y, -x, i) for i, (x, y) in enumerate(L)], key = lambda x : (x[0], x[1]))
    ans = calc_line(RR, LL, R, L, ans)

    if ans == INF:
        print('SAFE')
    else:
        print(int(ans))



if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque

INF = 1 << 50

def calc(Asum, Bsum, A, B, ans, ope = 1):
    if not len(A) or not len(B):
        return ans

    #print(Asum, Bsum)
    B_ = [val for val, _, _ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        if bi >= len(B_): continue
        x1, y1 = A[ai]

        val, _, bi2 = Bsum[bi]
        #print(bi, val, asum)
        if val == asum:
            x2, y2 = B[bi2]
            time = (x2 - x1) * ope
            if time < 0:
                continue
            ans = min(ans, time*10)
        else:
            continue
    return ans

def calc_line(Asum, Bsum, A, B, ans):
    if not len(A) or not len(B):
        return ans

    # print(Asum, Bsum)
    B_ = [val for val, _,_ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        if bi >= len(B_):continue
        x1, y1 = A[ai]
        val, _, bi2 = Bsum[bi]
        if val == asum:
            bi2 = Bsum[bi][2]
            x2, y2 = B[bi2]
            time = y2 - y1 if x1 == x2 else x2 - x1
            if time < 0:
                continue
            ans = min(ans, time*5)
        else:
            continue
    return ans

def run():
    U, D, R, L = [],[],[],[]
    N = int(sysread())
    for i in range(N):
        x,y,u = input().split()
        #print(x, y, u)
        x,y = int(x), int(y)
        if u == 'U':
            U.append((x,y))
        elif u == 'D':
            D.append((x,y))
        elif u == 'R':
            R.append((x,y))
        elif u == 'L':
            L.append((x,y))

    #print(U, D, R, L)
    ans = INF
    # U vs R
    Usum = sorted([(x+y, i) for i, (x,y) in enumerate(U)])
    Rsum = sorted([(x+y,x, i) for i, (x,y) in enumerate(R)], key = lambda x : (x[0], x[1]))
    ans = calc(Usum, Rsum, U, R, ans, ope=-1)

    # D vs L
    Dsum = sorted([(x + y, i) for i, (x, y) in enumerate(D)])
    Lsum = sorted([(x + y,-x, i) for i, (x, y) in enumerate(L)], key = lambda x : (x[0], x[1]))
    ans = calc(Dsum, Lsum, D, L, ans, ope=1)

    # U vs L
    Usub = sorted([(x - y, i) for i, (x, y) in enumerate(U)])
    Lsub = sorted([(x - y,-x, i) for i, (x, y) in enumerate(L)], key = lambda x : (x[0], x[1]))
    ans = calc(Usub, Lsub, U, L, ans, ope=1)

    # D vs R
    Dsub = sorted([(x - y, i) for i, (x, y) in enumerate(D)])
    Rsub = sorted([(x - y,x, i) for i, (x, y) in enumerate(R)], key = lambda x : (x[0], x[1]))
    ans = calc(Dsub, Rsub, D, R, ans, ope=-1)

    # U vs D
    UU = sorted([(x, i) for i, (x,y) in enumerate(U)])
    DD = sorted([(x, y,i) for i, (x, y) in enumerate(D)], key = lambda x : (x[0], x[1]))
    ans = calc_line(UU, DD, U, D, ans)

    # R vs L
    RR = sorted([(y, i) for i, (x, y) in enumerate(R)])
    LL = sorted([(y, x, i) for i, (x, y) in enumerate(L)], key = lambda x : (x[0], x[1]))
    ans = calc_line(RR, LL, R, L, ans)

    if ans == INF:
        print('SAFE')
    else:
        print(int(ans))



if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque

INF = 1 << 50

def calc(Asum, Bsum, A, B, ans, ope = 1):
    if not len(A) or not len(B):
        return ans

    #print(Asum, Bsum)
    B_ = [val for val, _, _ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        if bi >= len(B_): continue
        x1, y1 = A[ai]

        val = B_[bi]
        #print(bi, val, asum)
        if val == asum:
            bi2 = Bsum[bi][2]
            x2, y2 = B[bi2]
            time = (x2 - x1) * ope
            if time < 0:
                continue
            ans = min(ans, time*10)
        else:
            continue
    return ans

def calc_line(Asum, Bsum, A, B, ans):
    if not len(A) or not len(B):
        return ans

    # print(Asum, Bsum)
    B_ = [val for val, _,_ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        if bi >= len(B_):continue
        x1, y1 = A[ai]
        val = B_[bi]
        if val == asum:
            bi2 = Bsum[bi][2]
            x2, y2 = B[bi2]
            time = y2 - y1 if x1 == x2 else x2 - x1
            if time < 0:
                continue
            ans = min(ans, time*5)
        else:
            continue
    return ans

def run():
    U, D, R, L = [],[],[],[]
    N = int(sysread())
    for i in range(N):
        x,y,u = input().split()
        #print(x, y, u)
        x,y = int(x), int(y)
        if u == 'U':
            U.append((x,y))
        elif u == 'D':
            D.append((x,y))
        elif u == 'R':
            R.append((x,y))
        elif u == 'L':
            L.append((x,y))

    #print(U, D, R, L)
    ans = INF
    # U vs R
    Usum = sorted([(x+y, i) for i, (x,y) in enumerate(U)])
    Rsum = sorted([(x+y,x, i) for i, (x,y) in enumerate(R)], key = lambda x : (x[0], x[1]))
    ans = calc(Usum, Rsum, U, R, ans, ope=-1)

    # D vs L
    Dsum = sorted([(x + y, i) for i, (x, y) in enumerate(D)])
    Lsum = sorted([(x + y,-x, i) for i, (x, y) in enumerate(L)], key = lambda x : (x[0], x[1]))
    ans = calc(Dsum, Lsum, D, L, ans, ope=1)

    # U vs L
    Usub = sorted([(x - y, i) for i, (x, y) in enumerate(U)])
    Lsub = sorted([(x - y,-x, i) for i, (x, y) in enumerate(L)], key = lambda x : (x[0], x[1]))
    ans = calc(Usub, Lsub, U, L, ans, ope=1)

    # D vs R
    Dsub = sorted([(x - y, i) for i, (x, y) in enumerate(D)])
    Rsub = sorted([(x - y,x, i) for i, (x, y) in enumerate(R)], key = lambda x : (x[0], x[1]))
    ans = calc(Dsub, Rsub, D, R, ans, ope=-1)

    # U vs D
    UU = sorted([(x, i) for i, (x,y) in enumerate(U)])
    DD = sorted([(x, -y,i) for i, (x, y) in enumerate(D)], key = lambda x : (x[0], x[1]))
    ans = calc_line(UU, DD, U, D, ans)

    # R vs L
    RR = sorted([(y, i) for i, (x, y) in enumerate(R)])
    LL = sorted([(y, -x, i) for i, (x, y) in enumerate(L)], key = lambda x : (x[0], x[1]))
    ans = calc_line(RR, LL, R, L, ans)

    if ans == INF:
        print('SAFE')
    else:
        print(int(ans))



if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque

INF = 1 << 50

def calc(Asum, Bsum, A, B, ans, ope = 1):
    if not len(A) or not len(B):
        return ans

    #print(Asum, Bsum)
    B_ = [val for val, _, _ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        if bi >= len(B_): continue
        x1, y1 = A[ai]

        val = B_[bi]
        #print(bi, val, asum)
        if val == asum:
            bi2 = Bsum[bi][2]
            x2, y2 = B[bi2]
            time = (x2 - x1) * ope
            if time < 0:
                continue
            ans = min(ans, time*10)
        else:
            continue
    return ans

def calc_line(Asum, Bsum, A, B, ans):
    if not len(A) or not len(B):
        return ans

    # print(Asum, Bsum)
    B_ = [val for val, _,_ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        if bi >= len(B_):continue
        x1, y1 = A[ai]
        val = B_[bi]
        if val == asum:
            bi2 = Bsum[bi][2]
            x2, y2 = B[bi2]
            time = y2 - y1 if x1 == x2 else x2 - x1
            if time < 0:
                continue
            ans = min(ans, time*5)
        else:
            continue
    return ans

def run():
    U, D, R, L = [],[],[],[]
    N = int(sysread())
    for i in range(N):
        x,y,u = input().split()
        #print(x, y, u)
        x,y = int(x), int(y)
        if u == 'U':
            U.append((x,y))
        elif u == 'D':
            D.append((x,y))
        elif u == 'R':
            R.append((x,y))
        elif u == 'L':
            L.append((x,y))

    #print(U, D, R, L)
    ans = INF
    # U vs R
    Usum = sorted([(x+y, i) for i, (x,y) in enumerate(U)])
    Rsum = sorted([(x+y,x, i) for i, (x,y) in enumerate(R)])
    ans = calc(Usum, Rsum, U, R, ans, ope=-1)

    # D vs L
    Dsum = sorted([(x + y, i) for i, (x, y) in enumerate(D)])
    Lsum = sorted([(x + y,-x, i) for i, (x, y) in enumerate(L)])
    ans = calc(Dsum, Lsum, D, L, ans, ope=1)

    # U vs L
    Usub = sorted([(x - y, i) for i, (x, y) in enumerate(U)])
    Lsub = sorted([(x - y,-x, i) for i, (x, y) in enumerate(L)])
    ans = calc(Usub, Lsub, U, L, ans, ope=1)

    # D vs R
    Dsub = sorted([(x - y, i) for i, (x, y) in enumerate(D)])
    Rsub = sorted([(x - y,x, i) for i, (x, y) in enumerate(R)])
    ans = calc(Dsub, Rsub, D, R, ans, ope=-1)

    # U vs D
    UU = sorted([(x, i) for i, (x,y) in enumerate(U)])
    DD = sorted([(x, -y,i) for i, (x, y) in enumerate(D)])
    ans = calc_line(UU, DD, U, D, ans)

    # R vs L
    RR = sorted([(y, i) for i, (x, y) in enumerate(R)])
    LL = sorted([(y, -x, i) for i, (x, y) in enumerate(L)])
    ans = calc_line(RR, LL, R, L, ans)

    if ans == INF:
        print('SAFE')
    else:
        print(int(ans))



if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
N = int(input())
XYUs = [list(map(str,input().split())) for _ in range(N)]

for i in range(len(XYUs)):
    XYUs[i][0] = int(XYUs[i][0])
    XYUs[i][1] = int(XYUs[i][1])


def check_the_conflict(XYUs):
    slopes = [[] for _ in range(400001)]
    for XYU in XYUs:
        if XYU[2] == "R" or XYU[2] == "D":       
            slopes[int(XYU[1])-int(XYU[0])+200000].append([int(XYU[0]),XYU[2]])    
    return slopes

def rotate_matrix(XYUs):
    converted = []
    for XYU in XYUs:
        if XYU[2] == "R":
            direction = "D"
        elif XYU[2] == "D":
            direction = "L"
        elif XYU[2] == "L":
            direction = "U"
        elif XYU[2] == "U":
            direction = "R"
        converted.append([XYU[1], 200000-XYU[0], direction])
    return converted
    
min_val = int(1e+10)

for i in range(4):
    if i > 0:
        XYUs = rotate_matrix(XYUs)
    slopes = check_the_conflict(XYUs)
    for slope in slopes:
        now_R = -int(1e+10)
        slope = sorted(slope)
        for airplane in slope:
            if airplane[1] == "R":
                now_R = airplane[0]
            elif airplane[1] == "D":
                candidate = airplane[0] - now_R
                if candidate < min_val:
                    min_val = candidate
    if i in [0,1]:
        rows = [[] for _ in range(200001)]
        for XYU in XYUs:
            rows[XYU[0]].append([XYU[1],XYU[2]])
        for row in rows:
            now_U = -int(1e+10)
            row = sorted(row)
            for airplane in row:
                if airplane[1] == "U":
                    now_U = airplane[0]
                elif airplane[1] == "D":
                    candidate = (airplane[0] - now_U)/2
                    if candidate < min_val:
                        min_val = candidate
                        
if min_val < 200010:
	print(min_val)
else:
  print("SAFE")

Submission status: Wrong Answer

Solution code:

N = int(input())
XYUs = [list(map(str,input().split())) for _ in range(N)]

for i in range(len(XYUs)):
    XYUs[i][0] = int(XYUs[i][0])
    XYUs[i][1] = int(XYUs[i][1])


def check_the_conflict(XYUs):
    slopes = [[] for _ in range(400001)]
    for XYU in XYUs:
        if XYU[2] == "R" or XYU[2] == "D":       
            slopes[int(XYU[1])-int(XYU[0])+200000].append([int(XYU[0]),XYU[2]])    
    return slopes

def rotate_matrix(XYUs):
    converted = []
    for XYU in XYUs:
        if XYU[2] == "R":
            direction = "D"
        elif XYU[2] == "D":
            direction = "L"
        elif XYU[2] == "L":
            direction = "U"
        elif XYU[2] == "U":
            direction = "R"
        converted.append([XYU[1], 200000-XYU[0], direction])
    return converted
    
min_val = int(1e+10)

for i in range(4):
    if i > 0:
        XYUs = rotate_matrix(XYUs)
    slopes = check_the_conflict(XYUs)
    for slope in slopes:
        now_R = -int(1e+10)
        slope = sorted(slope)
        for airplane in slope:
            if airplane[1] == "R":
                now_R = airplane[0]
            elif airplane[1] == "D":
                candidate = airplane[0] - now_R
                if candidate < min_val:
                    min_val = candidate
    if i in [0,1]:
        rows = [[] for _ in range(200001)]
        for XYU in XYUs:
            rows[XYU[0]].append([XYU[1],XYU[2]])
        for row in rows:
            now_U = -int(1e+10)
            row = sorted(row)
            for airplane in row:
                if airplane[1] == "U":
                    now_U = airplane[0]
                elif airplane[1] == "D":
                    candidate = (airplane[0] - now_U)/2
                    if candidate < min_val:
                        min_val = candidate
print(min_val)

Submission status: Wrong Answer

Solution code:
def x_ijo_min(lst, x):
    if lst[0] >= x: return lst[0]
    left = 0 # xより小さい
    right = len(lst) # x以上
    while left+1 < right:
        mid = (left+right) // 2
        if lst[mid] < x:
            left = mid
        else:
            right = mid
    if right == len(lst): return False
    else: return lst[right]

n = int(input())

dic_up = {}
dic_down = {}
dic_hor = {}
dic_ver = {}
lst = []
for i in range(n):
    xi, yi, ui = input().split()
    xi, yi = int(xi), int(yi)
    up = xi - yi
    down = xi + yi
    if (ui, up) in dic_up:
        dic_up[(ui, up)].append(xi)
    else:
        dic_up[(ui, up)] = [xi]
    if (ui, down) in dic_down:
        dic_down[(ui, down)].append(xi)
    else:
        dic_down[(ui, down)] = [xi]
    if (ui, xi) in dic_ver:
        dic_ver[(ui, xi)].append(yi)
    else:
        dic_ver[(ui, xi)] = [yi]
    if (ui, yi) in dic_hor:
        dic_hor[(ui, yi)].append(xi)
    else:
        dic_hor[(ui, yi)] = [xi]
    lst.append([xi, yi, ui, up, down])

for key in dic_up.keys():
    dic_up[key].sort()
for key in dic_down.keys():
    dic_down[key].sort()
for key in dic_hor.keys():
    dic_hor[key].sort()
for key in dic_ver.keys():
    dic_ver[key].sort()

ans = 10**9
for i in range(n):
    xi, yi, ui, up, down = lst[i]
    if ui == 'U':
        if ('D', xi) in dic_ver:
            dy = x_ijo_min(dic_ver[('D', xi)], yi)
            if dy: ans = min(ans, (dy-yi)*5)
        if ('L', up) in dic_up:
            lx = x_ijo_min(dic_up[('L', up)], xi)
            if lx: ans = min(ans, (lx-xi)*10)
    if ui == 'R':
        if ('U', down) in dic_down:
            ux = x_ijo_min(dic_down[('U', down)], xi)
            if ux: ans = min(ans, (ux-xi)*10)
        if ('D', up) in dic_up:
            dx = x_ijo_min(dic_up[('D', up)], xi)
            if dx: ans = min(ans, (dx-xi)*10)
        if ('L', yi) in dic_hor:
            lx = x_ijo_min(dic_hor[('L', yi)], yi)
            if lx: ans = min(ans, (lx-xi)*5)
    if ui == 'D':
        if ('L', down) in dic_down:
            lx = x_ijo_min(dic_down[('L', down)], xi)
            if lx: ans = min(ans, (lx-xi)*10)

if ans == 10**9:
    print('SAFE')
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
def x_ijo_min(lst, x):
    if lst[0] >= x: return lst[0]
    left = 0 # xより小さい
    right = len(lst) # x以上
    while left+1 < right:
        mid = (left+right) // 2
        if lst[mid] < x:
            left = mid
        else:
            right = mid
    if right == len(lst): return False
    else: return right

n = int(input())

dic_up = {}
dic_down = {}
dic_hor = {}
dic_ver = {}
lst = []
for i in range(n):
    xi, yi, ui = input().split()
    xi, yi = int(xi), int(yi)
    up = xi - yi
    down = xi + yi
    if (ui, up) in dic_up:
        dic_up[(ui, up)].append(xi)
    else:
        dic_up[(ui, up)] = [xi]
    if (ui, down) in dic_down:
        dic_down[(ui, down)].append(xi)
    else:
        dic_down[(ui, down)] = [xi]
    if (ui, xi) in dic_ver:
        dic_ver[(ui, xi)].append(yi)
    else:
        dic_ver[(ui, xi)] = [yi]
    if (ui, yi) in dic_hor:
        dic_hor[(ui, yi)].append(xi)
    else:
        dic_hor[(ui, yi)] = [xi]
    lst.append([xi, yi, ui, up, down])

for key in dic_up.keys():
    dic_up[key].sort()
for key in dic_down.keys():
    dic_down[key].sort()
for key in dic_hor.keys():
    dic_hor[key].sort()
for key in dic_ver.keys():
    dic_ver[key].sort()

ans = 10**9
for i in range(n):
    xi, yi, ui, up, down = lst[i]
    if ui == 'U':
        if ('D', xi) in dic_ver:
            dy = x_ijo_min(dic_ver[('D', xi)], yi)
            if dy: ans = min(ans, (dy-yi)*5)
        if ('L', up) in dic_up:
            lx = x_ijo_min(dic_up[('L', up)], xi)
            if lx: ans = min(ans, (lx-xi)*10)
    if ui == 'R':
        if ('U', down) in dic_down:
            ux = x_ijo_min(dic_down[('U', down)], xi)
            if ux: ans = min(ans, (ux-xi)*10)
        if ('D', up) in dic_up:
            dx = x_ijo_min(dic_up[('D', up)], xi)
            if dx: ans = min(ans, (dx-xi)*10)
        if ('L', yi) in dic_hor:
            lx = x_ijo_min(dic_hor[('L', yi)], yi)
            if lx: ans = min(ans, (lx-xi)*5)
    if ui == 'D':
        if ('L', down) in dic_down:
            lx = x_ijo_min(dic_down[('L', down)], xi)
            if lx: ans = min(ans, (lx-xi)*10)

if ans == 10**9:
    print('SAFE')
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import copy
N = int(input())
XYUs = [list(map(str,input().split())) for _ in range(N)]

up_slopes = [[] for _ in range(400005)]
down_slopes = [[] for _ in range(400005)]
xs = [[] for _ in range(200005)]
ys = [[] for _ in range(200005)]



for XYU in XYUs:
    #y切片
    up_slopes[int(XYU[1])-int(XYU[0])+200000].append([int(XYU[0]),XYU[2]])
    down_slopes[int(XYU[1])+int(XYU[0])].append([int(XYU[0]),XYU[2]])
    xs[int(XYU[0])].append([int(XYU[1]),XYU[2]])
    ys[int(XYU[1])].append([int(XYU[0]),XYU[2]])
    
min_val = int(1e+100)
for up_slope in up_slopes:
    U_posi = -1
    R_posi = -1
    incidents = copy.deepcopy(up_slope)
    incidents = sorted(incidents)
    for incident in up_slope:
        if incident[1] == "U":
            U_posi = incident[0]
        elif incident[1] == "R":
            R_posi = incident[0]
        elif incident[1] == "D" and R_posi != -1:
            accident = incident[0]-R_posi
            if min_val > accident:
                min_val = accident
        elif incident[1] == "L" and U_posi != -1:
            accident = incident[0]-U_posi
            if min_val > accident*10:
                min_val = accident*10

for down_slope in down_slopes:
    D_posi = -1
    R_posi = -1
    incidents = copy.deepcopy(down_slope)
    incidents = sorted(incidents)
    for incident in incidents:
        if incident[1] == "D":
            D_posi = incident[0]
        elif incident[1] == "R":
            R_posi = incident[0]
        elif incident[1] == "U" and R_posi != -1:
            accident = incident[0]-R_posi
            if min_val > accident*10:
                min_val = accident*10
        elif incident[1] == "L" and D_posi != -1:
            accident = incident[0]-D_posi
            if min_val > accident:
                min_val = accident

for x in xs:
    U_posi = -1
    incidents = copy.deepcopy(x)
    incidents = sorted(incidents)
    for incident in incidents:
        if incident[1] == "U":
            U_posi = incident[0]
        elif incident[1] == "D" and U_posi!=-1:
            if min_val > 10*(incident[0]-U_posi)/2:
                min_val = 10*(incident[0]-U_posi)/2
            
for y in ys:
    R_posi = -1
    incidents = copy.deepcopy(y)
    incidents = sorted(incidents)
    for incident in incidents:
        if incident[1] == "R":
            R_posi = incident[0]
        elif incident[1] == "L" and R_posi!=-1:
            if min_val > 10*(incident[0]-R_posi)/2:
                min_val = 10*(incident[0]-R_posi)/2
if min_val < 3000000:
    print(int(min_val))
else:
    print("SAFE")

Submission status: Wrong Answer

Solution code:
n = int(input())

dic_up = {}
dic_down = {}
dic_hor = {}
dic_ver = {}
lst = []
for i in range(n):
    xi, yi, ui = input().split()
    xi, yi = int(xi), int(yi)
    up = xi - yi
    down = xi + yi
    if (ui, up) in dic_up:
        dic_up[(ui, up)].append([xi, yi])
    else:
        dic_up[(ui, up)] = [[xi, yi]]
    if (ui, down) in dic_down:
        dic_down[(ui, down)].append([xi, yi])
    else:
        dic_down[(ui, down)] = [[xi, yi]]
    if (ui, xi) in dic_ver:
        dic_ver[(ui, xi)].append(yi)
    else:
        dic_ver[(ui, xi)] = [yi]
    if (ui, yi) in dic_hor:
        dic_hor[(ui, yi)].append(xi)
    else:
        dic_hor[(ui, yi)] = [xi]
    lst.append([xi, yi, ui, up, down])

ans = 10**9
for i in range(n):
    xi, yi, ui, up, down = lst[i]
    if ui == 'U':
        if ('R', down) in dic_down:
            kouho = 10**9
            for rx, ry in dic_down[('R', down)]:
                if xi-rx > 0:
                    kouho = min(kouho, (xi-rx)*10)
            ans = min(ans, kouho)
        if ('D', xi) in dic_ver:
            kouho = 10**9
            for dy in dic_ver[('D', xi)]:
                if dy > yi:
                    kouho = min(kouho, (dy-yi)*5)
            ans = min(ans, kouho)
        if ('L', up) in dic_up:
            kouho = 10**9
            for lx, ly in dic_up[('L', up)]:
                if lx > xi:
                    kouho = min(kouho, (lx-xi)*10)
    if ui == 'R':
        if ('D', up) in dic_up:
            kouho = 10**9
            for dx, dy in dic_up[('D', up)]:
                if dx > xi:
                    kouho = min(kouho, (dx-xi)*10)
            ans = min(ans, kouho)
        if ('L', yi) in dic_hor:
            kouho = 10**9
            for lx in dic_hor[('L', yi)]:
                if lx > xi:
                    kouho = min(kouho, (lx-xi)*5)
            ans = min(ans, kouho)
    if ui == 'D':
        if ('L', down) in dic_down:
            kouho = 10**9
            for lx, ly in dic_down[('L', down)]:
                if lx > xi:
                    kouho = min(kouho, (lx-xi)*10)
            ans = min(ans, kouho)

if ans == 10**9:
    print('SAFE')
else:
    print(ans)