ZOJ 2112 Dynamic Rankings (Chairman Tree set tree array + static chairman tree)

Source: Internet
Author: User

Test instructions: Given an interval, the number of k in this interval is obtained, which supports single point modification.

Idea: The Chairman tree is really a magical thing ... Very fast But there is also a problem is to occupy a large amount of memory, generally support the single-point modification of the chairman tree set of tree array space complexity of O (N*LOGN*LOGN), if the query is less, you can initially use a static chairman tree, so that the space complexity can be reduced to O (n*logn+q* LOGN*LOGN), can barely cross zoj this problem.

This problem has been looking for a long time to understand ... The online puzzle is generally a few words .... Here are some of your own understandings.

First static chairman Tree This thing actually better understand, is for each prefix [1,1],[1,2] .... [1,n] All built a line segment tree, the data is discretized, in the segment tree in the statistical interval of the number of occurrences of all values, each segment tree is the same shape, then we get a very good property, these segment trees can be "subtracted", assuming that the current query is the interval [l,r] k value, Then we use the prefix [1, R] This segment tree and prefix [1, l-1] This segment tree by subtracting plus two can find the answer. Since the two adjacent segments of the tree are at most LOGN nodes, we do not need to completely create a new line tree, just need to point the same node with a pointer to the line, and then create a new Logn node, so the space-time complexity of N*LOGN.

For dynamic query of the Chairman tree, if you modify the value of a point, we certainly cannot modify all of the segment tree containing this point, time complexity can not withstand.

Then we can set up a tree-like array, personal feeling here is more difficult to understand.

Each node of the tree array is a segment tree, but the segment tree no longer holds the information for each prefix, but rather the information of the prefix is computed by the sum function of the tree array, so it is obvious that the segment tree holds the value of the auxiliary array s, i.e. s=a[i-lowbit+1]+...+a[i], Where A[i] represents the number of occurrences of an element with a value of I.

So for each modification, we want to modify the tree array of Logn tree, for each tree, we want to modify LOGN nodes, so the space-time complexity of

O ((n+q) *logn*logn), because this problem n relatively large, the number of query q is relatively small, so we can initially establish a static chairman tree, the tree-like array only to save the information of each modification, then the space-time complexity reduced to O (N*logn+q*logn*logn).

Code reference from: http://www.cnblogs.com/kuangbin/p/3308118.html

#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <vector> #include <map> #include <queue> #include <stack > #include <string> #include <map> #include <set> #include <ctime> #define EPS 1e-6#define LL Long Long#define PII (Pair<int, int>)//#pragma comment (linker, "/stack:1024000000,1024000000") using namespace Std;//zoj 2104 Dynamic Chairman Tree Modify + static chairman Tree const int MAXN = 60000+1000;const int M = 2400000;int N, q, M, Tot;int A[MAXN], T[maxn];int t [MAXN], lson[m], rson[m], c[m];int s[maxn];struct Query {int kind;int L, r, K;} query[10010];void init_hash (int k) {sort (t , t+k); m = Unique (t, t+k)-t;} int Hash (int x) {return Lower_bound (T, t+m, X)-T;} int build (int l, int r) {int root = Tot++;c[root] = 0;if (l! = r) {int mid = (l+r) >> 1;lson[root] = Build (L, mid); RS On[root] = Build (mid+1, r);} return root;} int Insert (int root, int pos, int val) {int newroot = tot++, tmp= Newroot;int L = 0, r = m-1;c[newroot] = C[root] + val;        while (L < r) {int mid = (l+r) >>1;            if (pos <= mid) {Lson[newroot] = tot++; Rson[newroot] = Rson[root]; Newroot = Lson[newroot];            root = Lson[root];        R = Mid;            } else {Rson[newroot] = tot++; Lson[newroot] = Lson[root]; Newroot = Rson[newroot];            root = Rson[root];        L = mid+1;    } C[newroot] = C[root] + val; }return tmp;} int lowbit (int x) {return x& (-X);} int use[maxn];void Add (int x, int pos, int d) {while (x < n) {s[x] = Insert (s[x], POS, d); x + = Lowbit (x);}} int Sum (int x) {int ret = 0;while (x > 0) {ret + = c[lson[use[x]]];x-= lowbit (x);} return ret;} int Query (int left, int right, int k) {int left_root = t[left-1], Right_root = t[right];int L = 0, r = m-1;for (int i = Lef t-1; I I-= Lowbit (i)) use[i] = s[i];for (int i = right; i; I-= Lowbit (i)) use[i] = S[i];while (L < r) {int mid = (l+r) >&gt ; 1;intTMP = SUM (right)-sum (left-1) + C[lson[right_root]]-c[lson[left_root]];if (tmp >= k) {r = mid;for (int i = left-1; i; I-= Lowbit (i)) use[i] = lson[use[i]];for (int i = right; i; I-= Lowbit (i)) use[i] = Lson[use[i]];left_root = Lson[left_ro Ot];right_root = Lson[right_root];} else {L = mid + 1;k-= tmp;for (int i = left-1; i; i-= Lowbit (i)) use[i] = rson[use[i]];for (int i = right; i; I-= Lowbit ( i)) use[i] = Rson[use[i]];left_root = Rson[left_root];right_root = Rson[right_root];}} return l;}    int main () {//freopen ("Input.txt", "R", stdin); int Tcase; Cin >> Tcase; while (tcase--) {scanf ("%d%d", &n, &q), tot = 0;m = 0;for (int i = 1; I <= n; i++) {scanf ("%d", &a[i]); t[m++] = A[i];}           char op[10];for (int i = 0;i < q;i++) {scanf ("%s", op);               if (op[0] = = ' Q ') {query[i].kind = 0;           scanf ("%d%d%d", &AMP;QUERY[I].L,&AMP;QUERY[I].R,&AMP;QUERY[I].K);               } else {query[i].kind = 1; scanf("%d%d", &AMP;QUERY[I].L, &AMP;QUERY[I].R);           t[m++] = QUERY[I].R; }}init_hash (M); T[0] = Build (0, m-1); for (int i = 1; I <= n; i++) T[i] = Insert (T[i-1], Hash (A[i]), 1); for (int i = 1; I <= n; i++) s[ I] = t[0];for (int i = 0; i < Q; i++) {if (Query[i].kind = = 0) printf ("%d\n", T[query (Query[i].l,query[i].r,qu            ERY[I].K)]);                else {Add (QUERY[I].L, Hash (A[QUERY[I].L]),-1);                Add (QUERY[I].L, Hash (QUERY[I].R), 1);            A[QUERY[I].L] = QUERY[I].R; }}} return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

ZOJ 2112 Dynamic Rankings (Chairman Tree set tree array + static chairman tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.