Kth number

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>
using namespace std;

#define rep(i,a,b) for(int i=a;i<b;i++)
const int maxn=1e5+10;

int sz[maxn],h[maxn];
int T[maxn],L[maxn*20],R[maxn*20],sum[maxn*20];

int tot;

void build(int& rt,int l,int r){

    rt=++tot;
    //printf("rt:%d l:%d r:%d\n",rt,l,r);
    sum[rt]=0;
    if(l==r)return;
    int mid=(l+r)>>1;
    build(L[rt],l,mid);
    build(R[rt],mid+1,r);
}

void update(int& rt,int l,int r,int pre,int x){
    rt=++tot;
    L[rt]=L[pre];
    R[rt]=R[pre];
    sum[rt]=sum[pre]+1;
    if(l==r) return;
    int mid=(l+r)>>1;
    if(x<=mid)update(L[rt],l,mid,L[pre],x);
    else update(R[rt],mid+1,r,R[pre],x);
}

int query(int s,int e,int l,int r,int k){
    if(l==r)return l;
    int mid=(l+r)>>1;
    int res=sum[L[e]]-sum[L[s]];
    if(k<=res)return query(L[s],L[e],l,mid,k);
    else return query(R[s],R[e],mid+1,r,k-res);
}

int main(){
    int Kase;
    scanf("%d",&Kase);
    while(Kase--){
        int n,q;
        scanf("%d %d",&n,&q);
        rep(i,1,n+1) scanf("%d",&sz[i]),h[i]=sz[i];

        sort(h+1,h+n+1);
        int num=unique(h+1,h+n+1)-h-1;

        tot=0;
        build(T[0],1,num);

        rep(i,1,n+1) update(T[i],1,num,T[i-1],lower_bound(h+1,h+num+1,sz[i])-h);

        while(q--){
            int ql,qr,k;
            scanf("%d %d %d",&ql,&qr,&k);
            int ans=query(T[ql-1],T[qr],1,num,k);
            printf("%d\n",h[ans]);
        }

    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值