lucasincyber不会做他自己的题/cf

· · 题解

设 x 在 b 中的位置为 pos_x,那么 a 中 [l,r] 合法当且仅当 pos_{a_l}<pos_{a_{l+1}}<\cdots <pos_{a_r}。

那么很显然对于 i,设 L_i 为最小的 L 满足 [L_i,i] 合法,L 肯定是单调不减的。

所以只用双指针维护一下 L 即可。

::::info[code]

#include<bits/stdc++.h>
using namespace std;
#define int long long

const int N=1e6+5;
int n,a[N],b[N];
int pos[N],ans;

signed main(){
    ios::sync_with_stdio(0),cin.tie(0);
    cin>>n;for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++) cin>>b[i];
    for(int i=1;i<=n;i++) pos[b[i]]=i;
    int l=1,lst=pos[a[1]];
    for(int i=1;i<=n;i++){
        if(pos[a[i]]<lst) l=i;
        lst=pos[a[i]];ans+=i-l+1; 
    }
    cout<<ans;
    return 0;
}

::::