atcoder#ARC107E. [ARC107E] Mex Mat

[ARC107E] Mex Mat

Score : 800800 points

Problem Statement

Consider an N×NN \times N matrix. Let us denote by ai,ja_{i, j} the entry in the ii-th row and jj-th column. For ai,ja_{i, j} where i=1i=1 or j=1j=1 holds, its value is one of 00, 11 and 22 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 mex(x,y)\mathrm{mex}(x, y) is defined by the following table:
mex(x,y)\mathrm{mex}(x, y) y=0y=0 y=1y=1 y=2y=2
x=0x=0 11 22 11
x=1x=1 22 00
x=2x=2 11

How many entries of the matrix are 0,1,0, 1, and 22, respectively?

Constraints

  • 1N500,0001 \leq N \leq 500{,}000
  • ai,ja_{i,j}'s given in input are one of 00, 11 and 22.

Input

Input is given from Standard Input in the following format:

NN

a1,1a_{1, 1} a1,1a_{1, 1} ...... a1,Na_{1, N}

a2,1a_{2, 1}

::

aN,1a_{N, 1}

Output

Print the number of 00's, 11's, and 22's separated by whitespaces.

4
1 2 0 2
0
0
0
7 4 5

The matrix is as follows:

1 2 0 2
0 1 2 0
0 2 0 1
0 1 2 0