HDU 2665 durable segment tree for interval k large value (functional Segment Tree | | Chairman Tree)

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=2665



Problem descriptiongive you a sequence and ask you the kth big number of a inteval.
Inputthe first line is the number of the test cases.
For each test case, the first line contain both integer n and m (n, M <= 100000), indicates the number of integers in th E sequence and the number of the quaere.
The second line contains n integers, describe the sequence.
Each of following m lines contains three integers s, T, K.
[s, T] indicates the interval and K indicates the kth big number in interval [s, T]
Outputfor each test case, output m lines. Each line contains the kth big number.
Sample Input

Sample Output
2

/**HDU 2665 durable segment tree for interval k large value (functional Segment Tree | | Chairman of the tree); Given an interval, M asks: the number of K-large numbers in the specified interval: the so-called chairman Tree, that is, the tree that can be persisted, that is, each time we insert a new element, we create a new node, so that all the historical versions of the segment tree can be preserved. Then consider the line tree subtraction, the two segment tree subtraction is each node subtraction, then we each node update once, then each element in the sequence corresponds to a version of the segment tree, that is, all the prefixes in the sequence of the weight of the segment tree, then for an interval, by the prefix subtraction will quickly Can get out of this section of the corresponding segment tree, and then ask this line tree of the K-value Note: If the interval K decimal value is converted to a large value (l-r-k+1) can be */#include <stdio.h> #include <string.h># Include <algorithm> #include <iostream>using namespace std;const int n=100010;int t[n];int num[n];int san[n]    ; int ls[n*20];int rs[n*20];int sum[n*20];int tot,rt;int n,m;void Build (int l,int r,int &rt) {rt=++tot;    sum[rt]=0;    if (l==r) return;    int m= (L+R)/2;    Build (L,m,ls[rt]); Build (M+1,r,rs[rt]);}    void update (int last,int p,int l,int r,int &rt) {rt=++tot;    Ls[rt]=ls[last];    Rs[rt]=rs[last];    sum[rt]=sum[last]+1;    if (l==r) return;    int m= (L+R) >>1;    if (p<=m) update (LS[LAST],P,L,M,LS[RT]); else update (RS[LAST],P,M+1,R,RS[RT]);} Int Query (int ss,int tt,int l,int r,int k) {if (l==r) return l;    int m= (L+R) >>1;    int CNT=SUM[LS[TT]]-SUM[LS[SS]];    if (k<=cnt) return query (LS[SS],LS[TT],L,M,K); else return query (RS[SS],RS[TT],M+1,R,K-CNT);}    int main () {int TT;    scanf ("%d", &AMP;TT);        while (tt--) {scanf ("%d%d", &n,&m);            for (int i=1; i<=n; i++) {scanf ("%d", &num[i]);        San[i]=num[i];        } tot=0;        Sort (san+1,san+n+1);        int Cnt=unique (san+1,san+n+1)-san-1;        Build (1,cnt,t[0]);        for (int i=1; i<=n; i++) {num[i]=lower_bound (san+1,san+1+cnt,num[i])-san;        } for (int i=1; i<=n; i++) update (t[i-1],num[i],1,cnt,t[i]);            while (m--) {int l,r,k;            scanf ("%d%d%d", &l,&r,&k);            int Id=query (T[L-1],T[R],1,CNT,K);        printf ("%d\n", San[id]); }} return 0;}

Hdu 2665 to persist segment tree for interval k large value (functional Segment Tree | | 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.