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:
#十字で合流する場合
n=int(input())
ud=[[[],[]] for i in range(300000)]
rl=[[[],[]] for i in range(300000)]
from bisect import *
cross=[[dict(),dict()] for i in range(4)]
for i in range(n):
    x,y,u=input().split()
    x,y=int(x),int(y)
    if u=="U":
        ud[x][0].append(y)
        if x+y in cross[0][1]:
            cross[0][1][x+y].append(x)
        else:
            cross[0][1][x+y]=[x]
        if x-y in cross[1][1]:
            cross[1][1][x-y].append(x)
        else:
            cross[1][1][x-y]=[x]
    elif u=="D":
        ud[x][1].append(y)
        if x+y in cross[3][1]:
            cross[3][1][x+y].append(x)
        else:
            cross[3][1][x+y]=[x]
        if x-y in cross[2][1]:
            cross[2][1][x-y].append(x)
        else:
            cross[2][1][x-y]=[x]
    elif u=="R":
        rl[y][0].append(x)
        if x+y in cross[0][0]:
            cross[0][0][x+y].append(x)
        else:
            cross[0][0][x+y]=[x]
        if x-y in cross[2][0]:
            cross[2][0][x-y].append(x)
        else:
            cross[2][0][x-y]=[x]
    else:
        rl[y][1].append(x)
        if x+y in cross[3][0]:
            cross[3][0][x+y].append(x)
        else:
            cross[3][0][x+y]=[x]
        if x-y in cross[1][0]:
            cross[1][0][x-y].append(x)
        else:
            cross[1][0][x-y]=[x]

for i in range(300000):
    ud[i][0].sort()
    ud[i][1].sort()
    rl[i][0].sort()
    rl[i][1].sort()

ans=[]
for i in range(300000):
    l1,l2=len(ud[i][0]),len(ud[i][1])
    if l1 and l2:
        for j in ud[i][0]:
            b=bisect_left(ud[i][1],j)
            if b!=l2:
                ans.append(ud[i][1][b]-j)
    l1,l2=len(rl[i][0]),len(rl[i][1])
    if l1 and l2:
        for j in rl[i][0]:
            b=bisect_left(rl[i][1],j)
            if b!=l2:
                ans.append(rl[i][1][b]-j)

for i in range(4):
    for j in range(2):
        for k in cross[i][j]:
            cross[i][j][k].sort()

for i in range(4):
    for j in cross[i][1]:
        if j in cross[i][0]:
            l=len(cross[i][0][j])
            if i%2==1:
                for k in cross[i][1][j]:
                    b=bisect_left(cross[i][0][j],k)
                if b!=l:
                    ans.append(2*cross[i][0][j][b]-2*k)
            else:
                for k in cross[i][1][j]:
                    b=bisect_right(cross[i][0][j],k)-1
                if b!=-1:
                    ans.append(2*k-2*cross[i][0][j][b])

if len(ans):
    print(min(ans)*5)
else:
    print("SAFE")

Submission status: Wrong Answer

Solution code:
INF = 2000000000


def calc_dl(D, L):
    DD = list(sorted(D, key=lambda pp: pp[0] + pp[1]))
    LL = list(sorted(L, key=lambda pp: pp[0] + pp[1]))
    d = 0
    l = 0
    limit = min(len(DD), len(LL)) - 1

    min_time = INF
    while d < len(D) and l < len(L):
        # 衝突判定
        if (DD[d][0] + DD[d][1]) == (LL[l][0] + LL[l][1]) and DD[d][0] < LL[l][0]:
            hit_time = (LL[l][0] - DD[d][0]) * 10
            min_time = min(min_time, hit_time)

        # インデックス進める
        if d == len(D) - 1:
            l += 1
        elif l == len(L) - 1:
            d += 1
        elif (DD[d][0] + DD[d][1]) > (LL[l][0] + LL[l][1]):
            l += 1
        else:
            d += 1
    return min_time


def calc_rl(R, L):
    RR = list(sorted(R, key=lambda pp: pp[1]))
    LL = list(sorted(L, key=lambda pp: pp[1]))
    r = 0
    l = 0

    min_time = INF
    while r < len(R) and l < len(L):
        # 衝突判定
        if RR[r][1] == LL[l][1] and RR[r][0] < LL[l][0]:
            hit_time = (LL[l][0] - RR[r][0]) * 5
            min_time = min(min_time, hit_time)

        # インデックス進める
        if r == len(R) - 1:
            l += 1
        elif l == len(L) - 1:
            r += 1
        elif RR[r][1] > LL[l][1]:
            l += 1
        else:
            r += 1
    return min_time


def calc_rd(R, D):
    RR = list(sorted(R, key=lambda pp: pp[1] - pp[0]))
    DD = list(sorted(D, key=lambda pp: pp[1] - pp[0]))
    r = 0
    d = 0

    min_time = INF
    while r < len(R) and d < len(D):
        # 衝突判定
        if (RR[r][1] - RR[r][0]) == (DD[d][1] - DD[d][0]) and RR[r][0] < DD[d][0]:
            hit_time = (DD[d][0] - RR[r][0]) * 10
            min_time = min(min_time, hit_time)

        # インデックス進める
        if r == len(R) - 1:
            d += 1
        elif d == len(D) - 1:
            r += 1
        elif (RR[r][1] - RR[r][0]) > (DD[d][1] - DD[d][0]):
            d += 1
        else:
            r += 1
    return min_time


def calc_ul(U, L):
    UU = list(sorted(U, key=lambda pp: pp[1] - pp[0]))
    LL = list(sorted(L, key=lambda pp: pp[1] - pp[0]))
    u = 0
    l = 0

    min_time = INF
    while u < len(U) and l < len(L):
        # 衝突判定
        if (UU[u][1] - UU[u][0]) == (LL[l][1] - LL[l][0]) and UU[u][0] < LL[l][0]:
            hit_time = (LL[l][0] - UU[u][0]) * 10
            min_time = min(min_time, hit_time)

        # インデックス進める
        if u == len(U) - 1:
            l += 1
        elif l == len(L) - 1:
            u += 1
        elif (UU[u][1] - UU[u][0]) > (LL[l][1] - LL[l][0]):
            l += 1
        else:
            u += 1
    return min_time


def calc_ur(U, R):
    UU = list(sorted(U, key=lambda pp: pp[0] + pp[1]))
    RR = list(sorted(R, key=lambda pp: pp[0] + pp[1]))
    u = 0
    r = 0

    min_time = INF
    while u < len(U) and r < len(R):
        # 衝突判定
        if (UU[u][0] + UU[u][1]) == (RR[r][0] + RR[r][1]) and UU[u][0] > RR[r][0]:
            hit_time = (UU[u][0] - RR[r][0]) * 10
            min_time = min(min_time, hit_time)

        # インデックス進める
        if u == len(U) - 1:
            r += 1
        elif r == len(R) - 1:
            u += 1
        elif (UU[u][0] + UU[u][1]) > (RR[r][0] + RR[r][1]):
            r += 1
        else:
            u += 1
    return min_time


def calc_ud(U, D):
    UU = list(sorted(U, key=lambda pp: pp[0]))
    DD = list(sorted(D, key=lambda pp: pp[0]))
    u = 0
    d = 0

    min_time = INF
    while u < len(U) and d < len(D):
        # 衝突判定
        if UU[u][0] == DD[d][0] and UU[u][1] < DD[d][1]:
            hit_time = (DD[d][1] - UU[u][1]) * 5
            min_time = min(min_time, hit_time)

        # インデックス進める
        if u == len(U) - 1:
            d += 1
        elif d == len(D) - 1:
            u += 1
        elif UU[u][0] > DD[d][0]:
            d += 1
        else:
            u += 1
    return min_time


def main():
    N = int(input())
    PLANES = []

    U = []
    R = []
    D = []
    L = []
    for i in range(N):
        # x=c[0]
        # y=c[1]
        # u=c[2]
        p = input().split()
        p[0] = int(p[0])
        p[1] = int(p[1])
        PLANES.append(p)
        if p[2] == "U":
            U.append(p)
        elif p[2] == "R":
            R.append(p)
        elif p[2] == "D":
            D.append(p)
        else:
            L.append(p)

    # print(calc_ur(U, R))
    # print(calc_ud(U, D))
    # print(calc_ul(U, L))
    # print(calc_rd(R, D))
    # print(calc_rl(R, L))
    # print(calc_dl(D, L))

    min_time = min(
        calc_ur(U, R),
        calc_ud(U, D),
        calc_ul(U, L),
        calc_rd(R, D),
        calc_rl(R, L),
        calc_dl(D, L)
    )

    if min_time == INF:
        print("SAFE")
    else:
        print(min_time)


main()

Submission status: Wrong Answer

Solution code:
INF = 2000000000


def calc_dl(D, L):
    DD = list(sorted(D, key=lambda pp: pp[0] + pp[1]))
    LL = list(sorted(L, key=lambda pp: pp[0] + pp[1]))
    d = 0
    l = 0
    limit = min(len(DD), len(LL)) - 1

    min_time = INF
    while d < len(D) and l < len(L):
        # 衝突判定
        if (DD[d][0] + DD[d][1]) == (LL[l][0] + LL[l][1]) and DD[d][0] < LL[l][0]:
            hit_time = (LL[l][0] - DD[d][0]) * 10
            min_time = min(min_time, hit_time)

        # インデックス進める
        if d == len(D) - 1:
            l += 1
        elif l == len(L) - 1:
            d += 1
        elif (DD[d][0] + DD[d][1]) > (LL[l][0] + LL[l][1]):
            l += 1
        else:
            d += 1
    return min_time


def calc_rl(R, L):
    RR = list(sorted(R, key=lambda pp: pp[0]))
    LL = list(sorted(L, key=lambda pp: pp[0]))
    r = 0
    l = 0

    min_time = INF
    while r < len(R) and l < len(L):
        # 衝突判定
        if RR[r][1] == LL[l][1] and RR[r][0] < LL[l][0]:
            hit_time = (LL[l][0] - RR[r][0]) * 5
            min_time = min(min_time, hit_time)

        # インデックス進める
        if r == len(R) - 1:
            l += 1
        elif l == len(L) - 1:
            r += 1
        elif RR[r][1] > LL[l][1]:
            l += 1
        else:
            r += 1
    return min_time


def calc_rd(R, D):
    RR = list(sorted(R, key=lambda pp: pp[1] - pp[0]))
    DD = list(sorted(D, key=lambda pp: pp[1] - pp[0]))
    r = 0
    d = 0

    min_time = INF
    while r < len(R) and d < len(D):
        # 衝突判定
        if (RR[r][1] - RR[r][0]) == (DD[d][1] - DD[d][0]) and RR[r][0] < DD[d][0]:
            hit_time = (DD[d][0] - RR[r][0]) * 10
            min_time = min(min_time, hit_time)

        # インデックス進める
        if r == len(R) - 1:
            d += 1
        elif d == len(D) - 1:
            r += 1
        elif (RR[r][1] - RR[r][0]) > (DD[d][1] - DD[d][0]):
            d += 1
        else:
            r += 1
    return min_time


def calc_ul(U, L):
    UU = list(sorted(U, key=lambda pp: pp[1] - pp[0]))
    LL = list(sorted(L, key=lambda pp: pp[1] - pp[0]))
    u = 0
    l = 0

    min_time = INF
    while u < len(U) and l < len(L):
        # 衝突判定
        if (UU[u][1] - UU[u][0]) == (LL[l][1] - LL[l][0]) and UU[u][0] < LL[l][0]:
            hit_time = (LL[l][0] - UU[u][0]) * 10
            min_time = min(min_time, hit_time)

        # インデックス進める
        if u == len(U) - 1:
            l += 1
        elif l == len(L) - 1:
            u += 1
        elif (UU[u][1] - UU[u][0]) > (LL[l][1] - LL[l][0]):
            l += 1
        else:
            u += 1
    return min_time


def calc_ur(U, R):
    UU = list(sorted(U, key=lambda pp: pp[0] + pp[1]))
    RR = list(sorted(R, key=lambda pp: pp[0] + pp[1]))
    u = 0
    r = 0

    min_time = INF
    while u < len(U) and r < len(R):
        # 衝突判定
        if (UU[u][0] + UU[u][1]) == (RR[r][0] + RR[r][1]) and UU[u][0] > RR[r][0]:
            hit_time = (UU[u][0] - RR[r][0]) * 10
            min_time = min(min_time, hit_time)

        # インデックス進める
        if u == len(U) - 1:
            r += 1
        elif r == len(R) - 1:
            u += 1
        elif (UU[u][0] + UU[u][1]) > (RR[r][0] + RR[r][1]):
            r += 1
        else:
            u += 1
    return min_time


def calc_ud(U, D):
    UU = list(sorted(U, key=lambda pp: pp[0]))
    DD = list(sorted(D, key=lambda pp: pp[0]))
    u = 0
    d = 0

    min_time = INF
    while u < len(U) and d < len(D):
        # 衝突判定
        if UU[u][0] == DD[d][0] and UU[u][1] < DD[d][1]:
            hit_time = (DD[d][1] - UU[u][1]) * 5
            min_time = min(min_time, hit_time)

        # インデックス進める
        if u == len(U) - 1:
            d += 1
        elif d == len(D) - 1:
            u += 1
        elif UU[u][0] > DD[d][0]:
            d += 1
        else:
            u += 1
    return min_time


def main():
    N = int(input())
    PLANES = []

    U = []
    R = []
    D = []
    L = []
    for i in range(N):
        # x=c[0]
        # y=c[1]
        # u=c[2]
        p = input().split()
        p[0] = int(p[0])
        p[1] = int(p[1])
        PLANES.append(p)
        if p[2] == "U":
            U.append(p)
        elif p[2] == "R":
            R.append(p)
        elif p[2] == "D":
            D.append(p)
        else:
            L.append(p)

    # print(calc_ur(U, R))
    # print(calc_ud(U, D))
    # print(calc_ul(U, L))
    # print(calc_rd(R, D))
    # print(calc_rl(R, L))
    # print(calc_dl(D, L))

    min_time = min(
        calc_ur(U, R),
        calc_ud(U, D),
        calc_ul(U, L),
        calc_rd(R, D),
        calc_rl(R, L),
        calc_dl(D, L)
    )

    if min_time == INF:
        print("SAFE")
    else:
        print(min_time)


main()

Submission status: Wrong Answer

Solution code:
n = int(input())
from collections import defaultdict
import bisect
xd=defaultdict(list)
yd=defaultdict(list)
addd=defaultdict(list)
subd=defaultdict(list)

for _ in range(n):
    x,y,u=input().split()
    x,y=int(x),int(y)
    bisect.insort(xd[(x,u)],y)
    bisect.insort(yd[(y,u)],x)
    addd[x+y].append(u)
    addd[abs(x-y)].append(u)

tmp=float('inf')
for x in range(200001):
    for d in xd[(x,'D')]:
        ind=bisect.bisect_left(xd[(x,'U')], d)
        if ind!=0:
            u=xd[(x,'U')][ind-1]
            tmp=min((d-u)*10//2,tmp)

for y in range(200001):
    for l in yd[(y,'L')]:
        ind=bisect.bisect_left(yd[(y,'R')], l)
        if ind!=0:
            r=yd[(y,'R')][ind-1]
            tmp=min((r-l)*10//2,tmp)


if tmp!=float('inf'):
    print(tmp)
else: print('SAFE')

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline

N=int(input())

X1={}
X2={}

X={}
Y={}

for i in range(N):
    x,y,u=input().split()
    x=int(x)
    y=int(y)
    if x+y not in X1:
        X1[x+y]=[[x,u]]
    else:
        X1[x+y].append([x,u])
    if y-x not in X2:
        X2[y-x]=[[x,u]]
    else:
        X2[y-x].append([x,u])
    if x not in X:
        X[x]=[[y,u]]
    else:
        X[x].append([y,u])
    if y not in Y:
        Y[y]=[[x,u]]
    else:
        Y[y].append([x,u])


Dead=10**9


for k,v in X1.items():
    D=-1
    R=-1
    v.sort()
    #print(v)
    for a,u in v:
        if u=="D":
            D=a
        elif u=="R":
            R=a
        elif u=="L":
            if D>=0:
                Dead=min(Dead,(a-D)*10)
        elif u=="U":
            if R>=0:
                Dead=min(Dead,(a-R)*10)
for k,v in X2.items():
    U=-1
    L=-1
    v.sort()
    #print(v)
    for a,u in v:
        if u=="U":
            U=a
        elif u=="L":
            L=a
        elif u=="R":
            if U>=0:
                Dead=min(Dead,(a-U)*10)
        elif u=="D":
            if L>=0:
                Dead=min(Dead,(a-L)*10)
for k,v in X.items():
    U=-1
    v.sort()
    #print(v)
    for a,u in v:
        if u=="U":
            U=a
        elif u=="D":
            if U>=0:
                Dead=min(Dead,(a-U)*5)
for k,v in Y.items():
    R=-1
    v.sort()
    #print(v)
    for a,u in v:
        if u=="R":
            R=a
        elif u=="L":
            if R>=0:
                Dead=min(Dead,(a-R)*5)
if Dead==10**9:
    print("SAFE")
else:
    print(Dead)

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import deque
import heapq as hq

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


def main():
    N = NI()
    planes = [list(SI().split()) for _ in range(N)]

    XS = [[] for _ in range(200001)]
    YS = [[] for _ in range(200001)]
    ZS = [[] for _ in range(400001)]
    WS = [[] for _ in range(400001)]
    for plane in planes:
        x, y, u = int(plane[0]), int(plane[1]), plane[2]
        XS[x].append((y, u))
        YS[y].append((x, u))
        ZS[x+y].append((x, u))
        WS[x-y+200000].append((x, u))
    for X in XS:
        hq.heapify(X)
    for Y in YS:
        hq.heapify(Y)
    for Z in ZS:
        hq.heapify(Z)
    for W in WS:
        hq.heapify(W)

    ans = float("inf")

    for X in XS:
        if len(X) <= 1:
            continue
        c = -1
        for _ in range(len(X)):
            k, u = hq.heappop(X)
            if u == "U":
                c = k
            if u == "D" and c > 0:
                ans = min(ans, (k - c)*5)
                c = -1

    for Y in YS:
        if len(Y) <= 1:
            continue
        c = -1
        for _ in range(len(Y)):
            k, u = hq.heappop(Y)
            if u == "R":
                c = k
            if u == "L" and c > 0:
                ans = min(ans, (k - c)*5)
                c = -1

    for Z in ZS:
        if len(Z) <= 1:
            continue
        c = -1
        d = -1
        for _ in range(len(Z)):
            k, u = hq.heappop(Z)
            if u == "R":
                c = k
            if u == "D":
                d = k
            if u == "U" and c > 0:
                ans = min(ans, (k - c)*10)
                c = -1
            if u == "L" and d > 0:
                ans = min(ans, (k - d)*10)
                d = -1

    for W in WS:
        if len(W) <= 1:
            continue
        c = -1
        d = -1
        for _ in range(len(W)):
            k, u = hq.heappop(W)
            if u == "R":
                c = k
            if u == "U":
                d = k
            if u == "D" and c > 0:
                ans = min(ans, (k - c)*10)
                c = -1
            if u == "L" and d > 0:
                ans = min(ans, (k - d)*10)
                d = -1

    if ans == float("inf"):
        print("SAFE")
    else:
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#十字で合流する場合
n=int(input())
ud=[[[],[]] for i in range(200001)]
rl=[[[],[]] for i in range(200001)]
from bisect import *
cross=[[dict(),dict()] for i in range(4)]
for i in range(n):
    x,y,u=input().split()
    x,y=int(x),int(y)
    if u=="U":
        ud[x][0].append(y)
        if x+y in cross[0][1]:
            cross[0][1][x+y].append(x)
        else:
            cross[0][1][x+y]=[x]
        if x-y in cross[1][1]:
            cross[1][1][x-y].append(x)
        else:
            cross[1][1][x-y]=[x]
    elif u=="D":
        ud[x][1].append(y)
        if x+y in cross[3][1]:
            cross[3][1][x+y].append(x)
        else:
            cross[3][1][x+y]=[x]
        if x-y in cross[2][1]:
            cross[2][1][x-y].append(x)
        else:
            cross[2][1][x-y]=[x]
    elif u=="R":
        rl[y][0].append(x)
        if x+y in cross[0][0]:
            cross[0][0][x+y].append(x)
        else:
            cross[0][0][x+y]=[x]
        if x-y in cross[2][0]:
            cross[2][0][x-y].append(x)
        else:
            cross[2][0][x-y]=[x]
    else:
        rl[y][1].append(x)
        if x+y in cross[3][0]:
            cross[3][0][x+y].append(x)
        else:
            cross[3][0][x+y]=[x]
        if x-y in cross[1][0]:
            cross[1][0][x-y].append(x)
        else:
            cross[1][0][x-y]=[x]

for i in range(200001):
    ud[i][0].sort()
    ud[i][1].sort()
    rl[i][0].sort()
    rl[i][1].sort()

ans=[]
for i in range(200001):
    l1,l2=len(ud[i][0]),len(ud[i][1])
    if l1 and l2:
        for j in ud[i][0]:
            b=bisect_left(ud[i][1],j)
            if b!=l2:
                ans.append(ud[i][1][b]-j)
    l1,l2=len(rl[i][0]),len(rl[i][1])
    if l1 and l2:
        for j in rl[i][0]:
            b=bisect_left(rl[i][1],j)
            if b!=l2:
                ans.append(rl[i][1][b]-j)

for i in range(4):
    for j in range(2):
        for k in cross[i][j]:
            cross[i][j][k].sort()

for i in range(4):
    for j in cross[i][1]:
        if j in cross[i][0]:
            l=len(cross[i][0][j])
            if i%2==1:
                for k in cross[i][1][j]:
                    b=bisect_left(cross[i][0][j],k)
                if b!=l:
                    ans.append(abs(2*cross[i][0][j][b]-2*k))
            else:
                for k in cross[i][1][j]:
                    b=bisect_right(cross[i][0][j],k)-1
                if b!=-1:
                    ans.append(abs(2*k-2*cross[i][0][j][b]))

if len(ans):
    print(min(ans)*5)
else:
    print("SAFE")

Submission status: Wrong Answer

Solution code:
#十字で合流する場合
n=int(input())
ud=[[[],[]] for i in range(200001)]
rl=[[[],[]] for i in range(200001)]
from bisect import *
cross=[[dict(),dict()] for i in range(4)]
for i in range(n):
    x,y,u=input().split()
    x,y=int(x),int(y)
    if u=="U":
        ud[x][0].append(y)
        if x+y in cross[0][1]:
            cross[0][1][x+y].append(x)
        else:
            cross[0][1][x+y]=[x]
        if x-y in cross[1][1]:
            cross[1][1][x-y].append(x)
        else:
            cross[1][1][x-y]=[x]
    elif u=="D":
        ud[x][1].append(y)
        if x+y in cross[3][1]:
            cross[3][1][x+y].append(x)
        else:
            cross[3][1][x+y]=[x]
        if x-y in cross[2][1]:
            cross[2][1][x-y].append(x)
        else:
            cross[2][1][x-y]=[x]
    elif u=="R":
        rl[y][0].append(x)
        if x+y in cross[0][0]:
            cross[0][0][x+y].append(x)
        else:
            cross[0][0][x+y]=[x]
        if x-y in cross[2][0]:
            cross[2][0][x-y].append(x)
        else:
            cross[2][0][x-y]=[x]
    else:
        rl[y][1].append(x)
        if x+y in cross[3][0]:
            cross[3][0][x+y].append(x)
        else:
            cross[3][0][x+y]=[x]
        if x-y in cross[1][0]:
            cross[1][0][x-y].append(x)
        else:
            cross[1][0][x-y]=[x]

for i in range(200001):
    ud[i][0].sort()
    ud[i][1].sort()
    rl[i][0].sort()
    rl[i][1].sort()

ans=[]
for i in range(200001):
    l1,l2=len(ud[i][0]),len(ud[i][1])
    if l1 and l2:
        for j in ud[i][0]:
            b=bisect_left(ud[i][1],j)
            if b!=l2:
                ans.append(ud[i][1][b]-j)
    l1,l2=len(rl[i][0]),len(rl[i][1])
    if l1 and l2:
        for j in rl[i][0]:
            b=bisect_left(rl[i][1],j)
            if b!=l2:
                ans.append(rl[i][1][b]-j)

for i in range(4):
    for j in range(2):
        for k in cross[i][j]:
            cross[i][j][k]=sorted(cross[i][j][k])
for i in range(4):
    for j in cross[i][1]:
        if j in cross[i][0]:
            l=len(cross[i][0][j])
            if i%2==1:
                for k in cross[i][1][j]:
                    b=bisect_left(cross[i][0][j],k)
                if b!=l:
                    ans.append(2*cross[i][0][j][b]-2*k)
            else:
                for k in cross[i][1][j]:
                    b=bisect_right(cross[i][0][j],k)-1
                if b!=-1:
                    ans.append(2*k-2*cross[i][0][j][b])

if len(ans):
    print(min(ans)*5)
else:
    print("SAFE")

Submission status: Wrong Answer

Solution code:
#十字で合流する場合
n=int(input())
ud=[[[],[]] for i in range(200001)]
rl=[[[],[]] for i in range(200001)]
from bisect import *
cross=[[dict(),dict()] for i in range(4)]
for i in range(n):
    x,y,u=input().split()
    x,y=int(x),int(y)
    if u=="U":
        ud[x][0].append(y)
        if x+y in cross[0][1]:
            cross[0][1][x+y].append(x)
        else:
            cross[0][1][x+y]=[x]
        if x-y in cross[1][1]:
            cross[1][1][x-y].append(x)
        else:
            cross[1][1][x-y]=[x]
    elif u=="D":
        ud[x][1].append(y)
        if x+y in cross[3][1]:
            cross[3][1][x+y].append(x)
        else:
            cross[3][1][x+y]=[x]
        if x-y in cross[2][1]:
            cross[2][1][x-y].append(x)
        else:
            cross[2][1][x-y]=[x]
    elif u=="R":
        rl[y][0].append(x)
        if x+y in cross[0][0]:
            cross[0][0][x+y].append(x)
        else:
            cross[0][0][x+y]=[x]
        if x-y in cross[2][0]:
            cross[2][0][x-y].append(x)
        else:
            cross[2][0][x-y]=[x]
    else:
        rl[y][1].append(x)
        if x+y in cross[3][0]:
            cross[3][0][x+y].append(x)
        else:
            cross[3][0][x+y]=[x]
        if x-y in cross[1][0]:
            cross[1][0][x-y].append(x)
        else:
            cross[1][0][x-y]=[x]
for i in range(200001):
    ud[i][0].sort()
    ud[i][1].sort()
    rl[i][0].sort()
    rl[i][1].sort()
ans=[]
for i in range(200001):
    l1,l2=len(ud[i][0]),len(ud[i][1])
    if l1 and l2:
        for j in ud[i][0]:
            b=bisect_left(ud[i][1],j)
            if b!=l2:
                ans.append(ud[i][1][b]-j)
    l1,l2=len(rl[i][0]),len(rl[i][1])
    if l1 and l2:
        for j in rl[i][0]:
            b=bisect_left(rl[i][1],j)
            if b!=l2:
                ans.append(rl[i][1][b]-j)
for i in range(4):
    for j in range(2):
        for k in cross[i][j]:
            cross[i][j][k].sort()
#print(cross)
for i in range(4):
    for j in cross[i][1]:
        if j in cross[i][0]:
            l=len(cross[i][0][j])
            if i%2==1:
                for k in cross[i][1][j]:
                    b=bisect_left(cross[i][0][j],k)
                if b!=l:
                    ans.append(2*cross[i][0][j][b]-2*k)
            else:
                for k in cross[i][1][j]:
                    b=bisect_right(cross[i][0][j],k)-1
                if b!=-1:
                    ans.append(2*k-2*cross[i][0][j][b])
if len(ans) :
    print(min(ans)*5)
else:
    print(x)
    print("SAFE")

Submission status: Wrong Answer

Solution code:
#十字で合流する場合
n=int(input())
ud=[[[],[]] for i in range(200001)]
rl=[[[],[]] for i in range(200001)]
from bisect import *
cross=[[dict(),dict()] for i in range(4)]
for i in range(n):
    x,y,u=input().split()
    x,y=int(x),int(y)
    if u=="U":
        ud[x][0].append(y)
        if x+y in cross[0][1]:
            cross[0][1][x+y].append(x)
        else:
            cross[0][1][x+y]=[x]
        if x-y in cross[1][1]:
            cross[1][1][x-y].append(x)
        else:
            cross[1][1][x-y]=[x]
    elif u=="D":
        ud[x][1].append(y)
        if x+y in cross[3][1]:
            cross[3][1][x+y].append(x)
        else:
            cross[3][1][x+y]=[x]
        if x-y in cross[2][1]:
            cross[2][1][x-y].append(x)
        else:
            cross[2][1][x-y]=[x]
    elif u=="R":
        rl[y][0].append(x)
        if x+y in cross[0][0]:
            cross[0][0][x+y].append(x)
        else:
            cross[0][0][x+y]=[x]
        if x-y in cross[2][0]:
            cross[2][0][x-y].append(x)
        else:
            cross[2][0][x-y]=[x]
    else:
        rl[y][1].append(x)
        if x+y in cross[3][0]:
            cross[3][0][x+y].append(x)
        else:
            cross[3][0][x+y]=[x]
        if x-y in cross[1][0]:
            cross[1][0][x-y].append(x)
        else:
            cross[1][0][x-y]=[x]
for i in range(200001):
    ud[i][0].sort()
    ud[i][1].sort()
    rl[i][0].sort()
    rl[i][1].sort()
ans=[]
for i in range(200001):
    l1,l2=len(ud[i][0]),len(ud[i][1])
    if l1 and l2:
        for j in ud[i][0]:
            b=bisect_left(ud[i][1],j)
            if b!=l2:
                ans.append(ud[i][1][b]-j)
    l1,l2=len(rl[i][0]),len(rl[i][1])
    if l1 and l2:
        for j in rl[i][0]:
            b=bisect_left(rl[i][1],j)
            if b!=l2:
                ans.append(rl[i][1][b]-j)
for i in range(4):
    for j in range(2):
        for k in cross[i][j]:
            cross[i][j][k].sort()
#print(cross)
for i in range(4):
    for j in cross[i][1]:
        if j in cross[i][0]:
            l=len(cross[i][0][j])
            if i%2==1:
                for k in cross[i][1][j]:
                    b=bisect_left(cross[i][0][j],k)
                if b!=l:
                    ans.append(2*cross[i][0][j][b]-2*k)
            else:
                for k in cross[i][1][j]:
                    b=bisect_right(cross[i][0][j],k)-1
                if b!=-1:
                    ans.append(2*k-2*cross[i][0][j][b])
if len(ans) :
    print(min(ans)*5)
else:
    print("SAFE")