Problem:
 Score : 200 points    Problem Statement There is a rectangle in the xy -plane, with its lower left corner at (0, 0) and its upper right corner at (W, H) . Each of its sides is parallel to the x -axis or y -axis. Initially, the whole region within the rectangle is painted white.  Snuke plotted N points into the rectangle. The coordinate of the i -th ( 1 ≦ i ≦ N ) point was (x_i, y_i) .  Then, he created an integer sequence a of length N , and for each 1 ≦ i ≦ N , he painted some region within the rectangle black, as follows:   If a_i = 1 , he painted the region satisfying x < x_i within the rectangle.  If a_i = 2 , he painted the region satisfying x > x_i within the rectangle.  If a_i = 3 , he painted the region satisfying y < y_i within the rectangle.  If a_i = 4 , he painted the region satisfying y > y_i within the rectangle.   Find the area of the white region within the rectangle after he finished painting.      Constraints  1 ≦ W, H ≦ 100  1 ≦ N ≦ 100  0 ≦ x_i ≦ W ( 1 ≦ i ≦ N )  0 ≦ y_i ≦ H ( 1 ≦ i ≦ N )  W , H (21:32, added), x_i and y_i are integers.  a_i ( 1 ≦ i ≦ N ) is 1, 2, 3 or 4 .         Input The input is given from Standard Input in the following format:  W  H  N  x_1  y_1  a_1  x_2  y_2  a_2  :  x_N  y_N  a_N       Output Print the area of the white region within the rectangle after Snuke finished painting.        Sample Input 1 5 4 2
2 1 1
3 3 4      Sample Output 1 9  The figure below shows the rectangle before Snuke starts painting.     First, as (x_1, y_1) = (2, 1) and a_1 = 1 , he paints the region satisfying x < 2 within the rectangle:     Then, as (x_2, y_2) = (3, 3) and a_2 = 4 , he paints the region satisfying y > 3 within the rectangle:     Now, the area of the white region within the rectangle is 9 .       Sample Input 2 5 4 3
2 1 1
3 3 4
1 4 2      Sample Output 2 0  It is possible that the whole region within the rectangle is painted black.       Sample Input 3 10 10 5
1 6 1
4 1 3
6 9 4
9 4 2
3 1 3      Sample Output 3 64

Solution (Python):
w,h,n=map(int,input().split())
t=[list(map(int,input().split()))for i in range(n)]
x_left=0
x_right=w
y_low=0
y_hight=h
for i in range(n):
    if t[i][2]==1:
        if x_left<=t[i][0]:
            x_left=t[i][0]
    elif t[i][2]==2:
        if t[i][0]<=x_right:
            x_right=t[i][0]
    elif t[i][2]==3:
        if y_low<=t[i][1]:
            y_low=t[i][1]
    elif t[i][2]==4:
        if t[i][1]<=y_hight:
            y_hight=t[i][1]
if x_right<=x_left or y_hight<=y_low:
    print(0)
else:
    print((x_right-x_left)*(y_hight-y_low))

Solution (Python):
W,H,N=map(int,input().split())
R=[0,0,W,0,H]
for _ in[0]*N:
    *wh,a=map(int,input().split())
    R[a]=eval(['min','max'][a%2]+'(R[a],wh[a//3])')
_,w,W,h,H=R
print((W-w)*(H-h)*(W>w)*(H>h))

Solution (Python):
W,H,N=map(int,input().split())
R=[0,0,W,0,H]
for _ in[0]*N:
    *wh,a=map(int,input().split())
    R[a]=[min(R[a],wh[a//3]),max(R[a],wh[a//3])][a%2]
_,w,W,h,H=R
print((W-w)*(H-h)*(W>w)*(H>h))

Solution (Python):
W,H,N = map(int,input().split())
w=h=0
for _ in[0]*N:
    x,y,a = map(int,input().split())
    if a == 1:
        w = max(w,x)
    elif a == 2:
        W = min(W,x)
    elif a == 3:
        h = max(h,y)
    else:
        H = min(H,y)
print((W-w)*(H-h)*(W>w)*(H>h))

Solution (Python):


w, h, n = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(n)]
field = [['.' for j in range(w)] for i in range(h)]
for a in A:
    if a[2] == 1:
        for i in range(h):
            for j in range(a[0]):
                field[i][j] = '
    elif a[2] == 2:
        for i in range(h):
            for j in range(a[0], w):
                field[i][j] = '
    elif a[2] == 3:
        for i in range(a[1]):
            for j in range(w):
                field[i][j] = '
    elif a[2] == 4:
        for i in range(a[1], h):
            for j in range(w):
                field[i][j] = '
ans = 0
for line in field:
    ans += line.count('.')
print(ans)

Solution (Python):
W, H, N = map(int, input().split())    
xya_list = [list(map(int, input().split())) for  _ in range(N)]
x_lower, x_upper, y_lower, y_upper =0, W,  0, H
for i in xya_list:
    if i[2] == 1 and i[0] > x_lower:
        x_lower = i[0]
    elif i[2] == 2 and i[0] < x_upper:
        x_upper = i[0]
    elif i[2] == 3 and i[1] > y_lower:
        y_lower = i[1]
    elif i[2] == 4 and i[1] < y_upper:
        y_upper = i[1]

area = (x_upper - x_lower) * (y_upper - y_lower)
if x_upper - x_lower > 0 and y_upper - y_lower > 0:
    print(area)
else:
    print(0)

Solution (Python):


W, H, N = map(int, input().split())
aN = [list(map(int, input().split())) for i in range (N)]


minXY = [0, 0]
maxXY = [W, H]
for i in range(N):
    
    if aN[i][2] == 1:
        minXY[0] = max(minXY[0], aN[i][0])

    elif aN[i][2] == 2:
        maxXY[0] = min(maxXY[0], aN[i][0])

    elif aN[i][2] == 3:
        minXY[1] = max(minXY[1], aN[i][1])

    elif aN[i][2] == 4:
        maxXY[1] = min(maxXY[1], aN[i][1])


ansW = maxXY[0] - minXY[0]
ansH = maxXY[1] - minXY[1]
if ansW < 0:
    ansW = 0
if ansH < 0:
    ansH = 0

print(ansH * ansW)

Solution (Python):


W, H, N = map(int, input().split())
HW = [[0 for i in range(W)] for j in range(H)]

aN = [list(map(int, input().split())) for i in range (N)]

for i in range(N):

    if aN[i][2] == 1:
        for j in range(H):
            for k in range(aN[i][0]):
                HW[j][k] = 1

    elif aN[i][2] == 2:
        for j in range(H):
            for k in range(aN[i][0], W):
                HW[j][k] = 1

    elif aN[i][2] == 3:
        for j in range(aN[i][1]):
            for k in range(W):
                HW[j][k] = 1

    elif aN[i][2] == 4:
        for j in range(aN[i][1], H):
            for k in range(W):
                HW[j][k] = 1

ans = 0
for i in range(H):
    ans += HW[i].count(0)
print(ans)

Solution (Python):
w, h, n = map(int, raw_input().split())
nums = []
for i in range(n):
  nums.append(map(int, raw_input().split()))

res = 0
x_1, x_2, y_1, y_2 = 0, w, 0, h
for a in nums:
  if a[2] == 1:
    if a[0] > x_1:
      x_1 = a[0]
  elif a[2] == 2:
    if a[0] < x_2:
      x_2 = a[0]
  elif a[2] == 3:
    if a[1] > y_1:
      y_1 = a[1]
  elif a[2] == 4:
    if a[1] < y_2:
      y_2 = a[1]
if x_2 <= x_1 or y_2 <= y_1:
  print(0)
else:
  print((y_2-y_1)*(x_2-x_1))

Solution (Python):
w, h, n = map(int, input().split())
g = [[1]*h for _ in range(w)]
for _ in range(n):
  x, y, a = map(int, input().split())
  for i in range(w):
    for j in range(h):
      if (a==1 and i<x) or (a==2 and i>=x) or (a==3 and j<y) or (a==4 and j>=y):
        g[i][j] = 0

print(sum(map(sum, g)))

Solution (Python):
w, h, n = list(map(int, input().split()))

points_dir = []
for i in range(n):
  points_dir.append(list(map(int, input().split())))


left_lim = 0
right_lim = w
bottom_lim = 0
top_lim = h

for i in range(n):
  if points_dir[i][2] == 1:
    left_lim = points_dir[i][0] if points_dir[i][0] > left_lim else left_lim
  elif points_dir[i][2] == 2:
    right_lim = points_dir[i][0] if points_dir[i][0] < right_lim else right_lim
  elif points_dir[i][2] == 3:
    bottom_lim = points_dir[i][1] if points_dir[i][1] > bottom_lim else bottom_lim
  elif points_dir[i][2] == 4:
    top_lim = points_dir[i][1] if points_dir[i][1] < top_lim else top_lim

if right_lim - left_lim < 0 or top_lim - bottom_lim < 0:
  print("0")
else:
  print((right_lim - left_lim) * (top_lim - bottom_lim))

Solution (Python):
def solve(W,H,N):
    B = [[1 for i in range(W)] for j in range(H)]
    for i in range(N):
        x,y,a =[int(j) for j in input().split(' ')]
        i
        if a==1:
            for j in range(x):
                for k in range(H):
                    B[k][j]=0
        if a==2:
            for j in range(x,W):
                for k in range(H):
                    B[k][j]=0
        if a==3:
            for j in range(y):
                B[j] =[0]*W
        if a==4:
            for j in range(y,H):
                B[j] =[0]*W
    
    c =0
    for i in range(W):
        for j in range(H):
            if B[j][i] ==1:
                c +=1
    print(c)
W,H,N = [int(i) for i in input().split(' ')]

solve(W,H,N)

Solution (Python):
W, H, N = map(int, input().split())
xya = []
for i in range(N):
    xya.append(list(map(int, input().split())))

x = [False]*(W)
y = [False]*(H)
for i in range(N):
    if xya[i][2] == 1:
        for j in range(xya[i][0]):
            x[j] = True
    elif xya[i][2] == 2:
        for j in range(xya[i][0], W):
            x[j] = True
    elif xya[i][2] == 3:
        for j in range(xya[i][1]):
            y[j] = True
    elif xya[i][2] == 4:
        for j in range(xya[i][1], H):
            y[j] = True

print(x.count(False)*y.count(False))

Solution (Python):
[w,h,n]=[int(i) for i in input().split()]
a=[]
for i in range(n):
    a.append([int(j) for j in input().split()])



b=[]
for i in range(w):
    for j in range(h):
        b.append([i,j])


for i in range(n):
    if a[i][2]==1:
        for j in range(w*h):
            if b[j][0]<a[i][0]:
                b[j]=[-10,-10]
    elif a[i][2]==2:
        for j in range(w*h):
            if b[j][0]>=a[i][0]:
                b[j]=[-10,-10]
    elif a[i][2]==3:
        for j in range(w*h):
            if b[j][1]<a[i][1]:
                b[j]=[-10,-10]
    elif a[i][2]==4:
        for j in range(w*h):
            if b[j][1]>=a[i][1]:
                b[j]=[-10,-10]

c=[i for i in b if i!=[-10,-10]]

print(len(c))

Solution (Python):
W,H,N = (int(i) for i in input().split())

upright = [W,H]
downleft = [0,0]

for i in range(N):
    x,y,a = (int(k) for k in input().split())
    if a == 1:
        if x >= downleft[0]:
            downleft[0] = x
    elif a == 2:
        if x <= upright[0]:
            upright[0] = x
    elif a == 3:
        if y >= downleft[1]:
            downleft[1] = y
    else:
        if y <= upright[1]:
            upright[1] = y

height = upright[1]-downleft[1]
width = upright[0]-downleft[0]

if height >= 0 and width >= 0:
    area = height*width
else:
    area = 0

print(area)

Solution (Python):
import numpy as np
w,h,n=map(int,raw_input().split())
mp = np.zeros((w,h))
for i in range(n):
  xi,yi,ai=map(int,raw_input().split())
  if ai==1: 
    mp[:xi,:] = 1
  elif ai==2: 
    mp[xi:,:] = 1
  elif ai==3: 
    mp[:,:yi] = 1
  elif ai==4: 
    mp[:,yi:] = 1
print w*h - np.sum(mp)

Solution (Python):
w,h,n = map(int, input().split())
b = [[0]*(w+1) for _ in range(h+1)]

for _ in range(n):
    x,y,a = map(int, input().split())
    if a <= 2:
        f = 0 if a == 1 else x
        t = x if a == 1 else w
        b[0][f] += 1
        b[0][t] -= 1
    else:
        f = 0 if a == 3 else y
        t = y if a == 3 else h
        b[f][0] += 1
        b[t][0] -= 1

ans = 0
for i in range(w):
    for j in range(h):
        b[j+1][i] += b[j][i]
for j in range(h):
    for i in range(w):
        b[j][i+1] += b[j][i]
        if b[j][i] == 0:
            ans += 1
print(ans)

Solution (Python):
l,d=0,0
w,h,n=map(int,input().split())
for _ in [0]*n:
    x,y,a=map(int,input().split())
    if a==1 and l<x:l=x
    elif a==2 and w>x:w=x
    elif a==3 and d<y:d=y
    elif a==4 and h>y:h=y
if w<=l or h<=d:print(0)
else:print((w-l)*(h-d))

Solution (Python):
W, H, N = map(int, input().split())
points = [list(map(int, input().split())) for _ in range(N)]

low_left_x = 0
low_left_y = 0
s = 0

for i in range(N):
    if points[i][2] == 1:
        if points[i][0] > low_left_x:
            low_left_x = points[i][0]
    elif points[i][2] == 2:
        if points[i][0] < W:
            W = points[i][0]
    elif points[i][2] == 3:
        if points[i][1] > low_left_y:
            low_left_y = points[i][1]
    elif points[i][2] == 4:
        if points[i][1] < H:
            H = points[i][1]
    
    if W<low_left_x:
        W = low_left_x
    if H < low_left_y:
        H = low_left_x

s = (W-low_left_x)*(H-low_left_y)
print(s)

Solution (Python):

W,H,N = list(map(int, input().split()))
mesh = [[0]*W for i in range(H)]
points = []
for i in range(N):
    points.append(list(map(int, input().split())))

for i in range(N):
    selection = points[i][2]
    if selection == 1:
        for j in range(0,H):
            for k in range(0, points[i][0]):
                mesh[j][k] = 1
    elif selection == 2:
        for j in range(0,H):
            for k in range(points[i][0], W):
                mesh[j][k] = 1
    elif selection == 3:
        for j in range(0, points[i][1]):
            for k in range(0,W):
                mesh[j][k] = 1
    elif selection == 4:
        for j in range(points[i][1], H):
            for k in range(0,W):
                mesh[j][k] = 1
    else:
        print("invalid value!")

colored_sum = 0
for i in range(H):
    colored_sum += sum(mesh[i])

white_sum = W*H - colored_sum
print(white_sum)

Solution (Python):
e,n,N = map(int,input().split())
w=s=0
for _ in[0]*N:
    x,y,a = map(int,input().split())
    if a == 1:
        w = max(w,x)
    elif a == 2:
        e = min(e,x)
    elif a == 3:
        s = max(s,y)
    else:
        n = min(n,y)
print((e-w)*(n-s)*(e>w)*(n>s))

Solution (Python):
W, H, N = (int(i) for i in input().split())
points = [[int(i) for i in input().split()] for i in range(N)]
low_left_x = 0
low_left_y = 0
s = 0
j = 0

for j in range(N):
    if(points[j][2] == 1): 
        if(points[j][0] > low_left_x):
            low_left_x = points[j][0]
    elif(points[j][2] == 2): 
        if(points[j][0] < W):
            W = points[j][0]
    elif(points[j][2] == 3): 
        if(points[j][1] > low_left_y):
            low_left_y = points[j][1]
    elif(points[j][2] == 4): 
        if(points[j][1] < H):
            H = points[j][1]
    
    if(W < low_left_x):
        W = low_left_x
    if(H < low_left_y):
        H = low_left_y

s = (W - low_left_x) * (H - low_left_y)
print(s)

Solution (Python):
w,h,n=map(int,input().split())
wl=hl=0
for i in range(n):
    x,y,a=map(int,input().split())
    if a==1:
        wl=max(wl,x)
    elif a==2:
        w=min(w,x)
    elif a==3:
        hl=max(hl,y)
    else:
        h=min(h,y)
print(max(0,(h-hl))*max(0,(w-wl)))

Solution (Python):
w, h, n = input().split()
lx ,ly ,hx ,hy = 0, 0, int(w), int(h)
for i in range(int(n)):
    S = list(map(int, input().split()))
    if S[2]==1 and lx<S[0]:
        lx = S[0]
    elif S[2]==2 and hx>S[0]:
        hx = S[0]
    elif S[2]==3 and ly<S[1]:
        ly = S[1]
    elif S[2]==4 and hy>S[1]:
        hy = S[1]
ans = int(hx-lx)*int(hy-ly) 
if hx-lx<=0 or hy-ly<=0:
    print(0)
else: print(ans)

Solution (Python):
W,H,N = map(int,input().split())
src = [tuple(map(int,input().split())) for i in range(N)]
top,bottom,left,right = H,0,0,W

for x,y,a in src:
    if a == 1:
        left = max(left,x)
    elif a == 2:
        right = min(right,x)
    elif a == 3:
        bottom = max(bottom,y)
    else:
        top = min(top,y)
w = max(0, right-left)
h = max(0, top-bottom)
print(w*h)

Solution (Python):





if __name__ == '__main__':
    w, h, n = list(map(int, input().split()))
    w_min, w_max = 0, w
    h_min, h_max = 0, h

    for _ in range(n):
        x, y, a = list(map(int, input().split()))

        if a == 1:
            w_min = max(x, w_min)
        elif a == 2:
            w_max = min(x, w_max)
        elif a == 3:
            h_min = max(y, h_min)
        elif a == 4:
            h_max = min(y, h_max)

    print(max(w_max - w_min, 0) * max(h_max - h_min, 0))

Solution (Python):





if __name__ == '__main__':
    w, h, n = list(map(int, input().split()))
    xya = [list(map(int, input().split())) for _ in range(n)]
    w_min = 0
    w_max = w
    h_min = 0
    h_max = h

    for x, y, a in xya:
        if (a == 1) and (x > w_min):
            w_min = x
        elif (a == 2) and (x < w_max):
            w_max = x
        elif (a == 3) and (y > h_min):
            h_min = y
        elif (a == 4) and (y < h_max):
            h_max = y

    print(max(w_max - w_min, 0) * max(h_max - h_min, 0))

Solution (Python):
w, h, n = map(int, input().split())
p, q = 0, 0
for i in range(n):
    x, y, a = map(int, input().split())
    if a == 1:
        p = max(x, p)
    elif a == 2:
        w = min(x, w)
    elif a == 3:
        q = max(y, q)
    else:
        h = min(y, h)

if w - p >= 0 and h - q >= 0:
    print((w - p) * (h - q))
else:
    print(0)

Solution (Python):
def main():
    w, h, n = map(int, input().split())
    
    w_min, w_max = [0], [w]
    h_min, h_max = [0], [h]
    
    for i in range(n):
        x, y, a = map(int, input().split())
        if a == 1:
            w_min.append(x)
        elif a == 2:
            w_max.append(x)
        elif a == 3:
            h_min.append(y)
        else:
            h_max.append(y)
            
    w = min(w_max) - max(w_min)
    h = min(h_max) - max(h_min)
    
    if w > 0 and h > 0:
        print(w*h)
    else:
        print(0)

        
if __name__ == "__main__":
    main()

Solution (Python):
w,h,n=map(int,input().split())
t=[[int(i) for i in input().split()]for i in range(n)]
x_1,x_2,y_1,y_2=0,w,0,h
for i in range(n):
    if t[i][2]==1:
        ans=t[i][0]
        x_1=max(ans,x_1)
    elif t[i][2]==2:
        ans_2=t[i][0]
        x_2=min(ans_2,x_2)
    elif t[i][2]==3:
        ans_3=t[i][1]
        y_1=max(ans_3,y_1)
    elif t[i][2]==4:
        ans_4=t[i][1]
        y_2=min(ans_4,y_2)
if x_1<x_2 and y_1<y_2:
    print(abs(x_2-x_1)*abs(y_2-y_1))
else:
    print(0)

Solution (Python):
w, h, n = map(int, input().split())

x_min = 0
x_max = w
y_min = 0
y_max = h

for i in range(n):
    x, y, a = map(int, input().split())
    if a == 1:
        x_min = max(x, x_min)
    elif a == 2:
        x_max = min(x, x_max)
    elif a == 3:
        y_min = max(y, y_min)
    else:
        y_max = min(y, y_max)
print(max(y_max - y_min, 0) * max(x_max - x_min, 0))

Solution (Python):
W,H,N = map(int,input().split())

l,r,t,b = [0],[W],[H],[0]

for i in range(N):
    x,y,a = map(int,input().split())
    if a==1: l.append(x)
    elif a==2: r.append(x)
    elif a==3: b.append(y)
    elif a==4: t.append(y)
        
W = min(r)-max(l) if min(r)-max(l)>=0 else 0
H = min(t)-max(b) if min(t)-max(b)>=0 else 0
        
print(W*H)

Solution (Python):
W, H, N = map(int,input().split())
D = [list(map(int,input().split())) for k in range(N)]
mx = 0
my = 0
MX = W
MY = H
for d in D:
    if d[2] == 1:
        mx = max(mx,d[0])
    elif d[2] == 2:
        MX = min(MX,d[0])
    elif d[2] == 3:
        my = max(my,d[1])
    else:
        MY = min(MY,d[1])

if MX <= mx or MY <= my:
    print(0)
else:
    print((MX-mx)*(MY-my))

Solution (Python):
w,h,n=map(int,input().split());b=d=0
for _ in[0]*n:
    x,y,a=map(int,input().split())
    if a<2:b=max(x,b)
    elif a<3:w=min(x,w)
    elif a<4:d=max(y,d)
    else:h=min(h,y)
print([(w-b)*(h-d),0][(w<b)|(h<d)])

Solution (Python):
W, H, N = map(int, input().split(' '))
minx = 0
maxx = W
miny = 0
maxy = H
for i in range(N):
    x, y, a = map(int, input().split(' '))
    if a == 1:
        minx = max(minx, x)
    elif a == 2:
        maxx = min(maxx, x)
    elif a == 3:
        miny = max(miny, y)
    else:
        maxy = min(maxy, y)
if minx <= maxx and miny <= maxy:
    print((maxx-minx) * (maxy-miny))
else:
    print(0)

Solution (Python):
W, H, N = map(int, input().split(' '))
minx = 0
maxx = W
miny = 0
maxy = H
for i in range(N):
    x, y, a = map(int, input().split(' '))
    if a == 1:
        minx = max(minx, x)
    elif a == 2:
        maxx = min(maxx, x)
    elif a == 3:
        miny = max(miny, y)
    else:
        maxy = min(maxy, y)
if minx <= maxx and miny <= maxy:
    print((maxx-minx) * (maxy-miny))
else:
    print(0)

Solution (Python):
import numpy as np
w, h, n = map(int, input().split())
xya = [list(map(int, input().split())) for i in range(n)]
x,y,a=[],[],[]
for z in xya:
    x.append(z[0])
    y.append(z[1])
    a.append(z[2])
pp = np.array([[1]*w]*h)
for i in range(n):
    if a[i]==1:
        pp[:, :x[i]]=0
    if a[i]==2:
        pp[:, x[i]:]=0
    if a[i]==3:
        pp[(h-y[i]):, :]=0
    if a[i]==4:
        pp[:(h-y[i]), :]=0
print(sum(sum(pp)))

Solution (Python):
w, h, n = list(map(int, input().split()))

left = [0]
right = [w]
down = [0]
up = [h]

for _ in range(n):
    x, y, a = list(map(int, input().split()))
    if a == 1:
        left.append(x)
    elif a == 2:
        right.append(x)
    elif a == 3:
        down.append(y)
    elif a == 4:
        up.append(y)
        
if min(right) - max(left) <= 0 or min(up) - max(down) <= 0:
    print(0)
else:
    print((min(right) - max(left)) * (min(up) - max(down)))

Solution (Python):
def inpl(): return map(int, input().split())
W, H, N = inpl()
a = [0, W, 0, H]
for i in range(N):
    x, y, a_i = inpl()
    if a_i == 1:
        a[0] = max(a[0], x)
    elif a_i == 2:
        a[1] = min(a[1], x)
    elif a_i == 3:
        a[2] = max(a[2], y)
    else:
        a[3] = min(a[3], y)

if a[0]<a[1] and a[2]<a[3]:
    print((a[1]-a[0])*(a[3]-a[2]))
else:
    print(0)

Solution (Python):
W,H,N = map(int,input().split())
X1,X2,Y1,Y2 = 0,W,0,H
for i in range(N):
    x,y,a = map(int,input().split())
    if a == 1:
        X1 = max(x,X1)
    elif a == 2:
        X2 = min(x,X2)
    elif a == 3:
        Y1 = max(y,Y1)
    elif a == 4:
        Y2 = min(y,Y2)
print(max(0,(X2-X1))*max(0,(Y2-Y1)))

Solution (Python):
a, b, n = list(map(int,input().split()))

c = []
for i in range(n):
    c.append(list(map(int,input().split())))

aa =0
ab =a
ac =0
ad =b

for i in range(n):
    if c[i][2] == 1:
        aa = max(aa,c[i][0])
    elif c[i][2] == 2:
        ab = min(ab,c[i][0])
    elif c[i][2] == 3:
        ac = max(ac,c[i][1])
    else:
        ad = min(ad,c[i][1])

if ab-aa > 0 and ad-ac > 0:
    print((ab-aa)*(ad-ac))
else:
    print(0)

Solution (Python):
w,h,n = map(int,input().split())
l = [[int(i) for i in input().split()] for i in range(n)]
start,end = [0,0],[w,h]

for i in range(n):
    if l[i][2]==1 and start[0]<l[i][0]:
        start[0] = l[i][0]
    elif l[i][2]==2 and end[0]>l[i][0]:
        end[0] = l[i][0]
    elif l[i][2]==3 and start[1]<l[i][1]:
        start[1] = l[i][1]
    elif l[i][2]==4 and end[1]>l[i][1]:
        end[1] = l[i][1]

ans = (end[0]-start[0])*(end[1]-start[1]) if start[0]<end[0] or start[1]<end[1] else -1
print(ans if ans > 0 else 0)

Solution (Python):
w,h,n=map(int,input().split())
t=[tuple(map(int,input().split()))for i in range(n)]
A=C=0
B=w
D=h
for i in range(n):
    if t[i][2]==1:
        A=max(A,t[i][0])
    elif t[i][2]==2:
        B=min(B,t[i][0])
    elif t[i][2]==3:
        C=max(C,t[i][1])
    elif t[i][2]==4:
        D=min(D,t[i][1])
n_h=D-C
n_w=B-A
if n_h<=0 or n_w<=0:
    print(0)
else:
    print(n_h*n_w)

Solution (Python):
W,H,N = map(int,input().split())
x = [0]*N
y = [0]*N
a = [0]*N
for i in range(N):
    x[i],y[i],a[i] = map(int,input().split())
x_min = 0
y_min = 0
x_max = W
y_max = H
for i in range(N):
    if a[i] == 1:
        x_min = max(x_min,x[i])
    if a[i] == 2:
        x_max = min(x_max,x[i])
    if a[i] == 3:
        y_min = max(y_min,y[i])
    if a[i] == 4:
        y_max = min(y_max,y[i])

x_sub = max(x_max-x_min,0)
y_sub = max(y_max-y_min,0)
print(x_sub*y_sub)

Solution (Python):
w, h, n = map(int, input().split())
field = [[0 for _ in range(w)] for _ in range(h)]
for i in range(n):
    x, y, a = map(int, input().split())
    for j in range(w):
        for k in range(h):
            if a == 1:
                if j < x:
                   field[k][j] = 1
            elif a == 2:
                if j >= x:
                    field[k][j] = 1
            elif a == 3:
                if k < y:
                    field[k][j] = 1
            elif a == 4:
                if k >= y:
                    field[k][j] = 1
cnt = 0
for i in range(h):
    cnt += sum(field[i])
print(w * h - cnt)

Solution (Python):
import numpy as np
w, h, n = map(int, input().split())
xy = np.zeros((w,h))
xya = [[int(a) for a in input().split()] for _ in range(n)]
for i in range(n):
    if xya[i][2]==1:
        for x in range(xya[i][0]):
            xy[x][:h] = 1
    elif xya[i][2] == 2:
        for x in range(xya[i][0], w):
            xy[x][:h] = 1
    elif xya[i][2]==3:
        for x in range(w):
            for y in range(xya[i][1]):
                xy[x][y] = 1
    elif xya[i][2] == 4:
        for x in range(w):
            for y in range(xya[i][1], h):
                xy[x][y] = 1
wh = [int(item) for wh in xy for item in wh]
print(wh.count(0))

Solution (Python):
w, h ,n = map(int, input().split())
sq = list(list(True for _ in range(h)) for _ in range(w))
for i in range(n):
    x, y, a = map(int, input().split())
    if a == 1:
        for ix in range(x):
            for iy in range(h):
                sq[ix][iy] = False
    if a == 2:
        for ix in range(x, w):
            for iy in range(h):
                sq[ix][iy] = False
    if a == 3:
        for ix in range(w):
            for iy in range(y):
                sq[ix][iy] = False
    if a == 4:
        for ix in range(w):
            for iy in range(y, h):
                sq[ix][iy] = False
s = 0
for h in range(w):
    s += sq[h].count(True)
print(s)

Solution (Python):
import numpy as np
W, H, N = map(int, input().split())
x = []
y = []
a = []
for _ in range(N):
    b, c, d = map(int, input().split())
    x.append(b)
    y.append(c)
    a.append(d)
A = np.zeros((H, W))

for i in range(N):
    if a[i] == 1:
        for j in range(x[i]):
            for k in range(H):
                A[k][j] = 1
    elif a[i] == 2:
        for j in range(x[i],W):
            for k in range(H):
                A[k][j] = 1
    elif a[i] == 3:
        for j in range(y[i]):
            for k in range(W):
                A[j][k] = 1
    elif a[i] == 4:
        for j in range(y[i], H):
            for k in range(W):
                A[j][k] = 1

print(np.sum(A==0))

Solution (Python):
W,H,N=map(int,input().split())
x=[0 for i in range(N)]
y=[0 for i in range(N)]
a=[0 for i in range(N)]
for i in range(N):
    x[i],y[i],a[i]=map(int,input().split())
C=[[0 for i in range(W)] for j in range(H)]
for k in range(N):
    if a[k]==1:
        for i in range(H):
            for j in range(x[k]):
                C[i][j]=1
    elif a[k]==2:
        for i in range(H):
            for j in range(x[k],W):
                C[i][j]=1
    elif a[k]==3:
        for i in range(y[k]):
            for j in range(W):
                C[i][j]=1
    else:
        for i in range(y[k],H):
            for j in range(W):
                C[i][j]=1
Ans=0
for i in range(H):
    for j in range(W):
        if C[i][j]==0:
            Ans+=1
print(Ans)

Solution (Python):
w,h,n = (int(i) for i in input().split())
a = [[int(i) for i in input().split()] for i in range(n)]
x = [0,w,0,h]
for i in a:
	if i[2]==1 and i[0]>x[0]: x[0]=i[0]
	elif i[2]==2 and i[0]<x[1]: x[1]=i[0]
	elif i[2]==3 and i[1]>x[2]: x[2]=i[1]
	elif i[2]==4 and i[1]<x[3]: x[3]=i[1]
print(max(x[1]-x[0],0)*max(x[3]-x[2],0))

Solution (Python):
def area(x2, y2, points):
    x1 = y1 = 0
    for x, y, a in points:
        if a == 1:
            x1 = min(max(x1, x), x2)
        elif a == 2:
            x2 = max(min(x2, x), x1)
        elif a == 3:
            y1 = min(max(y1, y), y2)
        else:
            y2 = max(min(y2, y), y1)
    return (x2 - x1) * (y2 - y1)


if __name__ == '__main__':
    w, h, n = list(map(int, input().split()))
    points = [list(map(int, input().split())) for _ in range(n)]
    print(area(w, h, points))

Solution (Python):
def init_rectangle(w, h):
    rectangle = []
    for x in range(w):
        x_list = []
        for y in range(h):
            x_list.append(True)
        rectangle.append(x_list)
    return rectangle

def area(rectangle):
    count = 0
    for i in rectangle:
        for j in i:
            if j:
                count += 1
    return count

def paint_point(rectangle, w, h, x1, x2, y1, y2):
    for x in range(x1, x2):
        for y in range(h):
            rectangle[x][y] = False
    for x in range(w):
        for y in range(y1, y2):
            rectangle[x][y] = False
        
def paint(rectangle, w, h, points):
    for x, y, a in points:
        x1 = x2 = y1 = y2 = 0
        if a == 1:
            x2 = x
        elif a == 2:
            x1 = x
            x2 = w
        elif a == 3:
            y2 = y
        else:
            y1 = y
            y2 = h
        paint_point(rectangle, w, h, x1, x2, y1, y2)


if __name__ == '__main__':
    w, h, n = list(map(int, input().split()))
    points = [list(map(int, input().split())) for _ in range(n)]
    rectangle = init_rectangle(w, h)
    paint(rectangle, w, h, points)
    print(area(rectangle))

Solution (Python):
w, h, n = [int(x) for x in input().split()]
xm, xM, ym, yM = 0, w, 0, h
for _ in range(n):
	x, y, a = [int(x) for x in input().split()]
	if a == 1: xm = max(xm, x)
	elif a == 2: xM = min(xM, x)
	elif a == 3: ym = max(ym, y)
	else: yM = min(yM, y)
if yM > ym and xM > xm: print((xM - xm) * (yM - ym))
else: print(0)

Solution (Python):
W,H,N=map(int,input().split())
top = H
right = W
left = 0
bot = 0
for i in range(N):
  x,y,a = map(int,input().split())
  if a == 1 and left < x:
    left = min(x,right)
  elif a == 2 and x < right:
    right = max(x,left)
  elif a == 3 and bot < y:
    bot = min(y,top)
  elif a == 4 and y < top:
    top = max(y,bot)
print((right - left)*(top - bot))

Solution (Python):
w,h,n = map(int,input().split())
lx,hx,ly,hy = 0,w,0,h
for i in range(n):
  x,y,a = map(int,input().split())
  if a == 1:
    lx = max(lx, x)
  elif a == 2:
    hx = min(hx, x)
  elif a == 3:
    ly = max(ly, y)
  elif a == 4:
    hy = min(hy, y)
print(max(0, hx-lx) * max(0, hy-ly))

Solution (Python):
w,h,n = map(int,input().split())
lis1 = [0]
lis2 = [w]
lis3 = [0]
lis4 = [h]
for i in range(n):
    x,y,a = map(int,input().split())
    if a == 1:lis1.append(x)
    if a == 2:lis2.append(x)
    if a == 3:lis3.append(y)
    if a == 4:lis4.append(y)
cou1 = max(lis1)
cou2 = min(lis2)
cou3 = max(lis3)
cou4 = min(lis4)

if cou1 + (w -cou2) >= w or cou3 + (h - cou4) >= h:
    print("0")
else:
    print((w-(cou1 + (w - cou2))) * ((h-(cou3 + (h - cou4)))))

Solution (Python):
x_r, y_o, N = map(int, input().split())
a = [list(map(int, input().split())) for i in range(N)]
x_l = 0
y_u = 0
area = x_r * y_o
if area == 0:
    print(area)
else:
    for i in a:
        if i[2] == 1:
            if i[0] <= x_l:
                continue
            else:
                area -= (i[0] - x_l) * (y_o - y_u)
                if i[0] >= x_r:
                    area = 0
                    break
                else:
                    x_l = i[0]
        elif i[2] == 2:
            if i[0] >= x_r:
                continue
            else:
                area -= (x_r - i[0]) * (y_o - y_u)
                if i[0] <= x_l:
                    area = 0
                    break
                else:
                    x_r = i[0]
        elif i[2] == 3:
            if i[1] <= y_u:
                continue
            else:
                area -= (i[1] - y_u) * (x_r - x_l)
                if i[1] >= y_o:
                    area = 0
                    break
                else:
                    y_u = i[1]
        else:
            if i[1] >= y_o:
                continue
            else:
                area -= (y_o - i[1]) * (x_r - x_l)
                if i[1] <= y_u:
                    area = 0
                    break
                else:
                    y_o = i[1]
    print(area)

Solution (Python):
w, h, n = map(int, input().split())
minx, maxx, miny, maxy = 0, w, 0, h
for _ in range(n):
    x, y, a = map(int, input().split())
    if a == 1:
        minx = max(minx, x)
    if a == 2:
        maxx = min(maxx, x)
    if a == 3:
        miny = max(miny, y)
    if a == 4:
        maxy = min(maxy, y)
print(max(0, maxx - minx) * max(0, maxy - miny))