蒟蒻8pts求助……调了两天了
查看原帖
蒟蒻8pts求助……调了两天了
234282
鸽子爱咕咕楼主2022/11/13 00:26

一些变量的解释:

  1. emp:emp: 该区间是否(1/0)全为空房

  2. update()update() 函数中的参数 v:v: 将该区间全部改为可入住(1)或不可入住(0)

#include <iostream>
#include <cstdio>
using namespace std;
const int N=(int)5e4+10;

int n,m;

struct segtree{
    int l,r,v,suml,sumr;//v:该区间内最长的连续空房区间长度
    //suml/sumr:该区间内从左/从右数的最大连续空房区间长度
    int tag;
    bool emp;//该区间是否全为空房
}t[N<<2];

inline int max(int a,int b){return (a>b)? a: b;}
inline int max3(int a,int b,int c){return max(max(a,b),c);}

inline void pushup(int p){
    t[p].emp = t[p<<1].emp & t[p<<1|1].emp;
    t[p].v = max3(t[p<<1].v, t[p<<1|1].v, t[p<<1].sumr+t[p<<1|1].suml);
    t[p].suml = (t[p<<1].emp)? t[p<<1].v+t[p<<1|1].suml: t[p<<1].suml;
    t[p].sumr = (t[p<<1|1].emp)? t[p<<1|1].v+t[p<<1].sumr: t[p<<1|1].sumr;
}

void build(int l,int r,int p){
    t[p].l=l,t[p].r=r,t[p].tag=-1;
    if(l==r){t[p].emp=t[p].v=t[p].suml=t[p].sumr=1;return;}
    int mid=(l+r)>>1;
    build(l,mid,p<<1);build(mid+1,r,p<<1|1);
    pushup(p);
}

inline void work_tag(int p,int v){
    t[p].emp=(bool)v;
    if(v) t[p].v=t[p].suml=t[p].sumr=(t[p].r-t[p].l+1);
    else t[p].v=t[p].suml=t[p].sumr=0;
    t[p].tag=v;
}

inline void pushdown(int p){
    if(t[p].tag!=-1){
        work_tag(p<<1,t[p].tag);work_tag(p<<1|1,t[p].tag);
        t[p].tag=-1;
    }
}

inline void update(int l,int r,bool v,int p){
    if(l<=t[p].l && t[p].r<=r){work_tag(p,v);return;}
    int mid=(t[p].l+t[p].r)>>1;
    pushdown(p);
    if(l<=mid) update(l,r,v,p<<1);
    if(r>mid) update(l,r,v,p<<1|1);
    pushup(p);
}

inline int query(int x,int p){
    if(t[p].l==t[p].r) return t[p].l;
    int mid=(t[p].l+t[p].r)>>1;
    pushdown(p);
    if(t[p<<1].suml>=x) return query(x,p<<1);
    else if(t[p<<1].sumr+t[p<<1|1].suml>=x) return mid-t[p<<1].sumr+1;
    else return query(x,p<<1|1);
}

inline int read(){
	int x=0;
	char c=getchar();
	while(c<'0' || c>'9') c=getchar();
	while(c>='0' && c<='9'){x=(x<<3)+(x<<1)+(c^48);c=getchar();}
	return x;
}

int main(){
    freopen("2894.in","r",stdin);
    freopen("2894.out","w",stdout);
    n=read(),m=read();
    build(1,n,1);
    for(int i=1;i<=m;++i){
        int type=read();
        if(type==1){
            int x=read();
            if(t[1].v<x){cout<<0<<endl;continue;}
            int ans=query(x,1);
            printf("%d\n",ans);
            if(ans) update(ans,x+ans-1,0,1);
        } else {
            int l=read(),r=read();
            update(l,l+r-1,1,1);
        }
    }
    return 0;
}

调疯了 不知道哪里有问题 感谢大佬

2022/11/13 00:26
加载中...