题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4747
题意:求一个数列中,所有mex(L,R)的和。
注意到mex是单调不降的,那么首先预处理出mex(1,j)的值,复杂度O(n),因为mex最大为n。同时预处理出每个数a[i]的右边第一次出现a[i]的位置,用next[i]表示。然后依次从1开始枚举起点 i,则就是求 i 到n的所有mex的和了。i从i+1变化,j>next[i]的mex值都不会变化,因为还是存在a[i]。那么只要考虑i+1到next[i]-1这个区间了,这个区间中,mex第一次大于a[i]的位置k,[k,next[i]-1]的mex都会变成a[i],因为不存在a[i]了,而[i+1,k-1]区间的mex不变。这里就是一个线段树的区间
//STATUS:C++_AC_937MS_11616KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int mextr[N<<],hig[N<<],a[N],mex[N],next[N],vis[N];
LL sum[N<<];
int n; void pushdown(int l,int r,int mid,int rt)
{
if(mextr[rt]!=-){
mextr[rt<<]=mextr[rt<<|]=mextr[rt];
sum[rt<<]=mextr[rt]*(mid-l+);
sum[rt<<|]=mextr[rt]*(r-mid);
hig[rt<<]=hig[rt<<|]=hig[rt];
}
} void pushup(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
hig[rt]=Max(hig[rt<<],hig[rt<<|]);
if(mextr[rt<<]!=- && mextr[rt<<]==mextr[rt<<|])mextr[rt]=mextr[rt<<];
else mextr[rt]=-;
} void build(int l,int r,int rt)
{
if(l==r){
sum[rt]=mextr[rt]=hig[rt]=mex[l];
return ;
}
int mid=(l+r)>>;
build(lson);
build(rson);
pushup(rt);
} int Upper_Bound(int l,int r,int rt,int tar)
{
if(l==r)return l;
int ret,mid=(l+r)>>;
pushdown(l,r,mid,rt);
if(hig[rt<<]>tar)ret=Upper_Bound(lson,tar);
else ret=Upper_Bound(rson,tar);
pushup(rt);
return ret;
} void update(int l,int r,int rt,int L,int R,int val)
{
if(L<=l && r<=R){
mextr[rt]=hig[rt]=val;
sum[rt]=(LL)val*(r-l+);
return ;
}
int mid=(l+r)>>;
pushdown(l,r,mid,rt);
if(L<=mid)update(lson,L,R,val);
if(R>mid)update(rson,L,R,val);
pushup(rt);
} int main(){
// freopen("in.txt","r",stdin);
int i,j,k,hig,L,R;
LL ans;
while(~scanf("%d",&n) && n)
{
mem(vis,);mex[n]=;
for(i=hig=;i<n;i++){
scanf("%d",&a[i]);
if(a[i]>n+)a[i]=n+;
vis[a[i]]=;
while(vis[hig])hig++;
mex[i]=hig;
} for(i=n+;i>=;i--)vis[i]=n;
for(i=n-;i>=;i--){
next[i]=vis[a[i]];
vis[a[i]]=i;
}
ans=;build(,n,);
for(i=;i<n;i++){
ans+=sum[];
L=Upper_Bound(,n,,a[i]);
R=next[i]-;
if(L<=R)update(,n,,L,R,a[i]);
} printf("%I64d\n",ans);
}
return ;
}
求和操作了。。