题意:求多次查询区间第k大
题解:主席树模板题
自己太菜了,花了两天时间才把主席树给搞懂,哎,可能我的智商都不适合搞acm吧
写了一份模板安慰一下自己(模板待更新)
//离散化
#define DCT(dct) SOT(dct), ERA(dct)
#define SOT(dct) sort(dct.begin(), dct.end())
#define ERA(dct) dct.erase(unique(dct.begin(), dct.end()), dct.end())
#define GID(dct, num) (int)(lower_bound(dct.begin(), dct.end(), num) - dct.begin())
vector<int>v;
//主席树模板
struct dd{ int l, r, sum; } tree[maxn<<5];
int root[maxn], a[maxn], cnt = 0;
void build(int l, int r, int &x){
x = ++ cnt;
if(l == r) return ;
int mid = (l + r) >> 1;
build(l ,mid, tree[x].l);
build(mid+1, r, tree[x].r);
}
int update(int l, int r, int pre, int pos){
int now = ++ cnt;
tree[now] = tree[pre]; tree[cnt].sum ++;
if(l == r) return now;
int mid = (l + r) >> 1;
if(pos <= mid) tree[now].l = update(l, mid, tree[pre].l, pos);
else tree[now].r = update(mid+1, r, tree[pre].r, pos);
return now;
}
int query(int l, int r, int lo, int ro, int k){
int sum = tree[tree[ro].l].sum - tree[tree[lo].l].sum;
int mid = (l + r) >> 1;
if(l == r) return l;
if(k <= sum) return query(l, mid, tree[lo].l, tree[ro].l, k);
else return query(mid + 1, r, tree[lo].r, tree[ro].r, k-sum);
}
//#include"bits/stdc++.h"
//#include<unordered_map>
//#include<unordered_set>
#include<iostream>
#include<sstream>
#include<iterator>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
#define lson l, mid, node<<1
#define rson mid + 1, r, node<<1|1
#define Root 1, t, 1
#define Node l, r, node
const int INF = 0x3f3f3f3f;
const int O = 1e6;
const int mod = 20071027;
const int maxn = 1e5 + 5;
const double PI = acos(-1.0);
const double E = 2.718281828459;
//离散化
#define DCT(dct) SOT(dct), ERA(dct)
#define SOT(dct) sort(dct.begin(), dct.end())
#define ERA(dct) dct.erase(unique(dct.begin(), dct.end()), dct.end())
#define GID(dct, num) (int)(lower_bound(dct.begin(), dct.end(), num) - dct.begin())
vector<int>v;
//主席树模板
struct dd{ int l, r, sum; } tree[maxn<<5];
int root[maxn], a[maxn], cnt = 0;
void build(int l, int r, int &x){
x = ++ cnt;
if(l == r) return ;
int mid = (l + r) >> 1;
build(l ,mid, tree[x].l);
build(mid+1, r, tree[x].r);
}
int update(int l, int r, int pre, int pos){
int now = ++ cnt;
tree[now] = tree[pre]; tree[cnt].sum ++;
if(l == r) return now;
int mid = (l + r) >> 1;
if(pos <= mid) tree[now].l = update(l, mid, tree[pre].l, pos);
else tree[now].r = update(mid+1, r, tree[pre].r, pos);
return now;
}
int query(int l, int r, int lo, int ro, int k){
int sum = tree[tree[ro].l].sum - tree[tree[lo].l].sum;
int mid = (l + r) >> 1;
if(l == r) return l;
if(k <= sum) return query(l, mid, tree[lo].l, tree[ro].l, k);
else return query(mid + 1, r, tree[lo].r, tree[ro].r, k-sum);
}
int main(){
int T; scanf("%d", &T);
while( T -- ){
int n, m; scanf("%d%d", &n, &m);
for(int i=1; i<=n; i++) { scanf("%d", &a[i]); v.push_back(a[i]); }
DCT(v); //离散化
int t = (int)v.size();
cnt = 0; build(1, t, root[0]);
for(int i=1; i<=n; i++) root[i] = update(1, t, root[i-1], GID(v, a[i]) + 1);
while(m -- ){
int ql, qr, k; scanf("%d%d%d", &ql, &qr, &k);
int ans = query(1, t, root[ql-1], root[qr], k);
printf("%d\n", v[ans-1]);
}
}
return 0;
}