#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,root[200005],a[200005],len,b[200005],tot,li,ri,ki,ans;
struct segmenttree{
int lc,rc;
int cnt;
}tree[4000005];
int build(int l,int r)
{
int p=++tot;
tree[p].cnt=0;
if(l==r) return p;
int mid=(l+r)>>1;
tree[p].lc=build(l,mid);
tree[p].rc=build(mid+1,r);
return p;
}
int insert(int now, int l, int r, int x, int delta) {
int p = ++tot;
tree[p] = tree[now];
if (l == r) {
tree[p].cnt += delta;
return p;
}
int mid = (l + r) >> 1;
if (x <= mid) tree[p].lc = insert(tree[now].lc, l, mid, x, delta);
else tree[p].rc = insert(tree[now].rc, mid + 1, r, x, delta);
tree[p].cnt = tree[tree[p].lc].cnt + tree[tree[p].rc].cnt;
return p;
}
int ask(int p, int q, int l, int r, int k) {
if (l == r) return l;
int mid = (l + r) >> 1;
int lcnt = tree[tree[p].lc].cnt - tree[tree[q].lc].cnt;
if (k <= lcnt) return ask(tree[p].lc, tree[q].lc, l, mid, k);
else return ask(tree[p].rc, tree[q].rc, mid + 1, r, k - lcnt);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+1,b+1+n);
len=unique(b+1,b+n+1)-b-1;
root[0]=build(1,len);
for(int i=1;i<=n;i++)
{
int tmp=lower_bound(b+1,b+len+1,a[i])-b;
root[i]=insert(root[i-1],1,len,tmp,1);
}
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&li,&ri,&ki);
ans=ask(root[ri],root[li-1],1,len,ki);
cout<<b[ans]<<endl;
}
return 0;
}
可持久化线段树(主席树)
最新推荐文章于 2022-11-15 19:29:36 发布