Problem:


Score: 600 points

Problem Statement

There is a grass field that stretches infinitely.

In this field, there is a negligibly small cow. Let (x, y) denote the point that is x\ \mathrm{cm} south and y\ \mathrm{cm} east of the point where the cow stands now. The cow itself is standing at (0, 0).

There are also N north-south lines and M east-west lines drawn on the field. The i-th north-south line is the segment connecting the points (A_i, C_i) and (B_i, C_i), and the j-th east-west line is the segment connecting the points (D_j, E_j) and (D_j, F_j).

What is the area of the region the cow can reach when it can move around as long as it does not cross the segments (including the endpoints)? If this area is infinite, print INF instead.

Constraints

All values in input are integers between -10^9 and 10^9  (inclusive).

1 \leq N, M \leq 1000

A_i < B_i\ (1 \leq i \leq N)

E_j < F_j\ (1 \leq j \leq M)

The point (0, 0) does not lie on any of the given segments.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1 C_1
:
A_N B_N C_N
D_1 E_1 F_1
:
D_M E_M F_M

Output

If the area of the region the cow can reach is infinite, print INF; otherwise, print an integer representing the area in \mathrm{cm^2}.

(Under the constraints, it can be proved that the area of the region is always an integer if it is not infinite.)

Sample Input 1

5 6
1 2 0
0 1 1
0 2 2
-3 4 -1
-2 6 3
1 0 1
0 1 2
2 0 2
-1 -4 5
3 -2 4
1 2 4

Sample Output 1

13

The area of the region the cow can reach is 13\ \mathrm{cm^2}.

Sample Input 2

6 1
-3 -1 -2
-3 -1 1
-2 -1 2
1 4 -2
1 4 -1
1 4 1
3 1 4

Sample Output 2

INF

The area of the region the cow can reach is infinite.



Sample input:
5 6
1 2 0
0 1 1
0 2 2
-3 4 -1
-2 6 3
1 0 1
0 1 2
2 0 2
-1 -4 5
3 -2 4
1 2 4


Sample output:
13

Submission status: Wrong Answer

Solution code:
import sys
from itertools import chain, accumulate

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())
ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
banned_up_ij = [[0] * row for _ in range(col)]
banned_down_ij = [[0] * row for _ in range(col)]
banned_left_ij = [[0] * col for _ in range(row)]
banned_right_ij = [[0] * col for _ in range(row)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    banned_left_ij[j][ai] += 1
    banned_left_ij[j][bi] -= 1
    banned_right_ij[j - 1][ai] += 1
    banned_right_ij[j - 1][bi] -= 1
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up_ij[i][ej] += 1
    banned_up_ij[i][fj] -= 1
    banned_down_ij[i - 1][ej] += 1
    banned_down_ij[i - 1][fj] -= 1

banned_up = list(chain.from_iterable(map(accumulate, banned_up_ij)))
banned_down = list(chain.from_iterable(map(accumulate, banned_down_ij)))
banned_left = list(chain.from_iterable(zip(*map(accumulate, banned_left_ij))))
banned_right = list(chain.from_iterable(zip(*map(accumulate, banned_right_ij))))

# debug
exit()

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [0] * (row * col)
for ri in range(0, row * col, row):
    enable[ri] = -1
    enable[ri + row - 1] = -1
ri_last = row * (col - 1)
for j in range(row):
    enable[j] = -1
    enable[ri_last + j] = -1

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [-row, -1, 1, row]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc in moves:
        nc = c + dc
        if nc in walls[c]:
            continue
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

ans = 0
for i in range(row):
    ri = i * row
    for j in range(col):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
import sys
import bisect

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LIR(n):
    return [LI() for i in range(n)]

def solve():
    n,m = LI()
    p = LIR(n)
    q = LIR(m)
    M = 10**11
    fx = [-M,0,M]
    fy = [-M,0,M]
    for i in range(n):
        p[i][0] *= 10
        p[i][1] *= 10
        p[i][2] *= 10
        a,b,c = p[i]
        fy.append(a)
        fy.append(b)
        fy.append(b+1)
        fx.append(c)
        fx.append(c+1)

    for i in range(m):
        q[i][0] *= 10
        q[i][1] *= 10
        q[i][2] *= 10
        d,e,f = q[i]
        fy.append(d)
        fy.append(d+1)
        fx.append(e)
        fx.append(f)
        fx.append(f+1)
    fy = list(set(fy))
    fx = list(set(fx))
    fy.sort()
    fx.sort()
    h = len(fy)+1
    w = len(fx)+1
    s = [0]*h*w
    for a,b,c in p:
        ai = bisect.bisect_left(fy,a)
        bi = bisect.bisect_left(fy,b)
        ci = bisect.bisect_left(fx,c)
        ka = ai*w
        kb = (bi+1)*w
        s[ka+ci] += 1
        s[kb+ci] -= 1
        s[ka+ci+1] -= 1
        s[kb+ci+1] += 1
    for d,e,f in q:
        di = bisect.bisect_left(fy,d)
        ei = bisect.bisect_left(fx,e)
        fi = bisect.bisect_left(fx,f)
        kd = di*w
        kdd = kd+w
        s[kd+ei] += 1
        s[kd+fi+1] -= 1
        s[kdd+ei] -= 1
        s[kdd+fi+1] += 1
    for i in range(h):
        for j in range(len(fx)):
            ind = i*w+j
            s[ind+1] += s[ind]
    for i in range(len(fy)):
        for j in range(w):
            ind = i*w+j
            s[ind+w] += s[ind]
    d = [(1,0),(-1,0),(0,1),(0,-1)]
    sx = bisect.bisect_left(fx,0)
    sy = bisect.bisect_left(fy,0)
    nh,nw = len(fy)-1,len(fx)-1
    fx[1] //= 10
    fy[1] //= 10
    for i in range(1,nw-1):
        fx[i+1] = fx[i+1]//10
        fx[i] = fx[i+1]-fx[i]
    for i in range(1,nh-1):
        fy[i+1] = fy[i+1]//10
        fy[i] = fy[i+1]-fy[i]
    fx[0] = -fx[0]
    fy[0] = -fy[0]
    ans = fx[sx]*fy[sy]
    q = [(sy,sx)]
    f = [1]*nh*nw
    f[sy*nw+sx] = 0
    while q:
        y,x = q.pop()
        for dy,dx in d:
            ny,nx = y+dy,x+dx
            if 0 <= ny < nh and 0 <= nx < nw:
                ind = ny*nw+nx
                if f[ind] and not s[ind+2*ny]:
                    k = fx[nx]*fy[ny]
                    if k >= M:
                        print("INF")
                        return
                    ans += k
                    f[ind] = 0
                    q.append((ny,nx))
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
import sys
import bisect
from itertools import accumulate
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LIR(n):
    return [LI() for i in range(n)]

def solve():
    n,m = LI()
    p = LIR(n)
    q = LIR(m)
    M = 10**11
    fx = [-M,0,M]
    fy = [-M,0,M]
    for i in range(n):
        p[i][0] *= 10
        p[i][1] *= 10
        p[i][2] *= 10
        a,b,c = p[i]
        fy.append(a)
        fy.append(b)
        fy.append(b+1)
        fx.append(c)
        fx.append(c+1)

    for i in range(m):
        q[i][0] *= 10
        q[i][1] *= 10
        q[i][2] *= 10
        d,e,f = q[i]
        fy.append(d)
        fy.append(d+1)
        fx.append(e)
        fx.append(f)
        fx.append(f+1)
    fy = list(set(fy))
    fx = list(set(fx))
    fy.sort()
    fx.sort()
    h = len(fy)+1
    w = len(fx)+1
    s = [0]*h*w
    for a,b,c in p:
        ai = bisect.bisect_left(fy,a)
        bi = bisect.bisect_left(fy,b)
        ci = bisect.bisect_left(fx,c)
        ka = ai*w
        kb = (bi+1)*w
        s[ka+ci] += 1
        s[kb+ci] -= 1
        s[ka+ci+1] -= 1
        s[kb+ci+1] += 1
    for d,e,f in q:
        di = bisect.bisect_left(fy,d)
        ei = bisect.bisect_left(fx,e)
        fi = bisect.bisect_left(fx,f)
        kd = di*w
        kdd = kd+w
        s[kd+ei] += 1
        s[kd+fi+1] -= 1
        s[kdd+ei] -= 1
        s[kdd+fi+1] += 1
    for i in range(h):
        for j in range(len(fx)):
            ind = i*w+j
            s[ind+1] += s[ind]
    for i in range(len(fy)):
        for j in range(w):
            ind = i*w+j
            s[ind+w] += s[ind]
    d = [(1,0),(-1,0),(0,1),(0,-1)]
    sx = bisect.bisect_left(fx,0)
    sy = bisect.bisect_left(fy,0)
    nh,nw = len(fy)-1,len(fx)-1
    fx[1] //= 10
    fy[1] //= 10
    for i in range(1,nw-1):
        fx[i+1] = fx[i+1]//10
        fx[i] = fx[i+1]-fx[i]
    for i in range(1,nh-1):
        fy[i+1] = fy[i+1]//10
        fy[i] = fy[i+1]-fy[i]
    fx[0] = -fx[0]
    fy[0] = -fy[0]
    ans = fx[sx]*fy[sy]
    q = [(sy,sx)]
    f = [1]*nh*nw
    f[sy*nw+sx] = 0
    while q:
        y,x = q.pop()
        for dy,dx in d:
            ny,nx = y+dy,x+dx
            if 0 <= ny < nh and 0 <= nx < nw:
                ind = ny*nw+nx
                if f[ind] and not s[ind+2*ny]:
                    k = fx[nx]*fy[ny]
                    if k >= float("inf"):
                        print("INF")
                        return
                    ans += k
                    f[ind] = 0
                    q.append((ny,nx))
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import sys

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())
ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
walls = [set() for _ in range(row * col)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    for ri in range(ai * row, bi * row, row):
        walls[ri + j].add(ri + j - 1)
        walls[ri + j - 1].add(ri + j)
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    ri0 = row * i
    ri1 = row * (i - 1)
    for j in range(ej, fj):
        walls[ri0 + j].add(ri1 + j)
        walls[ri1 + j].add(ri0 + j)


# debug
exit()


# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [0] * (row * col)
for ri in range(0, row * col, row):
    enable[ri] = -1
    enable[ri + row - 1] = -1
ri_last = row * (col - 1)
for j in range(row):
    enable[j] = -1
    enable[ri_last + j] = -1

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [-row, -1, 1, row]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc in moves:
        nc = c + dc
        if nc in walls[c]:
            continue
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

ans = 0
for i in range(row):
    ri = i * row
    for j in range(col):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
import sys
import bisect
from itertools import accumulate
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LIR(n):
    return [LI() for i in range(n)]

def solve():
    n,m = LI()
    p = LIR(n)
    q = LIR(m)
    fx = [-float("inf"),0,float("inf")]
    fy = [-float("inf"),0,float("inf")]
    for a,b,c in p:
        fy.append(a)
        fy.append(b)
        fx.append(c)
        fx.append(c+1e-1)

    for d,e,f in q:
        fy.append(d)
        fy.append(d+1e-1)
        fx.append(e)
        fx.append(f)
    fy = list(set(fy))
    fx = list(set(fx))
    fy.sort()
    fx.sort()
    h = len(fy)+1
    w = len(fx)+1
    s = [0]*h*w
    for a,b,c in p:
        ai = bisect.bisect_left(fy,a)
        bi = bisect.bisect_left(fy,b)
        ci = bisect.bisect_left(fx,c)
        ka = ai*w
        kb = (bi+1)*w
        s[ka+ci] += 1
        s[kb+ci] -= 1
        s[ka+ci+1] -= 1
        s[kb+ci+1] += 1
    for d,e,f in q:
        di = bisect.bisect_left(fy,d)
        ei = bisect.bisect_left(fx,e)
        fi = bisect.bisect_left(fx,f)
        kd = di*w
        kdd = kd+w
        s[kd+ei] += 1
        s[kd+fi+1] -= 1
        s[kdd+ei] -= 1
        s[kdd+fi+1] += 1
    for i in range(h):
        for j in range(len(fx)):
            ind = i*w+j
            s[ind+1] += s[ind]
    for i in range(len(fy)):
        for j in range(w):
            ind = i*w+j
            s[ind+w] += s[ind]
    d = [(1,0),(-1,0),(0,1),(0,-1)]
    sx = bisect.bisect_left(fx,0)
    sy = bisect.bisect_left(fy,0)
    nh,nw = len(fy)-1,len(fx)-1
    for i in range(1,nw-1):
        fx[i+1] = round(fx[i+1])
        fx[i] = fx[i+1]-fx[i]
    for i in range(1,nh-1):
        fy[i+1] = round(fy[i+1])
        fy[i] = fy[i+1]-fy[i]
    fx[0] = -fx[0]
    fy[0] = -fy[0]
    ans = fx[sx]*fy[sy]
    q = [(sy,sx)]
    f = [[1]*nw for i in range(nh)]
    f[sy][sx] = 0
    while q:
        y,x = q.pop()
        for dy,dx in d:
            ny,nx = y+dy,x+dx
            if 0 <= ny < nh and 0 <= nx < nw:
                if f[ny][nx] and not s[ny*w+nx]:
                    k = fx[nx]*fy[ny]
                    if k >= float("inf"):
                        print("INF")
                        return
                    ans += k
                    f[ny][nx] = 0
                    q.append((ny,nx))
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
from numba import njit

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

INF = 10**9 + 1

N, M = map(int, readline().split())
data = np.array(read().split(), np.int64)
A = data[::3]
B = data[1::3]
C = data[2::3]

D = A[N:]
E = B[N:]
F = C[N:]
A = A[:N]
B = B[:N]
C = C[:N]

X = np.unique(np.concatenate([A, B, D, [0, -INF, INF]]))
Y = np.unique(np.concatenate([C, E, F, [0, -INF, INF]]))
DX = X[1:] - X[:-1]
DY = Y[1:] - Y[:-1]

A = np.searchsorted(X, A)
B = np.searchsorted(X, B)
C = np.searchsorted(Y, C)
D = np.searchsorted(X, D)
E = np.searchsorted(Y, E)
F = np.searchsorted(Y, F)

H, W = len(X), len(Y)
N = H * W

@njit
def set_ng(A, B, C, D, E, F):
    p = 0
    head = np.full(N, -1, np.int32)
    ng = np.empty(4 * N, np.int32)
    nxt = np.empty(4 * N, np.int32)

    def add(v, w):
        nonlocal p
        nxt[p] = head[v]
        head[v] = p
        ng[p] = w
        p += 1

    for i in range(len(A)):
        a, b, c = A[i], B[i], C[i]
        for x in range(a, b):
            v = x * W + c
            add(v, v - 1)
            add(v - 1, v)
    for i in range(len(D)):
        d, e, f = D[i], E[i], F[i]
        for y in range(e, f):
            v = d * W + y
            add(v, v - W)
            add(v - W, v)
    return head, ng[:p], nxt[:p]

head, ng, nxt = set_ng(A, B, C, D, E, F)

@njit
def next_w(head, ng, nxt, v):
    p = head[v]
    V = [v - W, v + W, v - 1, v + 1]
    #while p != -1:
    #    V.remove(ng[p])
    #    p = nxt[p]
    return V

x0, y0 = np.searchsorted(X, 0), np.searchsorted(Y, 0)
v0 = x0 * W + y0

@njit
def solve():
    visited = np.zeros(N, np.bool_)
    visited[v0] = 1
    stack = np.empty(N, np.int32)
    p = 0
    ret = 0

    def area(x):
        x, y = divmod(x, W)
        return DX[x] * DY[y]

    def push(x):
        nonlocal p, ret
        stack[p] = x
        visited[x] = 1
        ret += area(x)
        p += 1

    def pop():
        nonlocal p
        p -= 1
        return stack[p]

    push(v0)
    while p:
        v = pop()
        for w in next_w(head, ng, nxt, v):
            if visited[w]:
                continue
            x, y = divmod(w, W)
            if x == 0 or x == H - 1 or y == 0 or y == W - 1:
                return 0
            push(w)
    return ret

x = solve()
if x == 0:
    print('INF')
else:
    print(x)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque
inpl = lambda: list(map(int,input().split()))
N, M = inpl()
hlines_orig = []
Xgrid = set()
vlines_orig = []
Ygrid = set()
for _ in range(N):
    A, B, C = inpl()
    hlines_orig.append((A,B,C))
    Ygrid.add(C)
for _ in range(M):
    D, E, F = inpl()
    vlines_orig.append((D,E,F))
    Xgrid.add(D)

Xgrid = sorted(list(Xgrid))
Ygrid = sorted(list(Ygrid))
W = len(Xgrid)
H = len(Ygrid)
areas = [[(Xgrid[w+1]-Xgrid[w])*(Ygrid[h+1]-Ygrid[h]) for h in range(H-1)] for w in range(W-1)]
walled = defaultdict(bool)
dirs = {(0,1),(1,0),(-1,0),(0,-1)}
for A, B, C in hlines_orig:
    a = bisect_left(Xgrid, A)
    b = bisect_left(Xgrid, B)
    c = bisect_left(Ygrid, C)
    for i in range(a, b):
        if c < H-1:
            walled[((i,c),(0,-1))] = True
        if c > 0:
            walled[((i,c-1),(0,1))] = True

for D, E, F in vlines_orig:
    d = bisect_left(Ygrid, D)
    e = bisect_left(Ygrid, E)
    f = bisect_left(Xgrid, F)
    for i in range(e, f):
        if d < W-1:
            walled[((d,i),(-1,0))] = True
        if d > 0:
            walled[((d-1,i),(1,0))] = True

x, y = bisect_left(Xgrid, 0), bisect_left(Ygrid, 0)
if x == 0 or y == 0 or x >= W or y >= H:
    print('INF')
    exit()
else:
    x -= 1
    y -= 1
ans = areas[x][y]
visited = [[ False for _ in range(H-1) ] for _ in range(W-1)]
visited[x][y] = True
pool = deque([(x,y)])
while pool:
    x, y = pool.popleft()
    for d in dirs:
        if not walled[((x,y),d)]:
            cx, cy = x+d[0], y+d[1]
            if cx < 0 or cx >= W-1 or cy < 0 or cy >= H-1:
                print('INF')
                exit()
            elif not visited[cx][cy]:
                ans += areas[cx][cy]
                visited[cx][cy] = True
                pool.append((cx,cy))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
from numba import njit

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

INF = 10**9 + 1

N, M = map(int, readline().split())
data = np.array(read().split(), np.int64)
A = data[::3]
B = data[1::3]
C = data[2::3]

D = A[N:]
E = B[N:]
F = C[N:]
A = A[:N]
B = B[:N]
C = C[:N]

X = np.unique(np.concatenate([A, B, D, [0, -INF, INF]]))
Y = np.unique(np.concatenate([C, E, F, [0, -INF, INF]]))
DX = X[1:] - X[:-1]
DY = Y[1:] - Y[:-1]

A = np.searchsorted(X, A)
B = np.searchsorted(X, B)
C = np.searchsorted(Y, C)
D = np.searchsorted(X, D)
E = np.searchsorted(Y, E)
F = np.searchsorted(Y, F)

H, W = len(X), len(Y)
N = H * W

@njit
def set_ng(A, B, C, D, E, F):
    p = 0
    head = np.full(N, -1, np.int64)
    ng = np.empty(4 * N, np.int64)
    nxt = np.empty(4 * N, np.int64)

    def add(v, w):
        nonlocal p
        nxt[p] = head[v]
        head[v] = p
        ng[p] = w
        p += 1

    for i in range(len(A)):
        a, b, c = A[i], B[i], C[i]
        for x in range(a, b):
            v = x * W + c
            add(v, v - 1)
            add(v - 1, v)
    for i in range(len(D)):
        d, e, f = D[i], E[i], F[i]
        for y in range(e, f):
            v = d * W + y
            add(v, v - W)
            add(v - W, v)
    return head, ng[:p], nxt[:p]

head, ng, nxt = set_ng(A, B, C, D, E, F)

@njit
def next_w(head, ng, nxt, v):
    p = head[v]
    V = [v - W, v + W, v - 1, v + 1]
    while p != -1:
        V.remove(ng[p])
        p = nxt[p]
    return V

x0, y0 = np.searchsorted(X, 0), np.searchsorted(Y, 0)
v0 = x0 * W + y0

@njit
def solve():
    visited = np.zeros(N, np.bool_)
    visited[v0] = 1
    stack = np.empty(N, np.int64)
    p = 0
    ret = 0

    def area(x):
        x, y = divmod(x, W)
        return DX[x] * DY[y]

    def push(x):
        nonlocal p, ret
        stack[p] = x
        visited[x] = 1
        ret += area(x)
        p += 1

    def pop():
        nonlocal p
        p -= 1
        return stack[p]

    push(v0)
    while p:
        v = pop()
        for w in next_w(head, ng, nxt, v):
            if visited[w]:
                continue
            x, y = divmod(w, W)
            if x == 0 or x == H - 1 or y == 0 or y == W - 1:
                return 0
            #push(w)
    return ret

x = solve()
if x == 0:
    print('INF')
else:
    print(x)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
NS = []
X_num = set()
EW = []
Y_num = set()

INF = 10 ** 36

X_num.add(INF)
X_num.add(0)
X_num.add(-INF)
Y_num.add(INF)
Y_num.add(0)
Y_num.add(-INF)

cant_inc_x = [[0] * 3300 for i in range(3300)]
cant_dec_x = [[0] * 3300 for i in range(3300)]
cant_inc_y = [[0] * 3300 for i in range(3300)]
cant_dec_y = [[0] * 3300 for i in range(3300)]

for i in range(n):
    a, b, c = map(int, input().split())
    X_num.add(a)
    X_num.add(b)
    Y_num.add(c)
    NS.append((a,b,c))

for i in range(m):
    d, e, f = map(int, input().split())
    X_num.add(d)
    Y_num.add(e)
    Y_num.add(f)
    EW.append((d,e,f))

X_dict = {}
Y_dict = {}

X_num = sorted(list(X_num))
Y_num = sorted(list(Y_num))

xp = 0
yp = 0
xp += 1
yp += 1

for x in X_num:
    X_dict[x] = xp
    xp += 1

for y in Y_num:
    Y_dict[y] = yp
    yp += 1

#(a,b) (a,c)
for a, b, c in NS:
    a = X_dict[a]
    b = X_dict[b]
    c = Y_dict[c]
    cant_dec_y[a][c] += 1
    cant_dec_y[b][c] -= 1
    cant_inc_y[a][c-1] += 1
    cant_inc_y[b][c-1] -= 1
    

#(d,e) (d,f)
for d, e, f in EW:
    d = X_dict[d]
    e = Y_dict[e]
    f = Y_dict[f]
    cant_dec_x[d][e] += 1
    cant_dec_x[d][f] -= 1
    cant_inc_x[d-1][e] += 1
    cant_inc_x[d-1][f] -= 1

for i in range(3300):
    for j in range(3300):
        if(j):cant_dec_x[i][j] += cant_dec_x[i][j-1]
        if(j):cant_inc_x[i][j] += cant_inc_x[i][j-1]
        if(i):cant_dec_y[i][j] += cant_dec_y[i-1][j]
        if(i):cant_inc_y[i][j] += cant_inc_y[i-1][j]

from collections import deque
q = deque()
q.append((X_dict[0], Y_dict[0]))
done = [[False] * 3300 for _ in range(3300)]

ans = 0


while len(q) != 0:
    (nowx, nowy) = q.popleft()
    if nowx < 0 or nowy < 0 or nowx+1 >= len(X_num) or nowy+1 >= len(Y_num):
        continue
    if done[nowx][nowy]:continue
    done[nowx][nowy] = True
    ans += (X_num[nowx+1] - X_num[nowx]) * (Y_num[nowy+1] - Y_num[nowy])
    if cant_dec_x[nowx][nowy] == 0:q.append((nowx-1, nowy))
    if cant_dec_y[nowx][nowy] == 0:q.append((nowx, nowy-1))
    if cant_inc_x[nowx][nowy] == 0:q.append((nowx+1, nowy))
    if cant_inc_y[nowx][nowy] == 0:q.append((nowx, nowy+1))

if ans > INF:
    print("INF")
else:    
    print(ans)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
NS = []
X_num = set()
EW = []
Y_num = set()

INF = 10 ** 18

X_num.add(INF)
X_num.add(0)
X_num.add(-INF)
Y_num.add(INF)
Y_num.add(0)
Y_num.add(-INF)

cant_inc_x = [[0] * 3300 for i in range(3300)]
cant_dec_x = [[0] * 3300 for i in range(3300)]
cant_inc_y = [[0] * 3300 for i in range(3300)]
cant_dec_y = [[0] * 3300 for i in range(3300)]

for i in range(n):
    a, b, c = map(int, input().split())
    X_num.add(a)
    X_num.add(b)
    Y_num.add(c)
    NS.append((a,b,c))

for i in range(m):
    d, e, f = map(int, input().split())
    X_num.add(d)
    Y_num.add(e)
    Y_num.add(f)
    EW.append((d,e,f))

X_dict = {}
Y_dict = {}

X_num = sorted(list(X_num))
Y_num = sorted(list(Y_num))

xp = 0
yp = 0
xp += 1
yp += 1

for x in X_num:
    X_dict[x] = xp
    xp += 1

for y in Y_num:
    Y_dict[y] = yp
    yp += 1

#(a,b) (a,c)
for a, b, c in NS:
    a = X_dict[a]
    b = X_dict[b]
    c = Y_dict[c]
    cant_dec_y[a][c] += 1
    cant_dec_y[b][c] -= 1
    cant_inc_y[a][c-1] += 1
    cant_inc_y[b][c-1] -= 1
    

#(d,e) (d,f)
for d, e, f in EW:
    d = X_dict[d]
    e = Y_dict[e]
    f = Y_dict[f]
    cant_dec_x[d][e] += 1
    cant_dec_x[d][f] -= 1
    cant_inc_x[d-1][e] += 1
    cant_inc_x[d-1][f] -= 1

for i in range(3300):
    for j in range(3300):
        if(j):cant_dec_x[i][j] += cant_dec_x[i][j-1]
        if(j):cant_inc_x[i][j] += cant_inc_x[i][j-1]
        if(i):cant_dec_y[i][j] += cant_dec_y[i-1][j]
        if(i):cant_inc_y[i][j] += cant_inc_y[i-1][j]

from collections import deque
q = deque()
q.append((X_dict[0], Y_dict[0]))
done = [[False] * 3300 for _ in range(3300)]

ans = 0


while len(q) != 0:
    (nowx, nowy) = q.popleft()
    if nowx < 0 or nowy < 0 or nowx+1 >= len(X_num) or nowy+1 >= len(Y_num):
        continue
    if done[nowx][nowy]:continue
    done[nowx][nowy] = True
    ans += (X_num[nowx+1] - X_num[nowx]) * (Y_num[nowy+1] - Y_num[nowy])
    if cant_dec_x[nowx][nowy] == 0:q.append((nowx-1, nowy))
    if cant_dec_y[nowx][nowy] == 0:q.append((nowx, nowy-1))
    if cant_inc_x[nowx][nowy] == 0:q.append((nowx+1, nowy))
    if cant_inc_y[nowx][nowy] == 0:q.append((nowx, nowy+1))

if ans > INF:
    print("INF")
else:    
    print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    n, m = LI()
    abc = LIR(n)
    def_ = LIR(n)
    print("INF")
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

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

def compress(L):
    L2 = list(set(L))
    L2.sort()
    C = {v : k for k, v in enumerate(L2)}
    return L2, C

N, M = map(int, readline().split())
INF = 10**9+7

Wp = [tuple(map(int, readline().split())) for _ in range(N)]
Hp = [tuple(map(int, readline().split())) for _ in range(M)]

epx = [0, INF, -INF]
epy = [0, INF, -INF]

for X, st, en in Hp:
    epx.append(X)
    epy.append(st)
    epy.append(en)

for st, en, Y in Wp:
    epy.append(Y)
    epx.append(st)
    epx.append(en)
    

Elx, Cex = compress(epx)
Ely, Cey = compress(epy)
Lx = len(Elx)
Ly = len(Ely)

event = [[] for _ in range(Lx)]
for X, st, en in Hp:
    event[Cex[X]].append((0, Cey[st], Cey[en]))
for st, en, Y in Wp:
    event[Cex[st]].append((1, Cey[Y], 0))
    event[Cex[en]].append((-1, Cey[Y], 0))

geta = Ly
geta2 = 10**8
banned = set()

table = [0]*Ly
for xi in range(Lx):
    for a, b, c in event[xi]:
        if a == 0:
            for yi in range(b, c):
                banned.add(geta+geta2*((xi-1)*geta+yi))
                banned.add(-geta+geta2*(xi*geta+yi))
        elif a == 1:
            table[b] = 1
        else:
            table[b] = 0
    for i in range(Ly):
        if table[i]:
            banned.add(-1+geta2*(xi*geta+i))
            banned.add(1+geta2*(xi*geta+i-1))
                
ed = set()
for i in range(Lx):
    ed.add(i*geta)
    ed.add(i*geta+Ly-1)
for j in range(Ly):
    ed.add(j)
    ed.add((Lx-1)*geta+j)
used = set()
st = Cex[0]*geta+Cey[0]
stack = [st]
DIREC = (1, -1, -geta, geta)
ans = 'INF'
while stack:
    vn = stack.pop()
    if vn in ed:
        break
    for d in DIREC:
        if vn*geta2+d in banned:
            continue
        vf = vn+d
        if vf not in used:
            used.add(vf)
            stack.append(vf)

else:
    area = 0
    for k in used:
        x, y = divmod(k, geta)
        area += (Elx[x+1]-Elx[x])*(Ely[y+1]-Ely[y])
    ans = area
print(ans)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
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)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,m = LI()
    p = LIR(n)
    q = LIR(m)
    fx = set([-float("inf"),0,float("inf")])
    fy = set([-float("inf"),0,float("inf")])
    for a,b,c in p:
        fy.add(a)
        fy.add(a+1e-9)
        fy.add(a-1e-9)
        fy.add(b)
        fy.add(b+1e-9)
        fy.add(b-1e-9)
        fx.add(c)
        fx.add(c+1e-9)
        fx.add(c-1e-9)

    for d,e,f in q:
        fy.add(d)
        fy.add(d+1e-9)
        fy.add(d-1e-9)
        fx.add(e)
        fx.add(e+1e-9)
        fx.add(e-1e-9)
        fx.add(f)
        fx.add(f+1e-9)
        fx.add(f-1e-9)
    fy = list(fy)
    fx = list(fx)
    fy.sort()
    fx.sort()
    h = len(fy)+1
    w = len(fx)+1
    s = [[0]*w for i in range(h)]
    for a,b,c in p:
        ai = bisect.bisect_left(fy,a)
        bi = bisect.bisect_left(fy,b)
        ci = bisect.bisect_left(fx,c)
        s[ai][ci] += 1
        s[bi+1][ci] -= 1
        s[ai][ci+1] -= 1
        s[bi+1][ci+1] += 1
    for d,e,f in q:
        di = bisect.bisect_left(fy,d)
        ei = bisect.bisect_left(fx,e)
        fi = bisect.bisect_left(fx,f)
        s[di][ei] += 1
        s[di][fi+1] -= 1
        s[di+1][ei] -= 1
        s[di+1][fi+1] += 1
    for i in range(h):
        for j in range(len(fx)):
            s[i][j+1] += s[i][j]
    for i in range(len(fy)):
        for j in range(w):
            s[i+1][j] += s[i][j]
    d = [(1,0),(-1,0),(0,1),(0,-1)]
    ans = 0
    sx = bisect.bisect_left(fx,0)
    sy = bisect.bisect_left(fy,0)
    ans += (fx[sx+1]-fx[sx])*(fy[sy+1]-fy[sy])
    q = [(sy,sx)]
    nh,nw = len(fy)-1,len(fx)-1
    f = [[1]*nw for i in range(nh)]
    f[sy][sx] = 0
    while q:
        if ans == float("inf"):
            print("INF")
            return
        y,x = q.pop()
        for dy,dx in d:
            ny,nx = y+dy,x+dx
            if 0 <= ny < nh and 0 <= nx < nw:
                if f[ny][nx] and not s[ny][nx]:
                    ans += (fx[nx+1]-fx[nx])*(fy[ny+1]-fy[ny])
                    f[ny][nx] = 0
                    q.append((ny,nx))
    print(round(ans))
    return

#Solve
if __name__ == "__main__":
    solve()