K-th Number
http://poj.org/problem?id=2104
https://blog.csdn.net/Daxian911/article/details/89075748
题目:
代码:
#include<iostream> #include<stdio.h> #include<algorithm> using namespace std; const int maxn = 1e5+6; int a[maxn],b[maxn],root[maxn]; int n,q,tot=0; struct node{ int l,r; int val; }e[maxn*22]; void build(int l,int r,int &cur){ cur=++tot; e[cur].val = 0; if(l==r) return ; int mid = l+r>>1; build(l,mid,e[cur].l); build(mid+1,r,e[cur].r); } void update(int pre,int &cur,int l,int r,int pos){ cur = ++tot; e[cur] = e[pre]; e[cur].val++; if(l==r) return ; int mid = r+l>>1; if(pos<=mid) update(e[pre].l,e[cur].l,l,mid,pos); else update(e[pre].r,e[cur].r,mid+1,r,pos); } int query(int pre,int cur,int l,int r,int val){ if(l==r) return l; int sum = e[e[cur].l].val -e[e[pre].l].val; int mid = l+r>>1; if(val<=sum) return query(e[pre].l,e[cur].l,l,mid,val); else return query(e[pre].r,e[cur].r,mid+1,r,val-sum); } int main(){ scanf("%d%d",&n,&q); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); b[i] = a[i]; } sort(b+1,b+1+n); int len = unique(b+1,b+1+n)-(b+1); build(1,len,root[0]); for(int i=1;i<=n;i++){ int x = lower_bound(b+1,b+len+1,a[i])-b; update(root[i-1],root[i],1,len,x); } while(q--){ int l,r,val; scanf("%d%d%d",&l,&r,&val); printf("%d\n",b[query(root[l-1],root[r],1,len,val)]); } return 0; }