#include <stdio.h>
#include <math.h>
#include <set>
#include <algorithm>
#define Maxn 1000005
#define Maxm 1000005
int n,m,blg[Maxn],a[Maxn],b[Maxn],bs[Maxn],cnt[Maxn],len,ans[Maxn],cn,ls,c;
struct lxl {
int id,l,r,k;
bool operator < (const lxl a) const{
return blg[l] == blg[a.l] ? (r < a.r) : (blg[l] < blg[a.l]);
}
} q[Maxm];
struct node{
int id,val;
bool operator < (const node &a) const{
return val < a.val;
}
};
inline int read() {
int res = 0,ch = getchar();
while(ch < '0' || ch > '9') ch = getchar();
while(ch >= '0' && ch <= '9') res = (res << 3) + (res << 1) + (ch - '0') , ch = getchar();
return res;
}
inline void add(int x){
if(!cnt[x]) c++;
b[cnt[x]]--;
bs[blg[cnt[x]]]--;
b[++cnt[x]]++;
bs[blg[cnt[x]]]++;
}
inline void del(int x){
b[cnt[x]]--;
bs[blg[cnt[x]]]--;
b[--cnt[x]]++;
bs[blg[cnt[x]]]++;
if(!cnt[x]) c--;
}
inline int find(int k)
{
if(k > c) return -1;
int f = 1,p;
while(k > bs[f]) k -= bs[f],f++;
p = (f - 1) * len + 1;
while(k > b[p]) k -= b[p],p++;
return p;
}
std::multiset <node> s;
int main() {
n = read(),m = read();
len = (int) sqrt(n);
for(int i = 1 ; i <= n ; i++) blg[i] = (i - 1) / n + 1 , s.insert({i , read()});
for(std::multiset<node>::iterator it = s.begin() ; it != s.end() ; ls = it->val,it++) a[it->id] = it->val == ls ? cn : ++cn;
for(int i = 1 ; i <= m ; i++) q[i] = {i , read() , read() , read()};
std::sort(q + 1, q + m + 1);
int l = 1,r = 0;
for(int i = 1 ; i <= m ; i++)
{
while(l > q[i].l) add(a[--l]);
while(r < q[i].r) add(a[++r]);
while(l < q[i].l) del(a[l++]);
while(r > q[i].r) del(a[r--]);
ans[q[i].id] = find(q[i].k);
}
for(int i = 1 ; i <= m ; i++) printf("%d\n",ans[i]);
return 0;
}
我觉得复杂度是没问题的,为什么 7 个点全部 TLE 了