This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/range_kth_smallest"
#define FAST_IO
#include "../../template/template.hpp"
#include "../../other/coordinate_compression.hpp"
#include "../../data_structure/wavelet_matrix.hpp"
int main() {
i32 n, q;
cin >> n >> q;
Vec<i32> a(n);
REP(i, n) {
cin >> a[i];
}
CoordinateCompression<i32> cc(a);
WaveletMatrix<i32> wm(a);
REP(qi, q) {
i32 l, r, k;
cin >> l >> r >> k;
i32 sm = wm.kth_smallest(l, r, k);
cout << cc[sm] << '\n';
}
}#line 1 "data_structure/test/range_kth_smallest.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/range_kth_smallest"
#define FAST_IO
#line 1 "template/template.hpp"
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define OVERRIDE(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (i32) (n); ++i)
#define REP3(i, m, n) for (i32 i = (i32) (m); i < (i32) (n); ++i)
#define REP(...) OVERRIDE(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define PER(i, n) for (i32 i = (i32) (n) - 1; i >= 0; --i)
#define ALL(x) begin(x), end(x)
using namespace std;
using u32 = unsigned int;
using u64 = unsigned long long;
using u128 = __uint128_t;
using i32 = signed int;
using i64 = signed long long;
using i128 = __int128_t;
using f64 = double;
using f80 = long double;
template <typename T>
using Vec = vector<T>;
template <typename T>
bool chmin(T &x, const T &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T>
bool chmax(T &x, const T &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
istream &operator>>(istream &is, i128 &x) {
i64 v;
is >> v;
x = v;
return is;
}
ostream &operator<<(ostream &os, i128 x) {
os << (i64) x;
return os;
}
istream &operator>>(istream &is, u128 &x) {
u64 v;
is >> v;
x = v;
return is;
}
ostream &operator<<(ostream &os, u128 x) {
os << (u64) x;
return os;
}
[[maybe_unused]] constexpr i32 INF = 1000000100;
[[maybe_unused]] constexpr i64 INF64 = 3000000000000000100;
struct SetUpIO {
SetUpIO() {
#ifdef FAST_IO
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
cout << fixed << setprecision(15);
}
} set_up_io;
#line 2 "other/coordinate_compression.hpp"
#line 5 "other/coordinate_compression.hpp"
template <typename T>
class CoordinateCompression {
std::vector<T> data;
int size_sum() {
return 0;
}
template <typename... Tail>
int size_sum(const std::vector<T> &head, const Tail &...tail) {
return (int) head.size() + size_sum(tail...);
}
void push() {}
template <typename... Tail>
void push(const std::vector<T> &head, const Tail &...tail) {
for (const T &ele : head) {
data.emplace_back(ele);
}
push(tail...);
}
void compress() {}
template <typename... Tail>
void compress(std::vector<T> &head, Tail &...tail) {
for (T &ele : head) {
ele = (T) (std::lower_bound(data.begin(), data.end(), ele) - data.begin());
}
compress(tail...);
}
public:
template <typename... V>
CoordinateCompression(V &...v) {
data.reserve(size_sum(v...));
push(v...);
std::sort(data.begin(), data.end());
data.erase(std::unique(data.begin(), data.end()), data.end());
compress(v...);
}
const T &operator[](const T &ele) const {
return data[ele];
}
int size() const {
return data.size();
}
bool contains(const T &ele) const {
auto it = std::lower_bound(data.begin(), data.end(), ele);
return it != data.end() && *it == ele;
}
T cc(const T &ele) const {
return (T) (std::lower_bound(data.begin(), data.end(), ele) - data.begin());
}
};
#line 2 "data_structure/wavelet_matrix.hpp"
#line 6 "data_structure/wavelet_matrix.hpp"
#line 2 "data_structure/bit_vector.hpp"
#line 4 "data_structure/bit_vector.hpp"
#line 2 "template/bitop.hpp"
template <typename T, typename U>
bool ith_bit(T n, U i) {
return (n & ((T) 1 << i)) != 0;
}
int popcount(int x) {
return __builtin_popcount(x);
}
unsigned popcount(unsigned x) {
return __builtin_popcount(x);
}
long long popcount(long long x) {
return __builtin_popcountll(x);
}
unsigned long long popcount(unsigned long long x) {
return __builtin_popcountll(x);
}
// x must not be 0
int clz(int x) {
return __builtin_clz(x);
}
unsigned clz(unsigned x) {
return __builtin_clz(x);
}
long long clz(long long x) {
return __builtin_clzll(x);
}
unsigned long long clz(unsigned long long x) {
return __builtin_clzll(x);
}
// x must not be 0
int ctz(int x) {
return __builtin_ctz(x);
}
unsigned ctz(unsigned int x) {
return __builtin_ctz(x);
}
long long ctz(long long x) {
return __builtin_ctzll(x);
}
unsigned long long ctz(unsigned long long x) {
return __builtin_ctzll(x);
}
int floor_log2(int x) {
return 31 - clz(x);
}
unsigned floor_log2(unsigned x) {
return 31 - clz(x);
}
long long floor_log2(long long x) {
return 63 - clz(x);
}
unsigned long long floor_log2(unsigned long long x) {
return 63 - clz(x);
}
template <typename T>
T mask_n(T x, T n) {
T mask = ((T) 1 << n) - 1;
return x & mask;
}
#line 6 "data_structure/bit_vector.hpp"
class BitVector {
static constexpr int WIDTH = 64;
int n;
std::vector<unsigned long long> bits;
std::vector<unsigned long long> sum;
int zeros;
public:
BitVector(int _n) : n(_n), bits(n / WIDTH + 1, 0), sum(n / WIDTH + 1, 0), zeros(0) {}
void rev(int idx) {
bits[idx / WIDTH] ^= 1ULL << (idx % WIDTH);
}
bool operator[](int idx) const {
return (bits[idx / WIDTH] & (1ULL << (idx % WIDTH))) != 0;
}
void build() {
for (int i = 1; i < (int) sum.size(); ++i) {
sum[i] = sum[i - 1] + popcount(bits[i - 1]);
}
zeros = rank0(n);
}
int rank0(int n) const {
return n - rank1(n);
}
int rank1(int n) const {
return sum[n / WIDTH] + popcount(mask_n(bits[n / WIDTH], (unsigned long long) (n % WIDTH)));
}
int all_zeros() const {
return zeros;
}
};
#line 8 "data_structure/wavelet_matrix.hpp"
template <typename T>
class WaveletMatrix {
int n;
int ht;
std::vector<BitVector> vecs;
public:
WaveletMatrix(std::vector<T> a) : n((int) a.size()), ht(0), vecs() {
assert(n > 0);
for (T ele : a) {
assert(ele >= 0);
}
T mx = std::max(T(1), *std::max_element(a.begin(), a.end()));
ht = (int) floor_log2(mx) + 1;
vecs.reserve(ht);
for (T d = ht; d-- > 0;) {
BitVector vec(n);
for (int i = 0; i < n; ++i) {
if (ith_bit(a[i], d)) {
vec.rev(i);
}
}
vec.build();
std::vector<T> nxt(n);
auto it0 = nxt.begin();
auto it1 = nxt.begin() + vec.all_zeros();
for (int i = 0; i < n; ++i) {
if (vec[i]) {
*(it1++) = a[i];
} else {
*(it0++) = a[i];
}
}
std::swap(nxt, a);
vecs.emplace_back(std::move(vec));
}
}
T access(int idx) const {
assert(0 <= idx && idx < n);
T ret(0);
for (int i = 0; i < ht; ++i) {
ret <<= 1;
if (vecs[i][idx]) {
ret ^= 1;
idx = vecs[i].all_zeros() + vecs[i].rank1(idx);
} else {
idx = vecs[i].rank0(idx);
}
}
return ret;
}
T kth_smallest(int l, int r, int k) const {
assert(0 <= l && l < r && r <= n && 0 <= k && k < r - l);
T ret = 0;
for (int i = 0; i < ht; ++i) {
int l0 = vecs[i].rank0(l);
int r0 = vecs[i].rank0(r);
ret <<= 1;
if (k < r0 - l0) {
l = l0;
r = r0;
} else {
ret ^= T(1);
l += vecs[i].all_zeros() - l0;
r += vecs[i].all_zeros() - r0;
k -= r0 - l0;
}
}
return ret;
}
T kth_largest(int l, int r, int k) const {
return kth_smallest(l, r, r - l - k - 1);
}
// count i s.t. i \in [l, r) and a[i] = v
int rank(int l, int r, T v) const {
assert(0 <= l && l <= r && r <= n);
if (v != 0 && floor_log2(v) >= ht) {
return 0;
}
for (int i = 0; i < ht; ++i) {
i32 l0 = vecs[i].rank0(l);
i32 r0 = vecs[i].rank0(r);
if (ith_bit(v, ht - 1 - i)) {
l += vecs[i].all_zeros() - l0;
r += vecs[i].all_zeros() - r0;
} else {
l = l0;
r = r0;
}
}
return r - l;
}
// count i s.t. i \in [l, r) and a[i] < upper
int range_freq(int l, int r, T upper) const {
assert(0 <= l && l < r && r <= n);
if (upper != 0 && floor_log2(upper) >= ht) {
return r - l;
}
int cnt = 0;
for (int i = 0; i < ht; ++i) {
i32 l0 = vecs[i].rank0(l);
i32 r0 = vecs[i].rank0(r);
if (ith_bit(upper, ht - 1 - i)) {
cnt += r0 - l0;
l += vecs[i].all_zeros() - l0;
r += vecs[i].all_zeros() - r0;
} else {
l = l0;
r = r0;
}
}
return cnt;
}
// count i s.t. i \in [l, r) and a[i] \in [lower, upper)
int range_freq(int l, int r, T lower, T upper) const {
if (lower >= upper) {
return 0;
} else {
return range_freq(l, r, upper) - range_freq(l, r, lower);
}
}
// max v s.t. v \in a[l, r) and v < upper
int prev(int l, int r, T upper) const {
int freq = range_freq(l, r, upper);
if (freq == 0) {
return T(-1);
} else {
return kth_smallest(l, r, freq - 1);
}
}
// min v s.t. v \in a[l, r) and v \geq lower
int next(int l, int r, T lower) const {
int freq = range_freq(l, r, lower);
if (freq == r - l) {
return T(-1);
} else {
return kth_smallest(l, r, freq);
}
}
};
#line 8 "data_structure/test/range_kth_smallest.test.cpp"
int main() {
i32 n, q;
cin >> n >> q;
Vec<i32> a(n);
REP(i, n) {
cin >> a[i];
}
CoordinateCompression<i32> cc(a);
WaveletMatrix<i32> wm(a);
REP(qi, q) {
i32 l, r, k;
cin >> l >> r >> k;
i32 sm = wm.kth_smallest(l, r, k);
cout << cc[sm] << '\n';
}
}