Consider an N \times N matrix. Let us denote by a_{i, j} the entry in the i-th row and j-th column. For a_{i, j} where i=1 or j=1 holds, its value is one of 0, 1 and 2 and given in the input. The remaining entries are defined as follows:
 - a_{i,j} = \mathrm{mex}(a_{i-1,j}, a_{i,j-1}) (2 \leq i, j \leq N) where \mathrm{mex}(x, y) is defined by the following table:\mathrm{mex}(x, y)y=0y=1y=2x=0121x=1200x=2100
How many entries of the matrix are 0, 1, and 2, respectively?

-----Constraints-----
 - 1 \leq N \leq 500{,}000
 - a_{i,j}'s given in input are one of 0, 1 and 2.

-----Input-----
Input is given from Standard Input in the following format:
N
a_{1, 1} a_{1, 1} ... a_{1, N}
a_{2, 1}
:
a_{N, 1}

-----Output-----
Print the number of 0's, 1's, and 2's separated by whitespaces.

-----Sample Input-----
4
1 2 0 2
0
0
0

-----Sample Output-----
7 4 5

The matrix is as follows:
1 2 0 2
0 1 2 0
0 2 0 1
0 1 2 0

Difficulty: VERY_HARD

Solutions:
import sys
input = sys.stdin.readline

def solve():
	N = int(input())
	A = list(map(int, input().split()))
	B = [int(input()) for i in range(N - 1)]

	def mex(x, y):
		if x + y == 1:
			return 2
		if x == 0 or y == 0:
			return 1
		return 0
	n = min(4, N)
	arr = [[-1] * n for _ in range(n)]
	for (i, a) in enumerate(A):
		if i >= 4:
			break
		arr[0][i] = a
	for (i, b) in enumerate(B):
		if i + 1 >= 4:
			break
		arr[i + 1][0] = b
	for i in range(1, n):
		for j in range(1, n):
			arr[i][j] = mex(arr[i - 1][j], arr[i][j - 1])
	ctr = [0, 0, 0]
	for i in range(n):
		for j in range(n):
			ctr[arr[i][j]] += 1
	if N <= 4:
		print(*ctr)
		return
	arrtop = [[-1] * (N - 3) for _ in range(4)]
	for i in range(4):
		arrtop[i][0] = arr[i][-1]
	for j in range(4, N):
		arrtop[0][j - 3] = A[j]
	for i in range(1, 4):
		for j in range(1, N - 3):
			arrtop[i][j] = mex(arrtop[i - 1][j], arrtop[i][j - 1])
			ctr[arrtop[i][j]] += 1 if i < 3 else N - 3 - j
	for a in A[4:]:
		ctr[a] += 1
	arrleft = [[-1] * 4 for _ in range(N - 3)]
	for j in range(4):
		arrleft[0][j] = arr[-1][j]
	for i in range(4, N):
		arrleft[i - 3][0] = B[i - 1]
	for i in range(1, N - 3):
		for j in range(1, 4):
			arrleft[i][j] = mex(arrleft[i - 1][j], arrleft[i][j - 1])
			ctr[arrleft[i][j]] += 1 if j < 3 else N - 3 - i
	for b in B[3:]:
		ctr[b] += 1
	ctr[arr[-1][-1]] += N - 4
	print(*ctr)
solve()


import sys
readline = sys.stdin.readline
read = sys.stdin.read
sys.setrecursionlimit(10 ** 6)
n = int(readline())
res = [0] * 3
a = list(map(int, readline().split()))
mex = [[1, 2, 1], [2, 0, 0], [1, 0, 0]]

def brute(a):
	b = [0] * n
	b[0] = int(readline())
	for i in range(1, n):
		b[i] = mex[a[i]][b[i - 1]]
	return b
if n <= 10:
	for i in a:
		res[i] += 1
	for _ in range(n - 1):
		a = brute(a)
		for i in a:
			res[i] += 1
	print(*res)
	return
for _ in range(4):
	for i in a:
		res[i] += 1
	a = brute(a)
for i in range(3):
	res[a[i]] += 1
res[a[3]] += n - 4
for i in range(4, n):
	res[a[i]] += n - i
b = a[:4]
for i in range(5, n):
	x = int(readline())
	b[0] = x
	for j in range(1, 4):
		res[x] += 1
		x = mex[x][b[j]]
		b[j] = x
	res[x] += n - i
print(*res)