hdu 4747 Mex 线段树区间更新

本文深入解析MEX概念及其在解决特定问题中的应用,通过实例阐述如何优化线段树区间更新操作,避免常见错误并提高代码效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.cnblogs.com/kuangbin/p/3327674.html

有大神博客在。

mex有两个特征,一个是最小,一个是不在集合中

首先可以看出mex是递增的

删掉一个数a[i]对mex[L, R](R<i)是没有影响的

对L>i的影响是,a[i]不在集合中了,在a[i]下次出现之前,那些大于a[i]的mex[j]都可以被减小至a[i]


话说线段树区间更新每次都写挫

一开始pushdown()没有加上对sumv,maxv的修改

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <map>
using namespace std;
#define INF 1e9
#define maxn 200000
#define rep(i,x,y) for(int i=x;i<=y;i++)
#define mset(x) memset(x,0,sizeof(x))
typedef __int64 ll;

const int maxnode = maxn * 4;

int n;
int a[maxn];
int mex[maxn];
set<int> s;
map<int, int> mp;
int next[maxn];//a[i]下一次出现的位置next[i]

ll sumv[maxnode], setv[maxnode], maxv[maxnode];
void init(){
    mset(sumv);
    mset(mex);
    mset(maxv);
    memset(setv, -1, sizeof(setv));
    s.clear();
    mp.clear();
    rep(i,1,n) next[i] = n+1;
}

void pushup(int o){
    int lc = 2*o, rc = 2*o+1;
    sumv[o] = sumv[lc]+sumv[rc];
    maxv[o] = max(maxv[lc], maxv[rc]);
    if(setv[lc] == setv[rc])    setv[o] = setv[lc];
}

void pushdown(int o, int l, int r){
    int lc = 2*o, rc = 2*o+1, mid = l+(r-l)/2;
    if(setv[o]>=0){
        setv[lc] = setv[rc] = setv[o];
        sumv[lc] = (mid-l+1)*setv[o];
        sumv[rc] = (r-mid)*setv[o];
        maxv[lc] = maxv[rc] = setv[o];
        setv[o] = -1;
    }
}

void build(int o, int l, int r){
    if(l==r){
        sumv[o] = mex[l];
        maxv[o] = mex[l];
        return ;
    }
    int mid = l+(r-l)/2, lc = 2*o, rc = 2*o+1;
    build(lc, l, mid);
    build(rc, mid+1, r);
    pushup(o);
}

int x1, x2, v;
void update(int o, int l, int r){
    if(x1<=l && x2>=r){
        setv[o] = v;
        sumv[o] = (r-l+1)*v;
        maxv[o] = v;
        return;
    }
    int mid = l+(r-l)/2, lc = 2*o, rc = 2*o+1;
    pushdown(o, l, r);
    if(x1<=mid) update(lc, l, mid);
    if(x2>mid)  update(rc, mid+1, r);
    pushup(o);
}

//找mex[j]>=a[i]的第一个数j
//由于mex是递增的,可以二分查找
//线段树上进行二分查找可以用maxv标记
int res;
void query(int o, int l, int r){
    if(l==r){
        res = l;
        return ;
    }
    int mid = l+(r-l)/2, lc = 2*o, rc = 2*o+1;
    pushdown(o, l, r);
    if(maxv[lc]>v) query(lc, l, mid);
    else query(rc, mid+1, r);
}

int main(){
 //  freopen("a.txt","r",stdin);
//    freopen(".out","w",stdout);
    while(cin>>n, n){
        init();
        rep(i,1,n)  scanf("%d", &a[i]);
        
        rep(i,1,n){
            mex[i] = mex[i-1];//根据单调递增
            s.insert(a[i]);
            while(s.find(mex[i])!=s.end())  mex[i]++;
        }
        build(1,1,n);
        
        rep(i,1,n){
            if(mp.find(a[i])==mp.end())  mp.insert(make_pair(a[i], i));
            else{
                next[mp[a[i]]] = i;
                mp[a[i]] = i;
            }
        }

        ll ans=0;
        rep(i,1,n){ 
            ans += sumv[1];//取线段树根节点
            //printf("%d ", sumv[1]);
            if(maxv[1] > a[i]){///
                v = a[i], query(1,1,n);
                x1 = res, x2 = next[i]-1, v = a[i];
                //printf("%d %d %d\n", x1, x2, v);
                if(x1<=x2)  update(1,1,n);
            }
            x1=i, x2=i, v=0, update(1, 1, n);
        }
        printf("%I64d\n", ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值