1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| constexpr int N = 5E5 + 10; struct node { int l, r; int sum; }tr[N * 40];
int root[N], cnt; void modify(int &x, int y, int l, int r, int pos) { x = ++cnt; tr[x] = tr[y]; tr[x].sum++; if (l == r) { return ; }
int mid = l + r >> 1; if (pos <= mid) { modify(tr[x].l, tr[y].l, l, mid, pos); } else { modify(tr[x].r, tr[y].r, mid + 1, r, pos); } }
int query(int x, int y, int l, int r, i64 k) { if (l == r) { return l + k - 1; } int mid = l + r >> 1; int sum = (mid - l + 1) - (tr[tr[y].l].sum - tr[tr[x].l].sum); if (k <= sum) return query(tr[x].l, tr[y].l, l, mid, k); else return query(tr[x].r, tr[y].r, mid + 1, r, k - sum); }
signed main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, m; std::cin >> n >> m;
for (int i = 1; i <= n; i++) { int x; std::cin >> x;
modify(root[i], root[i - 1], 0, n, x); } while (m--) { int l, r; std::cin >> l >> r;
i64 k; std::cin >> k;
if (k >= n) { std::cout << k + (r - l) << "\n"; } else { std::cout << query(root[l - 1], root[r], 0, n, k) << "\n"; } }
return 0; }
|