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
| #include <cstdio> using namespace std; const int N = 5e5 + 10, M = 1e7 + 10; struct Node { int l, r, s; } tr[M]; int idx; int n, m, w[N], rt[N]; void pushup(int x) { tr[x].s = tr[tr[x].l].s + tr[tr[x].r].s; } void modify(int &x, int l, int r, int t) { tr[++idx] = tr[x]; x = idx; if (l == r) { tr[x].s++; return; } int mid = l + r >> 1; if (t <= mid) modify(tr[x].l, l, mid, t); else modify(tr[x].r, mid + 1, r, t); pushup(x); } int query(int x1, int x2, int l, int r, int k) { if (l == r) return l; int mid = l + r >> 1; if (tr[tr[x2].l].s - tr[tr[x1].l].s > k) return query(tr[x1].l, tr[x2].l, l, mid, k); else if (tr[tr[x2].r].s - tr[tr[x1].r].s > k) return query(tr[x1].r, tr[x2].r, mid + 1, r, k); return 0; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &w[i]); for (int i = 1; i <= n; i++) { rt[i] = rt[i - 1]; modify(rt[i], 1, n, w[i]); } for (int l, r; m; m--) { scanf("%d%d", &l, &r); printf("%d\n", query(rt[l - 1], rt[r], 1, n, r - l + 1 >> 1)); } return 0; }
|