hdu 4747 线段树

http://acm.hdu.edu.cn/showproblem.php?pid=4747

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.
/**
hdu 4747 线段树
这道题我搞了两天了,看着大神的代码终于A了,真是没有心情写总结了
http://www.cnblogs.com/kuangbin/p/3327674.html
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
typedef long long LL;
const int maxn= 200005;

int n;
int a[maxn];
int mex[maxn];///表示区间[1,i]的mex值
int Next[maxn];///表示下个a[i]值出现的位置
map<int,int>mp;

struct note
{
    int l,r,mx,lazy;
    LL sum;
} tree[maxn*4];

void update_same(int i,int v)
{
    tree[i].sum=(LL)v*(tree[i].r-tree[i].l+1);
    tree[i].mx=v;
    tree[i].lazy=1;
}

void push_up(int i)
{
    if(tree[i].l==tree[i].r) return;
    tree[i].mx=max(tree[i<<1].mx,tree[i<<1|1].mx);
    tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
}

void push_down(int i)
{
    if(tree[i].l==tree[i].r)return;
    if(tree[i].lazy)
    {
       update_same(i<<1,tree[i].mx);
       update_same(i<<1|1,tree[i].mx);
       tree[i].lazy=0;
    }
}

void build(int i,int l,int r)
{
    tree[i].l=l;
    tree[i].r=r;
    tree[i].lazy=0;
    if(l==r)
    {
        tree[i].mx=mex[l];
        tree[i].sum=mex[l];
        return;
    }
    int mid=(l+r)/2;
    build(i<<1,l,mid);
    build(i<<1|1,mid+1,r);
    push_up(i);
}

void update(int i,int l,int r,int v)
{
    if(l<=tree[i].l&&r>=tree[i].r)
    {
        update_same(i,v);
        return;
    }
    push_down(i);
    int mid=(tree[i].l+tree[i].r)/2;
    if(r<=mid)
    {
        update(i<<1,l,r,v);
    }
    else if(l>mid)
    {
        update(i<<1|1,l,r,v);
    }
    else
    {
        update(i<<1,l,mid,v);
        update(i<<1|1,mid+1,r,v);
    }
    push_up(i);
}
int Get_min_mx(int i,int v)
{
    if(tree[i].l==tree[i].r)
        return tree[i].l;
    if(tree[i<<1].mx>v)
        return Get_min_mx(i<<1,v);
    return Get_min_mx(i<<1|1,v);
}

int main()
{
    while(~scanf("%d",&n))
    {
        if(n==0)break;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
        }
        mp.clear();
        int tmp=0;
        for(int i=1; i<=n; i++)
        {
            mp[a[i]]=1;
            while(mp.find(tmp)!=mp.end())tmp++;
            mex[i]=tmp;
        }/**
        for(int i=1;i<=n;i++)
        {
            printf(i==n?"%d\n":"%d ",mex[i]);
        }*/
        mp.clear();
        for(int i=n; i>=1; i--)
        {
            if(mp.find(a[i])==mp.end())Next[i]=n+1;
            else Next[i]=mp[a[i]];
            mp[a[i]]=i;
        }/**
        for(int i=1;i<=n;i++)
        {
            printf(i==n?"%d\n":"%d ",Next[i]);
        }*/
        build(1,1,n);
        LL sum=0;
        for(int i=1; i<=n; i++)
        {
            sum+=tree[1].sum;
            if(tree[1].mx>a[i])
            {
                int l=Get_min_mx(1,a[i]);
                int r=Next[i];
                if(l<r)
                    update(1,l,r-1,a[i]);
            }
            update(1,i,i,0);
        }
        printf("%I64d\n",sum);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值