Submission #56299356


Source Code Expand

import heapq
from collections import defaultdict

N = int(input())

y_coords = set()
L = []
U = []
for _ in range(N):
    l, u = map(int, input().split())
    L.append(l)
    U.append(u)
    y_coords.add(l)
    y_coords.add(u)

Q = int(input())
queries = []
for _ in range(Q):
    sx, sy, tx, ty = map(int, input().split())
    queries.append((sx, sy, tx, ty))
    y_coords.add(sy)
    y_coords.add(ty)

y_coords = sorted(list(y_coords))
y_map = {y: i for i, y in enumerate(y_coords)}
y_map_inv = {i: y for y, i in y_map.items()}

graph = defaultdict(list)
for x in range(1, N + 1):
    for y in range(y_map[L[x - 1]], y_map[U[x - 1]] + 1):
        if x > 1:
            graph[(x - 1, y)].append((x, y))
            graph[(x, y)].append((x - 1, y))
        if y > y_map[L[x - 1]]:
            graph[(x, y)].append((x, y - 1))
            graph[(x, y - 1)].append((x, y))
        if y < y_map[U[x - 1]]:
            graph[(x, y)].append((x, y + 1))
            graph[(x, y + 1)].append((x, y))

def dijkstra(start, end):
    queue = [(0, start)]
    dist = {start: 0}
    while queue:
        current_dist, (x, y) = heapq.heappop(queue)
        if (x, y) == end:
            return current_dist
        if current_dist > dist[(x, y)]:
            continue
        for nx, ny in graph[(x, y)]:
            cost = 1 if x != nx else abs(y_map_inv[ny] - y_map_inv[y])
            new_dist = current_dist + cost
            if (nx, ny) not in dist or new_dist < dist[(nx, ny)]:
                dist[(nx, ny)] = new_dist
                heapq.heappush(queue, (new_dist, (nx, ny)))
    return -1

for sx, sy, tx, ty in queries:
    sy_compressed = y_map[sy]
    ty_compressed = y_map[ty]
    start = (sx, sy_compressed)
    end = (tx, ty_compressed)
    print(dijkstra(start, end))

Submission Info

Submission Time
Task F - Takahashi on Grid
User mu16
Language Python (PyPy 3.10-v7.3.12)
Score 0
Code Size 1837 Byte
Status TLE
Exec Time 5629 ms
Memory 1973896 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 575
Status
AC × 3
AC × 3
TLE × 60
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt, 01_random_51.txt, 01_random_52.txt, 02_handmade_45.txt, 02_handmade_46.txt, 02_handmade_47.txt, 02_handmade_48.txt, 02_handmade_49.txt, 02_handmade_53.txt, 02_handmade_54.txt, 02_handmade_55.txt, 02_handmade_56.txt, 02_handmade_57.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 72 ms 76864 KiB
00_sample_01.txt AC 72 ms 76868 KiB
00_sample_02.txt AC 122 ms 84100 KiB
01_random_03.txt TLE 5608 ms 1737740 KiB
01_random_04.txt TLE 5611 ms 1763560 KiB
01_random_05.txt TLE 5615 ms 1803632 KiB
01_random_06.txt TLE 5610 ms 1794904 KiB
01_random_07.txt TLE 5621 ms 1845980 KiB
01_random_08.txt TLE 5616 ms 1745352 KiB
01_random_09.txt TLE 5629 ms 1951856 KiB
01_random_10.txt TLE 5614 ms 1777212 KiB
01_random_11.txt TLE 5622 ms 1858696 KiB
01_random_12.txt TLE 5619 ms 1836144 KiB
01_random_13.txt TLE 5610 ms 1657468 KiB
01_random_14.txt TLE 5611 ms 1686412 KiB
01_random_15.txt TLE 5613 ms 1726316 KiB
01_random_16.txt TLE 5617 ms 1765004 KiB
01_random_17.txt TLE 5614 ms 1739756 KiB
01_random_18.txt TLE 5614 ms 1760432 KiB
01_random_19.txt TLE 5607 ms 1605408 KiB
01_random_20.txt TLE 5611 ms 1687400 KiB
01_random_21.txt TLE 5619 ms 1826096 KiB
01_random_22.txt TLE 5622 ms 1872100 KiB
01_random_23.txt TLE 5624 ms 1973896 KiB
01_random_24.txt TLE 5620 ms 1836132 KiB
01_random_25.txt TLE 5605 ms 1558036 KiB
01_random_26.txt TLE 5610 ms 1661664 KiB
01_random_27.txt TLE 5607 ms 1639692 KiB
01_random_28.txt TLE 5616 ms 1745940 KiB
01_random_29.txt TLE 5621 ms 1857404 KiB
01_random_30.txt TLE 5619 ms 1826876 KiB
01_random_31.txt TLE 5617 ms 1788808 KiB
01_random_32.txt TLE 5621 ms 1846760 KiB
01_random_33.txt TLE 5604 ms 1762132 KiB
01_random_34.txt TLE 5609 ms 1632936 KiB
01_random_35.txt TLE 5608 ms 1616496 KiB
01_random_36.txt TLE 5607 ms 1599764 KiB
01_random_37.txt TLE 5620 ms 1817604 KiB
01_random_38.txt TLE 5623 ms 1907504 KiB
01_random_39.txt TLE 5617 ms 1790848 KiB
01_random_40.txt TLE 5618 ms 1807640 KiB
01_random_41.txt TLE 5623 ms 1896372 KiB
01_random_42.txt TLE 5626 ms 1947924 KiB
01_random_43.txt TLE 5625 ms 1873132 KiB
01_random_44.txt TLE 5623 ms 1849960 KiB
01_random_45.txt TLE 5624 ms 1897120 KiB
01_random_46.txt TLE 5611 ms 1753716 KiB
01_random_47.txt TLE 5609 ms 1629484 KiB
01_random_48.txt TLE 5607 ms 1621192 KiB
01_random_49.txt TLE 5614 ms 1743852 KiB
01_random_50.txt TLE 5617 ms 1815328 KiB
01_random_51.txt TLE 5619 ms 1860968 KiB
01_random_52.txt TLE 5620 ms 1849512 KiB
02_handmade_45.txt TLE 5619 ms 1840792 KiB
02_handmade_46.txt TLE 5607 ms 1611532 KiB
02_handmade_47.txt TLE 5604 ms 1552016 KiB
02_handmade_48.txt TLE 5571 ms 933108 KiB
02_handmade_49.txt TLE 5547 ms 501876 KiB
02_handmade_53.txt TLE 5619 ms 1841180 KiB
02_handmade_54.txt TLE 5605 ms 1621096 KiB
02_handmade_55.txt TLE 5597 ms 1453624 KiB
02_handmade_56.txt TLE 5570 ms 881476 KiB
02_handmade_57.txt TLE 5545 ms 499984 KiB