zoukankan      html  css  js  c++  java
  • hdu2665可持久化线段树,求区间第K大

    Kth number

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


    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
     
     
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int N=100010;
    
    int T[N],num[N],san[N];
    int ls[N*20],rs[N*20],sum[N*20];
    int tot,n,m;
    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);
    }
    void work(){
        int l,r,k;
        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;for(int i=1;i<=n;++i)
        num[i]=lower_bound(san+1,san+cnt+1,num[i])-san;
        for(int i=1;i<=n;++i) Update(T[i-1],num[i],1,cnt,T[i]);
        while(m--) {
            scanf("%d%d%d",&l,&r,&k);
            int id=Query(T[l-1],T[r],1,cnt,k);
            printf("%d
    ",san[id]);
        }
    }
    int main(){
        int T;
        for(scanf("%d",&T);T--;){
            scanf("%d%d",&n,&m);
            work();
        }
    }
     
  • 相关阅读:
    WINDOWS操作系统各种版本(转)
    等待的日子
    多媒体大赛决赛纪事
    如何使用nero刻录数据光盘(转)
    无法显示隐藏文件的方法
    2007年教育技术学专业硕士点招生简章
    教育学专业基础综合推荐教材
    .NET 的发展与变迁
    Windows XP SP3与Vista SP1,谁更强?
    2007年教育学专业基础综合考试大纲
  • 原文地址:https://www.cnblogs.com/mfys/p/7736186.html
Copyright © 2011-2022 走看看