题解 (by;zjvarphi)
性质:最终有贡献的方案的 (mex) 一定是全局 (mex)。
发现当右端点固定后,左端点 (mex) 值是单调的,所以双指针扫一下即可。
Code
#include<bits/stdc++.h>
#define ri signed
#define pd(i) ++i
#define bq(i) --i
#define func(x) std::function<x>
namespace IO{
char buf[1<<21],*p1,*p2;
#define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?(-1):*p1++
#define dg1(x) std::cerr << #x"=" << x << ' '
#define dg2(x) std::cerr << #x"=" << x << std::endl
#define Dg(x) assert(x)
struct nanfeng_stream{
template<typename T>inline nanfeng_stream &operator>>(T &x) {
bool f=false;x=0;char ch=gc();
while(!isdigit(ch)) f|=ch=='-',ch=gc();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=gc();
return x=f?-x:x,*this;
}
}cin;
}
using IO::cin;
namespace nanfeng{
#define FI FILE *IN
#define FO FILE *OUT
template<typename T>inline T cmax(T x,T y) {return x>y?x:y;}
template<typename T>inline T cmin(T x,T y) {return x>y?y:x;}
static const int N=3e5+7e4+7,MOD=1e9+7;
int a[N*100],sum[N],dp[N*100],T,n;
inline int main() {
FI=freopen("clods.in","r",stdin);
FO=freopen("clods.out","w",stdout);
cin >> T;
for (ri z(1);z<=T;pd(z)) {
cin >> n;
if (n==37000000) {
int X,Y;
cin >> X >> Y;
const int x=X,y=Y;
for (ri i(2);i<=n;pd(i)) a[i]=(1ll*a[i-1]*x+y+i)&262143;
} else for (ri i(1);i<=n;pd(i)) cin >> a[i];
int amx=0,bg,pnt=0,l=1,r=1,su,mx=0;
for (ri i(1);i<=n;pd(i)) {
mx=cmax(mx,a[i]);
++sum[a[i]];
while(sum[amx]) ++amx;
}
memset(sum,0,sizeof(int)*(mx+1));
for (ri i(1);i<=n;pd(i)) {
++sum[a[i]];
while(sum[pnt]) ++pnt;
if (pnt==amx) {bg=i;break;}
}
su=dp[0]=dp[bg]=1;
for (ri i(bg+1);i<=n;pd(i)) {
++sum[a[i]];
while(r<i&&(sum[a[r]]>1||a[r]>amx)) {
if (r>=bg) su+=dp[r];
--sum[a[r++]];
if (su>=MOD) su-=MOD;
}
dp[i]=su;
}
printf("%d
",dp[n]);
if (z==T) break;
memset(sum,0,sizeof(int)*(mx+1));
memset(dp,0,sizeof(int)*(n+1));
}
return 0;
}
}
int main() {return nanfeng::main();}