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
| #include <cstdio> using namespace std; template <class Type> void read (Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0'; flag && (x = ~x + 1); } template <class Type> void write (Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar('0' + x % 10); } const int N = 3e5 + 10, M = 6e6 + 10; struct Node { int l, r, s; } tr[M]; int n, m, idx, rt[N]; void modify (int &x, int t, int l = 1, int r = n) { tr[++idx] = tr[x]; tr[x = idx].s++; if (l == r) return; int mid = l + r >> 1; t <= mid ? modify(tr[x].l, t, l, mid) : modify(tr[x].r, t, mid + 1, r); } int query (int x, int y, int k, int l = 1, int r = n) { if (tr[x].s - tr[y].s <= k) return -1; if (l == r) return l; int mid = l + r >> 1; int t = query(tr[x].l, tr[y].l, k, l, mid); return ~t ? t : query(tr[x].r, tr[y].r, k, mid + 1, r); } int main () { read(n), read(m); for (int i = 1, a; i <= n; i++) read(a), modify(rt[i] = rt[i - 1], a); for (int l, r, k; m; m--) { read(l), read(r), read(k); write(query(rt[r], rt[l - 1], (r - l + 1) / k)), puts(""); } return 0; }
|