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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| #include <iostream> #include <vector> #include <cmath> #include <cstring> #include <algorithm> using namespace std;
const int N = 100005,M = 320; class inquiry{ public: int l,r,x,block,id; inline bool operator < (const inquiry &tmp)const{ if(block!=tmp.block) return l<tmp.l; return block&1?r<tmp.r:r>tmp.r; } }; vector<inquiry> qus; int ans[N],cnt[N]; vector<int> has; int block,len; int a[N]; int n,m;
namespace sqrtrange{ int cnt[N],sum[M];
inline void update(int x,int c){ cnt[x] += c; sum[x/len] += c; }
inline int query(int x){ int top = ceil(n*1.0/len); for(int k=0;k<top;k++){ if(x>sum[k]) x -= sum[k]; else{ int l = k*len,r = min((k+1)*len,n); for(int j=l;j<=r;j++){ if(x>cnt[j]) x -= cnt[j]; else return j; } } } return -1; } }
inline int h(int x){ return lower_bound(has.begin(),has.end(),x)-has.begin(); }
inline void add(int x){ if(cnt[a[x]]) sqrtrange::update(cnt[a[x]],-1); cnt[a[x]]++; sqrtrange::update(cnt[a[x]],1); }
inline void del(int x){ sqrtrange::update(cnt[a[x]],-1); cnt[a[x]]--; if(cnt[a[x]]) sqrtrange::update(cnt[a[x]],1); }
int main(){ n = in(),m = in(); block = ceil(n/sqrt(m)); for(int k=1;k<=n;k++) has.emplace_back(a[k]=in()); sort(has.begin(),has.end()); has.erase(unique(has.begin(),has.end()),has.end()); for(int k=1;k<=n;k++) a[k] = h(a[k]); len = ceil(sqrt(n)); for(int k=1;k<=m;k++){ int l = in(),r = in(),x = in(); qus.push_back({l,r,x,l/block,k}); } sort(qus.begin(),qus.end()); int L = 1,R = 0; for(auto[l,r,x,tmp,id]:qus){ while(l<L) add(--L); while(l>L) del(L++); while(r<R) del(R--); while(r>R) add(++R); ans[id] = sqrtrange::query(x); } for(int k=1;k<=m;k++){ out(ans[k]); putchar('\n'); } return 0; }
|