这是一道主席树的模板题,
#include<iostream>
#include<set>
#include<map>
#include<algorithm>
#include<vector>
#include<cmath>
#include<climits>
#include<cstring>
#define int long long
const int N = 1e6+5;
using namespace std;
char* p1, * p2, buf[100000];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int read()
{int x = 0, f = 1;char ch = nc();while (ch < 48 || ch>57){if (ch == '-')f = -1;ch = nc();}while (ch >= 48 && ch <= 57)x = x * 10 + ch - 48, ch = nc();return x * f;
}
int id = 0;
vector<int>v;
int a[N];
struct tree {int ch[2];int s = 0;
}tr[N*22];
int root[N];
void build(int& x, int l, int r) {x = ++id;if (l == r)return;int m = (l + r) >> 1;build(tr[x].ch[0], l, m);build(tr[x].ch[1], m + 1, r);
}
void insert(int x, int& y, int l, int r, int v) {y = ++id;tr[y] = tr[x];tr[y].s++;if (l == r)return;int m = (l + r) >> 1;if (v <= m)insert(tr[x].ch[0], tr[y].ch[0], l, m, v);else insert(tr[x].ch[1], tr[y].ch[1], m + 1, r, v);
}
int query(int x, int y, int l, int r, int k) {if (l == r)return l;int m = (l + r) >> 1;int s = tr[tr[y].ch[0]].s - tr[tr[x].ch[0]].s;if (k <= s)return query(tr[x].ch[0], tr[y].ch[0], l, m, k);else return query(tr[x].ch[1], tr[y].ch[1], m+1, r, k-s);
}
int getid(int x) {return lower_bound(v.begin(), v.end(), x) - v.begin() + 1;
}
signed main() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int n, m;cin >> n >> m;for (int i = 1; i <= n; i++) {cin >> a[i];v.push_back(a[i]);}sort(v.begin(), v.end());v.erase(unique(v.begin(), v.end()), v.end());//build(root[0], 1, v.size());for (int i = 1; i <= n; i++) {insert(root[i - 1], root[i], 1, v.size(), getid(a[i]));}for (int i = 1; i <= m; i++) {int l, r, k;cin >> l >> r >> k;int id = query(root[l - 1], root[r], 1, v.size(), k) - 1;cout << v[id] << endl;}return 0;
}