HDU 2665 Kth number(主席树静态区间第K大)题解

时间:2022-09-10 05:47:19

题意:问你区间第k大是谁

思路:主席树就是可持久化线段树,他是由多个历史版本的权值线段树(不是普通线段树)组成的。

具体可以看q学姐的B站视频

代码:

#include<cmath>
#include<set>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5 + ;
const int M = maxn * ;
const ull seed = ;
const int INF = 0x3f3f3f3f;
const int MOD = ;
int n, q, tot;
int a[maxn], root[maxn];
vector<int> st;
int getId(int x){
return lower_bound(st.begin(), st.end(),x) - st.begin() + ;
}
struct node{
int lson, rson;
int sum;
}T[maxn * ];
void update(int l, int r, int &now, int pre, int v, int pos){
T[++tot] = T[pre], T[tot].sum += v, now = tot;
if(l == r) return;
int m = (l + r) >> ;
if(m >= pos)
update(l, m, T[now].lson, T[pre].lson, v, pos);
else
update(m + , r, T[now].rson, T[pre].rson, v, pos);
}
int query(int l, int r, int pre, int now, int k){
if(l == r) return l;
int m = (l + r) >> ;
int sum = T[T[now].lson].sum - T[T[pre].lson].sum;
if(sum >= k)
return query(l, m, T[pre].lson, T[now].lson, k);
else
return query(m + , r, T[pre].rson, T[now].rson, k - sum);
}
int main(){
int t;
scanf("%d", &t);
while(t--){
tot = ;
scanf("%d%d", &n, &q);
st.clear();
for(int i = ; i <= n; i++)
scanf("%d", &a[i]), st.push_back(a[i]);
sort(st.begin(), st.end());
st.erase(unique(st.begin(), st.end()), st.end()); for(int i = ; i <= n; i++)
update(, n, root[i], root[i - ], , getId(a[i]));
while(q--){
int l, r, k;
scanf("%d%d%d", &l, &r, &k);
printf("%d\n", st[query(, n, root[l - ], root[r], k) - ]);
}
}
return ;
}