Decomposition
Contest: Educational Codeforces Round 139 (Rated for Div. 2)
Difficulty rating: 2300
Tags: binary search, brute force, data structures, divide and conquer, dp, two pointers

For a sequence of integers $$$[x_1, x_2, \dots, x_k]$$$, let's define its decomposition as follows:

Process the sequence from the first element to the last one, maintaining the list of its subsequences. When you process the element $$$x_i$$$, append it to the end of the first subsequence in the list such that the bitwise AND of its last element and $$$x_i$$$ is greater than $$$0$$$. If there is no such subsequence in the list, create a new subsequence with only one element $$$x_i$$$ and append it to the end of the list of subsequences.

For example, let's analyze the decomposition of the sequence $$$[1, 3, 2, 0, 1, 3, 2, 1]$$$:

- processing element $$$1$$$, the list of subsequences is empty. There is no subsequence to append $$$1$$$ to, so we create a new subsequence $$$[1]$$$;
- processing element $$$3$$$, the list of subsequences is $$$[[1]]$$$. Since the bitwise AND of $$$3$$$ and $$$1$$$ is $$$1$$$, the element is appended to the first subsequence;
- processing element $$$2$$$, the list of subsequences is $$$[[1, 3]]$$$. Since the bitwise AND of $$$2$$$ and $$$3$$$ is $$$2$$$, the element is appended to the first subsequence;
- processing element $$$0$$$, the list of subsequences is $$$[[1, 3, 2]]$$$. There is no subsequence to append $$$0$$$ to, so we create a new subsequence $$$[0]$$$;
- processing element $$$1$$$, the list of subsequences is $$$[[1, 3, 2], [0]]$$$. There is no subsequence to append $$$1$$$ to, so we create a new subsequence $$$[1]$$$;
- processing element $$$3$$$, the list of subsequences is $$$[[1, 3, 2], [0], [1]]$$$. Since the bitwise AND of $$$3$$$ and $$$2$$$ is $$$2$$$, the element is appended to the first subsequence;
- processing element $$$2$$$, the list of subsequences is $$$[[1, 3, 2, 3], [0], [1]]$$$. Since the bitwise AND of $$$2$$$ and $$$3$$$ is $$$2$$$, the element is appended to the first subsequence;
- processing element $$$1$$$, the list of subsequences is $$$[[1, 3, 2, 3, 2], [0], [1]]$$$. The element $$$1$$$ cannot be appended to any of the first two subsequences, but can be appended to the third one.

The resulting list of subsequences is $$$[[1, 3, 2, 3, 2], [0], [1, 1]]$$$.

Let $$$f([x_1, x_2, \dots, x_k])$$$ be the number of subsequences the sequence $$$[x_1, x_2, \dots, x_k]$$$ is decomposed into.

Now, for the problem itself.

You are given a sequence $$$[a_1, a_2, \dots, a_n]$$$, where each element is an integer from $$$0$$$ to $$$3$$$. Let $$$a[i..j]$$$ be the sequence $$$[a_i, a_{i+1}, \dots, a_j]$$$. You have to calculate $$$\sum \limits_{i=1}^n \sum \limits_{j=i}^n f(a[i..j])$$$.

Input format:
The first line contains one integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$).

The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 3$$$).

Output format:
Print one integer, which should be equal to $$$\sum \limits_{i=1}^n \sum \limits_{j=i}^n f(a[i..j])$$$.

Constraints:
Time limit: 4.0 s
Memory limit: 512.0 MB

Example input:
8
1 3 2 0 1 3 2 1

Example output:
71

Example input:
5
0 0 0 0 0

Example output:
35

Editorial:
Let's assume that we don't have any zeroes in our array. We'll deal with them later.
The key observation is that the number of sequences in the decomposition is not more than $$$3$$$. To prove this, we can use the fact that each element $$$3$$$ will be appended to the first subsequence in the decomposition; so, if the second/third subsequence in the decomposition ends with the number $$$2$$$ or $$$1$$$, all such numbers can be appended to that subsequence, thus they won't create any new subsequences. So, if we consider the combination of the last elements in the subsequences of the decomposition, there are only $$$3^3 + 3^2 + 3^1 + 3^0 = 40$$$ such combinations (even less in practice).
Okay, now let's try to use the fact that the number of such combinations is small. There are many ways to abuse it, but, in my opinion, the most straightforward one (and also a bit slow, but fast enough to easily pass the time limit) is to run the following dynamic programming: $$$dp_{i,c}$$$, where $$$i$$$ is the index of the element we are processing, and $$$c$$$ is the vector representing the combination of last elements of subsequences in the decomposition.
But it's not clear what do we store in this dynamic programming. The model solution stores the total number of subsequences added to the decomposition, if right now the state of decomposition is $$$c$$$, we process the $$$i$$$-th element, and we consider all possible stopping points (i. e. we will consider the number of subsequences added while processing the elements $$$a[i..i], a[i..i+1], a[i..i+2], \dots, a[i..n]$$$). So, our dynamic programming automatically sums up the answers for all possible right borders of the segment we decompose. Transitions in this dynamic programming is easy: we need to see how does the element $$$a_i$$$ alter the state of decomposition $$$c$$$ (let it change it to $$$c'$$$), take the value of $$$dp_{i+1, c'}$$$, and if the element $$$a_i$$$ forms a new subsequence, let's account for it by increasing $$$dp_{i,c}$$$ by $$$n-i+1$$$, because this increase will affect $$$n-i+1$$$ different right endpoints of the segment we decompose.
And now it's easy to see how to add zeroes to our solution. We can just assume they don't change the state of decomposition, they simply add a new subsequence which won't take any other elements. So, in our transitions, processing $$$0$$$ means that $$$c' = c$$$, but the size of decomposition increases.
To actually get the answer to the problem, we need to consider all possible starting points of the segment, so we sum up $$$dp_{i,o}$$$ (where $$$o$$$ is the empty vector) for all $$$i \in [1, n]$$$.