hdu4553[线段树维护连续区间] 发表于 2018-04-12 | 分类于 算法 , 数据结构 , 线段树 | 阅读数 次 链接:hdu4553[线段树维护连续区间] 题解 poj3667升级版,注意更新顺序 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495#include<iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<algorithm>#define rep(i,x,y) for(register int i=x;i<=y;++i)#define repd(i,x,y) for(register int i=x;i>=y;--i)#define lson o<<1,l,mid#define rson o<<1|1,mid+1,rusing namespace std;const int N=1e6+7;int llen[2][N<<2],rlen[2][N<<2],mlen[2][N<<2],lz[2][N<<2],n,m,T,a,b;char op[10];inline void pushup(int o,int len ,int t){ llen[t][o]=llen[t][o<<1];rlen[t][o]=rlen[t][o<<1|1]; if(llen[t][o]==len-(len>>1))llen[t][o]+=llen[t][o<<1|1]; if(rlen[t][o]==len>>1)rlen[t][o]+=rlen[t][o<<1]; mlen[t][o]=max(max(mlen[t][o<<1],mlen[t][o<<1|1]),rlen[t][o<<1]+llen[t][o<<1|1]);}inline void pushdown(int o,int len,int t){ if(lz[t][o]==-1)return ; lz[t][o<<1]=lz[t][o<<1|1]=lz[t][o]; llen[t][o<<1]=rlen[t][o<<1]=mlen[t][o<<1]=lz[t][o]?len-(len>>1):0; llen[t][o<<1|1]=rlen[t][o<<1|1]=mlen[t][o<<1|1]=lz[t][o]?len>>1:0; lz[t][o]=-1;}inline void build(int o,int l,int r){ rep(t,0,1)llen[t][o]=rlen[t][o]=mlen[t][o]=r-l+1,lz[t][o]=-1; if(l==r)return ; int mid=l+r>>1; build(lson); build(rson);}inline void update(int o,int l,int r,int L,int R,int x,int t){ if(L<=l&&R>=r){ llen[t][o]=rlen[t][o]=mlen[t][o]=x?(r-l+1):0; lz[t][o]=x; return ; } pushdown(o,r-l+1,t); int mid=l+r>>1; if(L<=mid)update(lson,L,R,x,t); if(R>mid)update(rson,L,R,x,t); pushup(o,r-l+1,t);}inline int query(int o,int l,int r,int L,int R,int x,int t){ if(L==R)return L; pushdown(o,r-l+1,t); int mid=l+r>>1; if(mlen[t][o<<1]>=x)return query(lson,L,mid,x,t); if(rlen[t][o<<1]+llen[t][o<<1|1]>=x)return mid-rlen[t][o<<1]+1; return query(rson,mid+1,R,x,t);}int main(){ scanf("%d",&T); rep(k,1,T){ printf("Case %d:\n",k); scanf("%d%d",&n,&m); build(1,1,n); while(m--){ scanf("%s",op); if(op[0]=='D'){ scanf("%d",&a); if(mlen[0][1]>=a){ printf("%d,let's fly\n",b=query(1,1,n,1,n,a,0)); update(1,1,n,b,b+a-1,0,0); } else puts("fly with yourself"); } else if(op[0]=='N'){ scanf("%d",&a); if(mlen[0][1]>=a){ printf("%d,don't put my gezi\n",b=query(1,1,n,1,n,a,0)); update(1,1,n,b,b+a-1,0,0); update(1,1,n,b,b+a-1,0,1); } else if(mlen[1][1]>=a){ printf("%d,don't put my gezi\n",b=query(1,1,n,1,n,a,1)); update(1,1,n,b,b+a-1,0,0); update(1,1,n,b,b+a-1,0,1); } else puts("wait for me"); } else { scanf("%d%d",&a,&b); update(1,1,n,a,b,1,0); update(1,1,n,a,b,1,1); puts("I am the hope of chinese chengxuyuan!!"); } } } return 0;}