【HDU】1540 Tunnel Warfare
2012-05-23 11:45
148 查看
#include<cstdio> #include<stack> #define MAXN 50010 #define MAX(a,b) ((a)>(b)?(a):(b)) using namespace std; stack<int>st; struct node { int left,right; }; node tree[MAXN<<2]; void Build(int L,int R,int rt) { tree[rt].left=tree[rt].right=R-L+1; if(L!=R) { int mid=(L+R)>>1; Build(L,mid,rt<<1); Build(mid+1,R,rt<<1|1); } } inline void PushUp(int mid,int L,int R,int rt) { tree[rt].left=tree[rt<<1].left; tree[rt].right=tree[rt<<1|1].right; if(tree[rt].left==mid-L+1) tree[rt].left+=tree[rt<<1|1].left; if(tree[rt].right==R-mid) tree[rt].right+=tree[rt<<1].right; } void Update(int x,int val,int L,int R,int rt) { if(L==R) tree[rt].left=tree[rt].right=val; else { int mid=(L+R)>>1; if(x<=mid) Update(x,val,L,mid,rt<<1); else Update(x,val,mid+1,R,rt<<1|1); PushUp(mid,L,R,rt); } } int Query(int x,int L,int R,int rt) { if(L==R) return tree[rt].left; int mid=(L+R)>>1; if(x<=mid) { if(tree[rt<<1].left==mid-L+1) return tree[rt<<1].left+tree[rt<<1|1].left; else if(L+tree[rt<<1].left>x) return tree[rt<<1].left; else if(x>mid-tree[rt<<1].right) return tree[rt<<1].right+tree[rt<<1|1].left; else return Query(x,L,mid,rt<<1); } else { if(tree[rt<<1|1].right==R-mid) return tree[rt<<1|1].right+tree[rt<<1].right; else if(x<mid+1+tree[rt<<1|1].left) return tree[rt<<1].right+tree[rt<<1|1].left; else if(x>R-tree[rt<<1|1].right) return tree[rt<<1|1].right; else return Query(x,mid+1,R,rt<<1|1); } } int main() { int n,m,x; char ch; while(~scanf("%d%d",&n,&m)) { Build(1,n,1); for(;!st.empty();st.pop()); while(m--) { scanf(" %c",&ch); if(ch=='D') { scanf("%d",&x); st.push(x); Update(x,0,1,n,1); } else if(ch=='Q') { scanf("%d",&x); printf("%d\n",Query(x,1,n,1)); } else { if(!st.empty()) { Update(st.top(),1,1,n,1); st.pop(); } } } } return 0; }
相关文章推荐
- 文章标题 HDU 1540 : Tunnel Warfare (线段树+最大连续区间)
- HDU 1540 Tunnel Warfare
- hdu 1540 Tunnel Warfare
- hdu 1540 & poj 2892 Tunnel Warfare 线段树区间合并
- 【HDU - 1540】Tunnel Warfare 【线段树+单点更新+区间合并】
- HDU 1540 && POJ 2892 Tunnel Warfare (线段树,区间合并).
- HDU 1540 Tunnel Warfare
- HDU - 1540 Tunnel Warfare —— 区间合并
- HDU 1540 Tunnel Warfare
- HDU1540 Tunnel Warfare (线段树区间合并)
- hdu 1540 Tunnel Warfare(线段树区间合并)
- HDU 1540 Tunnel Warfare(线段树 区间合并 最大连续区间)
- HDU 1540 Tunnel Warfare
- hdu 1540 Tunnel Warfare(线段树)
- hdu 1540 Tunnel Warfare
- HDU 1540 Tunnel Warfare
- hdu 1540 Tunnel Warfare(单点更新,取最大连续区间)★
- HDU 1540 POJ 2892 Tunnel Warfare
- hdu 1540 Tunnel Warfare
- hdu 1540 Tunnel Warfare