Codeforces Round 852 (Div. 2)
D
Moscow Gorillas
Codeforces
1800
原题
← CE →
翻译中…

In winter, the inhabitants of the Moscow Zoo are very bored, in particular, it concerns gorillas. You decided to entertain them and brought a permutation $p$ of length $n$ to the zoo.

翻译中…

A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in any order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ occurs twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$, but $4$ is present in the array).

翻译中…

The gorillas had their own permutation $q$ of length $n$. They suggested that you count the number of pairs of integers $l, r$ ($1 \le l \le r \le n$) such that $\operatorname{MEX}([p_l, p_{l+1}, \ldots, p_r])=\operatorname{MEX}([q_l, q_{l+1}, \ldots, q_r])$.

翻译中…

The $\operatorname{MEX}$ of the sequence is the minimum integer positive number missing from this sequence. For example, $\operatorname{MEX}([1, 3]) = 2$, $\operatorname{MEX}([5]) = 1$, $\operatorname{MEX}([3, 1, 2, 6]) = 4$.

翻译中…

You do not want to risk your health, so you will not dare to refuse the gorillas.

翻译中…

Input

翻译中…

The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the permutations length.

翻译中…

The second line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$) — the elements of the permutation $p$.

翻译中…

The third line contains $n$ integers $q_1, q_2, \ldots, q_n$ ($1 \le q_i \le n$) — the elements of the permutation $q$.

翻译中…

Output

翻译中…

Print a single integer — the number of suitable pairs $l$ and $r$.

翻译中…

Note

翻译中…

In the first example, two segments are correct – $[1, 3]$ with $\operatorname{MEX}$ equal to $4$ in both arrays and $[3, 3]$ with $\operatorname{MEX}$ equal to $1$ in both of arrays.

翻译中…

In the second example, for example, the segment $[1, 4]$ is correct, and the segment $[6, 7]$ isn't correct, because $\operatorname{MEX}(5, 4) \neq \operatorname{MEX}(1, 4)$.

翻译中…

样例

Input #1
3
1 3 2
2 1 3
Output #1
2
InputCopy OutputCopy
Input #2
7
7 3 6 2 1 5 4
6 7 2 5 3 1 4
Output #2
16
InputCopy OutputCopy
Input #3
6
1 2 3 4 5 6
6 5 4 3 2 1
Output #3
11
InputCopy OutputCopy
题目信息
题号
1793D
时间限制
2 s
内存限制
256 MB
标签
二分
动态规划
贪心
模拟
数学
双指针

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。