zoukankan      html  css  js  c++  java
  • Kth number

    Kth number

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


    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<bits/stdc++.h>
    #include<queue>
    #include<cstdio>
    #include<iostream>
    #define REP(i, a, b) for(int i = (a); i <= (b); ++ i)
    #define REP(j, a, b) for(int j = (a); j <= (b); ++ j)
    #define PER(i, a, b) for(int i = (a); i >= (b); -- i)
    using namespace std;
    template <class T>
    inline void rd(T &ret){
        char c;
        ret = 0;
        while ((c = getchar()) < '0' || c > '9');
        while (c >= '0' && c <= '9'){
            ret = ret * 10 + (c - '0'), c = getchar();
        }
    }
    const int maxn=1e5+10;
    int T,n,m,lr[maxn<<5],rr[maxn<<5],rt[maxn],p[maxn],q[maxn],tot,s[maxn<<5];
    int build(int l,int r){
        int root=++tot;
        s[root]=0;
        if(l<r){
            int mid=l+r>>1;
            lr[root]=build(l,mid);
            rr[root]=build(mid+1,r);
        }
        return root;
    }
    void update(int l,int r,int x,int &y,int val){
         y=++tot;
         s[y]=s[x]+1;
         if(l==r)return;
         lr[y]=lr[x],rr[y]=rr[x];
         int mid=(l+r)/2;
         if(val<=mid)update(l,mid,lr[x],lr[y],val);
         else update(mid+1,r,rr[x],rr[y],val);
    }
    int query(int l,int r,int x,int y,int val){
         if(l==r)return l;
         int mid=(l+r)/2;
         int sum=s[lr[y]]-s[lr[x]];
         if(sum>=val)return query(l,mid,lr[x],lr[y],val);
         else return query(mid+1,r,rr[x],rr[y],val-sum);
         return 0;
    }
    int main()
    {
        scanf("%d",&T);
        while(T--){
            tot=0;
            scanf("%d%d",&n,&m);
            REP(i, 1, n){
                scanf("%d",&p[i]);
                q[i]=p[i];
            }
            sort(q+1,q+1+n);
            int cnt=unique(q+1,q+1+n)-q-1;
            rt[0]=build(1,cnt);
            REP(i, 1, n){
                 int now=lower_bound(q+1,q+1+cnt,p[i])-q;
                 update(1,cnt,rt[i-1],rt[i],now);
            }
            while(m--){
                int l,r,k;
                scanf("%d%d%d",&l,&r,&k);
                cout<<q[query(1,cnt,rt[l-1],rt[r],k)]<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    Spring Boot 详细简介
    Linux 安装 MySQL 8 数据库(图文详细教程)
    有了这个日期工具类,让日期转化不再烦恼
    Linux常用实用命令
    Java分割中英文,并且中文不能分割一半?
    Spring MVC或Spring Boot配置默认访问页面不生效?
    js如何判断当前页面是否处于激活状态
    博客园 & 陌上花开HIMMR | 脱单倒计时!只能帮你到这了
    博客园 & 陌上花开HIMMR | 距2020年脱单,只剩34天!
    博客园 & 陌上花开HIMMR | 脱单倒计时!刚过完10.24的你,还想一个人过11.11吗?
  • 原文地址:https://www.cnblogs.com/czy-power/p/10371821.html
Copyright © 2011-2022 走看看