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 63 64 65 66 67 68 69 70 71 72 73 74 75
| #include <iostream> #include <cstdio> #include <algorithm> #include <vector> #define maxn 200010 #define lowbit(i) ((i) & (-i)) using namespace std;
int n, m, a[maxn];
vector<int> v[maxn];
int b[maxn], cnt; void init_hash() { for (int i = 1; i <= n; ++i) b[i] = a[i]; sort(b + 1, b + n + 1); cnt = unique(b + 1, b + n + 1) - b - 1; for (int i = 1; i <= n; ++i) a[i] = lower_bound(b + 1, b + cnt + 1, a[i]) - b; }
int Bit[maxn]; void add(int i, int v) { while (i <= n) Bit[i] += v, i += lowbit(i); }
int get_sum(int i) { int s = 0; while (i) s += Bit[i], i -= lowbit(i); return s; }
struct Query { int l, r, k, id; } Q[maxn], t1[maxn], t2[maxn]; int ans[maxn];
int l, r; inline void add(int x) { for (auto u : v[x]) add(u, 1); }
inline void del(int x) { for (auto u : v[x]) add(u, -1); }
void update(int L, int R) { while (r < R) add(++r); while (l > L) add(--l); while (r > R) del(r--); while (l < L) del(l++); }
void solve(int l, int r, int L, int R) { if (L > R) return ; if (l == r) { for (int i = L; i <= R; ++i) ans[Q[i].id] = b[l]; return ; } int m = l + r >> 1, c1 = 0, c2 = 0; update(l, m); for (int i = L; i <= R; ++i) { int l = Q[i].l, r = Q[i].r, k = Q[i].k, v; v = get_sum(r) - get_sum(l - 1); if (k <= v) t1[++c1] = Q[i]; else t2[++c2] = Q[i], t2[c2].k -= v; } for (int i = 1; i <= c1; ++i) Q[L + i - 1] = t1[i]; for (int i = 1; i <= c2; ++i) Q[L + c1 + i - 1] = t2[i]; solve(l, m, L, L + c1 - 1); solve(m + 1, r, L + c1, R); }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i]; init_hash(); for (int i = 1; i <= n; ++i) v[a[i]].push_back(i); for (int i = 1; i <= m; ++i) cin >> Q[i].l >> Q[i].r >> Q[i].k, Q[i].id = i; add(l = r = 1); solve(1, cnt, 1, m); for (int i = 1; i <= m; ++i) cout << ans[i] << "\n"; return 0; }
|