1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| #include <bits/stdc++.h> using namespace std; const int maxn=1e5;
int n,q;
template <typename T> class CountSegmentTree{ private: struct Node { int cnt; T l, r; int lson, rson; };
struct Version{ int root; T revise_val; int revise_cnt; }; vector<Node> node; vector<Version> version;
int create(int val,T l,T r,int lson,int rson) { node.push_back({val,l,r,lson,rson}); return node.size()-1; } int clone(int u){ node.push_back(node[u]); return node.size()-1; } int build(T l,T r) { if (l==r) return create(0,l,r,-1,-1); else { T mid=(l+r)/2; return create(0,l,r,build(l,mid),build(mid+1,r)); } } void revise(T val,int x){ int u=version.back().root; int *fa=nullptr; while (true){ int v=clone(u); if (fa) *fa=v; else version.push_back(Version{v,val,x}); node[v].cnt+=x; if (node[u].l==node[u].r) return ; T mid=(node[u].l+node[u].r)/2; u=(val<=mid)?node[u].lson:node[u].rson; fa=(val<=mid)?(&node[v].lson):(&node[v].rson); } } int ask_range(int u,const T l,const T r){ if (l<=node[u].l&&node[u].r<=r) return node[u].cnt; T mid=(node[u].l+node[u].r)/2; int ret=0; if (l<=mid) ret+=ask_range(node[u].lson,l,r); if (r>mid) ret+=ask_range(node[u].rson,l,r); return ret; }
public: CountSegmentTree(T l, T r){ node.reserve((r-l+5)*32); version.push_back(Version{build(l,r),0}); } int insert(T val){ revise(val,1); return version.size()-1; } void remove(T val){ revise(val,-1); return version.size()-1; } int ask_cnt(int ver, T l, T r){ return ask_range(version[ver].root,l,r); } T ask_version_diff(int ver){ return version[ver].revise_val; } };
namespace fastio { const int bufl=1<<16; const double base1[16]={1,1e-1,1e-2,1e-3,1e-4,1e-5,1e-6,1e-7,1e-8,1e-9,1e-10,1e-11,1e-12,1e-13,1e-14,1e-15}; const double base2[16]={1,1e1,1e2,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13,1e14,1e15}; struct IN{ FILE *IT;char ibuf[bufl],*is=ibuf,*it=ibuf; IN(){IT=stdin;}IN(char *a){IT=fopen(a,"r");} inline char getChar(){if(is==it){it=(is=ibuf)+fread(ibuf,1,bufl,IT);if(is==it)return EOF;}return *is++;} template<typename Temp>inline void getInt(Temp &a){a=0;int b=0,c=getChar();while(c<48||c>57)b^=(c==45),c=getChar();while(c>=48&&c<=57)a=(a<<1)+(a<<3)+c-48,c=getChar();if(b)a=-a;} IN& operator>>(char &a){a=getChar();return *this;} IN& operator>>(char *a){do{*a=getChar();}while(*a<=32);while(*a>32)*++a=getChar();*a=0;return *this;} IN& operator>>(string &a){char b=getChar();while(b<=32)b=getChar();while(b>32)a+=b,b=getChar();return *this;} IN& operator>>(int &a){getInt(a);return *this;} IN& operator>>(long long &a){getInt(a);return *this;} }; struct OUT{ FILE *IT;char obuf[bufl],*os=obuf,*ot=obuf+bufl;int Eps;long double Acc; OUT(){IT=stdout,Eps=6,Acc=1e-6;}OUT(char *a){IT=fopen(a,"w"),Eps=6,Acc=1e-6;} inline void ChangEps(int x=6){Eps=x;} inline void flush(){fwrite(obuf,1,os-obuf,IT);os=obuf;} inline void putChar(int a){*os++=a;if(os==ot)flush();} template<typename Temp>inline void putInt(Temp a){if(a<0){putChar(45);a=-a;}if(a<10){putChar(a+48);return;}putInt(a/10);putChar(a%10+48);} OUT& operator<<(char a){putChar(a);return *this;} OUT& operator<<(char *a){while(*a>32)putChar(*a++);return *this;} OUT& operator<<(string a){for(auto c:a)putChar(c);return *this;} OUT& operator<<(int a){putInt(a);return *this;} OUT& operator<<(long long a){putInt(a);return *this;} ~OUT(){flush();} }; } using fastio::IN; using fastio::OUT; IN fin; OUT fout;
int main(){
fin>>n>>q; typedef pair<int,int> P; vector<int> x(n+1); vector<P> a(n); for (int i=0;i<n;i++){ fin>>a[i].first; a[i].second=i+1; x[i+1]=a[i].first; } sort(a.begin(),a.end()); CountSegmentTree<int> tree(1,n); for (P p: a) tree.insert(p.second); int L,R,k; int l,r,mid; while (q--){ fin>>L>>R>>k; l=0,r=n; while (r-l>1){ mid=(l+r)>>1; if (tree.ask_cnt(mid,L,R)>=k) r=mid; else l=mid; } fout<<x[tree.ask_version_diff(r)]<<'\n'; } return 0; }
|