zoukankan      html  css  js  c++  java
  • HDU 4747 Mex

    Time Limit: 5000MS   Memory Limit: 65535KB   64bit IO Format: %I64d & %I64u

    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.

    Source

    首先O(n)复杂度求出 mex(1,i),可以知道 mex(i,i),mex(i,i+1)到 mex(i,n)是递增的。
    然后使用线段树维护,需要不断删除前面的数。
    若删掉第一个数 a[1],那么在下一个 a[1]出现前的大于 a[1]的 mex 值都要变成 a[1]
    然后需要线段树实现区间修改和区间求和。

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<cstdio>
     6 #define ls l,mid,rt<<1
     7 #define rs mid+1,r,rt<<1|1
     8 #define warning keep silent
     9 using namespace std;
    10 const int mxn=200020;
    11 struct node{
    12     long long sum;
    13     int max;
    14     int mark;
    15 }t[mxn<<2];
    16 int a[mxn];
    17 int n;
    18 long long ans=0;
    19 int cnt[mxn];
    20 int mex[mxn],pos[mxn];
    21 int nxt[mxn];
    22 void pushdown(int l,int r,int rt){
    23     if(t[rt].mark!=-1){
    24         t[rt<<1].mark=t[rt<<1|1].mark=t[rt].mark;
    25         t[rt<<1].max=t[rt<<1|1].max=t[rt].mark;
    26         int mid=(l+r)>>1;
    27         t[rt<<1].sum=(long long)t[rt].mark*(mid-l+1);
    28         t[rt<<1|1].sum=(long long)t[rt].mark*(r-mid);
    29         t[rt].mark=-1;
    30     }
    31     return;
    32 }
    33 void Build(int l,int r,int rt){
    34     t[rt].mark=-1;
    35     if(l==r){
    36         t[rt].max=t[rt].sum=mex[l];
    37         return;
    38     }
    39     int mid=(l+r)>>1;
    40     Build(ls);Build(rs);
    41     t[rt].sum=t[rt<<1].sum+t[rt<<1|1].sum;
    42     t[rt].max=max(t[rt<<1].max,t[rt<<1|1].max);
    43     return;
    44 }
    45 void change(int v,int L,int R,int l,int r,int rt){
    46     if(L<=l && r<=R){
    47         t[rt].mark=v;
    48         t[rt].max=v;
    49         t[rt].sum=(long long)v*(r-l+1);
    50         return;
    51     }
    52     pushdown(l,r,rt);
    53     int mid=(l+r)>>1;
    54     if(L<=mid)change(v,L,R,ls);
    55     if(R>mid)change(v,L,R,rs);
    56     t[rt].sum=t[rt<<1].sum+t[rt<<1|1].sum;
    57     t[rt].max=max(t[rt<<1].max,t[rt<<1|1].max);
    58     return;
    59 }
    60 int posi(int v,int l,int r,int rt){
    61     if(l==r)return l;
    62     int mid=(l+r)>>1;
    63     pushdown(l,r,rt);
    64     if(t[rt<<1].max>v)return posi(v,ls);
    65     else return posi(v,rs);
    66 }
    67 int main(){
    68     while(scanf("%d",&n) && n){
    69         memset(t,0,sizeof t);
    70         memset(cnt,0,sizeof cnt);
    71         int i,j;
    72         for(i=1;i<=n;i++){scanf("%d",&a[i]);if(a[i]>n)a[i]=n;};
    73         int last=0;
    74         for(i=1;i<=n;i++){
    75             cnt[a[i]]=1;
    76             while(cnt[last])last++;
    77             mex[i]=last;
    78         }
    79         for(i=0;i<=n;i++)pos[i]=n+1;//
    80         for(i=n;i;i--){//预处理,找到相同值的下一个出现位置
    81             nxt[i]=pos[a[i]];
    82             pos[a[i]]=i;
    83         }
    84         Build(1,n,1);
    85         ans=0;
    86         for(i=1;i<=n;i++){
    87             ans+=t[1].sum;
    88             change(0,i,i,1,n,1);
    89             if(a[i]<t[1].max){
    90                 int l=posi(a[i],1,n,1);
    91                 int r=nxt[i]-1;
    92                 if(l<=r)change(a[i],l,r,1,n,1);
    93             }
    94         }
    95         printf("%lld
    ",ans);
    96     }
    97     return 0;
    98 }
  • 相关阅读:
    GitHub 实现多人协同提交代码并且权限分组管理
    前端第一篇文章-http标准
    介绍Android电量
    音频编码格式
    PPT
    Word
    HTML 之 JavaScript
    HTML 之 CSS
    HTML 之 HTTP 协议(请求协议以及响应协议)
    HTML 之 标签
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5793860.html
Copyright © 2011-2022 走看看