https://www.luogu.com.cn/problem/P2894
火山经营着一个停车场,假设的停车场有N车位(编号1-N)都在一条线上,最初所有车位都没停车。经常有人来定车位,
他要定连续的k(1 ≤ k ≤ N)个车位。火山要确定是否能够满足客人的要求,如果能,他要将这k个连续的车位安排在编
号最小的地方停下。若不能,则客人不停在火山的停车场。在某一时间,有些车会被车主开走了。火山的停车场很大,
火山想让学弟学妹写个程序帮助他。
Input
第1行输入 N 和 M。N是车位个数,M是(停车和开走车操作的总次数)
接下来M行,先输入操作类型(1或2)
若是1,表示有人来停车,再输入k
若是2,再输入l,r, 表示区间[l,l+r] 的车被开走了。
N (1 ≤ N ≤ 50,000) M (1 ≤ M < 50,000)
Output
当输入为1时,若火山的停车场有连续的k个车位,那么输出第一辆车停的最小的编号,否则输出0。
Sample Input
10 6 1 3 1 3 1 3 1 3 2 5 5 1 6
Sample Output
1 4 7 0 5
思路:线段树区间合并的板子。维护最长连续的1.找了老久的bug是push_up的num的最大值维护写错了。应该是左儿子的最大,右儿子的最大再加上中间的一段连续的。类似最大连续子段和。同时注意如果打的tag,如果tag=0是有意义的,那么push_down的时候用没有意义的标志,比如tag!=-1
启发:线段树debug的时候可以每次输出全部的叶子节点,会有一定帮助,但是真的维护错了=.=还是要找挺久的。
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=5e4+1000;
typedef int LL;
struct Tree{
LL l,r,pre,suf,sum,tag;//pre表示前缀最长连续1,suf表示后缀最长连续1,sum表示该区间的最长连续1
}tree[maxn*4];
LL rmax(LL A,LL B,LL C){
return max(A,max(B,C));
}
void push_up(LL p){
tree[p].pre=tree[p*2].pre;
tree[p].suf=tree[p*2+1].suf;
tree[p].sum=rmax(tree[p*2].sum,tree[p*2+1].sum,tree[p*2].suf+tree[p*2+1].pre);
if(tree[p*2].pre==tree[p*2].r-tree[p*2].l+1) tree[p].pre+=tree[p*2+1].pre;
if(tree[p*2+1].suf==tree[p*2+1].r-tree[p*2+1].l+1) tree[p].suf+=tree[p*2].suf;
}
void addtag(LL p,LL d){
tree[p].tag=d;
tree[p].pre=tree[p].suf=tree[p].sum=d?(tree[p].r-tree[p].l+1):0;
}
void push_down(LL p){
if(tree[p].tag!=-1){
addtag(p*2,tree[p].tag);
addtag(p*2+1,tree[p].tag);
tree[p].tag=-1;
}
}
void build(LL p,LL l,LL r){
tree[p].l=l;tree[p].r=r;tree[p].pre=tree[p].suf=tree[p].sum=r-l+1;
tree[p].tag=-1;
if(l==r){return;}
LL mid=(l+r)>>1;
build(p*2,l,mid);build(p*2+1,mid+1,r);
push_up(p);
}
void modify(LL p,LL l,LL r,LL d){
if(l<=tree[p].l&&r>=tree[p].r){
addtag(p,d);
return;
}
push_down(p);
LL mid=(tree[p].l+tree[p].r)>>1;
if(l<=mid) modify(p*2,l,r,d);
if(r>mid) modify(p*2+1,l,r,d);
push_up(p);
}
LL query(LL p,LL l,LL r,LL num){
if(l==r){
return l;
}
push_down(p);
LL mid=(l+r)>>1;
/// if(tree[p].sum>=num){
if(tree[p*2].sum>=num) return query(p*2,l,mid,num);
if(tree[p*2].suf+tree[p*2+1].pre>=num) return mid-tree[p*2].suf+1;
///if(tree[p*2+1].sum>=num)
return query(p*2+1,mid+1,r,num);
/// }
///return pos;
/// return 0;
}
LL debugquery(LL p,LL l,LL r)
{
if(l<=tree[p].l&&r>=tree[p].r){
return tree[p].sum;
}
push_down(p);
LL ans=0;
LL mid=(tree[p].l+tree[p].r)>>1;
if(l<=mid) ans+=debugquery(p*2,l,r);
if(r>mid) ans+=debugquery(p*2+1,l,r);
return ans;
}
int main(void)
{
///cin.tie(0);std::ios::sync_with_stdio(false);
LL n,m;
freopen("in1.in","r",stdin);
freopen("out1.out","w",stdout);
cin>>n>>m;
build(1,1,n);
for(int i=1;i<=m;i++)
{
LL op;cin>>op;
// for(LL i=1;i<=n;i++){
// cout<<debugquery(1,i,i)<<" ";
// }
// cout<<endl;
if(op==1){
LL k;cin>>k;
/// LL pos=query(1,1,n,k);
/// cout<<pos<<endl;
/// if(pos) modify(1,pos,pos+k-1,0);
if(tree[1].sum>=k){
LL pos=query(1,1,n,k);
cout<<pos<<endl;
modify(1,pos,pos+k-1,0);
}
else cout<<"0"<<endl;
}
else{
LL l,r;cin>>l>>r;
modify(1,l,l+r-1,1);
}
// for(LL i=1;i<=n;i++){
// cout<<debugquery(1,i,i)<<" ";
// }
// cout<<endl;
}
return 0;
}