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: Time Limit Exceeded

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

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

    for i in range(m):
        q[i][0] <<= 1
        q[i][1] <<= 1
        q[i][2] <<= 1
        d,e,f = q[i]
        fy.add(d)
        fy.add(d+1)
        fx.add(e)
        fx.add(f)
        fx.add(f+1)
    fy = list(fy)
    fx = list(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]
    sx = bisect.bisect_left(fx,0)
    sy = bisect.bisect_left(fy,0)
    nh,nw = len(fy)-1,len(fx)-1
    for i in range(nw-1):
        fx[i+1] >>= 1
        fx[i] = fx[i+1]-fx[i]
    for i in range(nh-1):
        fy[i+1] >>= 1
        fy[i] = fy[i+1]-fy[i]
    ans = fx[sx]*fy[sy]
    q = deque()
    q.append((sy,sx))
    f = [1]*nh*nw
    f[sy*nw+sx] = 0
    d = [(1,0),(-1,0),(0,1),(0,-1)]
    while q:
        y,x = q.popleft()
        i = y*nw+x
        for dy,dx in d:
            ny,nx = y+dy,x+dx
            if 0 <= ny < nh and 0 <= nx < nw:
                ind = i+dy*nw+dx
                if f[ind] and not s[ind+(ny<<1)]:
                    if ny < 1 or nx < 1 or ny+1 >= nh or nx+1 >= nw:
                        print("INF")
                        return
                    k = fx[nx]*fy[ny]
                    ans += k
                    f[ind] = 0
                    q.append((ny,nx))
    print(ans)
    return

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

Submission status: Time Limit Exceeded

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

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

    for i in range(m):
        q[i][0] <<= 1
        q[i][1] <<= 1
        q[i][2] <<= 1
        d,e,f = q[i]
        fy.add(d)
        fy.add(d+1)
        fx.add(e)
        fx.add(f)
        fx.add(f+1)
    fy = list(fy)
    fx = list(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]
    sx = bisect.bisect_left(fx,0)
    sy = bisect.bisect_left(fy,0)
    nh,nw = len(fy)-1,len(fx)-1
    for i in range(nw-1):
        fx[i+1] >>= 1
        fx[i] = fx[i+1]-fx[i]
    for i in range(nh-1):
        fy[i+1] >>= 1
        fy[i] = fy[i+1]-fy[i]
    ans = fx[sx]*fy[sy]
    q = deque()
    q.append((sy,sx))
    f = [1]*nh*nw
    f[sy*nw+sx] = 0
    d = [(1,0),(-1,0),(0,1),(0,-1)]
    while q:
        y,x = q.popleft()
        i = y*nw+x
        for dy,dx in d:
            ny,nx = y+dy,x+dx
            if 0 <= ny < nh and 0 <= nx < nw:
                ind = i+dy*nw+dx
                if f[ind] and not s[ind+(ny<<1)]:
                    k = fx[nx]*fy[ny]
                    if k >= M2:
                        print("INF")
                        return
                    ans += k
                    f[ind] = 0
                    q.append((ny,nx))
    print(ans)
    return

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

Submission status: Time Limit Exceeded

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)

geta = Ly

UD = [[0]*(Lx+1) for _ in range(Ly+1)]
LR = [[0]*(Lx+1) for _ in range(Ly+1)]
for X, st, en in Hp:
    LR[Cey[st]][Cex[X]] += 1
    LR[Cey[en]][Cex[X]] -= 1
for st, en, Y in Wp:
    UD[Cey[Y]][Cex[st]] += 1
    UD[Cey[Y]][Cex[en]] -= 1

for x in range(1, Lx):
    for y in range(Ly):
        UD[y][x] += UD[y][x-1]

for y in range(1, Ly):
    for x in range(Lx):
        LR[y][x] += LR[y-1][x]
      
ed = set()
for i in range(Lx):
    ed.add((i, 0))
    ed.add((i, Ly-1))
for j in range(Ly):
    ed.add((0, j))
    ed.add((Lx-1, j))

sx, sy = Cex[0], Cey[0]
st = (sx, sy)
stack = [st]
used = [[False]*Lx for _ in range(Ly)]
used[sy][sx] = True
ans = 'INF'
area = 0
while stack:
    vx, vy = stack.pop()
    if (vx, vy) in ed:
        break
    area += (Elx[vx+1]-Elx[vx])*(Ely[vy+1]-Ely[vy])
    if not UD[vy+1][vx] and not used[vy+1][vx]:
        used[vy+1][vx] = True
        stack.append((vx, vy+1))
    if not UD[vy][vx] and not used[vy-1][vx]:
        used[vy-1][vx] = True
        stack.append((vx, vy-1))
    if not LR[vy][vx+1] and not used[vy][vx+1]:
        used[vy][vx+1] = True
        stack.append((vx+1, vy))
    if not LR[vy][vx] and not used[vy][vx-1]:
        used[vy][vx-1] = True
        stack.append((vx-1, vy))

else:
    ans = area
print(ans)

Submission status: Time Limit Exceeded

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

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

    for i in range(m):
        q[i][0] <<= 1
        q[i][1] <<= 1
        q[i][2] <<= 1
        d,e,f = q[i]
        fy.add(d)
        fy.add(d+1)
        fx.add(e)
        fx.add(f)
        fx.add(f+1)
    fy = list(fy)
    fx = list(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]
    sx = bisect.bisect_left(fx,0)
    sy = bisect.bisect_left(fy,0)
    nh,nw = len(fy)-1,len(fx)-1
    fx[1] >>= 1
    fy[1] >>= 1
    for i in range(1,nw-1):
        fx[i+1] >>= 1
        fx[i] = fx[i+1]-fx[i]
    for i in range(1,nh-1):
        fy[i+1] >>= 1
        fy[i] = fy[i+1]-fy[i]
    fx[0] = -fx[0]
    fy[0] = -fy[0]
    ans = fx[sx]*fy[sy]
    q = deque()
    q.append((sy,sx))
    f = [1]*nh*nw
    f[sy*nw+sx] = 0
    d = [(1,0,nw),(-1,0,-nw),(0,1,1),(0,-1,-1)]
    while q:
        y,x = q.popleft()
        i = y*nw+x
        for dy,dx,di in d:
            ny,nx = y+dy,x+dx
            if 0 <= ny < nh and 0 <= nx < nw:
                ind = i+di
                if f[ind] and not s[ind+(ny<<1)]:
                    k = fx[nx]*fy[ny]
                    if k >= M2:
                        print("INF")
                        return
                    ans += k
                    f[ind] = 0
                    q.append((ny,nx))
    print(ans)
    return

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

Submission status: Time Limit Exceeded

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 = 2*10**9+1
    M2 = M**2
    fx = set([-M,0,M])
    fy = set([-M,0,M])
    for i in range(n):
        p[i][0] <<= 1
        p[i][1] <<= 1
        p[i][2] <<= 1
        a,b,c = p[i]
        fy.add(a)
        fy.add(b)
        fy.add(b+1)
        fx.add(c)
        fx.add(c+1)

    for i in range(m):
        q[i][0] <<= 1
        q[i][1] <<= 1
        q[i][2] <<= 1
        d,e,f = q[i]
        fy.add(d)
        fy.add(d+1)
        fx.add(e)
        fx.add(f)
        fx.add(f+1)
    fy = list(fy)
    fx = list(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] >>= 1
    fy[1] >>= 1
    for i in range(1,nw-1):
        fx[i+1] >>= 1
        fx[i] = fx[i+1]-fx[i]
    for i in range(1,nh-1):
        fy[i+1] >>= 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]*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+(ny<<1)]:
                    k = fx[nx]*fy[ny]
                    if k >= M2:
                        print("INF")
                        return
                    ans += k
                    f[ind] = 0
                    q.append((ny,nx))
    print(ans)
    return

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

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
import sys
import bisect
from collections import deque
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 = 2*10**9+1
    M2 = M**2
    fx = set([-M,0,M])
    fy = set([-M,0,M])
    for i in range(n):
        p[i][0] <<= 1
        p[i][1] <<= 1
        p[i][2] <<= 1
        a,b,c = p[i]
        fy.add(a)
        fy.add(b)
        fy.add(b+1)
        fx.add(c)
        fx.add(c+1)

    for i in range(m):
        q[i][0] <<= 1
        q[i][1] <<= 1
        q[i][2] <<= 1
        d,e,f = q[i]
        fy.add(d)
        fy.add(d+1)
        fx.add(e)
        fx.add(f)
        fx.add(f+1)
    fy = list(fy)
    fx = list(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]
    sx = bisect.bisect_left(fx,0)
    sy = bisect.bisect_left(fy,0)
    nh,nw = len(fy)-1,len(fx)-1
    fx[1] >>= 1
    fy[1] >>= 1
    for i in range(1,nw-1):
        fx[i+1] >>= 1
        fx[i] = fx[i+1]-fx[i]
    for i in range(1,nh-1):
        fy[i+1] >>= 1
        fy[i] = fy[i+1]-fy[i]
    fx[0] = -fx[0]
    fy[0] = -fy[0]
    ans = fx[sx]*fy[sy]
    q = deque()
    i = sy*nw+sx
    q.append((sy,sx,i))
    f = [1]*nh*nw
    f[i] = 0
    d = [(1,0,nw),(-1,0,-nw),(0,1,1),(0,-1,-1)]
    while q:
        y,x,i = q.popleft()
        for dy,dx,di in d:
            ny,nx,ni = y+dy,x+dx,i+di
            if 0 <= ny < nh and 0 <= nx < nw:
                if f[ni] and not s[ni+(ny<<1)]:
                    k = fx[nx]*fy[ny]
                    if k >= M2:
                        print("INF")
                        return
                    ans += k
                    f[ni] = 0
                    q.append((ny,nx,ni))
    print(ans)
    return

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

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
import sys
import bisect
from collections import deque
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 = 2*10**9+1
    M2 = M**2
    fx = set([-M,0,M])
    fy = set([-M,0,M])
    for i in range(n):
        p[i][0] <<= 1
        p[i][1] <<= 1
        p[i][2] <<= 1
        a,b,c = p[i]
        fy.add(a)
        fy.add(b)
        fy.add(b+1)
        fx.add(c)
        fx.add(c+1)

    for i in range(m):
        q[i][0] <<= 1
        q[i][1] <<= 1
        q[i][2] <<= 1
        d,e,f = q[i]
        fy.add(d)
        fy.add(d+1)
        fx.add(e)
        fx.add(f)
        fx.add(f+1)
    fy = list(fy)
    fx = list(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] >>= 1
    fy[1] >>= 1
    for i in range(1,nw-1):
        fx[i+1] >>= 1
        fx[i] = fx[i+1]-fx[i]
    for i in range(1,nh-1):
        fy[i+1] >>= 1
        fy[i] = fy[i+1]-fy[i]
    fx[0] = -fx[0]
    fy[0] = -fy[0]
    ans = fx[sx]*fy[sy]
    q = deque()
    q.append((sy,sx))
    f = [1]*nh*nw
    f[sy*nw+sx] = 0
    while q:
        y,x = q.popleft()
        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+(ny<<1)]:
                    k = fx[nx]*fy[ny]
                    if k >= M2:
                        print("INF")
                        return
                    ans += k
                    f[ind] = 0
                    q.append((ny,nx))
    print(ans)
    return

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

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
import sys
import bisect
from collections import deque
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 = 2*10**9+1
    M2 = M**2
    fx = set([-M,0,M])
    fy = set([-M,0,M])
    for i in range(n):
        p[i][0] <<= 1
        p[i][1] <<= 1
        p[i][2] <<= 1
        a,b,c = p[i]
        fy.add(a)
        fy.add(b)
        fy.add(b+1)
        fx.add(c)
        fx.add(c+1)

    for i in range(m):
        q[i][0] <<= 1
        q[i][1] <<= 1
        q[i][2] <<= 1
        d,e,f = q[i]
        fy.add(d)
        fy.add(d+1)
        fx.add(e)
        fx.add(f)
        fx.add(f+1)
    fy = list(fy)
    fx = list(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] >>= 1
    fy[1] >>= 1
    for i in range(1,nw-1):
        fx[i+1] >>= 1
        fx[i] = fx[i+1]-fx[i]
    for i in range(1,nh-1):
        fy[i+1] >>= 1
        fy[i] = fy[i+1]-fy[i]
    fx[0] = -fx[0]
    fy[0] = -fy[0]
    ans = fx[sx]*fy[sy]
    q = deque()
    q.append((sy,sx))
    f = [1]*nh*nw
    f[sy*nw+sx] = 0
    while q:
        y,x = q.popleft()
        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 >= M2:
                        print("INF")
                        return
                    ans += k
                    f[ind] = 0
                    q.append((ny,nx))
    print(ans)
    return

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

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
import sys
import bisect
from collections import deque
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 = 3*10**9
    M2 = M**2
    fx = set([-M,0,M])
    fy = set([-M,0,M])
    for i in range(n):
        p[i][0] <<= 1
        p[i][1] <<= 1
        p[i][2] <<= 1
        a,b,c = p[i]
        fy.add(a)
        fy.add(b)
        fy.add(b+1)
        fx.add(c)
        fx.add(c+1)

    for i in range(m):
        q[i][0] <<= 1
        q[i][1] <<= 1
        q[i][2] <<= 1
        d,e,f = q[i]
        fy.add(d)
        fy.add(d+1)
        fx.add(e)
        fx.add(f)
        fx.add(f+1)
    fy = list(fy)
    fx = list(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] >>= 1
    fy[1] >>= 1
    for i in range(1,nw-1):
        fx[i+1] >>= 1
        fx[i] = fx[i+1]-fx[i]
    for i in range(1,nh-1):
        fy[i+1] >>= 1
        fy[i] = fy[i+1]-fy[i]
    fx[0] = -fx[0]
    fy[0] = -fy[0]
    ans = fx[sx]*fy[sy]
    q = deque()
    q.append((sy,sx))
    f = [1]*nh*nw
    f[sy*nw+sx] = 0
    while q:
        y,x = q.popleft()
        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 >= M2:
                        print("INF")
                        return
                    ans += k
                    f[ind] = 0
                    q.append((ny,nx))
    print(ans)
    return

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

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
import sys
import bisect
from collections import deque
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**10
    M2 = M**2
    fx = set([-M,0,M])
    fy = set([-M,0,M])
    for i in range(n):
        p[i][0] *= 2
        p[i][1] *= 2
        p[i][2] *= 2
        a,b,c = p[i]
        fy.add(a)
        fy.add(b)
        fy.add(b+1)
        fx.add(c)
        fx.add(c+1)

    for i in range(m):
        q[i][0] *= 2
        q[i][1] *= 2
        q[i][2] *= 2
        d,e,f = q[i]
        fy.add(d)
        fy.add(d+1)
        fx.add(e)
        fx.add(f)
        fx.add(f+1)
    fy = list(fy)
    fx = list(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] //= 2
    fy[1] //= 2
    for i in range(1,nw-1):
        fx[i+1] = fx[i+1]//2
        fx[i] = fx[i+1]-fx[i]
    for i in range(1,nh-1):
        fy[i+1] = fy[i+1]//2
        fy[i] = fy[i+1]-fy[i]
    fx[0] = -fx[0]
    fy[0] = -fy[0]
    ans = fx[sx]*fy[sy]
    q = deque()
    q.append((sy,sx))
    f = [1]*nh*nw
    f[sy*nw+sx] = 0
    while q:
        y,x = q.popleft()
        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 >= M2:
                        print("INF")
                        return
                    ans += k
                    f[ind] = 0
                    q.append((ny,nx))
    print(ans)
    return

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

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
import sys
import bisect
from collections import deque
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
    M2 = M**2
    fx = set([-M,0,M])
    fy = set([-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.add(a)
        fy.add(b)
        fy.add(b+1)
        fx.add(c)
        fx.add(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.add(d)
        fy.add(d+1)
        fx.add(e)
        fx.add(f)
        fx.add(f+1)
    fy = list(fy)
    fx = list(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 = deque()
    q.append((sy,sx))
    f = [1]*nh*nw
    f[sy*nw+sx] = 0
    while q:
        y,x = q.popleft()
        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 >= M2:
                        print("INF")
                        return
                    ans += k
                    f[ind] = 0
                    q.append((ny,nx))
    print(ans)
    return

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