主席树模板题,这真的是模板题,好了,那就把模板贴在这里吧。
#include<iostream>
#include<algorithm>
#define int long long
using namespace std;
const int maxn = 1e5+10;
int n,q,m,tot,a[maxn],t[maxn],T[maxn],lson[maxn<<5],rson[maxn<<5],c[maxn<<5];
void Init()
{
for(int i = 1;i <= n; i++)
t[i] = a[i];
sort(t+1,t+1+n);
m = unique(t+1,t+1+n) - t - 1;
}
int Build(int l,int r)
{
int root = tot++;
c[root] = 0;
if(l != r)
{
int mid = l + (r - l) / 2;
lson[root] = Build(l,mid);
rson[root] = Build(mid + 1,r);
}
return root;
}
int Hash(int x)
{
return lower_bound(t + 1,t + 1 + m,x) - t;
}
int Update(int root,int pos,int val)
{
int newroot = tot++;
int tmp = newroot;
c[newroot] = c[root] + val;
int l = 1,r = m;
while(l < r)
{
int mid = l + (r - l) / 2;
if(pos <= mid)
{
lson[newroot] = tot++;
rson[newroot] = rson[root];
newroot = lson[newroot];
root = lson[root];
r = mid;
}
else
{
rson[newroot] = tot++;
lson[newroot] = lson[root];
newroot = rson[newroot];
root = rson[root];
l = mid + 1;
}
c[newroot] = c[root] + val;
}
return tmp;
}
int Query(int left_root,int right_root,int k)
{
int l = 1,r = m;
while(l < r)
{
int mid = l + (r - l) / 2;
if(c[lson[left_root]] - c[lson[right_root]] >= k)
{
r = mid;
left_root = lson[left_root];
right_root = lson[right_root];
}
else
{
l = mid + 1;
k -= c[lson[left_root]] - c[lson[right_root]];
left_root = rson[left_root];
right_root = rson[right_root];
}
}
return l;
}
signed main()
{
//freopen("in","r",stdin);
ios::sync_with_stdio(false);
cin.tie(0);
while(cin >> n >> q)
{
tot = 0;
for(int i = 1;i <= n; i++)
cin >> a[i];
Init();
T[n+1] = Build(1,m);
for(int i = n;i >= 0; i--)
{
int pos = Hash(a[i]);
T[i] = Update(T[i+1],pos,1);
}
while(q--)
{
int l,r,k;
cin >> l >> r >> k;
cout << t[Query(T[l],T[r+1],k)] << endl;
}
}
return 0;
}