#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
const int sq = 320;
int n;
int a[maxn], b[maxn];
int fq[maxn][sq], fq_aux[maxn];
ll best[sq][sq];
int soma(int l, int r, int v)
{
if (l == 0) return fq[v][r];
return fq[v][r] - fq[v][l-1];
}
int main(void)
{
int q;
scanf("%d %d", &n, &q);
map<int, int> mp;
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
mp[a[i]] = 0;
}
int aux = 0;
for (auto &x: mp)
x.second = ++aux;
for (int i = 1; i <= n; i++)
b[i] = mp[a[i]];
for (int i = 0; i*sq <= n; i++)
{
for (int j = 1; j <= n; j++)
fq[b[j]][i] = fq[b[j]][max(0, i-1)];
for (int j = max(1, i*sq); j <= min(n, (i+1)*sq-1); j++)
fq[b[j]][i]++;
}
for (int i = 0; i*sq <= n; i++)
{
for (int j = i; j*sq <= n; j++)
{
best[i][j] = best[i][max(0, j-1)];
for (int k = max(1, j*sq); k <= min(n, (j+1)*sq-1); k++)
{
fq_aux[b[k]]++;
best[i][j] = max(best[i][j], 1ll*a[k]*fq_aux[b[k]]);
}
}
for (int j = 1; j <= n; j++)
fq_aux[b[j]] = 0;
}
while (q--)
{
int l, r;
scanf("%d %d", &l, &r);
int b_l = l/sq, b_r = r/sq;
ll ans = 0;
if (b_l == b_r)
{
for (int i = l; i <= r; i++)
{
fq_aux[b[i]]++;
ans = max(ans, 1ll*a[i]*fq_aux[b[i]]);
}
printf("%lld\n", ans);
for (int i = l; i <= r; i++)
fq_aux[b[i]] = 0;
continue;
}
if (b_l < b_r-1) ans = best[b_l+1][b_r-1];
for (int i = l; i <= (b_l+1)*sq-1; i++)
fq_aux[b[i]]++;
for (int i = b_r*sq; i <= r; i++)
fq_aux[b[i]]++;
for (int i = l; i <= (b_l+1)*sq-1; i++)
ans = max(ans, 1ll*a[i]*(fq_aux[b[i]] + (b_l < b_r-1 ? soma(b_l+1, b_r-1, b[i]) : 0)));
for (int i = b_r*sq; i <= r; i++)
ans = max(ans, 1ll*a[i]*(fq_aux[b[i]] + (b_l < b_r-1 ? soma(b_l+1, b_r-1, b[i]) : 0)));
printf("%lld\n", ans);
for (int i = l; i <= (b_l+1)*sq-1; i++)
fq_aux[b[i]]--;
for (int i = b_r*sq; i <= r; i++)
fq_aux[b[i]]--;
}
}
Compilation message
historic.cpp: In function 'int main()':
historic.cpp:25:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | scanf("%d %d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~~
historic.cpp:31:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
historic.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | scanf("%d %d", &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
19 |
Correct |
5 ms |
588 KB |
Output is correct |
20 |
Correct |
12 ms |
716 KB |
Output is correct |
21 |
Correct |
18 ms |
588 KB |
Output is correct |
22 |
Correct |
19 ms |
824 KB |
Output is correct |
23 |
Correct |
17 ms |
548 KB |
Output is correct |
24 |
Correct |
17 ms |
460 KB |
Output is correct |
25 |
Correct |
21 ms |
2636 KB |
Output is correct |
26 |
Correct |
21 ms |
2508 KB |
Output is correct |
27 |
Correct |
21 ms |
2228 KB |
Output is correct |
28 |
Correct |
22 ms |
1996 KB |
Output is correct |
29 |
Correct |
20 ms |
1484 KB |
Output is correct |
30 |
Correct |
22 ms |
1840 KB |
Output is correct |
31 |
Correct |
18 ms |
588 KB |
Output is correct |
32 |
Correct |
17 ms |
460 KB |
Output is correct |
33 |
Correct |
20 ms |
1612 KB |
Output is correct |
34 |
Correct |
21 ms |
2844 KB |
Output is correct |
35 |
Correct |
22 ms |
3276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
972 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
5 ms |
1900 KB |
Output is correct |
8 |
Correct |
13 ms |
4428 KB |
Output is correct |
9 |
Correct |
38 ms |
9804 KB |
Output is correct |
10 |
Correct |
87 ms |
1344 KB |
Output is correct |
11 |
Correct |
401 ms |
2816 KB |
Output is correct |
12 |
Correct |
334 ms |
2624 KB |
Output is correct |
13 |
Correct |
453 ms |
10980 KB |
Output is correct |
14 |
Correct |
764 ms |
54908 KB |
Output is correct |
15 |
Correct |
771 ms |
79140 KB |
Output is correct |
16 |
Correct |
1096 ms |
134180 KB |
Output is correct |
17 |
Correct |
415 ms |
2840 KB |
Output is correct |
18 |
Correct |
561 ms |
4292 KB |
Output is correct |
19 |
Correct |
948 ms |
134896 KB |
Output is correct |
20 |
Correct |
752 ms |
133460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
19 |
Correct |
5 ms |
588 KB |
Output is correct |
20 |
Correct |
12 ms |
716 KB |
Output is correct |
21 |
Correct |
18 ms |
588 KB |
Output is correct |
22 |
Correct |
19 ms |
824 KB |
Output is correct |
23 |
Correct |
17 ms |
548 KB |
Output is correct |
24 |
Correct |
17 ms |
460 KB |
Output is correct |
25 |
Correct |
21 ms |
2636 KB |
Output is correct |
26 |
Correct |
21 ms |
2508 KB |
Output is correct |
27 |
Correct |
21 ms |
2228 KB |
Output is correct |
28 |
Correct |
22 ms |
1996 KB |
Output is correct |
29 |
Correct |
20 ms |
1484 KB |
Output is correct |
30 |
Correct |
22 ms |
1840 KB |
Output is correct |
31 |
Correct |
18 ms |
588 KB |
Output is correct |
32 |
Correct |
17 ms |
460 KB |
Output is correct |
33 |
Correct |
20 ms |
1612 KB |
Output is correct |
34 |
Correct |
21 ms |
2844 KB |
Output is correct |
35 |
Correct |
22 ms |
3276 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
332 KB |
Output is correct |
40 |
Correct |
2 ms |
972 KB |
Output is correct |
41 |
Correct |
2 ms |
332 KB |
Output is correct |
42 |
Correct |
5 ms |
1900 KB |
Output is correct |
43 |
Correct |
13 ms |
4428 KB |
Output is correct |
44 |
Correct |
38 ms |
9804 KB |
Output is correct |
45 |
Correct |
87 ms |
1344 KB |
Output is correct |
46 |
Correct |
401 ms |
2816 KB |
Output is correct |
47 |
Correct |
334 ms |
2624 KB |
Output is correct |
48 |
Correct |
453 ms |
10980 KB |
Output is correct |
49 |
Correct |
764 ms |
54908 KB |
Output is correct |
50 |
Correct |
771 ms |
79140 KB |
Output is correct |
51 |
Correct |
1096 ms |
134180 KB |
Output is correct |
52 |
Correct |
415 ms |
2840 KB |
Output is correct |
53 |
Correct |
561 ms |
4292 KB |
Output is correct |
54 |
Correct |
948 ms |
134896 KB |
Output is correct |
55 |
Correct |
752 ms |
133460 KB |
Output is correct |
56 |
Correct |
37 ms |
784 KB |
Output is correct |
57 |
Correct |
91 ms |
1996 KB |
Output is correct |
58 |
Correct |
190 ms |
7812 KB |
Output is correct |
59 |
Correct |
365 ms |
17824 KB |
Output is correct |
60 |
Correct |
553 ms |
22744 KB |
Output is correct |
61 |
Correct |
445 ms |
9772 KB |
Output is correct |
62 |
Correct |
429 ms |
4380 KB |
Output is correct |
63 |
Correct |
470 ms |
4424 KB |
Output is correct |
64 |
Correct |
532 ms |
4788 KB |
Output is correct |
65 |
Correct |
627 ms |
4664 KB |
Output is correct |
66 |
Correct |
636 ms |
5016 KB |
Output is correct |
67 |
Correct |
712 ms |
5356 KB |
Output is correct |
68 |
Correct |
1117 ms |
13432 KB |
Output is correct |
69 |
Correct |
1669 ms |
46660 KB |
Output is correct |
70 |
Correct |
1832 ms |
78872 KB |
Output is correct |
71 |
Correct |
1795 ms |
62460 KB |
Output is correct |
72 |
Correct |
1729 ms |
56640 KB |
Output is correct |
73 |
Correct |
1722 ms |
51660 KB |
Output is correct |
74 |
Correct |
1710 ms |
48060 KB |
Output is correct |
75 |
Correct |
1644 ms |
44616 KB |
Output is correct |
76 |
Correct |
1602 ms |
41936 KB |
Output is correct |
77 |
Correct |
1625 ms |
39724 KB |
Output is correct |
78 |
Correct |
1589 ms |
37916 KB |
Output is correct |
79 |
Correct |
1566 ms |
36132 KB |
Output is correct |
80 |
Correct |
639 ms |
5336 KB |
Output is correct |