Problem p02605 (python)

Fixed submission:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


D_sum = defaultdict(list)
D_diff = defaultdict(list)
DX = defaultdict(list)
DY = defaultdict(list)
n = I()
L = []
ans = INF
for i in range(n):
    x, y, u = LS()
    x = int(x)
    y = int(y)
    D_sum[x + y] += [(x, u)]
    D_diff[x - y] += [(x, u)]
    if u in "UD":
        DX[x] += [(y, u)]
    else:
        DY[y] += [(x, u)]


for _, co_list in D_sum.items():
    co_list.sort()
    r = d = -INF
    for co, direction in co_list:
        if direction == "R":
            r = co
        elif direction == "D":
            d = co
        elif direction == "U":
            ans = min(co - r, ans)
        else:
            ans = min(co - d, ans)

for _, co_list in D_diff.items():
    co_list.sort()
    u = r = -INF
    for co, direction in co_list:
        if direction == "U":
            u = co
        elif direction == "R":
            r = co
        elif direction == "D":
            ans = min(co - r, ans)
        else:
            ans = min(co - u, ans)


for _, co_list in DX.items():
    co_list.sort()
    u = -INF
    for co, direction in co_list:
        coo = co
        if direction == "U":
            u = co
        else:
            ans = min((co - u) / 2, ans)


for _, co_list in DY.items():
    co_list.sort()
    r = -INF
    for co, direction in co_list:
        if direction == "R":
            r = co
        else:
            ans = min((co - r) / 2, ans)


if ans > 10 ** 8:
    print("SAFE")
else:
    print(int(ans * 10))







"""
L1 = []
L2 = []
L3 = []
L4 = []
for xk, yk ,u in L:
    L1 += [(xk - yk)]
    L2 += [(- xk - yk)]
    L3 += [(xk + yk)]
    L4 += [(- xk + yk)]

print(L1)
print(L2)
print(L3)
print(L4)

"""

Buggy submission (verdict: Runtime Error):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7



D_sum = defaultdict(list)
D_diff = defaultdict(list)
DX = defaultdict(list)
DY = defaultdict(list)
n = I()
L = []
ans = INF
for i in range(n):
    x, y, u = LS()
    x = int(x)
    y = int(y)
    D_sum[(x + y, u)] += [x]
    D_diff[(x - y, u)] += [x]
    DX[x, u] += [(x, y)]
    DY[y, u] += [(x, y)]
    L += [(x, y, u)]

for xi, yi, u in L:
    if u == "R" and D_diff[(xi - yi, "D")]:
        ll = sorted(D_diff[(xi - yi, "D")])
        ii = bisect_right(ll, xi)
        if ii < len(ll):
            ans = min(ans, ll[ii] - xi)
    if u == "R" and D_sum[(xi + yi, "U")]:
        ll = sorted(D_sum[(xi + yi, "U")])
        ii = bisect_right(ll, xi)
        if ii < len(ll):
            ans = min(ans, ll[ii] - xi)
    if u == "L" and D_diff[(xi - yi , "U")]:
        ll = sorted(D_diff[(xi - yi, "D")])
        ii = bisect_right(ll, xi)
        if ii:
            ret = min(ll[ii] - xi)
    if u == "L" and D_sum[(xi + yi, "D")]:
        ll = sorted(D_sum[(xi + yi, "D")])
        ii = bisect_right(ll, xi)
        if ii:
            ans = min(ans, xi - ll[ii - 1])
    if u == "R" and DY[yi, "L"]:
        ll = sorted(DY[(yi, "L")])
        ii = bisect_right(ll, (xi, 0))
        if ii < len(ll):
            ans = min(ans, (ll[ii][0] - xi) / 2)
    if u == "U" and DX[xi, "D"]:
        ll = sorted(DX[(xi, "D")], key=lambda z:z[1])
        ii = bisect_right(ll, (yi, 0))
        if ii < len(ll):
            ans = min(ans, abs(ll[ii][1] - yi) / 2)


"""
L1 = []
L2 = []
L3 = []
L4 = []
for xk, yk ,u in L:
    L1 += [(xk - yk)]
    L2 += [(- xk - yk)]
    L3 += [(xk + yk)]
    L4 += [(- xk + yk)]

print(L1)
print(L2)
print(L3)
print(L4)

"""

if ans == INF:
    print("SAFE")
else:
    print(int(ans * 10) )

Buggy submission (verdict: Runtime Error):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7



D_sum = defaultdict(list)
D_diff = defaultdict(list)
DX = defaultdict(list)
DY = defaultdict(list)
n = I()
L = []
ans = INF
for i in range(n):
    x, y, u = LS()
    x = int(x)
    y = int(y)
    D_sum[(x + y, u)] += [x]
    D_diff[(x - y, u)] += [x]
    DX[x, u] += [(x, y)]
    DY[y, u] += [(x, y)]
    L += [(x, y, u)]

for xi, yi, u in L:
    if u == "R" and D_diff[(xi - yi, "D")]:
        ll = sorted(D_diff[(xi - yi, "D")])
        ii = bisect_right(ll, xi)
        if ii < len(ll):
            ans = min(ans, ll[ii] - xi)
    if u == "R" and D_sum[(xi + yi, "U")]:
        ll = sorted(D_sum[(xi + yi, "U")])
        ii = bisect_right(ll, xi)
        if ii < len(ll):
            ans = min(ans, ll[ii] - xi)
    if u == "L" and D_diff[(xi - yi , "U")]:
        ll = sorted(D_diff[(xi - yi, "D")])
        ii = bisect_right(ll, xi)
        if ii:
            ret = min(ll[ii] - xi)
    if u == "L" and D_sum[(xi + yi, "D")]:
        ll = sorted(D_sum[(xi + yi, "D")])
        ii = bisect_right(ll, xi)
        if ii:
            ans = min(ans, xi - ll[ii - 1])
    if u == "L" and DY[yi, "R"]:
        ll = sorted(DY[(yi, "R")])
        ii = bisect_right(ll, (xi, 0))
        if ii < len(ll):
            ans = min(ans, (ll[ii][0] - xi) / 2)
    if u == "U" and DX[xi, "D"]:
        ll = sorted(DX[(xi, "D")], key=lambda z:z[1])
        ii = bisect_right(ll, (yi, 0))
        if ii < len(ll):
            ans = min(ans, abs(ll[ii][1] - yi) / 2)


"""
L1 = []
L2 = []
L3 = []
L4 = []
for xk, yk ,u in L:
    L1 += [(xk - yk)]
    L2 += [(- xk - yk)]
    L3 += [(xk + yk)]
    L4 += [(- xk + yk)]

print(L1)
print(L2)
print(L3)
print(L4)

"""

if ans == INF:
    print("SAFE")
else:
    print(int(ans * 10) )

Buggy submission (verdict: Runtime Error):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7



D1 = defaultdict(list)
D2 = defaultdict(list)
D3 = defaultdict(list)
D4 = defaultdict(list)
n = I()
L = []
ans = INF
for i in range(n):
    x, y, u = LS()
    x = int(x)
    y = int(y)
    D1[(x + y, u)] += [x]
    D2[(x - y, u)] += [x]
    D3[x, u] += [(x, y)]
    D4[y, u] += [(x, y)]
    L += [(x, y, u)]

for xi, yi, u in L:
    if u == "R" and D2[(xi - yi, "D")]:
        ll = D2[(xi - yi, "D")]
        ii = bisect_right(ll, xi)
        if ii < len(ll):
            ans = min(ans, ll[ii] - xi)
    if u == "R" and D1[(xi + yi, "U")]:
        ll = D1[(xi + yi, "U")]
        ii = bisect_right(ll, xi)
        if ii < len(ll):
            ans = min(ans, ll[ii] - xi)
    if u == "L" and D2[(xi - yi , "U")]:
        ll = D2[(xi - yi, "D")]
        ii = bisect_right(ll, xi)
        if ii:
            ret = min(ll[ii] - xi)
    if u == "L" and D1[(xi + yi, "D")]:
        ll = D1[(xi + yi, "D")]
        ii = bisect_right(ll, xi)
        if ii:
            ans = min(ans, xi - ll[ii - 1])
    if u == "L" and D4[yi, "R"]:
        ll = D4[(yi, "R")]
        ii = bisect_right(ll, (yi, 0))
        ans = min(ans, (ll[ii][1] - yi) / 2)
    if u == "D" and D3[xi, "U"]:
        ll = D3[(xi, "U")]
        ii = bisect_right(ll, (xi, 0))
        ans = min(ans, abs(ll[ii][1] - yi) / 2)


"""
L1 = []
L2 = []
L3 = []
L4 = []
for xk, yk ,u in L:
    L1 += [(xk - yk)]
    L2 += [(- xk - yk)]
    L3 += [(xk + yk)]
    L4 += [(- xk + yk)]

print(L1)
print(L2)
print(L3)
print(L4)

"""

if ans == INF:
    print("SAFE")
else:
    print(int(ans * 10) )

Buggy submission (verdict: Runtime Error):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7



D1 = defaultdict(list)
D2 = defaultdict(list)
D3 = defaultdict(list)
D4 = defaultdict(list)
n = I()
L = []
ans = INF
for i in range(n):
    x, y, u = LS()
    x = int(x)
    y = int(y)
    D1[(x + y, u)] += [x]
    D2[(x - y, u)] += [x]
    D3[x, u] += [(x, y)]
    D4[y, u] += [(x, y)]
    L += [(x, y, u)]

for xi, yi, u in L:
    if u == "R" and D2[(xi - yi, "D")]:
        ll = D2[(xi - yi, "D")]
        ii = bisect_right(ll, xi)
        if ii < len(ll):
            ans = min(ans, ll[ii][0] - xi)
    if u == "R" and D1[(xi + yi, "U")]:
        ll = D1[(xi + yi, "U")]
        ii = bisect_right(ll, xi)
        if ii < len(ll):
            ans = min(ans, ll[ii][0] - xi)
    if u == "L" and D2[(xi - yi , "U")]:
        ll = D2[(xi - yi, "D")]
        ii = bisect_right(ll, xi)
        if ii:
            ret = min(ll[ii][0] - xi)
    if u == "L" and D1[(xi + yi, "D")]:
        ll = D1[(xi + yi, "D")]
        ii = bisect_right(ll, xi)
        if ii:
            ans = min(ans, xi - ll[ii - 1])
    if u == "L" and D4[yi, "R"]:
        ll = D4[(yi, "R")]
        ii = bisect_right(ll, (yi, 0))
        ans = min(ans, (ll[ii][1] - yi) / 2)
    if u == "D" and D3[xi, "U"]:
        ll = D3[(xi, "U")]
        ii = bisect_right(ll, (xi, 0))
        ans = min(ans, abs(ll[ii][1] - yi) / 2)


"""
L1 = []
L2 = []
L3 = []
L4 = []
for xk, yk ,u in L:
    L1 += [(xk - yk)]
    L2 += [(- xk - yk)]
    L3 += [(xk + yk)]
    L4 += [(- xk + yk)]

print(L1)
print(L2)
print(L3)
print(L4)

"""

if ans == INF:
    print("SAFE")
else:
    print(int(ans * 10) )