Chairman Tree Template

Source: Internet
Author: User

Use this template can direct a off HDU 2665 Kth number This problem!


/* Chairman tree for interval k large Template: * Template Special Description: * Each tree is maintained from 1 to CNT subscript information */#include <stdio.h> #include <algorithm> #define    Maxn 100010using namespace Std;int T, N, M, tot, A[MAXN], B[MAXN], CNT;     T is the number of input CAs, N is the number of arrays of the original array, M is the number of queries, tot for the achievement of the original node//a, B is used for discretization of the sequence, its unique after the number of elements of cntstruct N {int ls, RS, w;} tree[30 * MAXN];    LS is the left son node subscript, RS is the right son node subscript, W for this interval within the number of elements int ROOTS[MAXN];    Roots record the root node of each tree subscript int build_tree (int l, int r) {//First build an empty tree int newnode = tot++;    TREE[NEWNODE].W = 0;        if (L = r) {int mid = (L + r)/2;        Tree[newnode].ls = Build_tree (L, mid);    Tree[newnode].rs = Build_tree (mid + 1, R); } return newnode;}    int updata (int rt, int pos, int val) {//rt is the root node, plus val value int newnode = tot++, tmp = NewNode in pos position;    TREE[NEWNODE].W = Tree[rt].w + val;    int L = 1, r = CNT;        while (L < r) {int mid = (L + r)/2;            if (pos <= mid) {tree[newnode].ls = tot++;            tree[newnode].rs = tree[rt].rs; NewNode = TREe[newnode].ls;            RT = Tree[rt].ls;        R = Mid;            } else {tree[newnode].ls = Tree[rt].ls;            tree[newnode].rs = tot++;            NewNode = tree[newnode].rs;            RT = Tree[rt].rs;        L = mid + 1;    } TREE[NEWNODE].W = Tree[rt].w + val; } return TMP;    int query (int rt1, int rt2, int k) {//Ask the root node to be rt1,rt2 between two trees respectively the value of the K-large int l = 1, r = CNT;        while (L < r) {int mid = (L + r)/2;        int tmp = TREE[TREE[RT2].LS].W-TREE[TREE[RT1].LS].W;            if (tmp >= k) {rt1 = Tree[rt1].ls;            Rt2 = Tree[rt2].ls;        R = Mid;            } else {k-= tmp;            Rt1 = tree[rt1].rs;            Rt2 = tree[rt2].rs;        L = mid + 1; }} return L;}    int main () {//freopen ("in.in", "R", stdin);    Freopen ("Out.out", "w", stdout);    scanf ("%d", &t);        while (t--) {//read in data groups scanf ("%d%d", &n, &m); for (int i = 1; i <= N;            i++) {scanf ("%d", &a[i]);        B[I-1] = A[i];        } sort (b, B + N);     CNT = unique (b, B + N)-B;        discretization of tot = 0;        Roots[0] = Build_tree (1, CNT);            for (int i = 1; I <= n; i++) {int tmp = (int) (Lower_bound (b, B + CNT, a[i])-B) + 1;    Roots[i] = Updata (roots[i-1], TMP, 1);        Build on the last time} int l,r,k; for (int i=0;i<m;i++) {scanf ("%d%d%d", &l,&r,&k);//read in for L and R interval k large number int tmp = query (roo            TS[L-1],ROOTS[R],K);        printf ("%d\n", b[tmp-1]); }} return 0;}


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

Chairman Tree Template

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.