HDU-2665 Kth number

来源:互联网 发布:vr软件看不了 编辑:程序博客网 时间:2024/06/05 11:13



Kth number

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


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

题目大意:就是寻找第k大的数,无修改,主席树模板测试题

#include <iostream>#include <bits/stdc++.h>using namespace std;#define lson l,m#define rson m+1,rconst int N=1e5+5;int L[N<<5],R[N<<5],sum[N<<5];int tot;int a[N],T[N],Hash[N];int build(int l,int r){        int rt=(++tot);        sum[rt]=0;        if(l<r)        {            int m=(l+r)>>1;            L[rt]=build(lson);            R[rt]=build(rson);        }        return rt;}int update(int pre,int l,int r,int x){    int rt=(++tot);    L[rt]=L[pre],R[rt]=R[pre],sum[rt]=sum[pre]+1;    if(l<r)    {        int m=(l+r)>>1;        if(x<=m)            L[rt]=update(L[pre],lson,x);        else            R[rt]=update(R[pre],rson,x);    }    return rt;}int query(int u,int v,int l,int r,int k){    if(l>=r)        return l;    int m=(l+r)>>1;    int num=sum[L[v]]-sum[L[u]];    if(num>=k)        return query(L[u],L[v],lson,k);    else        return query(R[u],R[v],rson,k-num);}int main(){    int t;    cin>>t;    while(t--){        tot=0;        int n,m;        scanf("%d%d",&n,&m);        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);            Hash[i]=a[i];        }        sort(Hash+1,Hash+1+n);        int d=unique(Hash+1,Hash+1+n)-Hash-1;        T[0]=build(1,d);        for(int i=1;i<=n;i++)        {            int x=lower_bound(Hash+1,Hash+1+d,a[i])-Hash;            T[i]=update(T[i-1],1,d,x);        }        while(m--)        {            int l,r,k;            scanf("%d%d%d",&l,&r,&k);            int x=query(T[l-1],T[r],1,d,k);            printf("%d\n",Hash[x]);        }    }    //cout << "Hello world!" << endl;    return 0;}


原创粉丝点击