#include <bits/stdc++.h>
using namespace std;
#define maxn 100100
#define gc getchar_unlocked
void scanint(int &x)
{
register int c = gc();
x = 0;
bool neg=false;
if(c=='-')
neg=true;
for(;(c<48 || c>57);c = gc());
for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
if(neg)
x=-x;
}
struct node{
int cnt;
node *left;
node *right;
node(){
this->cnt=0;
this->left=this;
this->right=this;
}
node(int cnt, node *left, node *right):cnt(cnt), left(left), right(right){}
node *insert(int L, int R, int val);
};
node *null;
node *tree[maxn];
node *node::insert(int L, int R, int val)
{
if(L==R)
return new node(this->cnt+1, null, null); //insert no. in node
else{
int mid = (L+R)>>1;
if(val<=mid) //val lies in left child, update left
return new node(this->cnt+1, this->left->insert(L, mid, val), this->right);
else // val lies in right child, update right
return new node(this->cnt+1, this->left, this->right->insert(mid+1, R, val));
}
}
int getkth(node *low, node *high, int L, int R, int val)
{
if(L==R)
return L;
int lsum = high->left->cnt - low->left->cnt;
int mid = (L+R)>>1;
if(lsum>=val)
return getkth(low->left, high->left, L, mid, val);
else
return (getkth(low->right, high->right, mid+1, R, val-lsum));
}
int main()
{
int n, m, i, l, r, k;
scanint(n);
scanint(m);
int arr[n];
map<int, int> m1;
for(i=0; i<n; i++){
scanint(arr[i]);
m1[arr[i]];
}
map<int, int>::iterator it;
int ind=0;
int sor[n];
for(it=m1.begin(); it!=m1.end(); ++it){
m1[it->first] = ind;
sor[ind] = it->first;
ind++;
}
tree[0] = new node();
for(i=1; i<=n; i++)
tree[i] = tree[i-1]->insert(0, ind, m1[arr[i-1]]);
while(m--){
scanint(l);
scanint(r);
scanint(k);
int ans = sor[getkth(tree[l-1], tree[r], 0, ind, k)];
printf("%d\n", ans);
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKI2RlZmluZSBtYXhuIDEwMDEwMAojZGVmaW5lIGdjIGdldGNoYXJfdW5sb2NrZWQKCnZvaWQgc2NhbmludChpbnQgJngpCnsKICAgIHJlZ2lzdGVyIGludCBjID0gZ2MoKTsKICAgIHggPSAwOwogICAgYm9vbCBuZWc9ZmFsc2U7CiAgICBpZihjPT0nLScpCiAgICAgICAgbmVnPXRydWU7CiAgICBmb3IoOyhjPDQ4IHx8IGM+NTcpO2MgPSBnYygpKTsKICAgIGZvcig7Yz40NyAmJiBjPDU4O2MgPSBnYygpKSB7eCA9ICh4PDwxKSArICh4PDwzKSArIGMgLSA0ODt9CiAgICBpZihuZWcpCiAgICAgICAgeD0teDsKfQoKc3RydWN0IG5vZGV7CiAgICBpbnQgY250OwogICAgbm9kZSAqbGVmdDsKICAgIG5vZGUgKnJpZ2h0OwogICAgbm9kZSgpewogICAgICAgIHRoaXMtPmNudD0wOwogICAgICAgIHRoaXMtPmxlZnQ9dGhpczsKICAgICAgICB0aGlzLT5yaWdodD10aGlzOwogICAgfQogICAgbm9kZShpbnQgY250LCBub2RlICpsZWZ0LCBub2RlICpyaWdodCk6Y250KGNudCksIGxlZnQobGVmdCksIHJpZ2h0KHJpZ2h0KXt9CiAgICBub2RlICppbnNlcnQoaW50IEwsIGludCBSLCBpbnQgdmFsKTsKfTsKCm5vZGUgKm51bGw7Cm5vZGUgKnRyZWVbbWF4bl07Cgpub2RlICpub2RlOjppbnNlcnQoaW50IEwsIGludCBSLCBpbnQgdmFsKQp7CiAgICBpZihMPT1SKQogICAgICAgIHJldHVybiBuZXcgbm9kZSh0aGlzLT5jbnQrMSwgbnVsbCwgbnVsbCk7IC8vaW5zZXJ0IG5vLiBpbiBub2RlCiAgICBlbHNlewogICAgICAgIGludCBtaWQgPSAoTCtSKT4+MTsKICAgICAgICBpZih2YWw8PW1pZCkgLy92YWwgbGllcyBpbiBsZWZ0IGNoaWxkLCB1cGRhdGUgbGVmdAogICAgICAgICAgICByZXR1cm4gbmV3IG5vZGUodGhpcy0+Y250KzEsIHRoaXMtPmxlZnQtPmluc2VydChMLCBtaWQsIHZhbCksIHRoaXMt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ICBpbmQrKzsKICAgIH0KICAgIHRyZWVbMF0gPSBuZXcgbm9kZSgpOwogICAgZm9yKGk9MTsgaTw9bjsgaSsrKQogICAgICAgIHRyZWVbaV0gPSB0cmVlW2ktMV0tPmluc2VydCgwLCBpbmQsIG0xW2FycltpLTFdXSk7CiAgICB3aGlsZShtLS0pewogICAgICAgIHNjYW5pbnQobCk7CiAgICAgICAgc2NhbmludChyKTsKICAgICAgICBzY2FuaW50KGspOwogICAgICAgIGludCBhbnMgPSBzb3JbZ2V0a3RoKHRyZWVbbC0xXSwgdHJlZVtyXSwgMCwgaW5kLCBrKV07CiAgICAgICAgcHJpbnRmKCIlZFxuIiwgYW5zKTsKICAgIH0KICAgIHJldHVybiAwOwp9Cg==