1. 程式人生 > >洛谷P2894 [USACO08FEB]酒店Hotel_區間更新_區間查詢

洛谷P2894 [USACO08FEB]酒店Hotel_區間更新_區間查詢

Code:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 200000+5;
int n,m,k;
struct Segment_Tree{
    int lazy,left,right,maxv;
}Seg[N<<2];
inline void push_down(int o,int l,int r)
{
    if(Seg[o].lazy == -1 || l==r)return;
    int mid = (l+
r)>>1; int lz = Seg[o].lazy, ls=o<<1, rs=(o<<1)|1; Seg[ls].lazy = Seg[rs].lazy = lz; Seg[ls].maxv = lz==0 ? mid-l+1: 0; Seg[rs].maxv = lz==0 ? r-mid: 0; Seg[
ls].left = Seg[ls].right = Seg[ls].maxv; Seg[rs].left = Seg[rs].right = Seg[rs].maxv; Seg[o].lazy = -1; } inline void update_seg(int o,int l,int r) { int mid = (l+r)>>1, ls=o<<1, rs=(o<<1)|1
; Seg[o].maxv = max(Seg[ls].maxv, Seg[rs].maxv); Seg[o].maxv = max(Seg[o].maxv, Seg[ls].right+Seg[rs].left); Seg[o].left = Seg[ls].maxv == mid-l+1 ? Seg[ls].maxv+Seg[rs].left : Seg[ls].left; Seg[o].right = Seg[rs].maxv == r-mid ? Seg[rs].maxv+Seg[ls].right: Seg[rs].right; } void build_Tree(int l,int r,int o){ if(l>r)return; if(l==r) { Seg[o].left = Seg[o].right = Seg[o].maxv = 1; Seg[o].lazy = -1; return; } int mid = (l+r)>>1, ls=o<<1, rs=(o<<1)|1; build_Tree(l,mid,ls); build_Tree(mid+1,r,rs); update_seg(o,l,r); Seg[o].lazy = -1; } void update(int L,int R,int l,int r,int val,int o){ if(L>R)return; if(L>=l&&R<=r) { Seg[o].lazy = val; Seg[o].maxv = (val==0)?R-L+1: 0; Seg[o].left = Seg[o].right = Seg[o].maxv; return; } push_down(o,L,R); int mid = (L+R)>>1,ls=o<<1, rs=(o<<1)|1; if(l<=mid) update(L,mid,l,r,val,ls); if(r>mid) update(mid+1,R,l,r,val,rs); update_seg(o,L,R); } int query(int o,int l,int r){ int mid=(l+r)>>1, ls=o<<1, rs=(o<<1)|1, ans; push_down(o,l,r); if(Seg[o].left >= k) ans = l; else if(Seg[ls].maxv >=k ) ans = query(ls,l,mid); else if(Seg[ls].right+Seg[rs].left >= k) ans = mid+1-Seg[ls].right; else ans = query( rs ,mid+1,r); update_seg(o,l,r); return ans; } inline int solve_1(){ if(Seg[1].maxv < k)return 0; return query(1,1,n); } int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); scanf("%d%d",&n,&m); build_Tree(1,n,1); while(m--) { int op,x,y; scanf("%d%d",&op,&x); if(op==1) { k=x; int ans=solve_1(); printf("%d\n",ans); if(ans!=0)update(1,n,ans,ans+k-1,1,1); } if(op==2) { scanf("%d",&y); update(1,n,x,x+y-1,0,1); } } return 0; }