///#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
#define I_int ll
inline ll read()
{
ll x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-')f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a, ll b, ll p)
{
ll res = 1;
while(b)
{
if(b & 1)res = res * a % p;
a = a * a % p;
b >>= 1;
}
return res;
}
const int maxn=2e5+10;
int n,m,a[maxn];
vector<int>nums;
struct node{
int l,r,cnt;//第i个线段树维护的是[1,i]在[l,r]范围的个数
}tr[maxn*4+maxn*17];
int root[maxn],idx;
int Find(int x){
return lower_bound(nums.begin(),nums.end(),x)-nums.begin();
}
int build(int l,int r){
int p=++idx;//建立线段树
if(l==r) return p;
int mid=(l+r)/2;//递归建立左右子树
tr[p].l=build(l,mid);
tr[p].r=build(mid+1,r);
return p;
}
int Insert(int p,int l,int r,int x){
//p是历史版本,[l,r]是待查询区间 x是待插入的数
int q=++idx;
tr[q]=tr[p];//先复制过来
if(l==r){
tr[q].cnt++;
return q;
}
int mid=(l+r)/2;
if(x<=mid) tr[q].l=Insert(tr[p].l,l,mid,x);
else tr[q].r=Insert(tr[p].r,mid+1,r,x);
tr[q].cnt=tr[tr[q].l].cnt+tr[tr[q].r].cnt;
return q;
}
int qask(int q,int p,int l,int r,int k){
if(l==r) return r;
int cnt=tr[tr[q].l].cnt-tr[tr[p].l].cnt;
int mid=(l+r)/2;
if(k<=cnt) return qask(tr[q].l,tr[p].l,l,mid,k);
else return qask(tr[q].r,tr[p].r,mid+1,r,k-cnt);
}
int main() {
n=read,m=read;
rep(i,1,n){
a[i]=read;
nums.push_back(a[i]);///离散化
}
sort(nums.begin(),nums.end());//排序
nums.erase(unique(nums.begin(),nums.end()),nums.end());//去重
root[0]=build(0,nums.size()-1);//建树
rep(i,1,n)//插入元素
root[i]=Insert(root[i-1],0,nums.size()-1,Find(a[i]));
while(m--){
int l=read,r=read,k=read;
int t=qask(root[r],root[l-1],0,nums.size()-1,k);
printf("%d\n",nums[t]);
}
return 0;
}
luogu P3834 【模板】可持久化线段树 2(主席树)
最新推荐文章于 2022-11-12 22:38:56 发布