1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| #include<bits/stdc++.h> using namespace std; #define ll long long const int maxn = 5e4+10; int a[maxn]; struct node { int pre; int suf; int sum; }tree[maxn*4]; void pushup(int rt,int len) { tree[rt].pre=tree[rt<<1].pre; tree[rt].suf=tree[rt<<1|1].suf; if(tree[rt<<1].pre==(len-(len>>1)))tree[rt].pre=tree[rt<<1|1].pre+tree[rt<<1].pre; if(tree[rt<<1|1].suf==(len>>1))tree[rt].suf=tree[rt<<1|1].suf+tree[rt<<1].suf; tree[rt].sum=max(tree[1<<1].suf+tree[1<<1|1].pre,max(tree[rt<<1].sum,tree[rt<<1|1].sum)); } void build(int l,int r,int rt) { if(l==r) { tree[rt].pre=tree[rt].suf=tree[rt].sum=1; return ; } int mid = (l+r)>>1; build(l,mid,rt<<1); build(mid+1,r,rt<<1|1); pushup(rt,r-l+1); } void Update(int pos,int c,int l,int r,int rt) { if(l==r) { tree[rt].pre=tree[rt].suf=tree[rt].sum=c; return ; } int mid = (l+r)>>1; if(pos<=mid)Update(pos,c,l,mid,rt<<1); else Update(pos,c,mid+1,r,rt<<1|1); pushup(rt,r-l+1); } int query(int pos,int l,int r,int rt) { if(l==r)return tree[rt].sum; int mid = (l+r)>>1; if(pos<=mid) { if(pos+tree[rt<<1].suf>mid)return tree[rt<<1].suf+tree[rt<<1|1].pre; else return query(pos,l,mid,rt<<1); } else { if(mid+tree[rt<<1|1].pre>=pos)return tree[rt<<1|1].pre+tree[rt<<1].suf; else return query(pos,mid+1,r,rt<<1|1); } } int main() { int n,m,x,tot; char op[2]; while(~scanf("%d %d",&n,&m)) { build(1,n,1); tot=0; while(m--) { scanf("%s",op); if(op[0]=='Q') { scanf("%d",&x); printf("%d\n",query(x,1,n,1)); } else if(op[0]=='D') { scanf("%d",&x); a[++tot]=x; Update(x,0,1,n,1); } else { x=a[tot--]; Update(x,1,1,n,1); } } } }
|