poj3667 Hotel(线段树)

维护最大连续0的个数+区间修改打标记+查询最左连续0个数为k。

#include<cstdio>
#include<cstring>
int const N=50005;
int n,m;
inline int read(){
    int x=0;char ch=getchar();
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x;
}
inline int max(int x,int y){return x>y?x:y;}
struct node{
    int l,r,ls,rs,s,cover;
    node(){
        cover=0;
    }
}tree[N<<2];
void pushup(int p){
    int mid=tree[p].l+tree[p].r>>1;
    tree[p].ls=tree[p<<1].ls;
    if(tree[p<<1].s==mid-l+1) tree[p].ls+=tree[p<<1|1].ls;
    tree[p].rs=tree[p<<1|1].rs;
    if(tree[p<<1|1].rs==r-mid) tree[p].rs+=tree[p<<1].rs;
    tree[p].s=max(tree[p<<1].s,tree[p<<1|1].s);
    tree[p].s=max(tree[p].s,tree[p<<1].rs+tree[p<<1|1].ls);
}
void build(int p,int l,int r){
    tree[p].l=l,tree[p].r=r;
    if(l==r){
        tree[p].ls=tree[p].rs=tree[p].s=1;return;
    }
    int mid=(l+r)>>1;
    build(p<<1,l,mid);build(p<<1|1,mid+1,r);
    tree[p].ls=tree[p].rs=tree[p].s=tree[p<<1].s+tree[p<<1|1].s;
}
void pushdown(int p){
    if(tree[p].cover==-1) return;
    tree[p<<1].cover=tree[p<<1|1].cover=tree[p].cover;
    tree[p<<1].ls=tree[p<<1].rs=tree[p<<1].s=tree[p].cover?0:tree[p<<1].r-tree[p<<1].l+1;
    tree[p<<1|1].ls=tree[p<<1|1].rs=tree[p<<1|1].s=tree[p].cover?0:tree[p<<1|1].r-tree[p<<1|1].l+1;
    tree[p].cover=-1;
}
int query(int p,int x){
    int l=tree[p].l,r=tree[p].r;
    if(l==r) return l;
    pushdown(p);
    int mid=(l+r)>>1;
    if(tree[p<<1].s>=x) return query(p<<1,x);
    if(tree[p<<1].rs+tree[p<<1|1].ls>=x) return mid-tree[p<<1].rs+1;
    return query(p<<1|1,x);
}
void change(int p,int x,int y,int v){
    int l=tree[p].l,r=tree[p].r;
    if(x<=l&&r<=y){
        tree[p].cover=v;
        tree[p].s=tree[p].ls=tree[p].rs= v?0:r-l+1;return;
    }
    pushdown(p);
    int mid=(l+r)>>1;
    if(x<=mid) change(p<<1,x,y,v);
    if(y>mid) change(p<<1|1,x,y,v);
    pushup(p);
}
int main(){
//  freopen("a.in","r",stdin);
    n=read();m=read();
    build(1,1,n);
    while(m--){
        int op,x,y;
        op=read();x=read();
        if(op==1){
            if(tree[1].s<x) puts("0");
            else{
                int ans=query(1,x);
                change(1,ans,ans+x-1,1);
                printf("%d\n",ans);
            }
        }
        else{y=read();change(1,x,x+y-1,0);} 
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值