hdu4747(线段树区间更新)

本文介绍了一道关于区间MEX求和的算法题目,利用线段树进行区间更新、查询最大值及区间求和等操作,实现高效解决区间MEX求和问题。

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

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.
 

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

题意:求所有区间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.py?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;ja[i]){

            int l=querymx(1,n,1,a[i]);

            if(l
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值