1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include<iostream> #include<cstdio> #include<algorithm> using namespace std; int a[200005],ls[200005],nn; int root[200005]; int sum[4322005],lson[4322005],rson[4322005],num; int n,m; inline int read() { char ch=0;int sum=0,fu=1; while ((ch>'9'||ch<'0')&&(ch!='-')) ch=getchar(); if (ch=='-') fu=-1,ch=getchar(); while (ch<='9'&&ch>='0') sum=sum*10+ch-'0',ch=getchar(); return fu*sum; } void build(int &k,int l,int r) { k=++num; if (l==r) return; int mid=(l+r)>>1; build(lson[k],l,mid); build(rson[k],mid+1,r); } void update(int oldp,int &newp,int l,int r,int ll) { newp=++num; if (l==ll && l==r) { sum[newp]=sum[oldp]+1; return; } int mid=(l+r)>>1; lson[newp]=lson[oldp];rson[newp]=rson[oldp]; if (mid>=ll) update(lson[oldp],lson[newp],l,mid,ll); else update(rson[oldp],rson[newp],mid+1,r,ll); sum[newp]=sum[lson[newp]]+sum[rson[newp]]; } int query(int p1,int p2,int l,int r,int k) { if (l==r) return l; int mid=(l+r)>>1,tmp=sum[lson[p2]]-sum[lson[p1]]; if (tmp>=k) return query(lson[p1],lson[p2],l,mid,k); else return query(rson[p1],rson[p2],mid+1,r,k-tmp); } int main() { n=read();m=read(); for (int i=1;i<=n;i++) ls[i]=a[i]=read(); sort(ls+1,ls+n+1); nn=unique(ls+1,ls+n+1)-ls-1; build(root[0],1,n); for (int i=1;i<=n;i++) { int po=lower_bound(ls+1,ls+nn+1,a[i])-ls; update(root[i-1],root[i],1,n,po); } while (m--) { int x=read(),y=read(),k=read(); printf("%d\n",ls[query(root[x-1],root[y],1,n,k)]); } return 0; }
|