跟线段树类似的,对于每一层的中间位置的数 把比他小的放左边,比他大的放右边
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
const int maxn = 100000 + 7;
int s[maxn];
int tree[25][maxn];
int toleft[25][maxn];
int T, n, m;
void Build(int d, int l, int r) {
if(l == r) return ;
int mid = (l + r) / 2;
int same = mid - l + 1;
for(int i = l; i <= r; ++i)
if(tree[d][i] < s[mid]) same--;
int left = l, right = mid + 1;
for(int i = l; i <= r; ++i) {
if(tree[d][i] < s[mid])
tree[d+1][left++] = tree[d][i];
else if(tree[d][i] == s[mid] && same > 0)
tree[d+1][left++] = tree[d][i], same--;
else tree[d+1][right++] = tree[d][i];
toleft[d][i] = toleft[d][l-1] + left - l;
}
Build(d + 1, l, mid);
Build(d + 1, mid + 1, r);
}
int Query(int L, int R, int l, int r, int d, int k) {
if(l == r) return tree[d][l];
int mid = (L + R) / 2;
int cnt = toleft[d][r] - toleft[d][l-1];
if(cnt >= k) {
int newl = L + toleft[d][l-1] - toleft[d][L-1];
int newr = newl + cnt - 1;
return Query(L, mid, newl, newr, d + 1, k);
} else {
int newr = r + toleft[d][R] - toleft[d][r];
int newl = newr - (r - l - cnt);
return Query(mid + 1, R, newl, newr, d + 1, k - cnt);
}
}
int main() {
scanf("%d", &T);
while(T--) {
memset(tree, 0, sizeof(tree));
memset(toleft, 0, sizeof(toleft));
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i) {
scanf("%d", &tree[0][i]);
s[i] = tree[0][i];
}
sort(s + 1, s + n + 1);
Build(0, 1, n);
// for(int i = 1; i <= n; ++i) {
// cout << tree[2][i] << " ";
// }
// cout << endl;
int u, v, k;
for(int i = 0; i < m; ++i) {
scanf("%d%d%d", &u, &v, &k);
printf("%d\n", Query(1, n, u, v, 0, k));
}
}
return 0;
}