#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> l(q), r(q);
for (int i = 0; i < q; i++) {
cin >> l[i] >> r[i];
--l[i]; --r[i];
}
vector<int> xs;
for (int i = 0; i < n; i++) {
xs.push_back(a[i]);
}
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
for (int i = 0; i < n; i++) {
a[i] = (int) (lower_bound(xs.begin(), xs.end(), a[i]) - xs.begin());
}
int k = (int) xs.size();
const int BLOCK = 100;
vector<int> cnt(k);
for (int i = 0; i < n; i++) {
cnt[a[i]] += 1;
}
vector<long long> res(q);
for (int v = 0; v < k; v++) {
if (cnt[v] < BLOCK) {
continue;
}
vector<int> pref(n);
for (int i = 0; i < n; i++) {
if (i > 0) {
pref[i] = pref[i - 1];
}
pref[i] += (a[i] == v);
}
for (int i = 0; i < q; i++) {
int s = pref[r[i]] - (l[i] > 0 ? pref[l[i] - 1] : 0);
res[i] = max(res[i], s * 1LL * xs[v]);
}
}
vector<vector<int>> qs(n);
for (int i = 0; i < q; i++) {
qs[r[i]].push_back(i);
}
vector<long long> fenw(n + 1);
auto Modify = [&](int p, long long v) {
for (p++; p <= n; p += p & -p) {
fenw[p] = max(fenw[p], v);
}
};
auto Query = [&](int p) {
long long ret = 0;
for (p++; p; p -= p & -p) {
ret = max(ret, fenw[p]);
}
return ret;
};
vector<vector<int>> pos(k);
for (int i = 0; i < n; i++) {
if (cnt[a[i]] < BLOCK) {
pos[a[i]].push_back(i);
int sz = (int) pos[a[i]].size();
for (int j = 0; j < sz; j++) {
Modify(n - pos[a[i]][j] - 1, (sz - j) * 1LL * xs[a[i]]);
}
}
for (int j : qs[i]) {
res[j] = max(res[j], Query(n - l[j] - 1));
}
}
for (int i = 0; i < q; i++) {
cout << res[i] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
600 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
600 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
3 ms |
860 KB |
Output is correct |
22 |
Correct |
4 ms |
860 KB |
Output is correct |
23 |
Correct |
3 ms |
860 KB |
Output is correct |
24 |
Correct |
3 ms |
840 KB |
Output is correct |
25 |
Correct |
3 ms |
856 KB |
Output is correct |
26 |
Correct |
3 ms |
860 KB |
Output is correct |
27 |
Correct |
3 ms |
828 KB |
Output is correct |
28 |
Correct |
4 ms |
828 KB |
Output is correct |
29 |
Correct |
4 ms |
984 KB |
Output is correct |
30 |
Correct |
3 ms |
860 KB |
Output is correct |
31 |
Correct |
3 ms |
860 KB |
Output is correct |
32 |
Correct |
2 ms |
860 KB |
Output is correct |
33 |
Correct |
3 ms |
980 KB |
Output is correct |
34 |
Correct |
3 ms |
860 KB |
Output is correct |
35 |
Correct |
5 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
460 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
472 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
5 ms |
1116 KB |
Output is correct |
9 |
Correct |
10 ms |
2004 KB |
Output is correct |
10 |
Correct |
6 ms |
2832 KB |
Output is correct |
11 |
Correct |
38 ms |
10272 KB |
Output is correct |
12 |
Correct |
37 ms |
5460 KB |
Output is correct |
13 |
Correct |
62 ms |
7368 KB |
Output is correct |
14 |
Correct |
53 ms |
10964 KB |
Output is correct |
15 |
Correct |
67 ms |
15444 KB |
Output is correct |
16 |
Correct |
47 ms |
14432 KB |
Output is correct |
17 |
Correct |
36 ms |
6476 KB |
Output is correct |
18 |
Correct |
51 ms |
10024 KB |
Output is correct |
19 |
Correct |
40 ms |
16516 KB |
Output is correct |
20 |
Correct |
25 ms |
12236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
600 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
3 ms |
860 KB |
Output is correct |
22 |
Correct |
4 ms |
860 KB |
Output is correct |
23 |
Correct |
3 ms |
860 KB |
Output is correct |
24 |
Correct |
3 ms |
840 KB |
Output is correct |
25 |
Correct |
3 ms |
856 KB |
Output is correct |
26 |
Correct |
3 ms |
860 KB |
Output is correct |
27 |
Correct |
3 ms |
828 KB |
Output is correct |
28 |
Correct |
4 ms |
828 KB |
Output is correct |
29 |
Correct |
4 ms |
984 KB |
Output is correct |
30 |
Correct |
3 ms |
860 KB |
Output is correct |
31 |
Correct |
3 ms |
860 KB |
Output is correct |
32 |
Correct |
2 ms |
860 KB |
Output is correct |
33 |
Correct |
3 ms |
980 KB |
Output is correct |
34 |
Correct |
3 ms |
860 KB |
Output is correct |
35 |
Correct |
5 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
460 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
472 KB |
Output is correct |
42 |
Correct |
3 ms |
860 KB |
Output is correct |
43 |
Correct |
5 ms |
1116 KB |
Output is correct |
44 |
Correct |
10 ms |
2004 KB |
Output is correct |
45 |
Correct |
6 ms |
2832 KB |
Output is correct |
46 |
Correct |
38 ms |
10272 KB |
Output is correct |
47 |
Correct |
37 ms |
5460 KB |
Output is correct |
48 |
Correct |
62 ms |
7368 KB |
Output is correct |
49 |
Correct |
53 ms |
10964 KB |
Output is correct |
50 |
Correct |
67 ms |
15444 KB |
Output is correct |
51 |
Correct |
47 ms |
14432 KB |
Output is correct |
52 |
Correct |
36 ms |
6476 KB |
Output is correct |
53 |
Correct |
51 ms |
10024 KB |
Output is correct |
54 |
Correct |
40 ms |
16516 KB |
Output is correct |
55 |
Correct |
25 ms |
12236 KB |
Output is correct |
56 |
Correct |
6 ms |
1368 KB |
Output is correct |
57 |
Correct |
18 ms |
2380 KB |
Output is correct |
58 |
Correct |
19 ms |
3664 KB |
Output is correct |
59 |
Correct |
25 ms |
5064 KB |
Output is correct |
60 |
Correct |
31 ms |
6360 KB |
Output is correct |
61 |
Correct |
45 ms |
7128 KB |
Output is correct |
62 |
Correct |
84 ms |
7784 KB |
Output is correct |
63 |
Correct |
43 ms |
8852 KB |
Output is correct |
64 |
Correct |
38 ms |
10096 KB |
Output is correct |
65 |
Correct |
37 ms |
10312 KB |
Output is correct |
66 |
Correct |
47 ms |
10504 KB |
Output is correct |
67 |
Correct |
106 ms |
10572 KB |
Output is correct |
68 |
Correct |
83 ms |
11732 KB |
Output is correct |
69 |
Correct |
64 ms |
12976 KB |
Output is correct |
70 |
Correct |
63 ms |
14292 KB |
Output is correct |
71 |
Correct |
63 ms |
13516 KB |
Output is correct |
72 |
Correct |
64 ms |
13248 KB |
Output is correct |
73 |
Correct |
70 ms |
13256 KB |
Output is correct |
74 |
Correct |
72 ms |
13268 KB |
Output is correct |
75 |
Correct |
66 ms |
12744 KB |
Output is correct |
76 |
Correct |
68 ms |
12752 KB |
Output is correct |
77 |
Correct |
70 ms |
12764 KB |
Output is correct |
78 |
Correct |
68 ms |
12612 KB |
Output is correct |
79 |
Correct |
70 ms |
12500 KB |
Output is correct |
80 |
Correct |
41 ms |
11336 KB |
Output is correct |