#include using namespace std; #define ALL(x) begin(x),end(x) #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<bool chmax(T &a,const T &b){if(abool chmin(T &a,const T &b){if(b ostream &operator<<(ostream &os,const vector&v){ for(int i=0;i<(int)v.size();i++) os< istream &operator>>(istream &is,vector&v){ for(T &x:v)is>>x; return is; } #include #include using namespace __gnu_pbds; template using set_kth_less=tree,rb_tree_tag,tree_order_statistics_node_update>; template using set_kth_greater=tree,rb_tree_tag,tree_order_statistics_node_update>; struct Mo{ using F=function; int n,idx,ql,qr; vector> query; vector ord; // [l,r) // add_left : l-1を足す, add_right : rを足す // erase_left: lを消す, erase_right: r-1を消す F add_left,add_right,erase_left,erase_right; Mo(int n,F add_left,F add_right,F erase_left,F erase_right): n(n),idx(0),ql(0),qr(0), add_left(add_left),add_right(add_right),erase_left(erase_left),erase_right(erase_right) {} void add(int l,int r){ query.emplace_back(l,r); } void build(){ int q=(int)query.size(); int bs=n/min(n,int(sqrt(q))); // 左端のブロックでソート // 左端のブロックが同じなら右端でソート ord.resize(q); iota(begin(ord),end(ord),0); sort(begin(ord),end(ord),[&](int a,int b){ int al=query[a].first/bs,bl=query[b].first/bs; if(al!=bl) return alquery[b].second; return query[a].second=(int)ord.size()) return -1; int now=ord[idx++]; while(ql>query[now].first) add_left(--ql); while(qrquery[now].second) erase_right(--qr); return now; } }; signed main(){ int n,q;cin>>n>>q; string s;cin>>s; unordered_map cnt; using P=pair; set_kth_less

st; auto add=[&](int i){ st.insert({s[i],cnt[s[i]]}); cnt[s[i]]++; }; auto erase=[&](int i){ cnt[s[i]]--; st.erase({s[i],cnt[s[i]]}); }; Mo mo(n,add,add,erase,erase); vector l(q),r(q),x(q); rep(i,q){ cin>>l[i]>>r[i]>>x[i]; l[i]--,x[i]--; mo.add(l[i],r[i]); } vector ans(q); mo.build(); rep(i,q){ int j=mo.process(); ans[j]=st.find_by_order(x[j])->first; } rep(i,q) cout<