Mex
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1892 Accepted Submission(s): 625
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 24
题意:求所有区间mex的总和,定义mex[i,j]为区间[i,j]没有出现过的最小的数
思路:比较好的线段树题
先预处理出mex[1,i] (1=<i<=n)
然后预处理出每个位置 i 的下一个位置 next[i] ,假设现在位置的数为x,那么规定它的下一个位置为它后面离它最近的x的位置
然后 i 从1-n开始搞,每次删除i位置的数,然后在[ i+1,next[i]-1 ]这些位置找到满足位置p,且p位置的mex[i,p]值>value[i]
那么后面到next[i]-1的所有区间的mex都是大于value[i]的,因为mex满足单调性
然后就将mex[ p,next[i]-1 ]区间的值修改为value[i]
最后区间[i+1,n]求和就是以i+1为左端点,i+2~n为右端点的所有区间的mex和
涉及到的操作,区间更新,区间求和,区间求最大
#include
#include
#include
#include
#include
using namespace std; #define maxn 200010 #define lson l,m,o<<1 #define rson m+1,r,o<<1|1 typedef __int64 ll; ll sum[maxn<<2]; ll f[maxn<<2]; int mx[maxn<<2]; int n; int a[maxn]; int mex[maxn]; int ha[maxn]; int next[maxn]; struct node{ int v,p; }vex[maxn]; bool cmp(node a,node b) { if(a.v==b.v)return a.p
y?x:y; } void pushup(int o) { sum[o]=sum[o<<1]+sum[o<<1|1]; mx[o]=maxi(mx[o<<1],mx[o<<1|1]); } void pushdown(int o,int l) { if(f[o]) { f[o<<1]=1; f[o<<1|1]=1; sum[o<<1]=mx[o]*(l-(l>>1)); sum[o<<1|1]=mx[o]*(l>>1); mx[o<<1]=mx[o]; mx[o<<1|1]=mx[o]; f[o]=0; } } void build(int l,int r,int o) { f[o]=0; if(l==r){ sum[o]=mex[l]; mx[o]=mex[l]; return; } int m=l+r>>1; build(lson); build(rson); pushup(o); } void update(int l,int r,int o,int L,int R,int v) { if(L<=l&&r<=R){ sum[o]=(ll)v*(r-l+1); mx[o]=v; f[o]=1; return; } pushdown(o,r-l+1); int m=l+r>>1; if(L<=m)update(lson,L,R,v); if(m
>1; int ans; if(mx[o<<1]>v)ans=querymx(lson,v); else ans=querymx(rson,v); pushup(o); return ans; } int main() { int i,j; while(scanf("%d",&n)!=EOF) { if(!n)break; int cnt=0; memset(ha,0,sizeof(ha)); for(i=1;i<=n;i++){ scanf("%d",&a[i]); vex[i].v=a[i]; vex[i].p=i; for(j=cnt;j
a[i]){ int l=querymx(1,n,1,a[i]); if(l