Reprint please indicate the source, thank you http://blog.csdn.net/ACM_cxlove? Viewmode = contents by --- cxlove
There are n boxes, and each box has several balloons. Each operation can take a balloon from a box and give some intervals, ask how many boxes are empty after each operation.
Http://www.codechef.com/AUG13/problems/SEABAL
Practice: Use a linked list to maintain a non-zero position before the current position and a non-zero position next.
If the current position is K, the first non-0 position is l, and the next non-0 position is R.
Then, the current position is reduced to 0, and the added range is the left endpoint (L, K], and the right endpoint is in [K, R ).
The next step is to query how many intervals meet this condition.
Use the Chair tree to maintain the left endpoint position of the right endpoint of each interval.
# Include <iostream> # include <cstdio> # include <set> # include <vector> # include <cstring> # include <algorithm> # define MEM (A, B) memset (a, B, sizeof (A) using namespace STD; const int n = 100005; const int M = 30000005; struct node {int L, R; void input () {scanf ("% d", & L, & R) ;}bool operator <(const node & N) const {return r <n. r ;}} que [N]; int n, m, Q, A [n]; int T [m], lson [m], rson [m], S Um [m], TOT = 0; int L [N], R [N]; int bulid (int l, int R) {int root = ++ tot; sum [root] = 0; If (L! = R) {int M = (L + r)> 1; lson [root] = bulid (L, M); rson [root] = bulid (m + 1, r);} return root;} int Update (INT root, int L, int R, int POS) {int newroot = ++ tot; sum [newroot] = sum [root] + 1; if (L! = R) {int M = (L + r)> 1; if (Pos <= m) {lson [newroot] = Update (lson [root], L, M, pos); rson [newroot] = rson [root];} else {rson [newroot] = Update (rson [root], M + 1, R, POS ); lson [newroot] = lson [root];} // cout <sum [newroot] <"" <sum [lson [newroot] <"" <sum [rson [newroot] <Endl ;; return newroot;} int query (INT root, int L, int R, int L, int R) {If (L = L & R = R) {return sum [root];} int M = (L + r)> 1; if (r <= m) return query (lson [root], l, m, L, R); else if (L> m) return query (rson [root], M + 1, R, L, R ); else return query (lson [root], L, M, L, M) + query (rson [root], M + 1, R, m + 1, R );} void out (INT root, int L, int R) {cout <L <"" <r <"" <sum [root] <Endl; if (L! = R) {int M = (L + r)> 1; out (lson [root], l, m); Out (rson [root], M + 1, r) ;}} int main () {// freopen ("input.txt", "r", stdin); // freopen ("output.txt", "W", stdout ); scanf ("% d", & N, & M); L [0] = 0; R [n + 1] = n + 1; for (INT I = 1; I <= N; I ++) {scanf ("% d", & A [I]); L [I] = I-1; R [I] = I + 1 ;}for (INT I = 0; I <m; I ++) {que [I]. input () ;}sort (que, que + M); t [0] = bulid (1, N); For (INT I = 1, j = 0; I <= N; I ++) {T [I] = T [I-1]; while (j <M & que [J]. R = I) {T [I] = Update (T [I], 1, n, que [J]. l); j ++;} // cout <"root:" <I <Endl; // out (T [I], 1, n );} scanf ("% d", & Q); int ans = 0; while (Q --) {int K; scanf ("% d", & K ); K + = ans; // cout <k <Endl; A [k] --; if (a [k] = 0) {int L = L [K], R = R [k]; int ret = query (T [R-1], 1, n, l + 1, k)-query (T [k-1], 1, n, l + 1, k); ans + = ret; // cout <"RET:" <RET <Endl; if (R [k] <= N) L [R [k] = L [k]; If (L [k]> = 1) R [L [k] = R [k];} printf ("% d \ n", ANS);} return 0 ;}