#ifndef _DEBUG #define _DEBUG #include __FILE__ signed main() { int N, Q; cin >> N >> Q; string S; cin >> S; vector> query(Q); for (int i = 0; i < Q; i++) { int a, b, c; cin >> a >> b >> c, a--, c--; query.at(i) = {a, b, c}; } vector V(N); for (int i = 0; i < N; i++) { V.at(i) = S.at(i) - 'a'; } dump(V); WaveletMatrix WM(V); for (const auto& [a, b, c] : query) { dump(a, b, c); cout << char('a' + WM.quantile(c, a, b)) << "\n"; } } #else #undef _DEBUG #pragma GCC diagnostic warning "-Wextra" #pragma GCC diagnostic warning "-Wshadow" #include using namespace std; struct BitRank { vector block; vector count; BitRank() {} void resize(const unsigned int num) { block.resize(((num + 1) >> 6) + 1, 0); count.resize(block.size(), 0); } void set(const unsigned int i, const unsigned long long val) { block[i >> 6] |= (val << (i & 63)); } void build() { for (unsigned int i = 1; i < block.size(); i++) { count[i] = count[i - 1] + __builtin_popcountll(block[i - 1]); } } unsigned int rank1(const unsigned int i) const { return count[i >> 6] + __builtin_popcountll(block[i >> 6] & ((1ULL << (i & 63)) - 1ULL)); } unsigned int rank1(const unsigned int i, const unsigned int j) const { return rank1(j) - rank1(i); } unsigned int rank0(const unsigned int i) const { return i - rank1(i); } unsigned int rank0(const unsigned int i, const unsigned int j) const { return rank0(j) - rank0(i); } }; class WaveletMatrix { private: unsigned int height; vector B; vector pos; public: WaveletMatrix() {} WaveletMatrix(vector vec) : WaveletMatrix(vec, *max_element(vec.begin(), vec.end()) + 1) {} WaveletMatrix(vector vec, const unsigned int sigma) { init(vec, sigma); } void init(vector& vec, const unsigned int sigma) { height = (sigma == 1) ? 1 : (64 - __builtin_clzll(sigma - 1)); B.resize(height), pos.resize(height); for (unsigned int i = 0; i < height; ++i) { B[i].resize(vec.size()); for (unsigned int j = 0; j < vec.size(); ++j) { B[i].set(j, get(vec[j], height - i - 1)); } B[i].build(); auto it = stable_partition(vec.begin(), vec.end(), [&](int c) { return !get(c, height - i - 1); }); pos[i] = it - vec.begin(); } } int get(const int val, const int i) { return val >> i & 1; } int rank(const int val, const int l, const int r) { return rank(val, r) - rank(val, l); } int rank(int val, int i) { int p = 0; for (unsigned int j = 0; j < height; ++j) { if (get(val, height - j - 1)) { p = pos[j] + B[j].rank1(p); i = pos[j] + B[j].rank1(i); } else { p = B[j].rank0(p); i = B[j].rank0(i); } } return i - p; } int quantile(int k, int l, int r) { int res = 0; for (unsigned int i = 0; i < height; ++i) { const int j = B[i].rank0(l, r); if (j > k) { l = B[i].rank0(l); r = B[i].rank0(r); } else { l = pos[i] + B[i].rank1(l); r = pos[i] + B[i].rank1(r); k -= j; res |= (1 << (height - i - 1)); } } return res; } int rangefreq(const int i, const int j, const int a, const int b, const int l, const int r, const int x) { if (i == j || r <= a || b <= l) return 0; const int mid = (l + r) >> 1; if (a <= l && r <= b) { return j - i; } else { const int left = rangefreq(B[x].rank0(i), B[x].rank0(j), a, b, l, mid, x + 1); const int right = rangefreq(pos[x] + B[x].rank1(i), pos[x] + B[x].rank1(j), a, b, mid, r, x + 1); return left + right; } } int rangefreq(const int l, const int r, const int a, const int b) { return rangefreq(l, r, a, b, 0, 1 << height, 0); } int rangemin(const int i, const int j, const int a, const int b, const int l, const int r, const int x, const int val) { if (i == j || r <= a || b <= l) return -1; if (r - l == 1) return val; const int mid = (l + r) >> 1; const int res = rangemin(B[x].rank0(i), B[x].rank0(j), a, b, l, mid, x + 1, val); if (res < 0) return rangemin(pos[x] + B[x].rank1(i), pos[x] + B[x].rank1(j), a, b, mid, r, x + 1, val + (1 << (height - x - 1))); else return res; } int rangemin(int l, int r, int a, int b) { return rangemin(l, r, a, b, 0, 1 << height, 0, 0); } }; struct io_setup { io_setup() { cin.tie(nullptr)->sync_with_stdio(false); } } io_setup; template bool cmin(A& a, const B& b) { if (a > b) return a = b, true; return false; } template bool cmax(A& a, const B& b) { if (a < b) return a = b, true; return false; } template ostream& operator<<(ostream& os, const pair& p); template ostream& operator<<(ostream& os, const tuple& t); template ostream& operator<<(ostream& os, const tuple& t); #define foreach(it, a) for (auto it = a.begin(); it != a.end(); it++) ostream& operator<<(ostream& os, const vector& vec) { os << "{"; foreach (it, vec) { os << (it == vec.begin() ? "" : " ") << *it; } return os << "}"; } template ostream& operator<<(ostream& os, const vector& vec) { os << "{"; foreach (it, vec) { os << (it == vec.begin() ? "" : ", ") << *it; } return os << "}"; } template ostream& operator<<(ostream& os, const vector>& vec) { os << "{\n"; foreach (it, vec) { os << (it == vec.begin() ? " " : "\n ") << *it; } return os << "\n}"; } template ostream& operator<<(ostream& os, const vector>>& vec) { os << "{\n"; foreach (it, vec) { os << (it == vec.begin() ? "" : "\n") << it - vec.begin() << ": " << *it; } return os << "\n}"; } template ostream& operator<<(ostream& os, const set& se) { os << "{"; foreach (it, se) { os << (it == se.begin() ? "" : ", ") << *it; } return os << "}"; } template ostream& operator<<(ostream& os, const multiset& ms) { os << "{"; foreach (it, ms) { os << (it == ms.begin() ? "" : ", ") << *it; } return os << "}"; } template ostream& operator<<(ostream& os, const map& ma) { os << "{"; foreach (it, ma) { os << (it == ma.begin() ? "" : ", ") << *it; } return os << "}"; } template ostream& operator<<(ostream& os, priority_queue pq) { os << "{"; while (!pq.empty()) { os << pq.top(), pq.pop(); if (!pq.empty()) os << ", "; } return os << "}"; } template ostream& operator<<(ostream& os, const pair& p) { const auto& [a, b] = p; return os << "(" << a << ", " << b << ")"; } template ostream& operator<<(ostream& os, const tuple& t) { const auto& [a, b, c] = t; return os << "(" << a << ", " << b << ", " << c << ")"; } template ostream& operator<<(ostream& os, const tuple& t) { const auto& [a, b, c, d] = t; return os << "(" << a << ", " << b << ", " << c << ", " << d << ")"; } void dump_func() {} template void dump_func(const A& a, const B&... b) { cout << a << (sizeof...(b) ? ", " : "\n"); dump_func(b...); } #ifdef _DEBUG #define dump(...) cout << "[" << (#__VA_ARGS__) << "]: ", dump_func(__VA_ARGS__) #else #define dump(...) #endif #endif