7/13 训练笔记
闲话
回滚莫队板题被卡到 28pts 了
歴史の研究
回滚莫队题。莫队笔记
考虑很好加(维护 cnt
并更新答案即可),但是不好删。
那么回滚莫队
代码:
#include <bits/stdc++.h>
#define int long long
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
using namespace std;
int a[100010], bel[100010], ed[100010], ans[100010], n, q1, B, res;
unordered_map<int, int> cnt, cnt1;
int id(int i) { return bel[i]; }
struct query {
int l, r, id1;
query(int l = 0, int r = 0, int id1 = 0):l(l), r(r), id1(id1) {}
bool operator<(query o) {
return (id(l) != id(o.l)) ? id(l) < id(o.l) : r < o.r;
}
query operator=(query o) {
l = o.l;
r = o.r;
id1 = o.id1;
return *this;
}
}q[200010];
void add(int x) {
cnt[x]++;
res = max(res, cnt[x] * x);
}
int bruteForce(int l, int r) {
cnt1.clear();
int res = 0;
rep (i, l, r) {
cnt1[a[i]]++;
res = max(res, a[i] * cnt1[a[i]]);
}
return res;
}
signed main() {
cin >> n >> q1;
B = sqrt(n);
rep (i, 1, n) {
cin >> a[i];
bel[i] = (i - 1) / B + 1;
ed[bel[i]] = i;
}
rep (i, 1, q1) {
cin >> q[i].l >> q[i].r;
q[i].id1 = i;
}
sort(q + 1, q + q1 + 1);
// rep (i, 1, q1) {
// cout << q[i].l << " " << q[i].r << " " << id(q[i].l) << "\n";
// }
int l = 0, r = -1;
rep (i, 1, q1) {
if (id(l) != id(q[i].l)) {
l = ed[id(q[i].l)];
r = l - 1;
res = 0;
cnt.clear();
}
if (id(q[i].r) != id(l)) {
// if (q[i].l == 2 && q[i].r == 5) {
// cout << l << " " << r << "\n";
// }
// if (q[i].l == 1 && q[i].r == 7) {
// rep (i, 1, n) {
// cout << "[ " << a[i] << " " << cnt[a[i]] << " ]" << "\n";
// }
// cout << l << " " << r << " " << bel[q[i].l] << " " << ed[q[i].l] << " " << res << "\n";
// }
while (r < q[i].r) add(a[++r]);
// if (q[i].l == 1 && q[i].r == 4) {
// rep (i, 1, n) {
// cout << a[i] << " " << cnt[a[i]] << "\n";
// }
// cout << res << "\n";
// }
int t = res;
while (l > q[i].l) add(a[--l]);
// if (q[i].l == 3 && q[i].r == 6) {
// rep (i, 1, n) {
// cout << a[i] << " " << cnt[a[i]] << "\n";
// }
// cout << t << " " << res << "\n";
// }
// if (q[i].l == 1 && q[i].r == 4) {
// cout << t << "\n";
// }
ans[q[i].id1] = res;
res = t;
l = ed[id(l)];
rep (j, q[i].l, l - 1) {
cnt[a[j]]--;
}
} else {
ans[q[i].id1] = bruteForce(q[i].l, q[i].r);
}
}
rep (i, 1, q1) {
cout << ans[i] << "\n";
}
}