c++ 可持久化线段树(主席树),求区间第k次序树

洛谷 P3834 【模板】可持久化线段树 2

using namespace std;
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <map>
#include <vector>
#include <numeric>
#include <iostream>

const int MAX_N = 2 * 1e5 + 7;

#define int long long

namespace fast_IO{
#define FASTIO
#define IOSIZE 1000000
    char ibuf[IOSIZE],obuf[IOSIZE];char*p1=ibuf,*p2=ibuf,*p3=obuf;
#ifdef ONLINE_JUDGE
    #define getchar()((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
    #define putchar(x)((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#endif
#define isdigit(ch)(ch>47&&ch<58)
#define isspace(ch)(ch<33)
    template<typename T>inline T read(){T s=0;int w=1;char ch;while(ch=getchar(),!isdigit(ch)and(ch!=EOF))if(ch=='-')w=-1;if(ch==EOF)return false;while(isdigit(ch))s=s*10+ch-48,ch=getchar();return s*w;}template<typename T>inline bool read(T&s){s=0;int w=1;char ch;while(ch=getchar(),!isdigit(ch)and(ch!=EOF))if(ch=='-')w=-1;if(ch==EOF)return false;while(isdigit(ch))s=s*10+ch-48,ch=getchar();return s*=w,true;}inline bool read(char&s){while(s=getchar(),isspace(s));return true;}inline bool read(char*s){char ch;while(ch=getchar(),isspace(ch));if(ch==EOF)return false;while(!isspace(ch))*s++=ch,ch=getchar();*s='\000';return true;}template<typename T>inline void print(T x){if(x<0)putchar('-'),x=-x;if(x>9)print(x/10);putchar(x%10+48);}inline void print(char x){putchar(x);}inline void print(char*x){while(*x)putchar(*x++);}inline void print(const char*x){for(int i=0;x[i];i++)putchar(x[i]);}
#ifdef _GLIBCXX_STRING
    inline bool read(std::string&s){s="";char ch;while(ch=getchar(),isspace(ch));if(ch==EOF)return false;while(!isspace(ch))s+=ch,ch=getchar();return true;}inline void print(std::string x){for(int i=0,n=x.size();i<n;i++)putchar(x[i]);}
#endif
    template<typename T,typename...T1>inline int read(T&a,T1&...other){return read(a)+read(other...);}template<typename T,typename...T1>inline void print(T a,T1...other){print(a);print(other...);}struct Fast_IO{~Fast_IO(){fwrite(obuf,p3-obuf,1,stdout);}}io;template<typename T>Fast_IO&operator>>(Fast_IO&io,T&b){return read(b),io;}template<typename T>Fast_IO&operator<<(Fast_IO&io,T b){return print(b),io;}
}
#define cin fast_IO::io
#define cout fast_IO::io
#define endl '\n'

struct HJTNode {
    int l, r, sum;
}hjt[MAX_N * 40];

int cnt, root[MAX_N], v[MAX_N];

void insert(int l, int r, int pre, int& now, int p) {
    hjt[++cnt] = hjt[pre];
    now = cnt;
    hjt[now].sum++;
    if (l == r) {
        return;
    }
    int m = (l + r) >> 1;
    if (p <= m) insert(l, m, hjt[pre].l, hjt[now].l, p);
    else insert(m + 1, r, hjt[pre].r, hjt[now].r, p);
}

int query(int l, int r, int L, int R, int k) {
    if (l == r) {
        return l;
    }
    int tmp = hjt[hjt[R].l].sum - hjt[hjt[L].l].sum;
    int m = (l + r) >> 1;
    if (k <= tmp) return query(l, m, hjt[L].l, hjt[R].l, k);
    return query(m + 1, r,hjt[L].r, hjt[R].r, k - tmp);
}

void solve() {
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> v[i];
        a[i] = v[i];
    }
    ranges::sort(a);
    a.erase(unique(a.begin(), a.end()), a.end());
    auto getId = [&](int v) {
        return ranges::lower_bound(a, v) - a.begin() + 1;
    };
    for (int i = 0; i < n; i++) {
        insert(1, a.size(), root[i], root[i + 1], getId(v[i]));
    }
    for (int i = 0; i < m; i++) {
        int l, r, k;
        cin >> l >> r >> k;
        cout << a[query(1, a.size(), root[l - 1], root[r], k) - 1] << endl;
    }
}

signed main() {
    solve();
}
  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值