http://acm.hdu.edu.cn/showproblem.php?pid=4417
Test instructions is: give the number of N and Q of the query, each query has a l,r,h, ask in [l,r] This interval number is less than or equal to H.
Idea: Compare the bare chairman tree, notice that the interval given by the test instructions is from [0,n-1], from the beginning to see the wrong cause a lot of things wrong. When asked if M < H, then the left subtree are all less than H, plus the sum of the left subtree, continue to check the right subtree, or the left sub-tree. The last L = = R will be sentenced to H >= L, because this is also wrong several times. Brother studied. If you find a number, if you cannot find it, return a number smaller than it, then you can use Upper_bound () to find the subscript-1. Do not have to Lower_bound () to find after the judgment and so not equal, not equal to the words subscript-1 so troublesome. (Upper_bound () returns a subscript that is larger than the number of checks, so 1 can be equal to or less than. )
1#include <cstdio>2#include <cstring>3#include <cstdlib>4#include <algorithm>5 using namespacestd;6 #defineN 1000107 structnode8 {9 intL, R, sum;Ten}tree[n* +]; One intRoot[n], tot, a[n], b[n], CNT; A - voidUpdateintPreint&now,intIdintLintR) - { thenow = ++tot; Tree[now] =Tree[pre]; -tree[now].sum++; - if(L = = r)return ; - intm = (L + r) >>1; + if(ID <=m) Update (TREE[PRE].L, TREE[NOW].L, ID, L, m); - ElseUpdate (TREE[PRE].R, TREE[NOW].R, ID, M +1, R); + } A at intQueryintLintRintHintLintR) - { - intAns =0; - intm = (L + r) >>1; - if(L = =r) { - if(H >= l) ans + = tree[r].sum-tree[l].sum;//pay attention to the sentence . in /* - if interval query and update interval is (0, CNT), then it is not necessary to determine the to because if the check interval is (3, 5, 5) Check 2, then will be found subscript 0, + L > H, this is the wrong time. - */ the returnans; * } $ if(M <h) {Panax NotoginsengAns + = tree[tree[r].l].sum-tree[tree[l].l].sum;//if h > m, the left subtree is all smaller than H, all plus -Ans + = query (TREE[L].R, TREE[R].R, H, M +1, R); the}Else { +Ans + =query (TREE[L].L, TREE[R].L, H, L, m); A } the returnans; + } - $ voidDebugintRtintLintR) $ { - if(L = =r) { -printf"%d:%d\n", L, tree[rt].sum); the return ; - }Wuyi intm = (L + r) >>1; the Debug (TREE[RT].L, L, m); -Debug (TREE[RT].R, m+1, R); Wu } - About intMain () $ { - intT, CAS =1; -scanf"%d", &t); - while(t--) { A intN, Q; +scanf"%d%d", &n, &q); the -tot =0; $ the for(inti =1; I <= N; i++) { thescanf"%d", &a[i]); theB[i] =A[i]; the } -Sort (b +1, B +1+n); inCNT = Unique (b +1, B +1+ N)-B-1; the for(inti =1; I <= N; i++) { theA[i] = Lower_bound (b +1, B +1+ CNT, a[i])-b; AboutUpdate (root[i-1], Root[i], A[i],1, CNT); the } theDebug (Root[n],1, CNT); theprintf"Case %d:\n", cas++); + for(inti =1; I <= Q; i++) { - intL, R, C; thescanf"%d%d%d", &l, &r, &c);Bayil++, r++; the intTMP = Upper_bound (b +1, B +1+ CNT, c)-B-1; the //int tmp = Lower_bound (b + 1, B + 1 + CNT, c)-B; - //if (b[tmp]! = c) tmp--; - //int tmp = lb (c); theprintf"%d\n", Query (root[l-1], Root[r], TMP,1, CNT)); the } the } the return 0; -}
HDU 4417:super Mario (Chairman tree)