//回滾莫隊 裸題
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int,int>;
#define fs first
#define sc second
int n, q, a[510000], cnt[510000] = {0};
long long nowans, ans[510000];
vector <int> b;
int B;
vector <pair<pii,int>> qs[510000];
void add (int x) {
cnt[x]++;
nowans = max(nowans, b[x] * 1LL * cnt[x]);
}
int main () {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> q;
B = sqrt((n + q + 1) / 2);
for (int i = 1; i <= n; i++) {
cin >> a[i];
b.push_back(a[i]);
}
sort (b.begin(), b.end());
b.erase(unique(b.begin(), b.end()), b.end());
for (int i = 1; i <= n; i++) a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
for (int i = 1; i <= q; i++) {
pii p;
cin >> p.fs >> p.sc;
int lb = p.fs / B, rb = p.sc / B;
if (lb == rb) { // 特判塊太小
for (int j = p.fs; j <= p.sc; j++) cnt[a[j]] = 0;
nowans = 0;
for (int j = p.fs; j <= p.sc; j++) add(a[j]);
ans[i] = nowans;
continue;
}
qs[p.fs / B] . push_back({p, i});
}
for (int i = 1 / B; i <= n / B; i++) {
sort(qs[i].begin(), qs[i].end(), [](pair<pii,int>a, pair<pii,int>b){return a.fs.sc < b.fs.sc;} );
int RB = min(i * B + B, n), R = RB - 1;
nowans = 0;
memset(cnt, 0, sizeof(cnt));
for (auto qq : qs[i]) {
while (R < qq.fs.sc) {
R++;
add(a[R]);
}
long long oldans = nowans;
vector <pii> old;
for (int j = RB - 1; j >= qq.fs.fs; j--) old.push_back({a[j], cnt[a[j]]});
for (int j = RB - 1; j >= qq.fs.fs; j--) add(a[j]);
ans[qq.sc] = nowans;
nowans = oldans;
for (pii p : old) cnt[p.fs] = p.sc;
}
}
for (int i = 1; i <= q; i++) cout << ans[i] << '\n';
return 0;}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16984 KB |
Output is correct |
2 |
Correct |
7 ms |
17044 KB |
Output is correct |
3 |
Correct |
4 ms |
16984 KB |
Output is correct |
4 |
Correct |
4 ms |
16984 KB |
Output is correct |
5 |
Correct |
5 ms |
16988 KB |
Output is correct |
6 |
Correct |
4 ms |
16988 KB |
Output is correct |
7 |
Correct |
4 ms |
16988 KB |
Output is correct |
8 |
Correct |
4 ms |
16988 KB |
Output is correct |
9 |
Correct |
4 ms |
16988 KB |
Output is correct |
10 |
Correct |
4 ms |
16984 KB |
Output is correct |
11 |
Correct |
4 ms |
17048 KB |
Output is correct |
12 |
Correct |
4 ms |
16988 KB |
Output is correct |
13 |
Correct |
4 ms |
17052 KB |
Output is correct |
14 |
Correct |
4 ms |
16988 KB |
Output is correct |
15 |
Correct |
4 ms |
16988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16984 KB |
Output is correct |
2 |
Correct |
7 ms |
17044 KB |
Output is correct |
3 |
Correct |
4 ms |
16984 KB |
Output is correct |
4 |
Correct |
4 ms |
16984 KB |
Output is correct |
5 |
Correct |
5 ms |
16988 KB |
Output is correct |
6 |
Correct |
4 ms |
16988 KB |
Output is correct |
7 |
Correct |
4 ms |
16988 KB |
Output is correct |
8 |
Correct |
4 ms |
16988 KB |
Output is correct |
9 |
Correct |
4 ms |
16988 KB |
Output is correct |
10 |
Correct |
4 ms |
16984 KB |
Output is correct |
11 |
Correct |
4 ms |
17048 KB |
Output is correct |
12 |
Correct |
4 ms |
16988 KB |
Output is correct |
13 |
Correct |
4 ms |
17052 KB |
Output is correct |
14 |
Correct |
4 ms |
16988 KB |
Output is correct |
15 |
Correct |
4 ms |
16988 KB |
Output is correct |
16 |
Correct |
5 ms |
17048 KB |
Output is correct |
17 |
Correct |
5 ms |
16988 KB |
Output is correct |
18 |
Correct |
5 ms |
16984 KB |
Output is correct |
19 |
Correct |
6 ms |
16988 KB |
Output is correct |
20 |
Correct |
10 ms |
16984 KB |
Output is correct |
21 |
Correct |
11 ms |
17244 KB |
Output is correct |
22 |
Correct |
13 ms |
17244 KB |
Output is correct |
23 |
Correct |
11 ms |
17240 KB |
Output is correct |
24 |
Correct |
10 ms |
17244 KB |
Output is correct |
25 |
Correct |
11 ms |
17268 KB |
Output is correct |
26 |
Correct |
11 ms |
17280 KB |
Output is correct |
27 |
Correct |
11 ms |
17240 KB |
Output is correct |
28 |
Correct |
11 ms |
17244 KB |
Output is correct |
29 |
Correct |
10 ms |
17244 KB |
Output is correct |
30 |
Correct |
10 ms |
17244 KB |
Output is correct |
31 |
Correct |
10 ms |
17164 KB |
Output is correct |
32 |
Correct |
11 ms |
17244 KB |
Output is correct |
33 |
Correct |
14 ms |
17240 KB |
Output is correct |
34 |
Correct |
12 ms |
17244 KB |
Output is correct |
35 |
Correct |
11 ms |
17244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
17052 KB |
Output is correct |
2 |
Correct |
4 ms |
16988 KB |
Output is correct |
3 |
Correct |
5 ms |
16988 KB |
Output is correct |
4 |
Correct |
5 ms |
17240 KB |
Output is correct |
5 |
Correct |
5 ms |
16984 KB |
Output is correct |
6 |
Correct |
7 ms |
16988 KB |
Output is correct |
7 |
Correct |
8 ms |
16988 KB |
Output is correct |
8 |
Correct |
12 ms |
17240 KB |
Output is correct |
9 |
Correct |
17 ms |
17500 KB |
Output is correct |
10 |
Correct |
23 ms |
17632 KB |
Output is correct |
11 |
Correct |
71 ms |
21972 KB |
Output is correct |
12 |
Correct |
34 ms |
18128 KB |
Output is correct |
13 |
Correct |
77 ms |
18872 KB |
Output is correct |
14 |
Correct |
95 ms |
20192 KB |
Output is correct |
15 |
Correct |
56 ms |
22584 KB |
Output is correct |
16 |
Correct |
146 ms |
20604 KB |
Output is correct |
17 |
Correct |
86 ms |
18428 KB |
Output is correct |
18 |
Correct |
159 ms |
23248 KB |
Output is correct |
19 |
Correct |
182 ms |
23780 KB |
Output is correct |
20 |
Correct |
66 ms |
18892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16984 KB |
Output is correct |
2 |
Correct |
7 ms |
17044 KB |
Output is correct |
3 |
Correct |
4 ms |
16984 KB |
Output is correct |
4 |
Correct |
4 ms |
16984 KB |
Output is correct |
5 |
Correct |
5 ms |
16988 KB |
Output is correct |
6 |
Correct |
4 ms |
16988 KB |
Output is correct |
7 |
Correct |
4 ms |
16988 KB |
Output is correct |
8 |
Correct |
4 ms |
16988 KB |
Output is correct |
9 |
Correct |
4 ms |
16988 KB |
Output is correct |
10 |
Correct |
4 ms |
16984 KB |
Output is correct |
11 |
Correct |
4 ms |
17048 KB |
Output is correct |
12 |
Correct |
4 ms |
16988 KB |
Output is correct |
13 |
Correct |
4 ms |
17052 KB |
Output is correct |
14 |
Correct |
4 ms |
16988 KB |
Output is correct |
15 |
Correct |
4 ms |
16988 KB |
Output is correct |
16 |
Correct |
5 ms |
17048 KB |
Output is correct |
17 |
Correct |
5 ms |
16988 KB |
Output is correct |
18 |
Correct |
5 ms |
16984 KB |
Output is correct |
19 |
Correct |
6 ms |
16988 KB |
Output is correct |
20 |
Correct |
10 ms |
16984 KB |
Output is correct |
21 |
Correct |
11 ms |
17244 KB |
Output is correct |
22 |
Correct |
13 ms |
17244 KB |
Output is correct |
23 |
Correct |
11 ms |
17240 KB |
Output is correct |
24 |
Correct |
10 ms |
17244 KB |
Output is correct |
25 |
Correct |
11 ms |
17268 KB |
Output is correct |
26 |
Correct |
11 ms |
17280 KB |
Output is correct |
27 |
Correct |
11 ms |
17240 KB |
Output is correct |
28 |
Correct |
11 ms |
17244 KB |
Output is correct |
29 |
Correct |
10 ms |
17244 KB |
Output is correct |
30 |
Correct |
10 ms |
17244 KB |
Output is correct |
31 |
Correct |
10 ms |
17164 KB |
Output is correct |
32 |
Correct |
11 ms |
17244 KB |
Output is correct |
33 |
Correct |
14 ms |
17240 KB |
Output is correct |
34 |
Correct |
12 ms |
17244 KB |
Output is correct |
35 |
Correct |
11 ms |
17244 KB |
Output is correct |
36 |
Correct |
5 ms |
17052 KB |
Output is correct |
37 |
Correct |
4 ms |
16988 KB |
Output is correct |
38 |
Correct |
5 ms |
16988 KB |
Output is correct |
39 |
Correct |
5 ms |
17240 KB |
Output is correct |
40 |
Correct |
5 ms |
16984 KB |
Output is correct |
41 |
Correct |
7 ms |
16988 KB |
Output is correct |
42 |
Correct |
8 ms |
16988 KB |
Output is correct |
43 |
Correct |
12 ms |
17240 KB |
Output is correct |
44 |
Correct |
17 ms |
17500 KB |
Output is correct |
45 |
Correct |
23 ms |
17632 KB |
Output is correct |
46 |
Correct |
71 ms |
21972 KB |
Output is correct |
47 |
Correct |
34 ms |
18128 KB |
Output is correct |
48 |
Correct |
77 ms |
18872 KB |
Output is correct |
49 |
Correct |
95 ms |
20192 KB |
Output is correct |
50 |
Correct |
56 ms |
22584 KB |
Output is correct |
51 |
Correct |
146 ms |
20604 KB |
Output is correct |
52 |
Correct |
86 ms |
18428 KB |
Output is correct |
53 |
Correct |
159 ms |
23248 KB |
Output is correct |
54 |
Correct |
182 ms |
23780 KB |
Output is correct |
55 |
Correct |
66 ms |
18892 KB |
Output is correct |
56 |
Correct |
17 ms |
17492 KB |
Output is correct |
57 |
Correct |
38 ms |
18008 KB |
Output is correct |
58 |
Correct |
59 ms |
18340 KB |
Output is correct |
59 |
Correct |
91 ms |
18900 KB |
Output is correct |
60 |
Correct |
121 ms |
19416 KB |
Output is correct |
61 |
Correct |
137 ms |
20116 KB |
Output is correct |
62 |
Correct |
148 ms |
22832 KB |
Output is correct |
63 |
Correct |
189 ms |
23624 KB |
Output is correct |
64 |
Correct |
203 ms |
24016 KB |
Output is correct |
65 |
Correct |
281 ms |
24208 KB |
Output is correct |
66 |
Correct |
272 ms |
24272 KB |
Output is correct |
67 |
Correct |
253 ms |
24456 KB |
Output is correct |
68 |
Correct |
261 ms |
24360 KB |
Output is correct |
69 |
Correct |
268 ms |
24336 KB |
Output is correct |
70 |
Correct |
304 ms |
24592 KB |
Output is correct |
71 |
Correct |
279 ms |
24616 KB |
Output is correct |
72 |
Correct |
284 ms |
24628 KB |
Output is correct |
73 |
Correct |
283 ms |
24616 KB |
Output is correct |
74 |
Correct |
316 ms |
24532 KB |
Output is correct |
75 |
Correct |
283 ms |
24532 KB |
Output is correct |
76 |
Correct |
296 ms |
24592 KB |
Output is correct |
77 |
Correct |
271 ms |
24688 KB |
Output is correct |
78 |
Correct |
284 ms |
24568 KB |
Output is correct |
79 |
Correct |
265 ms |
24504 KB |
Output is correct |
80 |
Correct |
275 ms |
25044 KB |
Output is correct |