Luo Valley 2894

#include <iostream>
#include <cstdio>

#define ls(o) o<<1
#define rs(o) o<<1|1

using namespace std;

const int wx=50017;

inline int read(){
    int sum=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0'; ch=getchar();}
    return sum*f;
}

int n,m;

struct SGT{
    int l,r,tag,lsum,rsum,sum;
    #define tag(o) t[o].tag
    #define lsum(o) t[o].lsum
    #define rsum(o) t[o].rsum
    #define sum(o) t[o].sum
}t[wx*4];

void up(int o){
    if(sum(ls(o))==t[ls(o)].r-t[ls(o)].l+1)lsum(o)=sum(ls(o))+lsum(rs(o));
    else lsum(o)=lsum(ls(o));
    if(sum(rs(o))==t[rs(o)].r-t[rs(o)].l+1)rsum(o)=sum(rs(o))+rsum(ls(o));
    else rsum(o)=rsum(rs(o));
    sum(o)=max(max(sum(ls(o)),sum(rs(o))),rsum(ls(o))+lsum(rs(o)));
}

void down(int o){
    if(tag(o)==0){
        sum(ls(o))=lsum(ls(o))=rsum(ls(o))=t[ls(o)].r-t[ls(o)].l+1;
        sum(rs(o))=lsum(rs(o))=rsum(rs(o))=t[rs(o)].r-t[rs(o)].l+1;
        tag(ls(o))=tag(rs(o))=tag(o);
        tag(o)=-1;
    }
    if(tag(o)==1){
        sum(ls(o))=lsum(ls(o))=rsum(ls(o))=0;
        sum(rs(o))=lsum(rs(o))=rsum(rs(o))=0;
        tag(ls(o))=tag(rs(o))=tag(o);
        tag(o)=-1;
    }
}

void build(int o,int l,int r){
    t[o].l=l; t[o].r=r; tag(o)=-1;
    if(l==r){sum(o)=lsum(o)=rsum(o)=1; return ;}
    int mid=t[o].l+t[o].r>>1;
    if(l<=mid)build(ls(o),l,mid);
    if(r>mid)build(rs(o),mid+1,r);
    up(o);
}

void update(int o,int l,int r,int k){
    if(l<=t[o].l&&t[o].r<=r){
        if(!k){
            sum(o)=lsum(o)=rsum(o)=t[o].r-t[o].l+1;
            tag(o)=k;
        }
        else{
            sum(o)=lsum(o)=rsum(o)=0;
            tag(o)=k;
        } 
        return ;
    }
    down(o);
    int mid=t[o].l+t[o].r>>1;
    if(l<=mid)update(ls(o),l,r,k);
    if(r>mid)update(rs(o),l,r,to);
    up(o);
}

int query(int o,int l,int r,int len){
    if(l==r)return l;
    down(o);
    if(t[ls(o)].sum>=len)return query(ls(o),l,r,len);
    else if(t[ls(o)].rsum+t[rs(o)].lsum>=len)return t[ls(o)].r-rsum(ls(o))+1;
    else return query(rs(o),l,r,len);
}

int main(){
    n=read(); m=read(); build(1,1,n);
    for(int i=1;i<=m;i++){
        int opt;
        opt=read();
        if(opt==1){
            int x;
            x=read(); 
            if(sum(1)<x){
                puts("0");
                continue;
            }
            int tmp=query(1,1,n,x);
            printf("%d\n",tmp);
            update(1,tmp,tmp+x-1,1);
        }
        else{
            int x,y;
            x=read(); y=read();
            update(1,x,x+y-1,0);
        }
    }
    return 0;
}

  A copy of the code on the Internet, but unfortunately, that is, I was not familiar with the tree for this segment, only three numbers actually maintained, from the left spaces long, long, and the total length of the middle of the right space, I thought enough clear, so write to, this is normal, right

Guess you like

Origin www.cnblogs.com/coolwx/p/11639110.html