除了上次的新学的有 区间更新 延迟更新 区间合并
先说下区间更新以及延迟更新吧
既然是对区间的维护 在求解一些问题的时候 有的时候没有必要对所有的自区间都进行遍历 这个时候 延迟标记就派上用场了 (只有在需要的时候才对子区间更新)
struct node()
{
int l,r,len;
int flag=0;
}stu[maxn];
void pushdown(int i)
{
if(stu[i].flag)
{
stu[i*2].flag=stu[i*2+1].flag=stu[i].flag;
/// 还需要根据题目的意思 做一些处理
}
}
void updata(int l,int r,int i)//l , r 为需要查找区间的范围 x y 为目的区间
{
if(x<=l&&r<=y)//找到对应的区间
{
return;
}
int mid=(l+r)/2;
pushdown(i);
if(x<=mid) updata(l,mid,i*2);
if(y>mid) updata(mid+1,r,i*2+1);
}
poj hotel
#include<iostream>
#define maxn 50010
using namespace std;
int n,m,a,b;
int cmd;
struct stu
{
int r,l;
int flag;
int tlen,llen,rlen;
int up()
{
tlen=llen=rlen=(flag? 0:r-l+1);
}
};
stu mapp[maxn*4];
void build(int l,int r,int count)
{
mapp[count].l=l;
mapp[count].r=r;
mapp[count].tlen=mapp[count].llen=mapp[count].rlen=r-1+1;
mapp[count].flag=0;
if(l==r) return;
int mid=(l+r)/2;
build(l,mid,count*2);
build(mid+1,r,count*2+1);
}
void push(int count) //延迟更新
{
if(mapp[count].flag!=-1)
{
mapp[count*2].flag=mapp[count*2+1].flag=mapp[count].flag;
mapp[count].flag=-1;
mapp[count*2].up();
mapp[count*2+1].up();
}
}
int que(int l,int r,int count)
{
if(mapp[count].l==mapp[count].r&&mapp[count].tlen) return mapp[count].l;
push(count);
if(mapp[count*2].tlen>=a) return que(l,r,count*2);
else if(mapp[count*2].rlen+mapp[count*2+1].llen>=a)
{
return mapp[count*2].r-mapp[count*2].rlen+1;
}
else if(mapp[count*2+1].tlen>=a) return que(l,r,count*2+1);
else return 0;
}
void updata(int l,int r,int v,int count)
{
if(mapp[count].l==l&&mapp[count].r==r)
{
mapp[count].flag=v;
mapp[count].up();
return;
}
push(count);
int mid=(mapp[count].l+mapp[count].r)/2;
if(r<=mid) updata(l,r,v,count*2);
else if(l>=mid+1) updata(l,r,v,count*2+1);
else
{
updata(l,mid,v,count*2);
updata(mid+1,r,v,count*2+1);
}
int tmp=max(mapp[count*2].tlen,mapp[count*2+1].tlen); // 后面为pushup函数
mapp[count].tlen=max(tmp,mapp[count*2].rlen+mapp[count*2+1].llen);
mapp[count].llen=mapp[count*2].llen;
mapp[count].rlen=mapp[count*2+1].rlen;
if(mapp[count*2].tlen==(mapp[count*2].r-mapp[count*2].l+1))
{
mapp[count].llen+=mapp[count*2+1].llen;
}
if(mapp[count*2+1].tlen==(mapp[count*2+1].r-mapp[count*2+1].l+1))
{
mapp[count].rlen+=mapp[count*2].rlen;
}
}
int main()
{
cin.sync_with_stdio(false);
while(cin>>n>>m)
{
build(1,n,1);
for(int i=0;i<m;i++)
{
cin>>cmd;
if(cmd==1)
{
cin>>a;
int ans=que(1,n,1);
cout<<ans<<endl;
if(ans) updata(ans,ans+a-1,1,1);
}
else
{
cin>>a>>b;
updata(a,a+b-1,0,1);
}
}
}
return 0;
}