标签:
#include<stdio.h> #include<algorithm> #include<stack> #define N 50010 using namespace std; struct node { int left, right, sum, Lsum, Rsum; //sum存放与该点相连的点的总个数,Lsum存放从左端点开始向右与之相连的点的最大个数,Rsum存放从右端点开始向左与之相连的点的最大个数 }no[4*N]; void Bulid(int left, int right, int root) { int mid; no[root].left = left; no[root].right = right; no[root].sum = no[root].Lsum = no[root].Rsum = (no[root].right-no[root].left+1); //开始时所有点相连的个数都等于其区间的长度 if (left == right) return ; mid = (left+right)/2; Bulid(left, mid, root*2); Bulid(mid+1, right, root*2+1); } void Pushup(int root) //向上更新 { no[root].Lsum = no[root*2].Lsum; no[root].Rsum = no[root*2+1].Rsum; if (no[root*2].Lsum == no[root*2].right-no[root*2].left+1) //只有当左结点的Lsum与区间长度相等了才能加上右结点的部分,否则会有重复现象 no[root].Lsum = no[root*2].Lsum + no[root*2+1].Lsum; if (no[root*2+1].Rsum == no[root*2+1].right-no[root*2+1].left+1) no[root].Rsum = no[root*2+1].Rsum + no[root*2].Rsum; no[root].sum = max(no[root].Lsum, max(no[root].Rsum, no[root*2].Rsum+no[root*2+1].Lsum)); } void Update(int k, int num, int root) { int mid; if (no[root].left == no[root].right) { no[root].Lsum = no[root].Rsum = num; return ; } mid = (no[root].left+no[root].right)/2; if (k <= mid) Update(k, num, root*2); else Update(k, num, root*2+1); Pushup(root); } int Query(int k, int root) { int mid; if (no[root].sum == 0) return 0; if (k < no[root].left+no[root].Lsum) return no[root].Lsum; if (k > no[root].right-no[root].Rsum) return no[root].Rsum; if (k > no[root*2].right-no[root*2].Rsum && k < no[root*2+1].left+no[root*2+1].Lsum) return no[root*2].Rsum+no[root*2+1].Lsum; mid = (no[root].left+no[root].right)/2; if (k <= mid) return Query(k, root*2); else return Query(k, root*2+1); } int main () { int n, m, k, Sum; char s[10]; stack<int>Q; while (scanf("%d%d", &n, &m) != EOF) { Bulid(1, n, 1); while (m--) { scanf("%s", s); if (s[0] == ‘D‘) { scanf("%d", &k); Update(k, 0, 1); Q.push(k); } else if (s[0] == ‘Q‘) { scanf("%d", &k); Sum = Query(k, 1); printf("%d\n", Sum); } else if (s[0] == ‘R‘) { Update(Q.top(), 1, 1); Q.pop(); } } } return 0; }
标签:
原文地址:http://www.cnblogs.com/syhandll/p/4699510.html