#define PROBLEM "https://judge.yosupo.jp/problem/range_kth_smallest"
#include"template/template.hpp"
#include"data-structure/wavelet-matrix.hpp"intmain(){intn,q;in(n,q);vector<int>a(n);in(a);WaveletMatrix<int,30>wm(a);while(q--){intl,r,k;in(l,r,k);out(wm.kth_smallest(l,r,k));}}
#line 1 "verify/data-structure/LC_range_kth_smallest.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/range_kth_smallest"
#line 2 "template/template.hpp"
#include<bits/stdc++.h>usingnamespacestd;#line 2 "template/macro.hpp"
#define rep(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b) - 1; i >= (a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())
#define YN(b) cout << ((b) ? "YES" : "NO") << "\n";
#define Yn(b) cout << ((b) ? "Yes" : "No") << "\n";
#define yn(b) cout << ((b) ? "yes" : "no") << "\n";
#line 6 "template/template.hpp"
#line 2 "template/util.hpp"
usinguint=unsignedint;usingll=longlongint;usingull=unsignedlonglong;usingi128=__int128_t;usingu128=__uint128_t;template<classT,classS=T>SSUM(constvector<T>&a){returnaccumulate(ALL(a),S(0));}template<classT>inlineboolchmin(T&a,Tb){if(a>b){a=b;returntrue;}returnfalse;}template<classT>inlineboolchmax(T&a,Tb){if(a<b){a=b;returntrue;}returnfalse;}template<classT>intpopcnt(Tx){return__builtin_popcountll(x);}template<classT>inttopbit(Tx){return(x==0?-1:63-__builtin_clzll(x));}template<classT>intlowbit(Tx){return(x==0?-1:__builtin_ctzll(x));}#line 8 "template/template.hpp"
#line 2 "template/inout.hpp"
structFast{Fast(){cin.tie(nullptr);ios_base::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}fast;ostream&operator<<(ostream&os,__uint128_tx){charbuf[40];size_tk=0;while(x>0)buf[k++]=(char)(x%10+'0'),x/=10;if(k==0)buf[k++]='0';while(k)os<<buf[--k];returnos;}ostream&operator<<(ostream&os,__int128_tx){returnx<0?(os<<'-'<<(__uint128_t)(-x)):(os<<(__uint128_t)x);}template<classT1,classT2>istream&operator>>(istream&is,pair<T1,T2>&p){returnis>>p.first>>p.second;}template<classT1,classT2>ostream&operator<<(ostream&os,constpair<T1,T2>&p){returnos<<p.first<<" "<<p.second;}template<classT>istream&operator>>(istream&is,vector<T>&a){for(auto&v:a)is>>v;returnis;}template<classT>ostream&operator<<(ostream&os,constvector<T>&a){for(autoit=a.begin();it!=a.end();){os<<*it;if(++it!=a.end())os<<" ";}returnos;}template<classT>ostream&operator<<(ostream&os,constset<T>&st){os<<"{";for(autoit=st.begin();it!=st.end();){os<<*it;if(++it!=st.end())os<<",";}os<<"}";returnos;}template<classT1,classT2>ostream&operator<<(ostream&os,constmap<T1,T2>&mp){os<<"{";for(autoit=mp.begin();it!=mp.end();){os<<it->first<<":"<<it->second;if(++it!=mp.end())os<<",";}os<<"}";returnos;}voidin(){}template<typenameT,class...U>voidin(T&t,U&...u){cin>>t;in(u...);}voidout(){cout<<"\n";}template<typenameT,class...U,charsep=' '>voidout(constT&t,constU&...u){cout<<t;if(sizeof...(u))cout<<sep;out(u...);}namespaceIO{namespaceGraph{vector<vector<int>>unweighted(intn,intm,booldirected=false,intoffset=1){vector<vector<int>>g(n);for(inti=0;i<m;i++){intu,v;cin>>u>>v;u-=offset,v-=offset;g[u].push_back(v);if(!directed)g[v].push_back(u);}returng;}template<classT>vector<vector<pair<int,T>>>weighted(intn,intm,booldirected=false,intoffset=1){vector<vector<pair<int,T>>>g(n);for(inti=0;i<m;i++){intu,v;Tw;cin>>u>>v>>w;u-=offset,v-=offset;g[u].push_back({v,w});if(!directed)g[v].push_back({u,w});}returng;}}// namespace GraphnamespaceTree{vector<vector<int>>unweighted(intn,booldirected=false,intoffset=1){returnGraph::unweighted(n,n-1,directed,offset);}template<classT>vector<vector<pair<int,T>>>weighted(intn,booldirected=false,intoffset=1){returnGraph::weighted<T>(n,n-1,directed,offset);}vector<vector<int>>rooted(intn,boolto_root=true,boolto_leaf=true,intoffset=1){vector<vector<int>>g(n);for(inti=1;i<n;i++){intp;cin>>p;p-=offset;if(to_root)g[i].push_back(p);if(to_leaf)g[p].push_back(i);}returng;}}// namespace Tree}// namespace IO#line 10 "template/template.hpp"
#line 2 "template/debug.hpp"
#ifdef LOCAL
#define debug 1
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define debug 0
#define show(...) true
#endif
template<classT>void_show(inti,Tname){cerr<<'\n';}template<classT1,classT2,class...T3>void_show(inti,constT1&a,constT2&b,constT3&...c){for(;a[i]!=','&&a[i]!='\0';i++)cerr<<a[i];cerr<<":"<<b<<" ";_show(i+1,a,c...);}#line 2 "data-structure/wavelet-matrix.hpp"
#line 2 "data-structure/bit-vector.hpp"
structBitVector{usingi32=int32_t;usingu32=uint32_t;usingu64=uint64_t;staticconstexpru32W=64;inlineu32get(u32i)const{returnu32(block[i/W]>>(i%W))&1u;}inlinevoidset(u32i){block[i/W]|=1ull<<(i%W);}vector<u64>block;vector<i32>count;u32n,zeros;BitVector(){}BitVector(int_n):n(_n){block.resize(n/W+1,0);count.resize(block.size(),0);}voidbuild(){for(u32i=1;i<block.size();i++)count[i]=count[i-1]+__builtin_popcountll(block[i-1]);zeros=rank0(n);}inlineu32rank0(u32i)const{returni-rank1(i);}inlineu32rank1(u32i)const{returncount[i/W]+__builtin_popcountll(block[i/W]&((1ull<<i%W)-1));}};#line 4 "data-structure/wavelet-matrix.hpp"
template<classT,intB=30>structWaveletMatrix{usingu32=uint32_t;usingi64=int64_t;usingu64=uint64_t;intn;vector<T>a;vector<BitVector>bv;WaveletMatrix(u32_n):n(max<u32>(_n,1)),a(n){}WaveletMatrix(constvector<T>&_a):n(_a.size()),a(_a){build();}voidset(u32i,constT&x){assert(x>=0);a[i]=x;}voidbuild(){bv.assign(B,n);vector<T>cur=a,nxt(n);for(inth=B-1;h>=0;--h){for(inti=0;i<n;++i)if((cur[i]>>h)&1)bv[h].set(i);bv[h].build();array<decltype(begin(nxt)),2>it{begin(nxt),begin(nxt)+bv[h].zeros};for(inti=0;i<n;++i)*it[bv[h].get(i)]++=cur[i];swap(cur,nxt);}}inlinepair<u32,u32>succ0(intl,intr,inth)const{returnmake_pair(bv[h].rank0(l),bv[h].rank0(r));}inlinepair<u32,u32>succ1(intl,intr,inth)const{u32l0=bv[h].rank0(l);u32r0=bv[h].rank0(r);u32zeros=bv[h].zeros;returnmake_pair(l+zeros-l0,r+zeros-r0);}// return a[k]Taccess(u32k)const{Tret=0;for(inth=B-1;h>=0;--h){u32f=bv[h].get(k);ret|=f?T(1)<<h:0;k=f?bv[h].rank1(k)+bv[h].zeros:bv[h].rank0(k);}returnret;}// k-th (0-indexed) smallest number in { a[i] ^ value_xor : i in [l, r) }Tkth_smallest(u32l,u32r,u32k,Tvalue_xor=0)const{Tres=value_xor;for(inth=B-1;h>=0;--h){u32l0=bv[h].rank0(l),r0=bv[h].rank0(r);u32c0=r0-l0;if((k<c0)^((value_xor>>h)&1))l=l0,r=r0;else{k-=c0;res^=(T)1<<h;l+=bv[h].zeros-l0;r+=bv[h].zeros-r0;}}returnres;}// k-th (0-indexed) largest number in { a[i] ^ value_xor : i in [l, r) }Tkth_largest(u32l,u32r,u32k,Tvalue_xor=0){returnkth_smallest(l,r,r-l-k-1);}// count i s.t. (l <= i < r) && (v[i] ^ value_xor < upper)intrange_freq(intl,intr,Tupper,Tvalue_xor=0){if(upper>=(T(1)<<B))returnr-l;intret=0;for(inth=B-1;h>=0;--h){boolf=(upper>>h)&1;u32l0=bv[h].rank0(l),r0=bv[h].rank0(r);u32zeros=bv[h].zeros;u32l1=l+zeros-l0,r1=r+zeros-r0;if((value_xor>>h)&1){swap(l0,l1);swap(r0,r1);}if(f){ret+=r0-l0;l+=zeros-l0;r+=zeros-r0;}else{l=l0;r=r0;}}returnret;}intrange_freq(intl,intr,Tlower,Tupper,Tvalue_xor){returnrange_freq(l,r,upper,value_xor)-range_freq(l,r,lower,value_xor);}// max v[i] s.t. (l <= i < r) && (v[i] ^ value_xor < upper)Tprev_value(intl,intr,Tupper,Tvalue_xor=0){intcnt=range_freq(l,r,upper,value_xor);returncnt==0?T(-1):kth_smallest(l,r,cnt-1,value_xor);}// min v[i] s.t. (l <= i < r) && (lower ^ value_xor <= v[i])Tnext_value(intl,intr,Tlower,Tvalue_xor=0){intcnt=range_freq(l,r,lower,value_xor);returncnt==r-l?T(-1):kth_smallest(l,r,cnt,value_xor);}};/**
* @brief Wavelet Matrix
* @docs docs/data-structure/wavelet-matrix.md
*/#line 5 "verify/data-structure/LC_range_kth_smallest.test.cpp"
intmain(){intn,q;in(n,q);vector<int>a(n);in(a);WaveletMatrix<int,30>wm(a);while(q--){intl,r,k;in(l,r,k);out(wm.kth_smallest(l,r,k));}}