主席树模板
作者:互联网
5.3解锁新姿势
弱弱也会主席树啦
题目:查找区间第K小
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=100010;
struct Node{
int l,r;
int cnt;
}tr[N*4+N*17];
vector <int> num;
int a[N],idx,root[N];
int finx(int x)
{
return lower_bound(num.begin(),num.end(),x)-num.begin();
}
int intree(int p,int l,int r,int x)
{
int q=++idx;
tr[q]=tr[p];
if(l==r)
{
tr[q].cnt++;
return q;
}
int mid=l+r>>1;
if(x<=mid) tr[q].l=intree(tr[p].l,l,mid,x);
else tr[q].r=intree(tr[p].r,mid+1,r,x);
tr[q].cnt=tr[tr[q].l].cnt+tr[tr[q].r].cnt;
return q;
}
int build(int l,int r)
{
int p=++idx;
if(l==r) return p;
int mid=l+r>>1;
tr[p].l=build(l,mid);
tr[p].r=build(mid+1,r);
return p;
}
int quary(int p,int q,int l,int r,int k)
{
if(l==r) return l;
int cnt=tr[tr[q].l].cnt-tr[tr[p].l].cnt;
int mid=l+r>>1;
if(cnt>=k) return quary(tr[p].l,tr[q].l,l,mid,k);
else return quary(tr[p].r,tr[q].r,mid+1,r,k-cnt);
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
scanf("%d",a+i);
num.push_back(a[i]);
}
sort(num.begin(),num.end());
num.erase(unique(num.begin(),num.end()),num.end());
root[0]=build(0,num.size()-1);
for(int i=1;i<=n;i++)
root[i]=intree(root[i-1],0,num.size()-1,finx(a[i]));
while(m--)
{
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",num[quary(root[l-1],root[r],0,num.size()-1,k)]);
}
}
标签:cnt,return,int,tr,mid,num,模板,主席 来源: https://blog.csdn.net/Hello_wcrd/article/details/116379869