You are given a grid of size N x M consisting of '.' (empty), 'W' (white) or 'B' (black) cells. We follow the convention that the top left corner is the position (1,1) and bottom right corner is (N,M). 
From every '.' cell (i, j), a ray is shot towards the right. If the ray reaches a 'B' cell, it loses it's strength fully and stops there. When a ray
reaches a 'W' cell, it's strength drops drastically so that the  ray stops when it reaches a second 'W' cell. That is, if there is no 'B'
cell in between, a ray can cross at most one 'W' cell, and it will stop when it reaches the second 'W' cell. It passes unchanged through  any '.' cell. If it reaches a boundary cell (ie. (i,M), for some i), it stops there.
Let L(i, j) be length travelled by the ray starting from the cell (i, j). If (i,j) is 'W' or 'B', no ray starts from here, and hence L(i,j) is defined to be 0. If a ray starts from (i,j) and stops at (i,k), then the distance travelled by this ray is k-j+1. i.e, inclusive of both starting and ending cells.
For the given grid your task is to find the sum of L(i, j) over all 1 <= i <= N and 1 <= j <= M.  
The description of the grid is given as follows: In addition to N and M, you are given the number of 'W' cells (w) and the number of 'B' cells (b)
and you are given the locations of these w + b cells.  (The other cells contain '.')

-----Constraints:-----
For all testcases,
- 1 <= N, M <= 10^6. 
- 0 <= w,b <= 10^5
Subtask 1: 15%
It is guaranteed that 1 <= N,M <= 500
Subtask 2: 25%
It is guaranteed that 1 <= N,M <= 2000
Subtask 3: 60%
No additional guarantees.

-----Input format:-----
- There is only one line of input which contains 4 + 2w + 2b space separated integers. The first four integers are N, M, w and b. 
- The next 2*w integers denote the cells which contains a 'W': x1 y1 x2 y2 .. xw yw. These denote that (xi,yi) contains 'W'.
- The next 2*b integers denote the cells which contains a 'B': x1 y1 x2 y2 .. xb yb. These denote that (xi,yi) contains 'B'.
- The cells which are not in the input have to be assumed to be '.' 

-----Output format:-----
Output a single integer which is the sum of L(i,j) over all 1 <= i <= N and 1 <= j <= M.

-----Sample Input 1:-----
4 4 5 2 1 3 2 1 3 2 3 3 4 3 1 4 2 3

-----Sample Output 1:-----
22

-----Explanation:-----
The grid is:
. . W B
W . B .
. W W .
. . W .

L(i,j) for each cell is:
4 3 0 0
0 2 0 1
3 0 0 1
4 3 0 1

Therefore, the total is 22.

-----Note:-----
As the answer might be large, please use 64 bit integers (long long int in C/C++ and long in Java) instead of 32 bit int.

```python

USER_FILENAME = "<user_code>"
import sys, json, builtins
import math, heapq, bisect, itertools, functools, collections, statistics, fractions, decimal, operator, array, random, re, string, os, pathlib
try:
    import resource as _res
except Exception:
    _res = None
from collections import Counter, defaultdict, deque, OrderedDict
from functools import lru_cache, reduce
from itertools import accumulate, permutations, combinations, product
from math import gcd, ceil, floor, sqrt
from decimal import Decimal
try:
    from typing import *
except Exception:
    pass
try:
    sys.setrecursionlimit(1_000_000)
except Exception:
    pass
try:
    import site as _site
    try:
        _sys_sites = _site.getsitepackages() if hasattr(_site, "getsitepackages") else []
        for _p in _sys_sites:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
    try:
        _usp = _site.getusersitepackages()
        if isinstance(_usp, str):
            _usp = [_usp]
        for _p in _usp:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
except Exception:
    pass

ns = {"__name__": "__main__"}
for _k, _v in list(globals().items()):
    if not _k.startswith("_"):
        ns[_k] = _v
_CODE = '# cook your dish here\ndef suma(s, e, cnt):\n t = e - s\n if cnt < e:\n  t_1 = e - cnt + 1\n  ans = t * (t + 1) // 2 - t_1 * (t_1+1) // 2\n else:\n  ans = t * (t + 1) // 2 - 1\n return ans\n\nn, m, w, b, *l = list(map(int, input().split()))\n# mat = [[0 for _ in range(m)] for _ in range(n)]\ndic = {}\nfor i in range(0, 2*w, 2):\n y, x = l[i]-1, l[i+1]-1\n # mat[y-1][x-1] = 1\n if y not in dic:\n  dic[y] = [[x, 1]]\n else:\n  dic[y].append([x, 1])\n\nfor i in range(2*w, 2*(w+b), 2):\n y, x = l[i]-1, l[i+1]-1\n # mat[y-1][x-1] = 2\n if y not in dic:\n  dic[y] = [[x, 2]]\n else:\n  dic[y].append([x, 2])\n\nans = 0\nfor v in list(dic.values()):\n v.sort()\n v.append([m-1, 2])\n i = len(v) - 1\n cnt_i = m + 1\n ans += 1\n while i >= 0:\n  if i-1 >= 0 and v[i - 1][1] == 2:\n   ans += suma(v[i - 1][0], v[i][0], cnt_i)\n   cnt_i = v[i-1][0]\n   i -= 1\n  elif i-2 >= 0 and v[i - 2][1] == 1:\n   ans += suma(v[i - 2][0], v[i][0], cnt_i)\n   if cnt_i != v[i - 1][0]:\n    ans -= (v[i][0] - v[i - 1][0] + 1)\n   cnt_i = v[i - 2][0]\n   i -= 1\n  elif i-2 >= 0 and v[i - 2][1] == 2:\n   ans += suma(v[i - 2][0], v[i][0], cnt_i)\n   if cnt_i != v[i - 1][0]:\n    ans -= (v[i][0] - v[i - 1][0] + 1)\n   cnt_i = v[i - 2][0]\n   i -= 2\n  else:\n   ans += suma(-1, v[i][0], cnt_i)\n   if i == 1 and cnt_i != v[i - 1][0]:\n    ans -= (v[i][0] - v[i - 1][0] + 1)\n   break\n\n # prev_w = -1\n # blk = m - 1\n # cnted_i = m\n # for i in range(len(v) - 1, -1, -1):\n #     prev_blk = -1\n #     if v[i][1] == 2:\n #         prev_blk = blk\n #         blk = v[i][0]\n #         prev_w = -1\n #         ans -= 1\n #     elif v[i][1] == 1:\n #         if prev_w != -1:\n #             prev_blk = blk\n #             blk = prev_w\n #             ans -= 1\n #         prev_w = v[i][0]\n #         ans -= blk - v[i][0] + 1\n\n #     if prev_blk != -1:\n #         t = prev_blk - v[i][0]\n #         t_1 = prev_blk - cnted_i + 1\n #         if cnted_i < prev_blk and v[i][1] == 1:\n #             ans += 1\n #         # if v[i][1] == 1:\n #         #     ans += blk - v[i][0]+1\n #         ans += t * (t + 1) // 2 - t_1 * (t_1 + 1) // 2\n #         cnted_i = v[i][0]+1\n # if cnted_i < prev_blk and v[i][1] == 1:\n #     ans += 1\n # t = blk + 1\n # t_1 = blk - cnted_i + 1\n # ans += t * (t + 1) // 2 - t_1 * (t_1 + 1) // 2\n\nn -= len(dic)\nans += n*m*(m+1)//2\n# temp = [[0 for _ in range(m)] for _ in range(n)]\n# ans = 0\n# for j in range(n):\n#     prev_w = -1\n#     blk = m-1\n#     for i in range(m-1, -1, -1):\n#         if mat[j][i] == 2:\n#             blk = i\n#             prev_w = -1\n#         elif mat[j][i] == 1:\n#             if prev_w != -1:\n#                 blk = prev_w\n#             prev_w = i\n#         else:\n#             ans += blk-i+1\n#             temp[j][i] = blk-i+1\nprint(ans)\n# print(temp)\n'
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

```

Measured memory_bytes: 14683