Zoj2112--dynamic rankings (tree-like array + chairman tree)

Source: Internet
Author: User

Topic Connection: zoj2112

Give n points, two operations, Q: Ask for the number of k large in [L,r], C: Change the i digit X

Dynamic Inquiry K Large, using a tree-like array to modify and query prefixes and.

Because the given space is small, you can make n points into a static chairman tree, and then for the modified value, in the other chairman of the tree to modify, query the same time query the two chairmen tree can be.

#include <cstdio> #include <cstring> #include <algorithm>using namespace std; #pragma comment (linker, "/stack:1024000000,1024000000") struct node{int L, r, Num;} p[2500000];//all nodes, p[i].l of the left son of the first node, p[i].num node I nodes have a number of points int n, q, cnt; int a[50010], b[10010][3];//a is the initial input, B is the operand int s[ 60010], M;//discretization of the array, and the number of discrete mint tree[50010], s[50010];//tree dynamic chairman tree, using a tree array to modify/query prefixes and, S Static chairman Tree, retain the initial n-point prefix and. int use[50010]; int lowbit (int x) {return x & x;}    int build (int l,int r) {int x = cnt++, mid = (l+r)/2;    P[x].num = 0;        if (L < r) {P[X].L = build (L,mid);    P[X].R = Build (Mid+1,r); } return x;}    int search1 (int x) {int low = 0, Mid, high = m-1;        while (low <= high) {mid = (low + high)/2;        if (s[mid] = = x) return mid;        else if (S[mid] < x) Low = mid + 1;    else high = mid-1;    }}int Update (int y,int k,int num) {int root = cnt++, x = root;    int L = 0, R = m-1, mid; P[x].num = P[y].num + num;        while (L < r) {mid = (l+r)/2;            if (k <= mid) {r = Mid;            P[X].L = cnt++;            P[X].R = P[Y].R;            x = P[X].L;        y = p[y].l;            } else {L = mid + 1;            P[X].L = P[Y].L;            P[X].R = cnt++;            x = P[X].R;        y = P[Y].R;    } p[x].num = P[y].num + num; } return root;}    void init () {int k, I, J;    Sort (s,s+m);    m = Unique (s,s+m)-S;    CNT = 0;    S[0] = build (0,m-1);    for (i = 1; I <= n; i++) s[i] = Update (S[I-1],SEARCH1 (A[i]), 1); for (i = 1; I <= n; i++) tree[i] = s[0];}    int sum (int i) {int sum = 0;        while (i) {sum + = p[p[use[i]].L].num;    I-= lowbit (i); } return sum;}    void query (int ll,int rr,int k) {int num, I, l = 0, R = m-1, Mid, rt_l = S[ll-1], Rt_r = S[RR];    for (i = ll-1; i; i-= Lowbit (i)) use[i] = Tree[i]; for (i = RR; i; I-= Lowbit (i)) use[i] = Tree[i];        while (L < r) {num = SUM (RR)-sum (LL-1) + p[p[rt_r].l].num-p[p[rt_l].l].num;        Mid = (L + r)/2;            if (num >= k) {r = Mid;            for (i = ll-1; i; i-= Lowbit (i)) use[i] = p[Use[i]].L;            for (i = RR; i; I-= Lowbit (i)) use[i] = p[Use[i]].L;            rt_l = p[rt_l].l;        Rt_r = p[Rt_r].l;            } else {L = mid + 1;            K-= num;            for (i = ll-1; i; i-= Lowbit (i)) use[i] = p[Use[i]].R;            for (i = RR; i; I-= Lowbit (i)) use[i] = p[Use[i]].R;            rt_l = p[rt_l].r;        Rt_r = p[Rt_r].r; }} printf ("%d\n", S[l]);}    int main () {int T, I, J, K1, K2;    int L, R;    Char str[10];    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n, &q);        m = 0;   for (i = 1; I <= n; i++) {         scanf ("%d", &a[i]);        s[m++] = A[i];            } for (i = 0; i < Q; i++) {scanf ("%s", str);            if (str[0] = = ' Q ') {scanf ("%d%d", &b[i][0], &b[i][1], &b[i][2]);                } else {B[i][0] =-1;                scanf ("%d%d", &b[i][1], &b[i][2]);            s[m++] = b[i][2];        }} init ();                for (i = 0; i < Q; i++) {if (b[i][0] = = 1) {k1 = Search1 (a[b[i][1]);                K2 = Search1 (b[i][2]);                    for (j = b[i][1]; j <= N; j + = Lowbit (j)) {Tree[j] = update (TREE[J],K1,-1);                TREE[J] = update (tree[j],k2,1);            } a[b[i][1]] = b[i][2];            } else {query (b[i][0],b[i][1],b[i][2]); }}} return 0;}


Zoj2112--dynamic rankings (tree-like array + 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.