算法数据结构还是得时常复习,达到半夜被叫醒也能流畅写完的境界才算是学通透了。
传送门
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using p = pair<int, int>;
const double pi(acos(-1));
const int inf(0x3f3f3f3f);
const int mod(1e9 + 7);
const int maxn(2e5 + 10);
int cnt, a[maxn], root[maxn];
vector<int> nums;
struct node {
int l, r, sum;
} tree[maxn * 40];
template<typename T = int>
inline const T read()
{
T x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * f;
}
template<typename T>
inline void write(T x, bool ln)
{
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) write(x / 10, false);
putchar(x % 10 + '0');
if (ln) putchar(10);
}
inline int getId(int val)
{
return int(lower_bound(nums.begin(), nums.end(), val) - nums.begin()) + 1;
}
void insert(int l, int r, int pos, int pre, int& cur)
{
cur = ++cnt;
tree[cur] = tree[pre];
++tree[cur].sum;
if (l == r) return;
int mid = (l + r) >> 1;
if (pos <= mid) {
insert(l, mid, pos, tree[pre].l, tree[cur].l);
} else {
insert(mid + 1, r, pos, tree[pre].r, tree[cur].r);
}
}
int query(int l, int r, int k, int pre, int cur)
{
if (l == r) return l;
int mid = (l + r) >> 1;
int dif = tree[tree[cur].l].sum - tree[tree[pre].l].sum;
if (k <= dif) {
return query(l, mid, k, tree[pre].l, tree[cur].l);
}
return query(mid + 1, r, k - dif, tree[pre].r, tree[cur].r);
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
int n = read(), m = read();
for (int i = 1; i <= n; ++i) {
nums.push_back(a[i] = read());
}
sort(nums.begin(), nums.end());
nums.erase(unique(nums.begin(), nums.end()), nums.end());
for (int i = 1; i <= n; ++i) {
int pos = getId(a[i]);
insert(1, (int)nums.size(), pos, root[i - 1], root[i]);
}
while (m--) {
int l = read(), r = read(), k = read();
write(nums[query(1, (int)nums.size(), k, root[l - 1], root[r]) - 1], true);
}
return 0;
}