Problem p02680 (python)

Fixed submission:
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,M=map(int,input().split())
    X,Y=set([0]),set([0])
    A,B,C=[0]*N,[0]*N,[0]*N
    for i in range(N):
        A[i],B[i],C[i]=map(int,input().split())
        X.add(A[i])
        X.add(B[i])
        Y.add(C[i])
    D,E,F=[0]*M,[0]*M,[0]*M
    for i in range(M):
        D[i],E[i],F[i]=map(int,input().split())
        X.add(D[i])
        Y.add(E[i])
        Y.add(F[i])
    UX=list(X)
    UY=list(Y)
    UX.sort()
    UY.sort()
    dictx,dicty={n:i*2 for i,n in enumerate(UX)},{n:i*2 for i,n in enumerate(UY)}
    LX=[UX[i//2+1]-UX[i//2] if i%2 else 0 for i in range(len(UX)*2-1)]
    LY=[UY[i//2+1]-UY[i//2] if i%2 else 0 for i in range(len(UY)*2-1)]
    c=[[0]*(len(UX)*2-1) for _ in range(len(UY)*2-1)]
    for i in range(N):
        y=dicty[C[i]]
        for x in range(dictx[A[i]],dictx[B[i]]+1):
            c[y][x]=1
    for i in range(M):
        x=dictx[D[i]]
        for y in range(dicty[E[i]],dicty[F[i]]+1):
            c[y][x]=1
    que=deque()
    used=[[0]*len(UX)*2 for _ in range(len(UY)*2)]
    dy=[1,1,-1,-1]
    dx=[1,-1,1,-1]
    for k in range(4):
        nny,nnx=dicty[0]+dy[k],dictx[0]+dx[k]
        if 1<=nny<len(UY)*2-1 and 1<=nnx<len(UX)*2-1:
            que.append((nny,nnx))
            used[nny][nnx]=1
    ans=0
    dy=[1,0,-1,0]
    dx=[0,1,0,-1]
    while que:
        y,x=que.popleft()
        if y%2 and x%2:
            ans+=LY[y]*LX[x]
        for k in range(4):
            ny,nx=y+dy[k],x+dx[k]
            nny,nnx=ny+dy[k],nx+dx[k]
            if c[ny][nx]==0:
                if nny<0 or nny>=len(UY)*2-1 or nnx<0 or nnx>=len(UX)*2-1:
                    print('INF')
                    exit()
                if not used[nny][nnx]:
                    que.append((nny,nnx))
                    used[nny][nnx]=1
    print(ans)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,M=map(int,input().split())
    X,Y=set([0]),set([0])
    A,B,C=[0]*N,[0]*N,[0]*N
    for i in range(N):
        A[i],B[i],C[i]=map(int,input().split())
        X.add(A[i])
        X.add(B[i])
        Y.add(C[i])
    D,E,F=[0]*M,[0]*M,[0]*M
    for i in range(M):
        D[i],E[i],F[i]=map(int,input().split())
        X.add(D[i])
        Y.add(E[i])
        Y.add(F[i])
    UX=list(X)
    UY=list(Y)
    UX.sort()
    UY.sort()
    dictx,dicty={n:i*2 for i,n in enumerate(UX)},{n:i*2 for i,n in enumerate(UY)}
    LX=[UX[i//2+1]-UX[i//2] if i%2 else 0 for i in range(len(UX)*2-1)]
    LY=[UY[i//2+1]-UY[i//2] if i%2 else 0 for i in range(len(UY)*2-1)]
    c=[[0]*len(UX)*2 for _ in range(len(UY)*2)]
    for i in range(N):
        y=dicty[C[i]]
        for x in range(dictx[A[i]],dictx[B[i]]+1):
            c[y][x]=1
    for i in range(M):
        x=dictx[D[i]]
        for y in range(dicty[E[i]],dicty[F[i]]+1):
            c[y][x]=1
    que=deque()
    used=[[0]*len(UX)*2 for _ in range(len(UY)*2)]
    dy=[1,1,-1,-1]
    dx=[1,-1,1,-1]
    for k in range(4):
        nny,nnx=dicty[0]+dy[k],dictx[0]+dx[k]
        if not 2<=nny<len(UY)*2-2 or not 2<=nnx<len(UX)*2-2:
            print('INF')
            exit()
        que.append((nny,nnx))
        used[nny][nnx]=1
    ans=0
    dy=[1,0,-1,0]
    dx=[0,1,0,-1]
    while que:
        y,x=que.popleft()
        if y%2 and x%2:
            ans+=LY[y]*LX[x]
        for k in range(4):
            ny,nx=y+dy[k],x+dx[k]
            nny,nnx=ny+dy[k],nx+dx[k]
            if c[ny][nx]==0:
                if ny==0 or ny==len(UY)*2-1 or nx==0 or nx==len(UX)*2-1:
                    print('INF')
                    exit()
                if not used[nny][nnx]:
                    que.append((nny,nnx))
                    used[nny][nnx]=1
    print(ans)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,M=map(int,input().split())
    X,Y=set([0]),set([0])
    A,B,C=[0]*N,[0]*N,[0]*N
    for i in range(N):
        A[i],B[i],C[i]=map(int,input().split())
        X.add(A[i])
        X.add(B[i])
        Y.add(C[i])
    D,E,F=[0]*M,[0]*M,[0]*M
    for i in range(M):
        D[i],E[i],F[i]=map(int,input().split())
        X.add(D[i])
        Y.add(E[i])
        Y.add(F[i])
    UX=list(X)
    UY=list(Y)
    UX.sort()
    UY.sort()
    dictx,dicty={n:i*2 for i,n in enumerate(UX)},{n:i*2 for i,n in enumerate(UY)}
    LX=[UX[i//2+1]-UX[i//2] if i%2 else 0 for i in range(len(UX)*2-1)]
    LY=[UY[i//2+1]-UY[i//2] if i%2 else 0 for i in range(len(UY)*2-1)]
    c=[[0]*len(UX)*2 for _ in range(len(UY)*2)]
    for i in range(N):
        y=dicty[C[i]]
        for x in range(dictx[A[i]],dictx[B[i]]+1):
            c[y][x]=1
    for i in range(M):
        x=dictx[D[i]]
        for y in range(dicty[E[i]],dicty[F[i]]+1):
            c[y][x]=1
    que=deque()
    used=[[0]*len(UX)*2 for _ in range(len(UY)*2)]
    dy=[1,1,-1,-1]
    dx=[1,-1,1,-1]
    for k in range(4):
        ny,nx=dicty[0]+dy[k],dictx[0]+dx[k]
        if not 1<=ny<len(UY)*2-1 or not 1<=nx<len(UX)*2-1:
            print('INF')
            exit()
        que.append((ny,nx))
        used[ny][nx]=1
    ans=0
    dy=[1,0,-1,0]
    dx=[0,1,0,-1]
    while que:
        y,x=que.popleft()
        if y%2 and x%2:
            ans+=LY[y]*LX[x]
        for k in range(4):
            ny,nx=y+dy[k],x+dx[k]
            if c[ny][nx]==0:
                if not 1<=ny<len(UY)*2-1 or not 1<=nx<len(UX)*2-1:
                    print('INF')
                    exit()
                if not used[ny+dy[k]][nx+dx[k]]:
                    que.append((ny+dy[k],nx+dx[k]))
                    used[ny+dy[k]][nx+dx[k]]=1
    print(ans)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,M=map(int,input().split())
    X,Y=set([0]),set([0])
    A,B,C=[0]*N,[0]*N,[0]*N
    for i in range(N):
        A[i],B[i],C[i]=map(int,input().split())
        X.add(A[i])
        X.add(B[i])
        Y.add(C[i])
    D,E,F=[0]*M,[0]*M,[0]*M
    for i in range(M):
        D[i],E[i],F[i]=map(int,input().split())
        X.add(D[i])
        Y.add(E[i])
        Y.add(F[i])
    UX=list(X)
    UY=list(Y)
    UX.sort()
    UY.sort()
    dictx,dicty={n:i*2 for i,n in enumerate(UX)},{n:i*2 for i,n in enumerate(UY)}
    LX=[UX[i//2+1]-UX[i//2] if i%2 else 0 for i in range(len(UX)*2-1)]
    LY=[UY[i//2+1]-UY[i//2] if i%2 else 0 for i in range(len(UY)*2-1)]
    c=[[0]*len(UX)*2 for _ in range(len(UY)*2)]
    for i in range(N):
        y=dicty[C[i]]
        for x in range(dictx[A[i]],dictx[B[i]]+1):
            c[y][x]=1
    for i in range(M):
        x=dictx[D[i]]
        for y in range(dicty[E[i]],dicty[F[i]]+1):
            c[y][x]=1
    que=deque()
    used=[[0]*len(UX)*2 for _ in range(len(UY)*2)]
    dy=[1,1,-1,-1]
    dx=[1,-1,1,-1]
    for k in range(4):
        ny,nx=dicty[0]+dy[k],dictx[0]+dx[k]
        if not 0<=ny<len(UY)*2 or not 0<=nx<len(UX)*2:
            print('INF')
            exit()
        que.append((ny,nx))
        used[ny][nx]=1
    ans=0
    dy=[1,0,-1,0]
    dx=[0,1,0,-1]
    while que:
        y,x=que.popleft()
        if y%2 and x%2:
            ans+=LY[y]*LX[x]
        for k in range(4):
            ny,nx=y+dy[k],x+dx[k]
            if c[ny][nx]==0:
                if not 0<=ny<len(UY)*2 or not 0<=nx<len(UX)*2:
                    print('INF')
                    exit()
                if not used[ny+dy[k]][nx+dx[k]]:
                    que.append((ny+dy[k],nx+dx[k]))
                    used[ny+dy[k]][nx+dx[k]]=1
    print(ans)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,M=map(int,input().split())
    X,Y=set([0]),set([0])
    A,B,C=[0]*N,[0]*N,[0]*N
    for i in range(N):
        A[i],B[i],C[i]=map(int,input().split())
        X.add(A[i])
        X.add(B[i])
        Y.add(C[i])
    D,E,F=[0]*M,[0]*M,[0]*M
    for i in range(M):
        D[i],E[i],F[i]=map(int,input().split())
        X.add(D[i])
        Y.add(E[i])
        Y.add(F[i])
    UX=list(X)
    UY=list(Y)
    UX.sort()
    UY.sort()
    dictx,dicty={n:i*2 for i,n in enumerate(UX)},{n:i*2 for i,n in enumerate(UY)}
    LX=[UX[i//2+1]-UX[i//2] if i%2 else 0 for i in range(len(UX)*2-1)]
    LY=[UY[i//2+1]-UY[i//2] if i%2 else 0 for i in range(len(UY)*2-1)]
    c=[[0]*len(UX)*2 for _ in range(len(UY)*2)]
    for i in range(N):
        y=dicty[C[i]]
        for x in range(dictx[A[i]],dictx[B[i]]+1):
            c[y][x]=1
    for i in range(M):
        x=dictx[D[i]]
        for y in range(dicty[E[i]],dicty[F[i]]+1):
            c[y][x]=1
    que=deque()
    used=[[0]*len(UX)*2 for _ in range(len(UY)*2)]
    dy=[1,1,-1,-1]
    dx=[1,-1,1,-1]
    for k in range(4):
        ny,nx=dicty[0]+dy[k],dictx[0]+dx[k]
        que.append((ny,nx))
        used[ny][nx]=1
    ans=0
    dy=[1,0,-1,0]
    dx=[0,1,0,-1]
    while que:
        y,x=que.popleft()
        if y%2 and x%2:
            ans+=LY[y]*LX[x]
        for k in range(4):
            ny,nx=y+dy[k],x+dx[k]
            if c[ny][nx]==0:
                if ny==0 or ny==len(UY)*2-1 or nx==0 or nx==len(UX)*2-1:
                    print('INF')
                    exit()
                if not used[ny+dy[k]][nx+dx[k]]:
                    que.append((ny+dy[k],nx+dx[k]))
                    used[ny+dy[k]][nx+dx[k]]=1
    print(ans)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,M=map(int,input().split())
    X,Y=set([0]),set([0])
    A,B,C=[0]*N,[0]*N,[0]*N
    for i in range(N):
        A[i],B[i],C[i]=map(int,input().split())
        X.add(A[i])
        X.add(B[i])
        Y.add(C[i])
    D,E,F=[0]*M,[0]*M,[0]*M
    for i in range(M):
        D[i],E[i],F[i]=map(int,input().split())
        X.add(D[i])
        Y.add(E[i])
        Y.add(F[i])
    UX=list(X)
    UY=list(Y)
    UX.sort()
    UY.sort()
    dictx,dicty={n:i*2 for i,n in enumerate(UX)},{n:i*2 for i,n in enumerate(UY)}
    LX=[UX[i//2+1]-UX[i//2] if i%2 else 0 for i in range(len(UX)*2-1)]
    LY=[UY[i//2+1]-UY[i//2] if i%2 else 0 for i in range(len(UY)*2-1)]
    c=[[0]*len(UX)*2 for _ in range(len(UY)*2)]
    for i in range(N):
        y=dicty[C[i]]
        for x in range(dictx[A[i]],dictx[B[i]]+1):
            c[y][x]=1
    for i in range(M):
        x=dictx[D[i]]
        for y in range(dicty[E[i]],dicty[F[i]]+1):
            c[y][x]=1
    que=deque()
    used=[[0]*len(UX)*2 for _ in range(len(UY)*2)]
    dy=[1,1,-1,-1]
    dx=[1,-1,1,-1]
    for k in range(4):
        ny,nx=dicty[0]+dy[k],dictx[0]+dx[k]
        que.append((ny,nx))
        used[ny][nx]=1
    ans=0
    dy=[1,0,-1,0]
    dx=[0,1,0,-1]
    while que:
        y,x=que.popleft()
        if y%2 and x%2:
            ans+=LY[y]*LX[x]
        for k in range(4):
            ny,nx=y+dy[k],x+dx[k]
            if c[ny][nx]==0 and not used[ny+dy[k]][nx+dx[k]]:
                if ny==0 or ny==len(UY)*2-1 or nx==0 or nx==len(UX)*2-1:
                    print('INF')
                    exit()
                que.append((ny+dy[k],nx+dx[k]))
                used[ny+dy[k]][nx+dx[k]]=1
    print(ans)

if __name__ == '__main__':
    main()