您的位置:首页 > 其它

poj3667 Hotel(线段树)

2017-08-11 23:11 344 查看
维护最大连续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+
4000
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: