D. Moscow Gorillas

D. Moscow Gorillas

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 (1lrn) such that MEX([pl,pl+1,,pr])=MEX([ql,ql+1,,qr]).

The MEX of the sequence is the minimum integer positive number missing from this sequence. For example, MEX([1,3])=2, MEX([5])=1, 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 (1n2105) — the permutations length.

The second line contains n integers p1,p2,,pn (1pin) — the elements of the permutation p.

The third line contains n integers q1,q2,,qn (1qin) — the elements of the permutation q.

Output

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

Examples
input

3
1 3 2
2 1 3

output

2

input

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

output

16

input

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

output

11

Note

In the first example, two segments are correct – [1,3] with MEX equal to 4 in both arrays and [3,3] with 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 MEX(5,4)MEX(1,4).

 

解题思路

  比赛的时候想到了通过枚举mex来计算每个mex对答案的贡献是多少,但没写出来。

  先考虑mex=1的情况,首先选取的区间不能包含元素1,那么pq中能够选取的区间就被分成了3个部分,长度分别为s1,s2,s3

  那么很明显共有s1(s1+1)2+s2(s2+1)2+s3(s3+1)2个区间使得mex(p[lr])=mex(q[lr])=1

  下面考虑mex>1的情况。

  如果一个排列的区间满足mex(p[lr])=i,那么区间[l,r]一定包含元素1i1且一定不包含元素i。定义pos[i]表示元素i在排列中的位置,minp[i]表示前i个元素中最小的下标位置,即minp[i]=min1ji{pos[j]},同理maxp[i]=max1ji{pos[j]}。因此如果某个区间的mex=i,那么就要满足pos[i]<minp[i1]或者pos[i]>maxp[i1],即pos[i]不能在[minp[i1],maxp[i1]]的范围内。

  1. 如果pos[i]<minp[i1],那么满足mex(p[lr])=ilr要满足pos[i]<lminp[i1]maxp[i1]rn
  2. 如果pos[i]>maxp[i1],那么那么满足mex(p[lr])=ilr要满足1lminp[i1]maxp[i1]r<pos[i]
  3. 否则就是minp[i1]pos[i]maxp[i1],此时没有区间满足mex(p[lr])=i

  现在是有两个排列pq,那么对于同时满足mex(p[lr])=imex(q[lr])=i的区间可以对各自的lr分别通过区间求交的方式得到:

  在排列p中,lp的取值范围是[3,5](红色部分),rp的取值范围是[9,13](绿色部分)。在q中,lq的取值范围是[1,4](蓝色部分),rq的取值范围是[7,11](橙色部分)。

  因此能够使得两个排列同时满足mex(p[lr])=imex(q[lr])=il要满足lplq=[3,4]rprq=[9,11](记得选取的区间一定使得两个排列都包含元素1i1),那么满足的区间个数就是|lplq|×|rprq|=2×3=6,即分别是区间[3,9][3,10][3,11][4,9][4,10][4,11]

  AC代码如下:

复制代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 
 6 const int N = 2e5 + 10;
 7 
 8 int p[N], q[N];
 9 int minp[N], maxp[N], minq[N], maxq[N];
10 
11 LL get(int a, int b, int c, int d) {    // 区间[a,b]和[c,d]求交集
12     return max(0, min(b, d) - max(a, c) + 1);
13 }
14 
15 LL cal(LL x) {  // 计算1+2+...+x
16     return x * (x + 1) >> 1;
17 }
18 
19 int main() {
20     int n;
21     scanf("%d", &n);
22     for (int i = 1; i <= n; i++) {
23         int x;
24         scanf("%d", &x);
25         p[x] = i;
26     }
27     for (int i = 1; i <= n; i++) {
28         int x;
29         scanf("%d", &x);
30         q[x] = i;
31     }
32     minp[1] = maxp[1] = p[1];
33     minq[1] = maxq[1] = q[1];
34     for (int i = 2; i <= n; i++) {
35         minp[i] = min(minp[i - 1], p[i]);
36         maxp[i] = max(maxp[i - 1], p[i]);
37         minq[i] = min(minq[i - 1], q[i]);
38         maxq[i] = max(maxq[i - 1], q[i]);
39     }
40     LL ret = 1; // 当两个排列都选区间[1,n]那么就有mex=n+1,这里的1就是指这个情况
41     for (int i = 2; i <= n; i++) {  // 考虑mex=2~n的情况
42         if (p[i] >= minp[i - 1] && p[i] <= maxp[i - 1] || q[i] >= minq[i - 1] && q[i] <= maxq[i - 1]) continue; // 某个排列无法取得mex=i的区间
43         int lp, rp, lq, rq;
44         if (p[i] > maxp[i - 1]) lp = 1, rp = p[i] - 1;  // p[i]在区间右边
45         else lp = p[i] + 1, rp = n; // p[i]在区间左边
46         if (q[i] > maxq[i - 1]) lq = 1, rq = q[i] - 1;  // q[i]在区间右边
47         else lq = q[i] + 1, rq = n; //q[i]在区间左边
48         ret += get(lp, minp[i - 1], lq, minq[i - 1]) * get(maxp[i - 1], rp, maxq[i - 1], rq);   // 区间求交,然后乘法原理计算答案
49     }
50     // 最后考虑mex=1的3个区间
51     ret += cal(get(1, p[1] - 1, 1, q[1] - 1)) + cal(max(0, abs(p[1] - q[1]) - 1)) + cal(get(p[1] + 1, n, q[1] + 1, n));
52     printf("%lld", ret);
53     
54     return 0;
55 }
复制代码

 

参考资料

  Codeforces Round #852 Editorial:https://codeforces.com/blog/entry/112723

  Codeforces Round #852(Div. 2) D(枚举+分类讨论):https://zhuanlan.zhihu.com/p/605710274

posted @   onlyblues  阅读(149)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
历史上的今天:
2022-02-13 飞行员兄弟
Web Analytics
点击右上角即可分享
微信分享提示