This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/range_kth_smallest"
#include <bits/stdc++.h>
using namespace std;
#include "../../../lib/data_structure/mo.hpp"
#include "../../../lib/data_structure/binary_indexed_tree.hpp"
#include "../../../lib/others/compression.hpp"
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, q; cin >> n >> q;
vector<int> a(n), k(q);
for(int i = 0; i < n; i++){
cin >> a[i];
}
Mo mo(n);
for(int i = 0; i < q; i++){
int l, r; cin >> l >> r >> k[i];
mo.add(l, r);
}
compress<int> comp(a);
vector<int> compressed = comp.getCompressed();
vector<long long> res(q);
BinaryIndexedTree<int> BIT(200000);
long long now = 0;
auto add = [&](int i){
BIT.add(compressed[i], 1);
};
auto erase = [&](int i){
BIT.add(compressed[i], -1);
};
auto output = [&](int q){
int idx = BIT.lower_bound(k[q] + 1);
res[q] = comp.sorted[idx];
};
mo.build(add, erase, output);
for(auto ans : res){
cout << ans << "\n";
}
}
#line 1 "test/library_checker/data_structure/range_kth_smallest.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/range_kth_smallest"
#include <bits/stdc++.h>
using namespace std;
#line 2 "lib/data_structure/mo.hpp"
// [0 , N) 上の区間に対する Q 個のクエリを計算します。 : O(N√Q) (区間の伸縮が O(1) で行える場合)
struct Mo{
int n;
vector<pair<int, int>> lr;
Mo(const int n) : n(n) {}
/* [l, r) */
void add(const int l, const int r){
lr.emplace_back(l, r);
}
template <typename AL, typename AR, typename EL, typename ER, typename O>
void build(const AL &add_left, const AR &add_right, const EL &erase_left, const ER &erase_right, const O &out){
int q = (int) lr.size();
int border = max<int>(1, 1.0 * n / max<double>(1.0, sqrt(q * 2.0 / 3.0)));
vector<int> ord(q);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&](int a, int b){
int ablock = lr[a].first / border, bblock = lr[b].first / border;
if(ablock != bblock){
return ablock < bblock;
}
return (ablock & 1) ? lr[a].second > lr[b].second : lr[a].second < lr[b].second;
});
int l = 0, r = 0;
for(const auto &k : ord){
while(l > lr[k].first) add_left(--l);
while(r < lr[k].second) add_right(r++);
while(l < lr[k].first) erase_left(l++);
while(r > lr[k].second) erase_right(--r);
out(k);
}
}
template <typename A, typename E, typename O>
void build(const A &add, const E &erase, const O &out){
build(add, add, erase, erase, out);
}
};
#line 2 "lib/data_structure/binary_indexed_tree.hpp"
/**
* @brief Binary Indexed Tree
* @docs docs/data_structure/binary_indexed_tree.md
*/
#line 9 "lib/data_structure/binary_indexed_tree.hpp"
template <typename T>
struct BinaryIndexedTree{
int N;
std::vector<T> BIT;
BinaryIndexedTree(const int N) : N(N), BIT(N + 1, 0){
}
T get(int i){
return sum(i + 1) - sum(i);
}
void add(int i, T x){
i++;
while(i <= N){
BIT[i] += x;
i += i & -i;
}
}
T sum(int i) const {
T ans = 0;
while(i > 0){
ans += BIT[i];
i -= i & -i;
}
return ans;
}
T sum(int L, int R) const {
return sum(R) - sum(L);
}
int lower_bound(T x) const {
if(x <= 0){
return 0;
} else{
int v = 0, r = 1;
while(r < N) r = r << 1;
for(int len = r; len > 0; len = len >> 1){
if(v + len < N && BIT[v + len] < x){
x -= BIT[v + len];
v += len;
}
}
return v;
}
}
int upper_bound(T x) const {
if(x < 0){
return 0;
} else{
int v = 0, r = 1;
while(r <= N) r = r << 1;
for(int len = r; len > 0; len = len >> 1){
if(v + len <= N && BIT[v + len] <= x){
x -= BIT[v + len];
v += len;
}
}
return v;
}
}
T operator [](int i) const {
return sum(i, i + 1);
}
};
#line 2 "lib/others/compression.hpp"
/**
* @brief Compression (座標圧縮)
* @docs docs/others/compression.md
*/
#line 10 "lib/others/compression.hpp"
template <typename T>
struct compress{
std::vector<T> sorted;
std::vector<int> compressed;
compress(const std::vector<T> &vec){
int n = vec.size();
compressed.resize(n);
for(T x : vec){
sorted.emplace_back(x);
}
std::sort(sorted.begin(), sorted.end());
sorted.erase(std::unique(sorted.begin(), sorted.end()), sorted.end());
for(int i = 0; i < n; ++i){
compressed[i] = std::lower_bound(sorted.begin(), sorted.end(), vec[i]) - sorted.begin();
}
}
int get(const T &x) const{
return std::lower_bound(sorted.begin(), sorted.end(), x) - sorted.begin();
}
T inv(const int x) const{
return sorted[x];
}
size_t size() const{
return sorted.size();
}
std::vector<int> getCompressed() const{
return compressed;
}
};
#line 8 "test/library_checker/data_structure/range_kth_smallest.test.cpp"
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, q; cin >> n >> q;
vector<int> a(n), k(q);
for(int i = 0; i < n; i++){
cin >> a[i];
}
Mo mo(n);
for(int i = 0; i < q; i++){
int l, r; cin >> l >> r >> k[i];
mo.add(l, r);
}
compress<int> comp(a);
vector<int> compressed = comp.getCompressed();
vector<long long> res(q);
BinaryIndexedTree<int> BIT(200000);
long long now = 0;
auto add = [&](int i){
BIT.add(compressed[i], 1);
};
auto erase = [&](int i){
BIT.add(compressed[i], -1);
};
auto output = [&](int q){
int idx = BIT.lower_bound(k[q] + 1);
res[q] = comp.sorted[idx];
};
mo.build(add, erase, output);
for(auto ans : res){
cout << ans << "\n";
}
}