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
import numpy as np
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LIR(n):
    return np.array([LI() for i in range(n)])

def solve():
    n,m = LI()
    p = LIR(n)
    q = LIR(m)
    M = 10**11
    M2 = M**2
    fx = [-M,0,M]
    fy = [-M,0,M]
    for i in range(n):
        p[i] *= 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] *= 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 = np.zeros((h,w))
    for a,b,c in p:
        ai = bisect.bisect_left(fy,a)
        bi = bisect.bisect_left(fy,b)+1
        ci = bisect.bisect_left(fx,c)
        s[ai][ci] += 1
        s[bi][ci] -= 1
        s[ai][ci+1] -= 1
        s[bi][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)+1
        kd = di*w
        kdd = kd+w
        s[di][ei] += 1
        s[di][fi] -= 1
        s[di+1][ei] -= 1
        s[di+1][fi] += 1
    for j in range(len(fx)):
        s[:,j+1] += s[:,j]
    for i in range(len(fy)):
        s[i+1] += s[i]
    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[ny][nx]:
                    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 = [-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 = 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

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 = [-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 >= 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 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 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]
    aa = 10 ** 5
    q = [sy * aa + sx]
    f = [1]*nh*nw
    f[sy*nw+sx] = 0
    while q:
        yx = q.pop()
        y,x = divmod(yx,aa)
        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*aa+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 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 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: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
import sys
from bisect import bisect_left
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)
        fy.append(b+1e-1)
        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)
        fx.append(f+1e-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_left(fy,a)
        bi = bisect_left(fy,b)
        ci = 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_left(fy,d)
        ei = bisect_left(fx,e)
        fi = 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_left(fx,0)
    sy = 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]*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: Time Limit Exceeded

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)
        fy.append(b+1e-1)
        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)
        fx.append(f+1e-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
    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]*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: Time Limit Exceeded

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)
        fy.append(b+1e-1)
        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)
        fx.append(f+1e-1)
    fy = list(set(fy))
    fx = list(set(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):
        s[i] = list(accumulate(s[i]))
    for i in range(len(fy)):
        ni = i+1
        for j in range(w):
            s[ni][j] += s[i][j]
    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][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: Time Limit Exceeded

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)
        fy.append(b+1e-1)
        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)
        fx.append(f+1e-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
    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: Time Limit Exceeded

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(a+1e-1)
        fy.append(b)
        fy.append(b+1e-1)
        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(e+1e-1)
        fx.append(f)
        fx.append(f+1e-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
    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: Time Limit Exceeded

Solution code:
import sys
import numpy as np


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.readline().split(sep=sep, maxsplit=maxsplit))

def input_row(n : int, type=np.int, **kwargs):
	return np.fromiter(inputs(type, **kwargs), dtype=type)

def input_2d(nrows : int, ncols : int, type=np.int, **kwargs):
	data = np.zeros((nrows, ncols), dtype=type)
	for i in range(nrows):
		data[i, :] = input_row(ncols, type, **kwargs)
	return data

from collections import deque
from bisect import bisect_right
import numpy as np


N, M = inputs(int)
A, B, C = input_2d(N, 3).T
D, E, F = input_2d(M, 3).T
xs = set()
ys = set()

for i in range(N):
	xs.add(C[i])
	ys.add(A[i])
	ys.add(B[i])

for i in range(M):
	ys.add(D[i])
	xs.add(E[i])
	xs.add(F[i])

xs = sorted(xs)
ys = sorted(ys)

x_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)
y_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)

for i in range(N):
	c = bisect_right(xs, C[i])
	a = bisect_right(ys, A[i])
	b = bisect_right(ys, B[i])
	x_guard[c, a:b] = True

for i in range(M):
	d = bisect_right(ys, D[i])
	e = bisect_right(xs, E[i])
	f = bisect_right(xs, F[i])
	y_guard[e:f, d] = True

nexts = deque()
nexts.append((bisect_right(xs, 0), bisect_right(ys, 0)))
visited = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)

area = 0

while nexts:
	xi, yi = nexts.popleft()
	if not visited[xi, yi]:
		visited[xi, yi] = True
		if 0 < xi < len(xs) and 0 < yi < len(ys):
			area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
			if not x_guard[xi, yi]:
				nexts.append((xi-1, yi))
			if not x_guard[xi+1, yi]:
				nexts.append((xi+1, yi))
			if not y_guard[xi, yi]:
				nexts.append((xi, yi-1))
			if not y_guard[xi, yi+1]:
				nexts.append((xi, yi+1))
		else:
			area = 'INF'
			break

print(area)

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
import sys
import bisect
from collections import defaultdict,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)
    fx = [-float("inf"),0,float("inf")]
    fy = [-float("inf"),0,float("inf")]
    for a,b,c in p:
        fy.append(a)
        fy.append(a+1e-1)
        fy.append(b)
        fy.append(b+1e-1)
        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(e+1e-1)
        fx.append(f)
        fx.append(f+1e-1)
    fy = list(set(fy))
    fx = list(set(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)]
    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 = deque()
    q.append((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][nx]:
                    k = fx[nx]*fy[ny]
                    if k == float("inf"):
                        print("INF")
                        return
                    ans += k
                    f[ny][nx] = 0
                    q.appendleft((ny,nx))
    print(ans)
    return

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