hdu 2665 Kth number

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12119    Accepted Submission(s): 3697


Problem Description
Give you a sequence and ask you the kth big number of a inteval.
 
Input
The first line is the number of the test cases.
For each test case, the first line contain two integer n and m (n, m <= 100000), indicates the number of integers in the 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]
 
Output
For each test case, output m lines. Each line contains the kth big number.
 
Sample Input
1
10 1 1 4 2 3 5 6 7 8 9 0
1 3 2
 
Sample Output
2
 
Source
 
主席树模板
心疼我是个智障,retrun 0 写在for(;T--;)里面了 
这次本来打算写指针,各种蜜汁错误,样例都跑不动。。向数组低头。。
 
#include <algorithm>
#include <cstdio>
#define N 105000

using std::sort;
using std::unique;
using std::lower_bound;

struct cmt
{
    int l,r,Size;
}tr[N*20];
int a[N],b[N],t[N],tot,Size,n,m,T;
int build(int l,int r)
{
    int root=++tot;
    tr[root].Size=0;
    if(l==r) return root;
    int mid=(l+r)>>1;
    tr[root].l=build(l,mid);
    tr[root].r=build(mid+1,r);
    return root;
}
int update(int rt,int x)
{
    int now=++tot;
    int tmp=now;
    tr[now].Size=tr[rt].Size+1;
    for(int mid,l=1,r=Size;l<=r;)
    {
        mid=(l+r)>>1;
        if(x<=mid)
        {
            tr[now].l=++tot;
            tr[now].r=tr[rt].r;
            rt=tr[rt].l;
            now=tot;
            r=mid-1;
        }
        else
        {
            tr[now].l=tr[rt].l;
            tr[now].r=++tot;
            rt=tr[rt].r;
            now=tot;
            l=mid+1;
        }
        tr[now].Size=tr[rt].Size+1;
    }
    return tmp;
}
int ask(int lx,int rx,int k)
{
    int l=1,r=Size;
    for(int mid;l<=r;)
    {
        mid=(l+r)>>1;
        if(tr[tr[rx].l].Size-tr[tr[lx].l].Size>=k)
        {
            rx=tr[rx].l;
            lx=tr[lx].l;
            r=mid-1;
        }
        else
        {
            k-=tr[tr[rx].l].Size-tr[tr[lx].l].Size;
            rx=tr[rx].r;
            lx=tr[lx].r;
            l=mid+1;
        }
    }
    return l;
}
int main()
{
    scanf("%d",&T);
    for(;T--;)
    {
        tot=0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;++i) scanf("%d",&a[i]),b[i]=a[i];
        sort(b+1,b+1+n);
        Size=unique(b+1,b+1+n)-b-1;
        t[0]=build(1,Size);
        for(int i=1;i<=n;++i) a[i]=lower_bound(b+1,b+1+Size,a[i])-b;
        for(int i=1;i<=n;++i)
        t[i]=update(t[i-1],a[i]);
        for(int x,y,k;m--;)
        {
            scanf("%d%d%d",&x,&y,&k);
            printf("%d
",b[ask(t[x-1],t[y],k)]);
        }
    }
    return 0;
}
我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。
原文地址:https://www.cnblogs.com/ruojisun/p/7404428.html