• POJ2104主席树板子


    #include<iostream>
    #include <algorithm>
    #define pb push_back
    #define fi first
    #define se second
    #define io std::ios::sync_with_stdio(false)
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> pii;
    const int P = 1e9+7, INF = 0x3f3f3f3f;
    const int maxn=1e5+10;
    int ls[maxn*40],rs[maxn*40];
    int sum[maxn*40];
    int rt[maxn];
    int cnt;
    void update(int pre,int &now,int l,int r,int x)
    {
       now=++cnt;
       sum[now]=sum[pre]+1;
       ls[now]=ls[pre];
       rs[now]=rs[pre];
       if(l==r)
       {
           return ;
       }
       int mid=(l+r)/2;
       if(x>mid)
       {
           update(rs[pre],rs[now],mid+1,r,x);
       }
       else
       {
           update(ls[pre],ls[now],l,mid,x);
       }
    }
    int query(int pre,int now,int l,int r,int k)
    {
        int sm=sum[ls[now]]-sum[ls[pre]];
        int mid=(l+r)/2;
        if(l==r)
        return l;
        if(k<=sm)
        {
         return query(ls[pre],ls[now],l,mid,k);
        }
        else
        {
            return query(rs[pre],rs[now],mid+1,r,k-sm);
        }
    }
    int id[maxn];
    int a[maxn];
    bool cmp(int x,int y)
    {
        return a[x]<a[y];
    }
    int main()
    {
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            id[i]=i;
        }
        sort(id+1,id+1+n,cmp);
        int _rank[maxn];
        for(int i=1;i<=n;i++)
        {
            _rank[id[i]]=i;
        }
        for(int i=1;i<=n;i++)
        {
            update(rt[i-1],rt[i],1,n,_rank[i]);
        }
        while(m--)
        {
            int l,r,k;
            cin>>l>>r>>k;
            cout<<a[id[query(rt[l-1],rt[r],1,n,k)]]<<endl;
        }
    }
  • 相关阅读:
    代码1
    js中级第13天
    dom 浏览器模型
    js中级第12天
    js中级第11天
    js中级第十天
    js中级第九天
    js中级第8天
    js中级第六天
    js中级第七天
  • 原文地址:https://www.cnblogs.com/acmLLF/p/13442960.html
Copyright © 2020-2023  润新知