CF1793D.Moscow Gorillas

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 pp of length nn to the zoo.

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

The gorillas had their own permutation qq of length nn. They suggested that you count the number of pairs of integers l,rl, r (1≤l≤r≤n1 \le l \le r \le n) such that MEX⁡([pl,pl+1,…,pr])=MEX⁡([ql,ql+1,…,qr])\operatorname{MEX}([p_l, p_{l+1}, \ldots, p_r])=\operatorname{MEX}([q_l, q_{l+1}, \ldots, q_r]).

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

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

冬天,莫斯科动物园的动物们非常无聊,尤其是大猩猩。你决定逗它们开心,于是带了一个长度为 nn 的排列 pp 去了动物园。

长度为 nn 的排列是指由 11 到 nn 中互不相同的 nn 个整数以任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,但 [1,2,2][1,2,2] 不是排列(数组中 22 出现了两次),[1,3,4][1,3,4] 也不是排列(此时 n=3n=3,但数组中出现了 44)。

大猩猩们自己有一个长度为 nn 的排列 qq。它们建议你计算满足 MEX⁡([pl,pl+1,…,pr])=MEX⁡([ql,ql+1,…,qr])\operatorname{MEX}([p_l, p_{l+1}, \ldots, p_r])=\operatorname{MEX}([q_l, q_{l+1}, \ldots, q_r]) 的整数对 l,rl, r(其中 1≤l≤r≤n1 \le l \le r \le n)的个数。

序列的 MEX⁡\operatorname{MEX} 指该序列中缺失的最小正整数。例如,MEX⁡([1,3])=2\operatorname{MEX}([1, 3]) = 2,MEX⁡([5])=1\operatorname{MEX}([5]) = 1,MEX⁡([3,1,2,6])=4\operatorname{MEX}([3, 1, 2, 6]) = 4。

你可不想拿自己的健康冒险,因此不敢拒绝大猩猩们的要求。

输入格式

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

The second line contains nn integers p1,p2,…,pnp_1, p_2, \ldots, p_n (1≤pi≤n1 \le p_i \le n) — the elements of the permutation pp.

The third line contains nn integers q1,q2,…,qnq_1, q_2, \ldots, q_n (1≤qi≤n1 \le q_i \le n) — the elements of the permutation qq.

第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 表示排列的长度。

第二行包含 nn 个整数 p1,p2,…,pnp_1, p_2, \ldots, p_n(1≤pi≤n1 \le p_i \le n)—— 表示排列 pp 的元素。

第三行包含 nn 个整数 q1,q2,…,qnq_1, q_2, \ldots, q_n(1≤qi≤n1 \le q_i \le n)—— 表示排列 qq 的元素。

输出格式

Print a single integer — the number of suitable pairs ll and rr.

输出一个整数——满足条件的数对 ll 和 rr 的个数。

输入输出样例

  • 输入#1

    3
    1 3 2
    2 1 3

    输出#1

    2
  • 输入#2

    7
    7 3 6 2 1 5 4
    6 7 2 5 3 1 4

    输出#2

    16
  • 输入#3

    6
    1 2 3 4 5 6
    6 5 4 3 2 1

    输出#3

    11

说明/提示

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

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

在第一个例子中,有两个正确的区间:[1,3][1, 3],其在两个数组中的 MEX⁡\operatorname{MEX} 均为 44;以及 [3,3][3, 3],其在两个数组中的 MEX⁡\operatorname{MEX} 均为 11。

在第二个例子中,例如区间 [1,4][1, 4] 是正确的,而区间 [6,7][6, 7] 不正确,因为 MEX⁡(5,4)≠MEX⁡(1,4)\operatorname{MEX}(5, 4) \neq \operatorname{MEX}(1, 4)。

输入解题思路,AI测评打分。不知道怎么写?

首页