原理:https://www.zhihu.com/question/31133885
教程:http://www.cnblogs.com/zyf0163/p/4749042.html
基本半天可以学会,不好的教程可能扣一天
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef float ft;
typedef double db;
typedef long long LL;
#define lson i << 1
#define rson (i << 1) + 1
#define pi acos(-1.0)
#define mod 1000000007ll
#define maxn 100005
#define mid (l + r) / 2
#define lowbit(i) ((i)&(-i))
#define fl(i,n) for(int i = 0; i < n; i++)
#define fll(i, n) for(int i = 1; i <= n; i++)
#define flab(i, a, b) for(int i = a; i <= b; i++)
#define fr(i,n) for(int i = n - 1; i >=0 ;i--)
#define frr(i,n) for(int i = n; i >= 0; i--)
#define frab(i,a,b) for(int i = b; i >= a; i--)
template<class T> T Min(T a, T b){return a < b? a : b;}
template<class T> T Max(T a, T b){return a > b ? a : b;}
template<class T> T Abs(T x){if(x < 0) x = -x; return x;}
template<class T> void Swap(T& a, T& b){T t = a; a = b; b = a;}
template<class T> T fastMod(T &a, int n){T ans = 1;while(n){if(n&1)ans *= a;a *= a;n >>= 1;}return ans;}
using namespace std;
//-------------------------------------------------------------------------------------------------------------------------------------------
int cnt,rt[maxn * 20], ls[maxn * 20], rs[maxn * 20], sum[maxn * 20], a[maxn], b[maxn];
int n, m, Size;
void build(int &flag, int l, int r)
{
flag = ++ cnt;
sum[flag] = 0;
if(l == r) return;
build(ls[flag], l, mid);
build(rs[flag], mid + 1, r);
}
void update(int &flag, int l, int r, int last, int p)
{
flag = ++cnt;
ls[flag] = ls[last];
rs[flag] = rs[last];
sum[flag] = sum[last] + 1;
if(l == r) return ;
if(p <= mid) update(ls[flag], l, mid, ls[last], p);
else update(rs[flag], mid + 1, r, rs[last], p);
}
int query(int pre, int last, int l, int r, int k)
{
if(l == r) return l;
int SUM = sum[ls[pre]] - sum[ls[last]];
if(SUM >= k) return query(ls[pre], ls[last], l, mid, k);
else return query(rs[pre], rs[last], mid + 1, r, k - SUM);
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d%d",&n,&m);
fll(i, n)
{
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b + 1, b + n + 1);
Size = unique(b + 1, b + n + 1) - (b + 1);
cnt = 0;
build(rt[0], 1, Size);
fll(i,n)
{
int pre = lower_bound(b + 1, b + Size + 1, a[i]) - b;
update(rt[i], 1, Size, rt[i - 1], pre);
}
while(m--)
{
int aa, bb, k;
scanf("%d%d%d", &aa, &bb, &k);
cout << b[query(rt[bb], rt[aa - 1], 1, Size, k)] << endl;
}
}
return 0;
}