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
| #include <cstdio> #include <cmath> #include <algorithm> #include <vector> using namespace std; const int N = 2e5 + 10; int n, m, len, res, w[N], ans[N], tot[N], cnt[N]; struct Query { int l, r, id; bool operator<(const Query &_) const { if (l / len != _.l / len) return l < _.l; return (l / len) & 1 ? r < _.r : r > _.r; } } q[N]; void add(int x) { tot[cnt[x]]--; tot[++cnt[x]]++; res = max(res, cnt[x]); } void del(int x) { tot[cnt[x]]--; if (cnt[x] == res && !tot[cnt[x]]) res--; tot[--cnt[x]]++; } int main() { scanf("%d%d", &n, &m); len = sqrt(n); vector<int> ws; for (int i = 1; i <= n; i++) { scanf("%d", &w[i]); ws.push_back(w[i]); } sort(ws.begin(), ws.end()); ws.erase(unique(ws.begin(), ws.end()), ws.end()); for (int i = 1; i <= n; i++) w[i] = lower_bound(ws.begin(), ws.end(), w[i]) - ws.begin() + 1; for (int i = 1; i <= m; i++) { scanf("%d%d", &q[i].l, &q[i].r); q[i].id = i; } sort(q + 1, q + m + 1); for (int k = 1, i = 0, j = 1; k <= m; k++) { int l = q[k].l, r = q[k].r; while (i < r) add(w[++i]); while (i > r) del(w[i--]); while (j < l) del(w[j++]); while (j > l) add(w[--j]); ans[q[k].id] = res; } for (int i = 1; i <= m; i++) printf("%d\n", -ans[i]); return 0; }
|