题意
统计排列 的非空连续子段中,有多少个是排列 的子序列。
分析
我们需要证明: 的连续子段 是 的子序列,当且仅当该子段在 中的位置构成严格递增序列。
- 必要性
若 是 的子序列,则根据子序列定义,存在 中的位置序列 ,使得 (其中 )。
由于 表示 在 中的位置(即 ),因此 ,即位置序列严格递增。
- 充分性
若 在 中的位置序列严格递增(即 ),则在 中取这些位置对应的元素,恰好是 ,且顺序与 中一致。 根据子序列定义, 是 的子序列。
注意, 是元素到固定位置的映射,而 是动态的子序列位置。
综上,问题可转化为:统计数组 中所有非空连续严格递增子段的数量,其中 (即 中元素在 中的位置)。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1000005;
int a[maxn],b[maxn],n,pos[maxn],c[maxn];
int main() {
scanf("%d",&n);
for(int i=1;i<=n;++i) scanf("%d",a+i);
for(int i=1;i<=n;++i) scanf("%d",b+i),pos[b[i]]=i;
for(int i=1;i<=n;++i) c[i]=pos[a[i]];
ll sum=1;
int cnt=1;
for(int i=2;i<=n;++i){
if(c[i]>c[i-1]) ++cnt;
else cnt=1;
sum+=cnt;
}
printf("%lld",sum);
}