非常好的线段树题。。。。此题必定会火。。。。。

Mex

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 955    Accepted Submission(s): 320

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 10
 

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
 

Recommend
liuyiding
 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

using namespace std;

const int maxn=200005;

typedef long long int LL;

int a[maxn],mex[maxn],next[maxn],mx[maxn<<2],sig[maxn<<2],cnt;
LL sum[maxn<<2];
bool vis[maxn];

void pushUP(int rt)
{
    mx[rt]=max(mx[rt<<1],mx[rt<<1|1]);
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}

void pushDOWN(int rt,int len)
{
    if(~sig[rt])
    {
        sig[rt<<1]=sig[rt<<1|1]=sig[rt];
        mx[rt<<1]=mx[rt<<1|1]=sig[rt];
        sum[rt<<1]=sig[rt]*(len-len>>1);
        sum[rt<<1|1]=sig[rt]*(len>>1);
        sig[rt]=-1;
    }
}

void build(int l,int r,int rt)
{
    if(l==r)
    {
        mx[rt]=mex[cnt];
        sum[rt]=mex[cnt++];
        return ;
    }
    int m=(r+l)>>1;
    build(lson);
    build(rson);
    pushUP(rt);
}

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(R>m) ret+=query(L,R,rson);
    return ret;
}

int getID(int L,int R,int v,int l,int r,int rt)
{
    if(l==r)
        return l;
    pushDOWN(rt,r-l+1);
    int m=(r+l)>>1;
    if(L<=m&&mx[rt<<1]>v) return getID(L,R,v,lson);
    else if(R>m&&mx[rt<<1|1]>v) return getID(L,R,v,rson);
    return R+1;
}

void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        sig[rt]=c;
        mx[rt]=c;
        sum[rt]=c*(r-l+1);
        return ;
    }
    pushDOWN(rt,r-l+1);
    int m=(r+l)>>1;
    if(L<=m) update(L,R,c,lson);
    if(R>m) update(L,R,c,rson);
    pushUP(rt);
}

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF&&n)
    {
        for(int i=1;i<=n;i++)
            scanf("%d",a+i);
        memset(vis,false,sizeof(vis));
        mex[1]=0;
        if(a[1]<n) vis[a[1]]=true;
        while(vis[mex[1]]) mex[1]++;
        for(int i=2;i<=n;i++)
        {
            if(a<n) vis[a]=true;
            mex=mex[i-1];
            while(vis[mex]) mex++;
        }
        map<int,int> mII;
        for(int i=n;i>=1;i--)
        {
            if(mII.find(a)==mII.end()) next=n+1;
            else next=mII[a];
            mII[a]=i;
        }
        cnt=1;LL ans=0;
        memset(mx,0,sizeof(mx));
        memset(sum,0,sizeof(sum));
        memset(sig,-1,sizeof(sig));
        build(1,n,1);
        for(int i=1;i<=n;i++)
        {
            ans+=query(i,n,1,n,1);
            int st=getID(i+1,next-1,a,1,n,1);
            int ed=next-1;
            if(st<=ed) update(st,ed,a,1,n,1);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

HDOJ 4747 Mex的更多相关文章

  1. HDU 4747 Mex 递推/线段树

    题目链接: acm.hdu.edu.cn/showproblem.php?pid=4747 Mex Time Limit: 15000/5000 MS (Java/Others)Memory Limi ...

  2. 【HDU 4747 Mex】线段数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4747 题意:有一组序列a[i](1<=i<=N), 让你求所有的mex(l,r), mex ...

  3. [HDU 4747] Mex (线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4747 这道题是我去年刚入校队的时候参加网赛的题目. 一年过去了,我依然还是不会做.. 这是我难题计划的 ...

  4. HDU 4747 Mex(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4747 题意:给出一个数列A.计算所有的mex(i,j)之和.1<=i<=j<=n. ...

  5. hdu 4747 Mex (2013 ACM/ICPC Asia Regional Hangzhou Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4747 思路: 比赛打得太菜了,不想写....线段树莽一下 实现代码: #include<iost ...

  6. hdu 4747 mex 线段树+思维

    http://acm.hdu.edu.cn/showproblem.php?pid=4747 题意: 我们定义mex(l,r)表示一个序列a[l]....a[r]中没有出现过得最小的非负整数, 然后我 ...

  7. hdu 4747 Mex

    http://acm.hdu.edu.cn/showproblem.php?pid=4747 设我们输入的数组为 a[],我们需要从 1 到 n 遍历, 假设遍历到 i 时, 遍历的过程中用b[j]表 ...

  8. HDU 4747 Mex (2013杭州网络赛1010题,线段树)

    Mex Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  9. hdu 4747 Mex( 线段树? 不,区间处理就行(dp?))

    Mex Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

随机推荐

  1. RNN 入门教程 Part 3 – 介绍 BPTT 算法和梯度消失问题

    转载 - Recurrent Neural Networks Tutorial, Part 3 – Backpropagation Through Time and Vanishing Gradien ...

  2. easyUI的控件

    CSS类定义: div easyui-window                               window窗口样式 属性如下: 1)       modal:是否生成模态窗口.tru ...

  3. jQuery Ajax(异步请求)

    jQuery异步请求 原始的异步请求是需要创建的 XMLHttpRequest 对象.(IE5,6不支持)目前很多浏览器都支持XMLHttpRequest对象 jQuery ajax常用的回调函数:b ...

  4. Java工厂设计模式

    程序在接口和子类之间加入一个过渡类,通过此过渡类端取得接口的实例化对象,一般都会称这个过渡端为工厂类 //=============================================== ...

  5. Tomcat web.xml中定义了文件扩展名到MIME类型的对应关系

    Tomcat在返回静态资源时,会根据资源文件的扩展名产生对应的content-type头(也即MIME类型)添加到response header中. 在Tomcat的web.xml规定了扩展名及相应M ...

  6. 《CSS3实战》读书笔记 第4章:样式继承

    节省你的时间--样式继承 ### 什么是继承? 后代元素可以继承先代元素的一些属性.有了它,可以省去分别定义样式的时间.继承了方向是由外而内的. ### 继承的局限性 应该注意到,有些属性是不适宜继承 ...

  7. Winsock 入门 计算校验和 示例

    #include <stdio.h> #include <string.h> #define DATA_MAX_LEN 14 /* 最大数据长度 */ struct data_ ...

  8. thinkphp查询

    public function index(){ $result = M('content')->select() $this->assig('result',$result); $thi ...

  9. union联合体使用详解

    1.联合体联合体(union)与结构体(struct)有一些相似之处.但两者有本质上的不同.在结构体中,各成员有各自的内存空间, 一个结构变量的总长度是各成员长度之和.而在联合体中,各成员共享一段内存 ...

  10. 快速的mysql导入导出数据(load data和outfile)

    1.load data: ***实际应用:把日志生成的xls文件load到MySQL中: mysql_cmd = "iconv -c -f utf-8 -t gbk ./data/al_ve ...