模板—主席树(不修改)

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
    int l,r,sum;
    #define l(x) tr[x].l
    #define r(x) tr[x].r
    #define sum(x) tr[x].sum
}tr[100000000];

int cnt,T[100010];
int n,Q,m,a[100010],b[100010];

int build(int l,int r)
{
    int now=++cnt;
    if(l<r)
    {
        int mid=(l+r)>>1;
        l(now)=build(l,mid);
        r(now)=build(mid+1,r);
    }
    return now;
}
void build_new(int mark,int loc)
{
    T[mark]=++cnt;
    sum(cnt)=sum(T[mark-1])+1;
    int l=1,r=m,mid,now=cnt,still=T[mark-1];
    for(;l(still)||r(still);)
    {
        mid=(l+r)>>1;
        if(loc>mid)
        {
            l(now)=l(still),r(now)=++cnt;
            sum(cnt)=sum(r(still))+1;
            now=cnt;still=r(still);
            l=mid+1;
        }
        else
        {
            r(now)=r(still),l(now)=++cnt;
            sum(cnt)=sum(l(still))+1;
            now=cnt;still=l(still);
            r=mid;
        }
    }
}
int ask(int a,int b,int l,int r,int k)
{
    if(l==r)    return l;
    int lm=sum(l(b))-sum(l(a)),
        mid=(l+r)>>1;
    if(k<=lm)return ask(l(a),l(b),l,mid,k);
    return ask(r(a),r(b),mid+1,r,k-lm);
}
signed main()
{
    cin>>n>>Q;
    for(int i=1;i<=n;i++)cin>>a[i],b[i]=a[i];
    sort(b+1,b+n+1);
    m=unique(b+1,b+n+1)-b-1;
    T[0]=build(1,m);
    for(int i=1;i<=n;i++)
    {
        int loc=lower_bound(b+1,b+n+1,a[i])-b;
        build_new(i,loc);
    }
    int i,j,k;
    for(int num=1;num<=Q;num++)
    {
        cin>>i>>j>>k;
        cout<<b[ask(T[i-1],T[j],1,m,k)]<<endl;
    }
}
View Code

 

posted @ 2019-06-14 12:11  Al_Ca  阅读(126)  评论(0编辑  收藏  举报
ヾ(≧O≦)〃嗷~