#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<math.h>
#include<set>
using namespace std;
vector<int>v;
int getid(int x)
{
return lower_bound(v.begin(), v.end(), x) - v.begin() + 1;//查找位置
}
struct Node
{
int l, r, sum;
}hjt[2000000];
int cnt, root[2000000], a[201000];//cnt记录个状态
void insert(int l, int r, int pre, int &now, int p)
{
hjt[++cnt] = hjt[pre];
now = cnt;//更新树
hjt[now].sum++;//
if (l == r)
{
return;
}
int m = (l + r) >> 1;
if (p <= m)
{
insert(l, m, hjt[pre].l, hjt[now].l, p);//传入上一个和现在的
}
else
{
insert(m + 1, r, hjt[pre].r, hjt[now].r, p);
}
}
int query(int l, int r, int L, int R, int k)
{
if (l == r)
{
return l;
}
int m = (l + r) >> 1;
int tmp = hjt[hjt[R].l].sum - hjt[hjt[L].l].sum;//R,L的子树的sum值比较
if (k <= tmp)
{
return query(l, m, hjt[L].l, hjt[R].l, k);//传两个节点,比较他们的儿子的L,R
}
else
{
return query(m + 1, r, hjt[L].r, hjt[R].r, k - tmp);
}
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
v.push_back(a[i]);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());//去重
for (int i = 1; i <= n; i++)
{
insert(1, n, root[i - 1], root[i], getid(a[i]));//root记录当前根在第几个状态
}
while (m--)
{
int l, r, k;
scanf("%d%d%d", &l, &r, &k);
printf("%d\n", v[query(1, n, root[l - 1], root[r], k) - 1]);
}
return 0;
}
09-09