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:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10
    N = II()
    #positions = defaultdict(defaultdict(int))
    #planes = []
    #1:U 2:R 3: D 4:L
    U = 0
    R = 1
    D = 2
    L = 3
    direction = {'U':U,'R':R,'D':D,'L':L}
    
    x_axis = defaultdict(list)
    y_axis = defaultdict(list)
    ru = defaultdict(list)
    dr = defaultdict(list)
    ld = defaultdict(list)
    ul = defaultdict(list)
    

    for i in range(N):
        line = input().split()
        x = int(line[0])
        y = int(line[1])
        d = direction[line[2]]

        #planes.append((x,y,d))

        #positions[y][x] = d
        
        
        sum_cordinate = x+y
        dif_cordinate = x-y
        if d == U:
            y_axis[x].append((y,d))
            ul[dif_cordinate].append((x,y,d))#(x1,y1)(x2,y2)y2-y1=x2-x1, y2-x2=y1-x1
            ru[sum_cordinate].append((x,y,d))
        elif d == R:
            x_axis[y].append((x,d))
            ru[sum_cordinate].append((x,y,d))
            dr[dif_cordinate].append((x,y,d))
        elif d == D:
            y_axis[x].append((y,d))
            dr[dif_cordinate].append((x,y,d))
            ld[sum_cordinate].append((x,y,d))
        else:
            x_axis[y].append((x,d))
            ld[sum_cordinate].append((x,y,d))
            ul[dif_cordinate].append((x,y,d))

    

    min_time = 10**20
    for v in x_axis.values():
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][1] ==  R:
                j = i+1
                while j < len(v):
                    if v[j][1] == L:
                        min_time = min(min_time, (v[j][0]-v[i][0])*5)
                        break
                    j += 1
                i = j
            i += 1

    for v in y_axis.values():
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][1] ==  U:
                j = i+1
                while j < len(v):
                    if v[j][1] == D:
                        min_time = min(min_time, (v[j][0]-v[j-1][0])*5)
                        break
                    j += 1
                i = j
            i += 1
    
    for v in ru.values():
        #x座標でソート
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][2] == R:
                j = i+1
                while j < len(v):
                    if v[j][2] == U:
                        min_time = min(min_time,(v[j][0]-v[j-1][0])*10)
                        break
                    j += 1
                i = j
            i += 1
    
    for v in dr.values():
        #x座標でソート
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][2] == R:
                j = i+1
                while j < len(v):
                    if v[j][2] == D:
                        min_time = min(min_time,(v[j][0]-v[j-1][0])*10)
                        break
                    j += 1
                i = j
            i += 1
    
    for v in ld.values():
        #x座標でソート
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][2] == D:
                j = i+1
                while j < len(v):
                    if v[j][2] == L:
                        min_time = min(min_time,(v[j][0]-v[j-1][0])*10)
                        break
                    j += 1
                i = j
            i += 1
    
    for v in ul.values():
        #x座標でソート
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][2] == U:
                j = i+1
                while j < len(v):
                    if v[j][2] == L:
                        min_time = min(min_time,(v[j][0]-v[j-1][0])*10)
                        break
                    j += 1
                i = j
            i += 1


    print(min_time if min_time < 10**20 else 'SAFE')
    

    

    



    
main()

Submission status: Wrong Answer

Solution code:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10
    N = II()
    #positions = defaultdict(defaultdict(int))
    #planes = []
    #1:U 2:R 3: D 4:L
    U = 0
    R = 1
    D = 2
    L = 3
    direction = {'U':U,'R':R,'D':D,'L':L}
    
    x_axis = defaultdict(list)
    y_axis = defaultdict(list)
    ru = defaultdict(list)
    dr = defaultdict(list)
    ld = defaultdict(list)
    ul = defaultdict(list)
    

    for i in range(N):
        line = input().split()
        x = int(line[0])
        y = int(line[1])
        d = direction[line[2]]

        #planes.append((x,y,d))

        #positions[y][x] = d
        
        
        sum_cordinate = x+y
        dif_cordinate = x-y
        if d == U:
            y_axis[x].append((y,d))
            ul[dif_cordinate].append((x,y,d))#(x1,y1)(x2,y2)y2-y1=x2-x1, y2-x2=y1-x1
            ru[sum_cordinate].append((x,y,d))
        elif d == R:
            x_axis[y].append((x,d))
            ru[sum_cordinate].append((x,y,d))
            dr[dif_cordinate].append((x,y,d))
        elif d == D:
            y_axis[x].append((y,d))
            dr[dif_cordinate].append((x,y,d))
            ld[sum_cordinate].append((x,y,d))
        else:
            x_axis[y].append((x,d))
            ld[sum_cordinate].append((x,y,d))
            ul[dif_cordinate].append((x,y,d))

    

    min_time = 10**20
    for v in x_axis.values():
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][1] ==  R:
                j = i+1
                while j < len(v):
                    if v[j][1] == L:
                        min_time = min(min_time, (v[j][0]-v[i][0])*5)
                        break
                    j += 1
                i = j
            i += 1

    for v in y_axis.values():
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][1] ==  U:
                j = i+1
                while j < len(v):
                    if v[j][1] == D:
                        min_time = min(min_time, (v[j][0]-v[i][0])*5)
                        break
                    j += 1
                i = j
            i += 1
    
    for v in ru.values():
        #x座標でソート
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][2] == R:
                j = i+1
                while j < len(v):
                    if v[j][2] == U:
                        min_time = min(min_time,(v[j][0]-v[j-1][0])*10)
                        break
                    j += 1
                i = j
            i += 1
    
    for v in dr.values():
        #x座標でソート
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][2] == R:
                j = i+1
                while j < len(v):
                    if v[j][2] == D:
                        min_time = min(min_time,(v[j][0]-v[j-1][0])*10)
                        break
                    j += 1
                i = j
            i += 1
    
    for v in ld.values():
        #x座標でソート
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][2] == D:
                j = i+1
                while j < len(v):
                    if v[j][2] == L:
                        min_time = min(min_time,(v[j][0]-v[j-1][0])*10)
                        break
                    j += 1
                i = j
            i += 1
    
    for v in ul.values():
        #x座標でソート
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][2] == U:
                j = i+1
                while j < len(v):
                    if v[j][2] == L:
                        min_time = min(min_time,(v[j][0]-v[j-1][0])*10)
                        break
                    j += 1
                i = j
            i += 1


    print(min_time if min_time < 10**20 else 'SAFE')
    

    

    



    
main()

Submission status: Wrong Answer

Solution code:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10
    N = II()
    #positions = defaultdict(defaultdict(int))
    #planes = []
    #1:U 2:R 3: D 4:L
    U = 0
    R = 1
    D = 2
    L = 3
    direction = {'U':U,'R':R,'D':D,'L':L}
    
    x_axis = defaultdict(list)
    y_axis = defaultdict(list)
    ru = defaultdict(list)
    dr = defaultdict(list)
    ld = defaultdict(list)
    ul = defaultdict(list)
    

    for i in range(N):
        line = input().split()
        x = int(line[0])
        y = int(line[1])
        d = direction[line[2]]

        #planes.append((x,y,d))

        #positions[y][x] = d
        
        
        sum_cordinate = x+y
        dif_cordinate = x-y
        if d == U:
            y_axis[x].append((y,d))
            ul[dif_cordinate].append((x,y,d))
            ru[sum_cordinate].append((x,y,d))
        elif d == R:
            x_axis[y].append((x,d))
            ru[sum_cordinate].append((x,y,d))
            dr[dif_cordinate].append((x,y,d))
        elif d == D:
            y_axis[x].append((y,d))
            dr[dif_cordinate].append((x,y,d))
            ld[sum_cordinate].append((x,y,d))
        else:
            x_axis[y].append((x,d))
            ld[sum_cordinate].append((x,y,d))
            ul[dif_cordinate].append((x,y,d))

    

    min_time = 10**20
    for v in x_axis.values():
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][1] ==  R:
                j = i+1
                while j < len(v):
                    if v[j][1] == L:
                        min_time = min(min_time, (v[j][0]-v[i][0])*5)
                        break
                    j += 1
                i = j
            i += 1

    for v in y_axis.values():
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][1] ==  U:
                j = i+1
                while j < len(v):
                    if v[j][1] == D:
                        min_time = min(min_time, (v[j][0]-v[i][0])*5)
                        break
                    j += 1
                i = j
            i += 1
    
    for v in ru.values():
        #x座標でソート
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][2] == R:
                j = i+1
                while j < len(v):
                    if v[j][2] == U:
                        min_time = min(min_time,(v[j][0]-v[i][0])*10)
                        break
                    j += 1
                i = j
            i += 1
    
    for v in dr.values():
        #x座標でソート
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][2] == R:
                j = i+1
                while j < len(v):
                    if v[j][2] == D:
                        min_time = min(min_time,(v[j][0]-v[i][0])*10)
                        break
                    j += 1
                i = j
            i += 1
    
    for v in ld.values():
        #x座標でソート
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][2] == D:
                j = i+1
                while j < len(v):
                    if v[j][2] == L:
                        min_time = min(min_time,(v[j][0]-v[i][0])*10)
                        break
                    j += 1
                i = j
            i += 1
    
    for v in ul.values():
        #x座標でソート
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][2] == U:
                j = i+1
                while j < len(v):
                    if v[j][2] == L:
                        min_time = min(min_time,(v[j][0]-v[i][0])*10)
                        break
                    j += 1
                i = j
            i += 1


    print(min_time if min_time < 10**20 else 'SAFE')
    

    

    



    
main()

Submission status: Wrong Answer

Solution code:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10
    N = II()
    #positions = defaultdict(defaultdict(int))
    #planes = []
    #1:U 2:R 3: D 4:L
    U = 0
    R = 1
    D = 2
    L = 3
    direction = {'U':U,'R':R,'D':D,'L':L}
    
    x_axis = defaultdict(list)
    y_axis = defaultdict(list)
    ru = defaultdict(list)
    dr = defaultdict(list)
    ld = defaultdict(list)
    ul = defaultdict(list)
    

    for i in range(N):
        line = input().split()
        x = int(line[0])
        y = int(line[1])
        d = direction[line[2]]

        #planes.append((x,y,d))

        #positions[y][x] = d
        
        
        sum_cordinate = x+y
        dif_cordinate = abs(x-y)
        if d == U:
            y_axis[x].append((y,d))
            ul[dif_cordinate].append((x,y,d))
            ru[sum_cordinate].append((x,y,d))
        elif d == R:
            x_axis[y].append((x,d))
            ru[sum_cordinate].append((x,y,d))
            dr[dif_cordinate].append((x,y,d))
        elif d == D:
            y_axis[x].append((y,d))
            dr[dif_cordinate].append((x,y,d))
            ld[sum_cordinate].append((x,y,d))
        else:
            x_axis[y].append((x,d))
            ld[sum_cordinate].append((x,y,d))
            ul[dif_cordinate].append((x,y,d))

    

    min_time = 10**20
    for v in x_axis.values():
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][1] ==  R:
                j = i+1
                while j < len(v):
                    if v[j][1] == L:
                        min_time = min(min_time, (v[j][0]-v[i][0])*5)
                        break
                    j += 1
                i = j
            i += 1

    for v in y_axis.values():
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][1] ==  U:
                j = i+1
                while j < len(v):
                    if v[j][1] == D:
                        min_time = min(min_time, (v[j][0]-v[i][0])*5)
                        break
                    j += 1
                i = j
            i += 1
    
    for v in ru.values():
        #x座標でソート
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][2] == R:
                j = i+1
                while j < len(v):
                    if v[j][2] == U:
                        min_time = min(min_time,(v[j][0]-v[i][0])*10)
                        break
                    j += 1
                i = j
            i += 1
    
    for v in dr.values():
        #x座標でソート
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][2] == R:
                j = i+1
                while j < len(v):
                    if v[j][2] == D:
                        min_time = min(min_time,(v[j][0]-v[i][0])*10)
                        break
                    j += 1
                i = j
            i += 1
    
    for v in ld.values():
        #x座標でソート
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][2] == D:
                j = i+1
                while j < len(v):
                    if v[j][2] == L:
                        min_time = min(min_time,(v[j][0]-v[i][0])*10)
                        break
                    j += 1
                i = j
            i += 1
    
    for v in ul.values():
        #x座標でソート
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][2] == U:
                j = i+1
                while j < len(v):
                    if v[j][2] == L:
                        min_time = min(min_time,(v[j][0]-v[i][0])*10)
                        break
                    j += 1
                i = j
            i += 1


    print(min_time if min_time < 10**20 else 'SAFE')
    

    

    



    
main()

Submission status: Wrong Answer

Solution code:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10
    N = II()
    #positions = defaultdict(defaultdict(int))
    #planes = []
    #1:U 2:R 3: D 4:L
    U = 0
    R = 1
    D = 2
    L = 3
    direction = {'U':U,'R':R,'D':D,'L':L}
    
    x_axis = defaultdict(list)
    y_axis = defaultdict(list)
    ru = defaultdict(list)
    dr = defaultdict(list)
    ld = defaultdict(list)
    ul = defaultdict(list)
    

    for i in range(N):
        line = input().split()
        x = int(line[0])
        y = int(line[1])
        d = direction[line[2]]

        #planes.append((x,y,d))

        #positions[y][x] = d
        
        
        sum_cordinate = x+y
        if d == U:
            y_axis[x].append((y,d))
            ul[sum_cordinate].append((x,y,d))
            ru[sum_cordinate].append((x,y,d))
        elif d == R:
            x_axis[y].append((x,d))
            ru[sum_cordinate].append((x,y,d))
            dr[sum_cordinate].append((x,y,d))
        elif d == D:
            y_axis[x].append((y,d))
            dr[sum_cordinate].append((x,y,d))
            ld[sum_cordinate].append((x,y,d))
        else:
            x_axis[y].append((x,d))
            ld[sum_cordinate].append((x,y,d))
            ul[sum_cordinate].append((x,y,d))

    

    min_time = 10**20
    for v in x_axis.values():
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][1] ==  R:
                j = i+1
                while j < len(v):
                    if v[j][1] == L:
                        min_time = min(min_time, (v[j][0]-v[i][0])*5)
                        break
                    j += 1
                i = j
            i += 1

    for v in y_axis.values():
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][1] ==  U:
                j = i+1
                while j < len(v):
                    if v[j][1] == D:
                        min_time = min(min_time, (v[j][0]-v[i][0])*5)
                        break
                    j += 1
                i = j
            i += 1
    
    for v in ru.values():
        #x座標でソート
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][2] == R:
                j = i+1
                while j < len(v):
                    if v[j][2] == U:
                        min_time = min(min_time,(v[j][0]-v[i][0])*10)
                        break
                    j += 1
                i = j
            i += 1
    
    for v in dr.values():
        #x座標でソート
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][2] == R:
                j = i+1
                while j < len(v):
                    if v[j][2] == D:
                        min_time = min(min_time,(v[j][0]-v[i][0])*10)
                        break
                    j += 1
                i = j
            i += 1
    
    for v in ld.values():
        #x座標でソート
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][2] == D:
                j = i+1
                while j < len(v):
                    if v[j][2] == L:
                        min_time = min(min_time,(v[j][0]-v[i][0])*10)
                        break
                    j += 1
                i = j
            i += 1
    
    for v in ul.values():
        #x座標でソート
        v.sort()
        i = 0
        while i < len(v)-1:
            if v[i][2] == U:
                j = i+1
                while j < len(v):
                    if v[j][2] == L:
                        min_time = min(min_time,(v[j][0]-v[i][0])*10)
                        break
                    j += 1
                i = j
            i += 1


    print(min_time if min_time < 10**20 else 'SAFE')
    

    

    



    
main()

Submission status: Wrong Answer

Solution code:
import sys
# from collections import Counter

N_MAX = 200000 + 5
INF = 10**9 + 7
sys.setrecursionlimit(N_MAX)
MOD = 10**9 + 7


def main():
    N = int(sys.stdin.readline().rstrip())
    X = [[] for _ in range(200005)]
    Y = [[] for _ in range(200005)]
    A = [[] for _ in range(200005 * 2 + 1)]
    B = [[] for _ in range(200005 * 2 + 1)]

    for i in range(N):
        x, y, u = sys.stdin.readline().rstrip().split()
        x = int(x)
        y = int(y)
        X[x].append((y, u))  # 同一X に含まれるもの
        Y[y].append((x, u))
        A[x + y].append((x, u))
        B[x - y + 200005].append((x, u))

    # 正面衝突
    length = 1000000
    for xs in X:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min(abs(y - tmp_y) * 5, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in Y:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min(abs(y - tmp_y) * 5, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in A:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "U":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "D":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in B:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    if length == 1000000:
        print("SAFE")
    else:
        print(length)


main()

Submission status: Wrong Answer

Solution code:
import sys
# from collections import Counter

N_MAX = 200000 + 5
INF = 10**9 + 7
sys.setrecursionlimit(N_MAX)
MOD = 10**9 + 7


def main():
    N = int(sys.stdin.readline().rstrip())
    X = [[] for _ in range(200005)]
    Y = [[] for _ in range(200005)]
    A = [[] for _ in range(200005 * 2 + 1)]
    B = [[] for _ in range(200005 * 2 + 1)]

    for i in range(N):
        x, y, u = sys.stdin.readline().rstrip().split()
        x = int(x)
        y = int(y)
        X[x].append((y, u))  # 同一X に含まれるもの
        Y[y].append((x, u))
        A[x + y].append((x, u))
        B[x - y + 200005].append((x, u))

    # 正面衝突
    length = 1000000
    for xs in X:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min((y - tmp_y) * 5, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in Y:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min((y - tmp_y) * 5, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in A:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "U":
                    length = min((y - tmp_y) * 10, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min((y - tmp_y) * 10, length)
                    is_count = False
                if u == "D":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in B:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min((y - tmp_y) * 10, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min((y - tmp_y) * 10, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    if length == 1000000:
        print("SAFE")
    else:
        print(length)


main()