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):
import numpy as np
W, H, N = map(int, input().split())
S = np.zeros((H, W))

for i in range(N):
    x, y, a = map(int, input().split())
    if a == 1:
        S[:, :x] += 1
    elif a == 2:
        S[:, x:] += 1
    elif a == 3:
        S[:y, :] += 1
    elif a == 4:
        S[y:, :] += 1

print(len(S[S == 0]))

Solution (Python):
X,Y,n=map(int,input().split())
x=y=0
for i in range(n):
    a,b,c=map(int,input().split())
    if c==1 and x<a:x=a
    if c==2 and X>a:X=a
    if c==3 and y<b:y=b
    if c==4 and Y>b:Y=b
print(max(X-x,0)*max(Y-y,0))

Solution (Python):



w, h, n = [int(i) for i in input().split()]
x1 = 0
x2 = w
y1 = 0
y2 = h
for i in range(n):
    xi, yi, ai = [int(i) for i in input().split()]
    if ai == 1:
        x1 = max(x1, xi)
    elif ai == 2:
        x2 = min(x2, xi)
    elif ai == 3:
        y1 = max(y1, yi)
    else:
        y2 = min(y2, yi)

if x2 > x1 and y2 > y1:
    s = (x2 - x1) * (y2 - y1)
else:
    s = 0
print(s)

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

x = []
y = []
a = []

xmin = 0
xmax = w
ymin = 0
ymax = h

ans = 0

for i in range(n):
    x1, y1, a1 = map(int, input().split())
    x.append(x1)
    y.append(y1)
    a.append(a1)

for i in range(n):
    if a[i] == 1:
        if x[i] > xmin:
            xmin = x[i]
        
    elif a[i] == 2:
        if x[i] < xmax:
            xmax = x[i]

    elif a[i] == 3:
        if y[i] > ymin:
            ymin = y[i]

    else:
        if y[i] < ymax:
            ymax = y[i]






if (xmax - xmin) <= 0 or (ymax - ymin) <= 0:
    ans = 0
    
else:
    ans = (xmax - xmin) * (ymax - ymin)

print(ans)

Solution (Python):
w,h,n=map(int,input().split())
xa=[0]
xb=[w]
ya=[0]
yb=[h]
for i in range(n):
  x1,y1,a1=map(int,input().split())
  if a1==1:
    xa.append(x1)
  elif a1==2:
    xb.append(x1)
  elif a1==3:
    ya.append(y1)
  elif a1==4:
    yb.append(y1)

if min(xb)<=max(xa) or min(yb)<=max(ya):
  print(0)
else:
  print((min(xb)-max(xa))*(min(yb)-max(ya)))

Solution (Python):
W,H,N=map(int, input().split())
ls1=[0]
ls2=[W]
ls3=[0]
ls4=[H]
for i in range(N):
    tmp=list(map(int, input().split()))
    if tmp[2]==1:
        ls1.append(tmp[0])
    if tmp[2]==2:
        ls2.append(tmp[0])
    if tmp[2]==3:
        ls3.append(tmp[1])
    if tmp[2]==4:
        ls4.append(tmp[1])
print((min(ls2)-max(ls1))*(min(ls4)-max(ls3)) if min(ls2)>=max(ls1) and min(ls4)>=max(ls3) else 0)

Solution (Python):
W,H,N = map(int,input().split())
W1 = []
W2 = []
H1 = []
H2 = []
W1.append(0)
W2.append(W)
H1.append(0)
H2.append(H)
for i in range(N) :
    x,y,a = map(int,input().split())
    if a == 1 :
        W1.append(x)
    if a == 2 :
        W2.append(x)
    if a == 3 :
        H1.append(y)
    if a == 4 :
        H2.append(y)
width = max(0,min(W2)-max(W1))
height = max(0,min(H2)-max(H1))
print(width*height)

Solution (Python):
W,H,N = map(int,input().split())
W1 = []
W2 = []
H1 = []
H2 = []
W1.append(0)
W2.append(W)
H1.append(0)
H2.append(H)
for i in range(N) :
    x,y,a = map(int,input().split())
    if a == 1 :
        W1.append(x)
    if a == 2 :
        W2.append(x)
    if a == 3 :
        H1.append(y)
    if a == 4 :
        H2.append(y)
width = max(0,min(W2)-max(W1))
height = max(0,min(H2)-max(H1))
print(width*height)

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

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

xmin, xmax, ymin, ymax = 0, W, 0, H
for i in range(N):
  x,y,a = map(int, input().split())
  if a==1:
    xmin = max(xmin, x)
  elif a==2:
    xmax = min(xmax, x)
  elif a==3:
    ymin = max(ymin, y)
  else:
    ymax = min(ymax, y)
    
print( max(0, xmax-xmin) * max(0, ymax-ymin) )

Solution (Python):
w,h,n = map(int,input().split())
left,right,up,down = 0,w,0,h

for _ in range(n):
    x,y,a = map(int,input().split())
    if a == 1:
        left  = max(left,x)
    if a == 2:
        right = min(right,x)
    if a == 3:
        up   = max(up,y)
    if a == 4:
        down = min(down,y)

w = max(0,right-left)
h = max(0,down-up)

print(w*h)

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

Solution (Python):
w,h,n=map(int,input().split())
a=[list(map(int,input().split())) for i in range(n)]
a1=[0];a2=[w];a3=[0];a4=[h]
for i in a:
  if i[2]==1:a1.append(i[0])
  else:pass
for i in a:
  if i[2]==2:a2.append(i[0])
  else:pass
for i in a:
  if i[2]==3:a3.append(i[1])
  else:pass
for i in a:
  if i[2]==4:a4.append(i[1])
  else:pass
if max(a1)-min(a2)<0:x=max(a1)-min(a2)
else:x=0
if max(a3)-min(a4)<0:y=max(a3)-min(a4)
else:y=0
print(x*y)

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

x_max, x_min, y_max, y_min = W, 0, H, 0

for i in range(N):
    if input_list[i][2] == 1:
        x_min = max(x_min, input_list[i][0])
    elif input_list[i][2] == 2:
        x_max = min(x_max, input_list[i][0])
    elif input_list[i][2] == 3:
        y_min = max(y_min, input_list[i][1])
    elif input_list[i][2] == 4:
        y_max = min(y_max, input_list[i][1])

if x_max - x_min > 0 and y_max - y_min > 0:
    print((x_max-x_min)*(y_max-y_min))
else:
    print(0)

Solution (Python):
def solve():
  x_max, y_max, N_max = map(int, input().split())
  x_min, y_min = 0, 0
  area = x_max * y_max
  W, H, N = [], [], []
  for i in range(N_max):
    x, y, a = map(int, input().split())
    if a == 1 and x_min < x:
      x_min = x
    elif a == 2 and x_max > x:
      x_max = x
    elif a == 3 and y_min < y:
      y_min = y
    elif a == 4 and y_max > y:
      y_max = y

  if x_max <= x_min or y_max <= y_min:
    print(0)
  else:
    print((x_max - x_min) * (y_max - y_min))

solve()

Solution (Python):
w,h,n = map(int,input().split())
grid = [[1 for i in range(w)] for k in range(h)]

for i in range(n):
    x,y,a = map(int,input().split())
    if a == 1:
        for j in range(h):
            for k in range(x):
                grid[j][k] = 0
    elif a == 2:
        for j in range(h):
            for k in range(x,w):
                grid[j][k] = 0
    elif a == 3:
        for j in range(y):
            for k in range(w):
                grid[j][k] = 0
    elif a == 4:
        for j in range(y,h):
            for k in range(w):
                grid[j][k] = 0

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

Solution (Python):
w, h, n = map(int, input().split())
x, y, a = [], [], []
m1 = [0, 0]
m2 = [w, h]
for _ in range(n):
    X, Y, A = map(int, input().split())
    x.append(X)
    y.append(Y)
    a.append(A)
for i in range(n):
    if a[i] == 1:
        m1[0] = max(m1[0], x[i])
    elif a[i] == 2:
        m2[0] = min(m2[0], x[i])
    elif a[i] == 3:
        m1[1] = max(m1[1], y[i])
    else:
        m2[1] = min(m2[1], y[i])
s = max(0, (m2[0] - m1[0])) * max(0, (m2[1] - m1[1]))
print(s)

Solution (Python):
W, H, N = map(int, input().split())
x = [0] * N
y = [0] * N
a = [0] * N
ans = 0
tyoten = [0, 0]
for i in range(N):
    x[i], y[i], a[i] = map(int, input().split())

for i in range(N):
    if a[i] == 1:
        if W <= x[i]:
            ans = 1
            break
        elif tyoten[0] < x[i] < W:
            tyoten[0] = x[i]
        
    if a[i] == 2:
        if x[i] <= tyoten[0]:
            ans = 1
            break
        elif tyoten[0] < x[i] < W:
            W = x[i]
    
    if a[i] == 3:
        if H <= y[i]:
            ans = 1
            break
        elif tyoten[1] < y[i] < H:
            tyoten[1] = y[i]
    
    if a[i] == 4:
        if y[i] <= tyoten[1]:
            ans = 1
            break
        elif tyoten[1] < y[i] < H:
            H = y[i]
        
if ans == 1:
    print(0)
else:
    print((W-tyoten[0])*(H-tyoten[1]))

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

memox = [True]*(w+1)
memoy = [True]*(h+1)


for i in range(n):
    xi,yi,a=map(int, input().split())
    if a == 1:
        memox = [False] * xi + memox[xi:]
    elif a == 2:
        memox = memox[:xi+1] + [False] * (w - xi)
    elif a == 3:
        memoy = [False] * yi + memoy[yi:]
    elif a == 4:
        memoy = memoy[:yi+1] + [False] * (h - yi)
memox = [i for i in memox if i]
memoy = [i for i in memoy if i]

if memox and memoy:
    print(max([0, (len(memox) - 1) * (len(memoy) - 1)]))
else:
    print(0)

Solution (Python):
w, h, n = map(int, input().split())
x = []
y = []
a = []
for i in range(n):
    X, Y, A = map(int, input().split())
    x.append(X)
    y.append(Y)
    a.append(A)

xmin = 0
xmax = w
ymin = 0
ymax = h

for i in range(n):
    if a[i] == 1:
        xmin = max(xmin, x[i])
    elif a[i] == 2:
        xmax = min(xmax, x[i])
    elif a[i] == 3:
        ymin = max(ymin, y[i])
    elif a[i] == 4:
        ymax = min(ymax, y[i])

if xmin > xmax or ymax < ymin:
    print(0)
else:
    print((xmax - xmin) * (ymax - ymin))

Solution (Python):
import sys
sdin = sys.stdin.readline

w,h,n = map(int, sdin().split())
xy = [[0], [w], [0], [h]]
for _ in range(n):
    x,y,a = map(int, sdin().split())
    if a-1 < 2:
        xy[a-1].append(x)
    else:
        xy[a-1].append(y)
    
x_min = max([xy[0][i] for i in range(len(xy[0]))])
x_max = min([xy[1][i] for i in range(len(xy[1]))])
y_min = max([xy[2][i] for i in range(len(xy[2]))])
y_max = min([xy[3][i] for i in range(len(xy[3]))])

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

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

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

Solution (Python):
W,H,N=[int(i) for i in input().split()]
l,r,u,d = 0,W,0,H

for i in range(N):
	x,y,a=[int(i) for i in input().split()]
	if a==1:
		l=max(l,x)
	if a==2:
		r=min(r,x)
	if a==3:
		u=max(u,y)
	if a==4:
		d=min(d,y)
h=max(0,d-u)
w=max(0,r-l)

print(h*w)

Solution (Python):
import sys
import math
import collections
import itertools
import array
import inspect


sys.setrecursionlimit(1000000)



def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))



def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v



def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk



def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


class Combination:
    def __init__(self, n, mod):

        g1 = [1, 1]
        g2 = [1, 1]
        inverse = [0, 1]
        for i in range(2, n + 1):
            g1.append((g1[-1] * i) % mod)
            inverse.append((-inverse[mod % i] * (mod // i)) % mod)
            g2.append((g2[-1] * inverse[-1]) % mod)
        self.MOD = mod
        self.N = n
        self.g1 = g1
        self.g2 = g2
        self.inverse = inverse

    def __call__(self, n, r):
        if (r < 0 or r > n):
            return 0
        r = min(r, n - r)
        return self.g1[n] * self.g2[r] * self.g2[n - r] % self.MOD




dp = None


def main():
    W, H, N = li_input()
    G = [[1] * W for _ in range(H)]

    for i in range(N):
        x, y, a = li_input()

        if a == 1:
            for i in range(H):
                for j in range(x):
                    G[i][j] = 0

        if a == 2:
            for i in range(H):
                for j in range(x, W):
                    G[i][j] = 0

        if a == 3:
            for i in range(y):
                for j in range(W):
                    G[i][j] = 0

        if a == 4:
            for i in range(y, H):
                for j in range(W):
                    G[i][j] = 0

    ans = 0

    for row in G:
        ans += sum(row)

    print(ans)


main()

Solution (Python):
import sys

w,h,n = (int(_) for _ in input().split())
xa = ya = 0
xb = w
yb = h

for i in range(n):
    x,y,a=(int(_) for _ in input().split())
    if(a == 1):
        if(x > xa):
            xa = x
    elif (a == 2):
        if (x < xb):
            xb = x
    elif (a == 3):
        if (y > ya):
            ya = y
    elif (a == 4):
        if (y < yb):
            yb = y
    else:
        sys,exit(1)


xx = xb-xa
yy = yb-ya
ans = (xb-xa)*(yb-ya)
print('0') if (ans < 0) or (xx < 0) or(yy < 0) else print(ans)

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

for x in range(n):
    x, y, a = map(int, input().split())
    for idx_y, yy in enumerate(lst):
        for idx_x, _ in enumerate(yy):
            if a == 1 and idx_x < x:
                lst[idx_y][idx_x] = 0
            elif a == 2 and idx_x >= x:
                lst[idx_y][idx_x] = 0
            elif a == 3 and idx_y < y:
                lst[idx_y][idx_x] = 0
            elif a == 4 and idx_y >= y:
                lst[idx_y][idx_x] = 0

print(sum([sum(x) for x in lst]))

Solution (Python):
W, H, N = map(int, input().split())
h = {1: 0, 2: W, 3: 0, 4: H}
for i in range(N):
    x, y, a = map(int, input().split())
    if a == 1:
        h[a] = max(h[a], x)
    elif a == 2:
        h[a] = min(h[a], x)
    elif a == 3:
        h[a] = max(h[a], y)
    else:
        h[a] = min(h[a], y)
ans = W * H
ans -= H * (h[1] + W-h[2])
ans -= max(0, (W - (h[1] + W-h[2]))) * (h[3] + H-h[4])
print(max(ans, 0))

Solution (Python):


def main():
    w, h, n = map(int, input().split())
    arry = [list(map(int, input().split())) for _ in range(n)]

    x1 = 0
    x2 = w
    y1 = 0
    y2 = h
    for i in arry:
        if i[2] == 1:
            x1 = max(x1, i[0])
        elif i[2] == 2:
            x2 = min(x2, i[0])
        elif i[2] == 3:
            y1 = max(y1, i[1])
        elif i[2] == 4:
            y2 = min(y2, i[1])
    if x2 <= x1 or y2 <= y1:
        ans = 0
    else:
        ans = (x2 - x1) * (y2 - y1)
    
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):

"""
Created on Sun Aug 12 13:14:59 2018
ABC047B
@author: maezawa
"""
w, h, n = list(map(int, input().split()))
xl = 0
xu = w
yl = 0
yu = h

for i in range(n):
    xi, yi, ai = list(map(int, input().split()))
    if ai == 1:
        if xl < xi:
            xl = xi
    elif ai == 2:
        if xu > xi:
            xu = xi
    elif ai == 3:
        if yl < yi:
            yl = yi
    else:
        if yu > yi:
            yu = yi
if xl<=xu and yl<=yu:
    ans = (xu-xl)*(yu-yl)
else:
    ans = 0
print(ans)

Solution (Python):

w,h,n = map(int,input().split())
x,y=0,0

for _ in range(n):
    xi,yi,a = map(int,input().split())
    if a==1:
        x = max(x,xi)
    elif a==2:
        w = min(xi,w)
    elif a==3:
        y = max(y,yi)
    else:
        h = min(h,yi)
   

print(0 if x>=w or y>=h else (w-x)*(h-y))

Solution (Python):
import numpy as np
W, H, N = map(int, input().split())

z = np.zeros( (H, W) )


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

  if a == 1:
    z[:, :x] = 1

  if a == 2:
    z[:, x:] = 1

  y = H - y
  if a == 3:
    z[y:,:] = 1
  
  if a == 4:
    z[:y,:] = 1


ans = int(W*H - z.sum())
print(ans)

Solution (Python):
W,H,n=map(int,input().split())
l=[[0 for i in range(W)] for j in range(H)] 
for _ in range(n):
  x,y,a=map(int,input().split())
  if a==1: 
    for i in range(H):
      for j in range(x):
        l[i][j]=1
  elif a==2: 
    for i in range(H):
      for j in range(x,W):
        l[i][j]=1
  elif a==3: 
    for i in range(y):
      for j in range(W):
        l[i][j]=1
  else: 
    for i in range(y,H):
      for j in range(W):
        l[i][j]=1
print(W*H-sum([sum(i) for i in l]))

Solution (Python):
W, H, N = map(int, input().split())
x,y = 0,0
for i in range(N):
    xi, yi, a = map(int, input().split())
    if a == 1:
        x = max(x, xi)
    elif a == 2:
        W = min(W, xi)
    elif a == 3:
        y = max(y, yi)
    elif a == 4:
        H = min(H, yi)

print(0 if x >= W or y >= H else (W - x) * (H - y))

Solution (Python):
w, h, n = map(int, input().split(" "))
x_y_a = [tuple(map(int, input().split(" "))) for i in range(n)]

min_x, max_x, min_y, max_y = 0, w, 0, h
for x_y_a_i in x_y_a:
    x, y, a = x_y_a_i
    if a == 1:
        min_x = max(min_x, x)
    elif a == 2:
        max_x = min(max_x, x)
    elif a == 3:
        min_y = max(min_y, y)
    elif a == 4:
        max_y = min(max_y, y)

print(max(0, max_x-min_x) * max(0, max_y-min_y))

Solution (Python):
w, h, n = map(int, input().split(" "))
x_y_a = [tuple(map(int, input().split(" "))) for i in range(n)]

min_x, max_x, min_y, max_y = 0, w, 0, h
for x_y_a_i in x_y_a:
    x, y, a = x_y_a_i
    if a == 1:
        min_x = max(min_x, x)
    elif a == 2:
        max_x = min(max_x, x)
    elif a == 3:
        min_y = max(min_y, y)
    elif a == 4:
        max_y = min(max_y, y)

print(0 if min_x > max_x or min_y > max_y else (max_x-min_x) * (max_y-min_y))

Solution (Python):
W,H,N=map(int,input().split())
x=y=0
for i in range(N):
  xi,yi,a=map(int,input().split())
  if a==1:
    x=max(x,xi)
  elif a==2:
    W=min(W,xi)
  elif a==3:
    y=max(y,yi)
  elif a==4:
    H=min(H,yi)
    
print(0 if x>=W or y>=H else (W-x)*(H-y))

Solution (Python):
WHN=input()
WHN=WHN.rstrip().split(" ")
W=int(WHN[0])
H=int(WHN[1])
N=int(WHN[2])

list1=[]
for i in range(H):
    list2=[]
    for j in range(W):
        list2.append('w')
    list1.append(list2)
    list2=[]


i=0
for i in range(N):
    xya=input()
    xya=xya.rstrip().split(" ")
    x=int(xya[0])
    y=int(xya[1])
    a=int(xya[2])
    
    if a==1:
        j=0
        for j in range(H):
            k=0
            for k in range(W):
                if k<x:
                    list1[j][k]='b'
    elif a==2:
        j=0
        for j in range(H):
            k=0
            for k in range(W):
                if k+1>x:
                    list1[j][k]='b'
    elif a==3:
        j=0
        for j in range(H):
            k=0
            for k in range(W):
                if j<y:
                    list1[j][k]='b'
    elif a==4:
        j=0
        for j in range(H):
            k=0
            for k in range(W):
                if j+1>y:
                    list1[j][k]='b'
                    
                    
count=0
i=0
for i in range(H):
    k=0
    for j in range(W):
        if list1[i][j]=='w':
            count+=1
print(count)

Solution (Python):
fn = lambda: map(int, input().split())
W, H, N = fn()
min_x, max_x = 0, W
min_y, max_y = 0, H
for i in range(N):
    xi, yi, ai = fn()
    if ai == 1:
        min_x = max(xi, min_x)
    elif ai == 2:
        max_x = min(xi, max_x)
    elif ai == 3:
        min_y = max(yi, min_y)
    elif ai == 4:
        max_y = min(yi, max_y)
    else:
        print('Something wrong')

print(max(max_x - min_x, 0) * max(max_y - min_y, 0))

Solution (Python):

import numpy as np


def main():
    w, h, n = map(int, raw_input().split())
    sq = np.ones((h, w))
    for _ in range(n):
        x, y, a = map(int, raw_input().split())
        paint(sq, x, y, a, w, h)

    print(int(sq.sum()))



def paint(sq, x, y, a, w, h):
    if a == 1:
        for i in range(x):
            sq[:, i] = 0
    elif a == 2:
        for i in range(w - x):
            sq[:, i + x] = 0
    elif a == 3:
        for i in range(y):
            sq[i, :] = 0
    elif a == 4:
        for i in range(h - y):
            sq[i + y, :] = 0


if __name__ == '__main__':
    main()

Solution (Python):
W, H ,N = list(map(int, input().split()))
S = [[1]*W] * H

for i in range(N):
    x, y, a =  list(map(int, input().split()))
    if a == 3:
        for j in range(y):
            S[j] = [0] * W
    elif a == 4:
        for j in range(y, H):
            S[j] = [0] * W
    elif a == 1:
        for j in range(H):
            for k in range(x):
                S[j][k] = 0
    elif a == 2:
        for j in range(H):
            for k in range(x, W):
                S[j][k] = 0

re = 0
for i in range(H):
    re += sum(S[i])

print(re)

Solution (Python):
fn = lambda: map(int, input().split())
W, H, N = fn()
A = [[1 for _ in range(W)] for _ in range(H)]
for _ in range(N):
    x, y, a = fn()
    if a == 1:
        for i in range(H):
            for j in range(x):
                A[i][j] = 0
    elif a == 2:
        for i in range(H):
            for j in range(x, W):
                A[i][j] = 0
    elif a == 3:
        for i in range(W):
            for j in range(y):
                A[j][i] = 0
    elif a == 4:
        for i in range(W):
            for j in range(y, H):
                A[j][i] = 0
    else:
        print('Something wrong')

print(sum([sum(Ai) for Ai in A]))

Solution (Python):
W, H, N = map(int, input().split(" "))
sq = []
for i in range(H):
    sq.append([1] * W)

for i in range(N):
    x, y, a = map(int, input().split(" "))
    if a == 1:
        for i, w in enumerate(sq):
            for j, h in enumerate(w):
                if j < x:
                    sq[i][j] = 0
    if a == 2:
        for i, w in enumerate(sq):
            for j, h in enumerate(w):
                if j >= x:
                    sq[i][j] = 0

    if a == 3:
        for i, w in enumerate(sq):
            for j, h in enumerate(w):
                if i < y:
                    sq[i][j] = 0
    if a == 4:
        for i, w in enumerate(sq):
            for j, h in enumerate(w):
                if i >= y:
                    sq[i][j] = 0
area = 0
for w in sq:
    for h in w:
        area += h
print(area)

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

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

x1, y1 = 0, 0
x2, y2 = W, H

for _ in range(N):
    x, y, a = map(int, input().split())
    if a == 1:
        x1 = max(x1, x)
    elif a == 2:
        x2 = min(x2, x)
    elif a == 3:
        y1 = max(y1, y)
    elif a == 4:
        y2 = min(y2, y)

dx = max(0, (x2 - x1))
dy = max(0, (y2 - y1))

print(dx * dy)

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

rect = [(p%W, p//W) for p in range(W*H)]
for i in range(N):
    xi, yi, ai = map(int, input().split())
    if(ai==1):
        cond = lambda x_y: x_y[0] >= xi
    elif(ai==2):
        cond = lambda x_y: x_y[0] < xi
    elif(ai==3):
        cond = lambda x_y: x_y[1] >= yi
    else:
        cond = lambda x_y: x_y[1] < yi
    rect = list(filter(cond , rect))
        
print(len(rect))

Solution (Python):
W,H,N=map(int,input().split())
x=y=0
for i in range(N):
  xi,yi,a=map(int,input().split())
  if a==1:
    x=max(x,xi)
  elif a==2:
    W=min(W,xi)
  elif a==3:
    y=max(y,yi)
  elif a==4:
    H=min(H,yi)
    
print(0 if x>=W or y>=H else (W-x)*(H-y))

Solution (Python):
w,h,n=map(int,input().split())
grid=[[0 for i in range(w)] for j in range(h)]
for i in range(n):
    x,y,a=map(int,input().split())
    if a==1:
        for i in range(x):
            for j in range(h):
                grid[j][i]=1
    if a==2:
        for i in range(x,w):
            for j in range(h):
                grid[j][i]=1
    if a==3:
        for i in range(w):
            for j in range(y):
                grid[j][i]=1
    if a==4:
        for i in range(w):
            for j in range(y,h):
                grid[j][i]=1
res = 0
for i in grid:
    for j in i:
        if j==0:
            res += 1
print(res)

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

ma_x=w
ma_y=h
mi_x=0
mi_y=0

for i in range(n):
    x,y,a=map(int,input().split())
    if a==1:
        if mi_x<x:
            mi_x=x
    elif a==2:
        if ma_x>x:
            ma_x=x
    elif a==3:
        if mi_y<y:
            mi_y=y
    elif a==4:
        if ma_y>y:
            ma_y=y

if ma_x-mi_x<0 and ma_y-mi_y<0:
  ma_x=mi_x
            
ans=(ma_x-mi_x)*(ma_y-mi_y)

if ans<0:
    ans=0

print(ans)

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

s = [[1 for j in range(w)] for i in range(h)]

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

    if a == 1:
        for m in range(h):
            for n in range(x):
                s[m][n] = 0
    elif a == 2:
        for m in range(h):
            for n in range(x, w):
                s[m][n] = 0
    elif a == 3:
        for m in range(y):
            for n in range(w):
                s[m][n] = 0
    elif a == 4:
        for m in range(y, h):
            for n in range(w):
                s[m][n] = 0

print(sum(map(sum, s)))

Solution (Python):
w,h,n=map(int,input().split())
y=[0,w]
t=[0,h]
def ct(w0,h0,a0):
  if a0==1:
    v=max(w0,y[0])
    y[0]=v
  if a0==2:
    v=min(w0,y[1])
    y[1]=v
  if a0==3:
    v=max(h0,t[0])
    t[0]=v
  if a0==4:
    v=min(h0,t[1])
    t[1]=v
for i in range(n):
  w1,h1,a1=map(int,input().split())
  ct(w1,h1,a1)

print(max(0,y[1]-y[0])*max(0,t[1]-t[0]))

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

a1 = [0]
a2 = [W]
a3 = [0]
a4 = [H]
for x,y,a in XYA:
    if a==1:
        a1.append(x)
    elif a==2:
        a2.append(x)
    elif a==3:
        a3.append(y)
    elif a==4:
        a4.append(y)
        
x_min = max(a1)
x_max = min(a2)
y_min = max(a3)
y_max = min(a4)

ans = 0
if x_max-x_min > 0 and y_max-y_min > 0:
    ans = (x_max-x_min) * (y_max-y_min)

print(max(0,ans))

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

for _ in range(n):
    x, y, a = list(map(int, input().split()))
    if a == 1:
        for xx in range(x):
            for yy in range(h):
                rect[xx][yy] = 0
    elif a == 2:
        for xx in range(x, w):
            for yy in range(h):
                rect[xx][yy] = 0
    elif a == 3:
        for xx in range(w):
            for yy in range(y):
                rect[xx][yy] = 0
    else:
         for xx in range(w):
             for yy in range(y, h):
                 rect[xx][yy] = 0

ans = 0
for xx in range(w):
    for yy in range(h):
        ans += rect[xx][yy]

print(ans)

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

Solution (Python):
w, h, n = [int(i) for i in input().split()]
x = []
y = []
a = []
for i in range(n):
    tmp = [int(i) for i in input().split()]
    x.append(tmp[0])
    y.append(tmp[1])
    a.append(tmp[2])

p1 = [0, 0]
p2 = [w, h]

for i in range(n):
    if a[i] == 1:
        p1[0] = max(p1[0], x[i])
    elif a[i] == 2:
        p2[0] = min(p2[0], x[i])
    elif a[i] == 3:
        p1[1] = max(p1[1], y[i])
    else:
        p2[1] = min(p2[1], y[i])

if p1[0] >= p2[0] or p1[1] >= p2[1]:
    print(0)
else:
    print((p2[0] - p1[0]) * (p2[1] - p1[1]))

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