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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| #include <bits/stdc++.h>
#define debug freopen("in.txt","r",stdin); freopen("out.txt","w",stdout) #define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0) using namespace std; typedef long long ll; typedef unsigned long long ull; const int MAXN=1e6+100; const int MOD=1e9+7; const int INF=0x3f3f3f3f; const int SUB=-0x3f3f3f3f; const double eps=1e-4; const double E=exp(1); const double pi=acos(-1); struct node{ int lson,rson,sum; }hjt[MAXN*5]; struct name{ int id,val,fak; }arr[MAXN]; int bak[MAXN]; int root[MAXN*5]; int n,m,k,cnt,mid; bool cmp1(name a,name b){ return a.val<b.val; } bool cmp2(name a,name b){ return a.id<b.id; } int build(int l,int r){ int rt=++cnt; if(l==r) return rt; int mid=l+r>>1; hjt[rt].lson=build(l,mid); hjt[rt].rson=build(mid+1,r); return rt; } int insert(int pre,int l,int r,int pos){ int rt=++cnt; hjt[rt]=hjt[pre]; hjt[rt].sum++; if(l==r) return rt; mid=(l+r)>>1; if(pos<=mid) hjt[rt].lson=insert(hjt[pre].lson,l,mid,pos); else hjt[rt].rson=insert(hjt[pre].rson,mid+1,r,pos); return rt; } int query(int u,int v,int l,int r,int k){ if(l==r) return l; int cha=hjt[hjt[v].lson].sum-hjt[hjt[u].lson].sum; int mid=l+r>>1; if(cha>=k) return query(hjt[u].lson,hjt[v].lson,l,mid,k); else return query(hjt[u].rson,hjt[v].rson,mid+1,r,k-cha); } int main(){ ios; cin>>n>>m; for(int i=1;i<=n;i++){ cin>>arr[i].val; bak[i]=arr[i].val; arr[i].id=i; } sort(bak+1,bak+1+n); sort(arr+1,arr+1+n,cmp1); for(int i=1;i<=n;i++) arr[i].fak=i; sort(arr+1,arr+1+n,cmp2); root[0]=build(1,n); for(int i=1;i<=n;i++) root[i]=insert(root[i-1],1,n,arr[i].fak);
while(m--){ int l,r; cin>>l>>r>>k; cout<<bak[query(root[l-1],root[r],1,n,k)]<<'\n'; } return 0; }
|