http://acm.hdu.edu.cn/showproblem.php?pid=1540
7 9 D 3 D 6 D 5 Q 4 Q 5 R Q 4 R Q 4
1 0 2 4
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <cstdlib> #include <limits> #include <stack> using namespace std; #define N 150600 #define met(a, b) memset (a, b, sizeof (a)) typedef long long LL; //const int INF = ((1<<31)-1); struct node { int l, r, mid, len, lsum, rsum, sum, Lson, Rson; void Init (int rt, int L, int R) { l = L, r = R; mid = (L+R) / 2; len = R-L+1; Lson = rt<<1; Rson = rt<<1|1; } }tree[N*4]; void build (int rt, int l, int r) { tree[rt].Init(rt, l, r); tree[rt].sum = tree[rt].lsum = tree[rt].rsum = tree[rt].len; if (l == r) return; build (tree[rt].Lson, l, tree[rt].mid); build (tree[rt].Rson, tree[rt].mid+1, r); } void update (int rt) { tree[rt].lsum = tree[rt<<1].lsum; tree[rt].rsum = tree[rt<<1|1].rsum; if (tree[rt<<1].lsum == tree[rt<<1].len) tree[rt].lsum = tree[rt<<1].lsum + tree[rt<<1|1].lsum; if (tree[rt<<1|1].rsum == tree[rt<<1|1].len) tree[rt].rsum = tree[rt<<1|1].rsum + tree[rt<<1].rsum; tree[rt].sum = max (tree[rt].lsum, tree[rt].rsum); } void down (int rt, int k, int e) { if (tree[rt].l == tree[rt].r) { tree[rt].sum = tree[rt].lsum = tree[rt].rsum = e; return; } if (k <= tree[rt].mid) down (tree[rt].Lson, k, e); else down (tree[rt].Rson, k, e); update (rt); } int query (int rt, int k) { if (!tree[rt].sum) return 0; if (k < tree[rt<<1].l + tree[rt].lsum) return tree[rt].lsum;//判断是否在左边 if (k > tree[rt<<1|1].r - tree[rt<<1|1].rsum) return tree[rt].rsum;//判断是否在右边 if (k<tree[rt<<1|1].l+tree[rt<<1|1].lsum && k>tree[rt<<1].r-tree[rt<<1].rsum) return tree[rt<<1|1].lsum+tree[rt<<1].rsum; if (k <= tree[rt].mid) return query (tree[rt].Lson, k); else return query (tree[rt].Rson, k); } int main () { int n, m; while (scanf ("%d %d", &n, &m) != EOF) { stack <int> sta; build (1, 1, n); while (m--) { char s[10]; int x; scanf ("%s", s); if (s[0] == 'D') { scanf ("%d", &x); down (1, x, 0); sta.push (x); } else if (s[0] == 'R') { down (1, sta.top(), 1); sta.pop(); } else { scanf ("%d", &x); printf ("%d\n", query (1, x)); } } } return 0; }