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.
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.
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 24HintFor 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.
Source
2013 ACM/ICPC Asia Regional Hangzhou Online
我一开始看到这道题,只能想出n^2的算法(尴尬)。
于是看了题解,如果将Σ看作n个同起点的mex求和,所有同起点的mex是单调不减的(重要)。
如何转移呢,对于样例2
1 0 2 0 1
将1删除就表示将mex(2...x)都变成不大于1的数,x即1下一次出现的位置-1。
可以运用单调性,查找,预处理类似于链表。
剩下的就是线段树区间修改,维护和,最值。
注意对于a[i]大于n,可以直接舍去,比较方便。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=200005;
int n,data[N],t[N],mex[N],pos[N],nxt[N];
long long ans;
struct node
{
int l,r,tag,mx;
long long sum;
}a[4*N];
void build(int num,int l,int r)
{
a[num].l=l,a[num].r=r,a[num].tag=-1;
if(l==r)
{
a[num].sum=(long long)mex[l],a[num].mx=mex[l];
return ;
}
int mid=(l+r)/2;
build(2*num,l,mid);
build(2*num+1,mid+1,r);
a[num].sum=a[2*num].sum+a[2*num+1].sum;
a[num].mx=max(a[2*num].mx,a[2*num+1].mx);
}
void update(int num)
{
if(a[num].l!=a[num].r)
{
a[2*num].tag=a[2*num+1].tag=a[num].tag;
a[2*num].sum=(long long)(a[2*num].r-a[2*num].l+1)*a[2*num].tag;
a[2*num+1].sum=(long long)(a[2*num+1].r-a[2*num+1].l+1)*a[2*num+1].tag;
a[2*num].mx=a[2*num+1].mx=a[num].mx;
}
a[num].tag=-1;
}
void chg(int num,int l,int r,int x)//将区间[l,r]修改为x
{
if(a[num].l>r||a[num].r<l)
return ;
if(a[num].l>=l&&a[num].r<=r)
{
a[num].tag=x,a[num].mx=x;
a[num].sum=(long long)(a[num].r-a[num].l+1)*x;
return ;
}
if(a[num].tag>=0)
update(num);
chg(2*num,l,r,x);
chg(2*num+1,l,r,x);
a[num].sum=a[2*num].sum+a[2*num+1].sum;
a[num].mx=max(a[2*num].mx,a[2*num+1].mx);
}
int query(int num,int x)//查找第一个等于x的位置
{
if(a[num].l==a[num].r)
return a[num].l;
if(a[num].tag>=0)
update(num);
if(a[2*num].mx>=x)
return query(2*num,x);
return query(2*num+1,x);
}
int main()
{
while(scanf("%d",&n)&&n)
{
ans=0;
memset(t,0,sizeof(t));
for(int i=1;i<=n;i++)
{
scanf("%d",&data[i]);
if(data[i]>n)
data[i]=n;
}
for(int i=1;i<=n;i++)
if(data[i]<=n)
{
t[data[i]]++;
for(int j=mex[i-1];j<=n;j++)
if(t[j]==0)
{
mex[i]=j;
break;
}
}
else
mex[i]=mex[i-1];
build(1,1,n);
for(int i=0;i<=n;i++)
pos[i]=n+1;
for(int i=n;i>=1;i--)
if(data[i]<=n)
{
nxt[i]=pos[data[i]];
pos[data[i]]=i;
}
ans+=a[1].sum;
for(int i=1;i<=n;i++)
{
chg(1,i,i,0);
if(data[i]<=a[1].mx)
{
int v=query(1,data[i]);
if(v<=nxt[i]-1)
chg(1,v,nxt[i]-1,data[i]);
}
ans+=a[1].sum;
}
printf("%lld\n",ans);
}
return 0;
}