The Number of Subpermutations
Contest: Educational Codeforces Round 66 (Rated for Div. 2)
Difficulty rating: 2500
Tags: brute force, data structures, divide and conquer, hashing, math

You have an array $$$a_1, a_2, \dots, a_n$$$.

Let's call some subarray $$$a_l, a_{l + 1}, \dots , a_r$$$ of this array a subpermutation if it contains all integers from $$$1$$$ to $$$r-l+1$$$ exactly once. For example, array $$$a = [2, 2, 1, 3, 2, 3, 1]$$$ contains $$$6$$$ subarrays which are subpermutations: $$$[a_2 \dots a_3]$$$, $$$[a_2 \dots a_4]$$$, $$$[a_3 \dots a_3]$$$, $$$[a_3 \dots a_5]$$$, $$$[a_5 \dots a_7]$$$, $$$[a_7 \dots a_7]$$$.

You are asked to calculate the number of subpermutations.

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$$$ ($$$1 \le a_i \le n$$$).

This array can contain the same integers.

Output format:
Print the number of subpermutations of the array $$$a$$$.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
There are $$$7$$$ subpermutations in the first test case. Their segments of indices are $$$[1, 4]$$$, $$$[3, 3]$$$, $$$[3, 6]$$$, $$$[4, 7]$$$, $$$[6, 7]$$$, $$$[7, 7]$$$ and $$$[7, 8]$$$.

In the second test case $$$6$$$ subpermutations exist: $$$[1, 1]$$$, $$$[2, 2]$$$, $$$[2, 3]$$$, $$$[3, 4]$$$, $$$[4, 4]$$$ and $$$[4, 5]$$$.

Example input:
8
2 4 1 3 4 2 1 2

Example output:
7

Example input:
5
1 1 2 1 2

Example output:
6

Editorial:
At first, let's represent permutations in the next form. We assign to all numbers from $$$1$$$ to $$$n$$$ random 128-bit strings, so the $$$i$$$-th number gets the string $$$h_i$$$. Then the permutation of length $$$len$$$ can be hashed as $$$h_1 \oplus h_2 \oplus \dots \oplus h_{len}$$$, where $$$\oplus $$$ is bitwise exclusive OR (for example, $$$0110 \oplus 1010 = 1100$$$). This representation is convenient because if we have two sets of numbers with a total number of elements equal to $$$len$$$ (let's represent them as $$$s_1, s_2, \dots s_m$$$ and $$$t_1, t_2, \dots t_k$$$, $$$k + m = len$$$), we can easily check whether their union is a permutation of length $$$len$$$ (condition $$$h_{s_1} \oplus h_{s_2} \oplus \dots \oplus h_{s_m} \oplus h_{t_1} \oplus h_{t_2} \oplus \dots h_{t_k} = h_1 \oplus h_2 \oplus \dots \oplus h_{len}$$$ must be hold).
Let's denote $$$f(l, r)$$$ as $$$h_{a_l} \oplus h_{a_l + 1} \oplus \dots \oplus h_{a_r}$$$.
Now let's iterate over position $$$i$$$ such that $$$a_i = 1$$$ and calculate the number of permutations that contain this element. To do it, let's iterate over the right boundary $$$r$$$ and suppose, that maximum element of permutation $$$len$$$ (and its length at the same time) is one of positions $$$i + 1, i + 2, \dots , r$$$. If it's true, then the subpermutation should be on the positions $$$r - len + 1, r - len + 2, \dots , r$$$. And to check that this segment is a subpermutation we should just compare $$$f(r - len + 1, r)$$$ and $$$h_1 \oplus h_2 \oplus \dots \oplus h_{len}$$$.
Thus, we will calculate all permutations in which the position of the maximum is to the right of the position of the $$$1$$$. To calculate all permutations we need to reverse array $$$a$$$ and repeat this algorithm, and then add the number of ones in the array $$$a$$$.