# CF1175F The Number of Subpermutations

### codeforces

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<map>
using namespace std;
#define rg register
char ch;bool ok;
for(ok=0,ch=getchar();!isdigit(ch);ch=getchar())if(ch=='-')ok=1;
for(x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());if(ok)x=-x;
}
const int maxn=3e5+10;
int n,a[maxn],ans,nxt[maxn],mx[maxn*4];bool vis[maxn];
void change(int x,int l,int r,int a,int b){
if(l==r)return mx[x]=b,void();int mid=(l+r)>>1;
if(a<=mid)change(x<<1,l,mid,a,b);else change(x<<1|1,mid+1,r,a,b);
mx[x]=max(mx[x<<1],mx[x<<1|1]);
}
int get(int x,int l,int r,int a,int b){
if(a<=l&&b>=r)return mx[x];int mid=(l+r)>>1,ans=0;
if(a<=mid)ans=max(get(x<<1,l,mid,a,b),ans);
if(b>mid)ans=max(ans,get(x<<1|1,mid+1,r,a,b));
return ans;
}
int main(){
for(rg int i=1;i<=n;i++)
if(!vis[a[i]])vis[a[i]]=1;
else {
while(a[l]!=a[i])vis[a[l]]=0,nxt[l]=i-1,l++;
nxt[l]=i-1,l++;
}
for(rg int i=1;i<=n;i++){
for(rg int j=i;j<=nxt[i];j++){
int now=get(1,1,n,i,j);
if(now>j-i+1)j+=(now-(j-i+1))-1;else ans++;
}
}
printf("%d\n",ans);
}

posted @ 2019-09-11 13:27  蒟蒻--lichenxi  阅读(42)  评论(0编辑  收藏