标签:sign ret include fir lld back lse sum amp
//贴个主席树板子
#include<bits/stdc++.h> using namespace std; #define pb push_back #define sc(x) scanf("%lld",&x); #define int long long #define fi first #define se second #define endl ‘\n‘ #define P pair<int,int> int cnt; struct Node{ int l,r,sum; }; const int maxn = 2e5+5; Node A[maxn*40]; int a[maxn]; int root[maxn]; vector<int> v; void insert(int x,int l,int r,int pre,int &cur) { A[++cnt] = A[pre]; cur = cnt; A[cur].sum++; if(l==r){ return; } int m = (l+r)>>1; if(x <= m)insert(x,l,m,A[pre].l,A[cur].l); else insert(x,m+1,r,A[pre].r,A[cur].r); } int query(int l,int r,int L,int R,int k) { if(l==r){ return l; } int m = (l+r)>>1; int tmp = A[A[R].l].sum-A[A[L].l].sum; if(tmp>=k){ return query(l,m,A[L].l,A[R].l,k); }else return query(m+1,r,A[L].r,A[R].r,k-tmp); } signed main() { int n,m; sc(n)sc(m) for(int i =0 ;i < n;i++){ sc(a[i]) v.pb(a[i]); } sort(v.begin(),v.end()); int z = unique(v.begin(),v.end())-v.begin(); for(int i=0;i<n;i++){ int id = lower_bound(v.begin(),v.begin()+z,a[i])-v.begin()+1; insert(id,1,n,root[i],root[i+1]); } int l,r,k; for(int i=0;i<m;i++){ sc(l)sc(r)sc(k) //cout<<(query(1,n,root[l-1],root[r],k)-1)<<endl; cout<<v[query(1,n,root[l-1],root[r],k)-1]<<‘\n‘; } }
标签:sign ret include fir lld back lse sum amp
原文地址:https://www.cnblogs.com/liulex/p/12945146.html