Problem Description
Mex is a function on a set of integers, which is universally used for impartial game theorem. For a non-negative integer set S, mex(S) is defined as the least non-negative integer which is not appeared in S. Now our problem is about mex function on a sequence.
Consider a sequence of non-negative integers {ai}, we define mex(L,R) as the least non-negative integer which is not appeared in the continuous subsequence from aL to aR, inclusive. Now we want to calculate the sum of mex(L,R) for all 1 <= L <= R <= n.
Input
The input contains at most 20 test cases.
For each test case, the first line contains one integer n, denoting the length of sequence.
The next line contains n non-integers separated by space, denoting the sequence.
(1 <= n <= 200000, 0 <= ai <= 10^9)
The input ends with n = 0.
Output
For each test case, output one line containing a integer denoting the answer.
Sample Input
3
0 1 3
5
1 0 2 0 1
0
Sample Output
5
24
Hint
For the first test case:
mex(1,1)=1, mex(1,2)=2, mex(1,3)=2, mex(2,2)=0, mex(2,3)=0,mex(3,3)=0.
1 + 2 + 2 + 0 +0 +0 = 5.
大致题意:给你一个长度为n的序列,让你求该序列的所有连续子序列的mex(i,j)的和,mex(i,j)表示,区间i到j内没有出现过的最小非负整数,与博弈中的mex定义一样。
思路:膜一发大佬的博客https://www.cnblogs.com/20143605–pcx/p/5723148.html
首先预处理出每一个ai下一个出现相同值的位置,用数组nex[]来保存。求出所有mex(1,i)的值,然后以之为节点建立一颗线段树,接下来我们将a[1]这个数从序列中删除,将叶子节点的维护信息更新为mex(2,i),以此类推,然后每更新一次便求一次和,总和即为答案。然后当我们删除a[i]时,假设下一个与a[i]值相同的位置是k,那么只有k以前的mex(i+1,j)会有影响,j< k,假如mex(i+1,j)的值小于a[i],那么删掉a[i]后对其也不影响,有影响的只有那些大于a[i]的mex(i+1,j),此外可知当我们固定了左端点a后mex(a,b)的值是单调不减的,所以我们需要修改的是一段连续的区间,所以我们需要找出最小的那个p,满足mex(i+1,p)大于a[i],且p< k,那么我们需要修改的便是[p,k-1]这么一段连续的区间,总的时间复杂度为3*nlogn+n(查询,修改,求和的时间均为log的时间复杂度)
代码如下
//#include
#include
#include
#include
#include
using namespace std;
#define LL long long int
const int N=200005;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int mex[N];
int a[N];
int nex[N];
LL lazy[N<<2];
LL sum[N<<2];
int maxn[N<<2];
inline LL getmex(int n)
{
map<int,int> mp;
map<int,int> f;
fill(nex,nex+1+n,n+1);
int m=0;
LL ans=0;
for(int i=1;i<=n;i++)
{
mp[a[i]]=1;
while(mp[m]) ++m;
mex[i]=m;
ans+=m;
if(f[a[i]])
nex[f[a[i]]]=i;
f[a[i]]=i;
}
return ans;
}
inline void PushUp(int rt)
{
maxn[rt]=max(maxn[rt<<1],maxn[rt<<1|1]);
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
inline void PushDown(int rt,int m)
{
if(lazy[rt]!=-1)
{
lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
maxn[rt<<1]=maxn[rt<<1|1]=lazy[rt];
sum[rt<<1]=lazy[rt]*(m-(m>>1));
sum[rt<<1|1]=lazy[rt]*(m>>1);
lazy[rt]=-1;
}
}
inline void build(int l,int r,int rt)
{
lazy[rt]=-1;
if(l==r)
{
maxn[rt]=mex[l];
sum[rt]=mex[l];
return ;
}
int m=(l+r)>>1;
build(lson);
build(rson);
PushUp(rt);
}
inline void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
lazy[rt]=c;
sum[rt]=(LL)c*(r-l+1);
maxn[rt]=c;
return ;
}
PushDown(rt,r-l+1);
int m=(l+r)>>1;
if(L<=m) update(L,R,c,lson);
if(minline LL query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return sum[rt];
}
PushDown(rt,r-l+1);
int m=(l+r)>>1;
LL ret=0;
if(L<=m) ret+=query(L,R,lson);
if(mreturn ret;
}
inline int ask_k(int L,int R,int MAXN,int l,int r,int rt)
{
if(l==r)
{
if(sum[rt]return 0;
return l;
}
PushDown(rt,r-l+1);
int m=(l+r)>>1;
if(L<=m&&MAXN1])
{
int k=ask_k(L,R,MAXN,lson);
if(k)
return k;
}
if(m1|1])
{
int k=ask_k(L,R,MAXN,rson);
if(k)
return k;
}
return 0;
}
int main()
{
int n;
while(scanf("%d",&n))
{
if(!n) break;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
LL ans=getmex(n);
build(1,n,1);
for(int i=1;iint p=ask_k(i+1,nex[i]-1,a[i],1,n,1);
if(p>0) update(p,nex[i]-1,a[i],1,n,1);
ans+=query(i+1,n,1,n,1);
}
printf("%lld\n",ans);
}
return 0;
}