【CF526F】Pudding Monsters
题意:给你一个排列$p_i$,问你有对少个区间的值域段是连续的。
$n\le 3\times 10^5$
题解:bzoj3745 Norma 的弱化版。直接cdq分治,考虑最大值和最小值分别在左右两边的情况。这里就当练练手了。
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn=300010; typedef long long ll; const int inf=1<<30; ll ans; int n; int v[maxn],rm[maxn],rn[maxn],lm[maxn],ln[maxn],s1[maxn<<1],s2[maxn<<1]; inline int rd() { int ret=0,f=1; char gc=getchar(); while(gc<‘0‘||gc>‘9‘) {if(gc==‘-‘) f=-f; gc=getchar();} while(gc>=‘0‘&&gc<=‘9‘) ret=ret*10+(gc^‘0‘),gc=getchar(); return ret*f; } void solve(int l,int r) { if(l==r) return ; int i,j1,j2,mid=(l+r)>>1; solve(l,mid),solve(mid+1,r); for(lm[mid+1]=0,ln[mid+1]=inf,i=mid;i>=l;i--) lm[i]=max(lm[i+1],v[i]),ln[i]=min(ln[i+1],v[i]); for(rm[mid]=0,rn[mid]=inf,i=mid+1;i<=r;i++) rm[i]=max(rm[i-1],v[i]),rn[i]=min(rn[i-1],v[i]); for(i=mid,j1=j2=mid+1;i>=l;i--) { while(j1<=r&&rn[j1]>ln[i]&&rm[j1]<lm[i]) s1[rn[j1]+j1]++,j1++; while(j2<=r&&rm[j2]<lm[i]) s2[rn[j2]+j2]++,j2++; if(j1!=mid+1&&j1-1>=i+lm[i]-ln[i]) ans++; ans+=s2[i+lm[i]]-s1[i+lm[i]]; } for(i=mid+1;i<=r;i++) s1[rn[i]+i]=s2[rn[i]+i]=0; for(i=mid+1,j1=j2=mid;i<=r;i++) { while(j1>=l&&ln[j1]>rn[i]&&lm[j1]<rm[i]) s1[ln[j1]-j1+n]++,j1--; while(j2>=l&&lm[j2]<rm[i]) s2[ln[j2]-j2+n]++,j2--; if(j1!=mid&&j1+1<=i-(rm[i]-rn[i])) ans++; ans+=s2[rm[i]-i+n]-s1[rm[i]-i+n]; } for(i=mid;i>=l;i--) s1[ln[i]-i+n]=s2[ln[i]-i+n]=0; } int main() { n=rd(); int i,a; for(i=1;i<=n;i++) a=rd(),v[a]=rd(); solve(1,n); printf("%lld",ans+n); return 0; }//3 1 1 2 2 3 3