#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define Mup(a,x) a=max(a, x)
#define mup(a,x) a=min(a, x)
#define all(v) v.begin(),v.end()
#define lb lower_bound
#define INF (1ll<<60)
using namespace std;
typedef long long LL;
typedef pair<LL,LL> pii;
int N, Q, B=200;
LL A[101010], C[101010];
LL P[550][550];
vector<int> comp, V[101010];
int main(){
scanf("%d %d", &N, &Q);
for (int i=0; i<N; i++){
scanf("%lld", &A[i]);
comp.pb(A[i]);
}
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
for (int i=0; i<N; i++){
A[i] = lower_bound(comp.begin(), comp.end(), A[i])-comp.begin();
V[A[i]].pb(i);
}
for (int i=0; B*i<N; i++){
LL mx=0;
for (int j=0; j<N; j++) C[j]=0;
for (int j=i; B*j<N; j++){
for (int k=0; k<B && j*B+k<N; k++){
C[A[j*B+k]] += comp[A[j*B+k]];
mx = max(mx, C[A[j*B+k]]);
}
P[i][j] = mx;
}
}
while (Q--){
int l, r, s, e;
scanf("%d %d", &l, &r); l--, r--;
s=l, e=r;
LL ans = 0;
while (s<=e && s%B){
Mup(ans, (LL)comp[A[s]] * (upper_bound(all(V[A[s]]), r) - lower_bound(all(V[A[s]]), l)));
s++;
}
while (s<=e && e%B != B-1){
Mup(ans, (LL)comp[A[e]] * (upper_bound(all(V[A[e]]), r) - lower_bound(all(V[A[e]]), l)));
e--;
}
if (s<e) Mup(ans, P[s/B][e/B]);
printf("%lld\n", ans);
}
return 0;
}
Compilation message
historic.cpp: In function 'int main()':
historic.cpp:20:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &Q);
~~~~~^~~~~~~~~~~~~~~~~
historic.cpp:22:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &A[i]);
~~~~~^~~~~~~~~~~~~~~
historic.cpp:44:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &l, &r); l--, r--;
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2596 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
3 ms |
2688 KB |
Output is correct |
8 |
Correct |
2 ms |
2688 KB |
Output is correct |
9 |
Correct |
3 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
2 ms |
2816 KB |
Output is correct |
12 |
Correct |
3 ms |
2560 KB |
Output is correct |
13 |
Correct |
2 ms |
2560 KB |
Output is correct |
14 |
Correct |
2 ms |
2688 KB |
Output is correct |
15 |
Correct |
2 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2596 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
3 ms |
2688 KB |
Output is correct |
8 |
Correct |
2 ms |
2688 KB |
Output is correct |
9 |
Correct |
3 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
2 ms |
2816 KB |
Output is correct |
12 |
Correct |
3 ms |
2560 KB |
Output is correct |
13 |
Correct |
2 ms |
2560 KB |
Output is correct |
14 |
Correct |
2 ms |
2688 KB |
Output is correct |
15 |
Correct |
2 ms |
2688 KB |
Output is correct |
16 |
Correct |
2 ms |
2688 KB |
Output is correct |
17 |
Correct |
4 ms |
2688 KB |
Output is correct |
18 |
Correct |
9 ms |
2816 KB |
Output is correct |
19 |
Correct |
19 ms |
2816 KB |
Output is correct |
20 |
Correct |
41 ms |
2880 KB |
Output is correct |
21 |
Correct |
77 ms |
3072 KB |
Output is correct |
22 |
Correct |
68 ms |
3084 KB |
Output is correct |
23 |
Correct |
83 ms |
3064 KB |
Output is correct |
24 |
Correct |
82 ms |
3064 KB |
Output is correct |
25 |
Correct |
40 ms |
3072 KB |
Output is correct |
26 |
Correct |
41 ms |
3072 KB |
Output is correct |
27 |
Correct |
45 ms |
3072 KB |
Output is correct |
28 |
Correct |
47 ms |
3072 KB |
Output is correct |
29 |
Correct |
51 ms |
3072 KB |
Output is correct |
30 |
Correct |
47 ms |
3072 KB |
Output is correct |
31 |
Correct |
81 ms |
3072 KB |
Output is correct |
32 |
Correct |
60 ms |
3072 KB |
Output is correct |
33 |
Correct |
47 ms |
3072 KB |
Output is correct |
34 |
Correct |
37 ms |
3072 KB |
Output is correct |
35 |
Correct |
35 ms |
3072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
4 ms |
2816 KB |
Output is correct |
6 |
Correct |
4 ms |
2944 KB |
Output is correct |
7 |
Correct |
6 ms |
3072 KB |
Output is correct |
8 |
Correct |
10 ms |
3456 KB |
Output is correct |
9 |
Correct |
23 ms |
4224 KB |
Output is correct |
10 |
Correct |
52 ms |
5620 KB |
Output is correct |
11 |
Correct |
1002 ms |
9452 KB |
Output is correct |
12 |
Correct |
134 ms |
8176 KB |
Output is correct |
13 |
Correct |
406 ms |
8624 KB |
Output is correct |
14 |
Correct |
450 ms |
9580 KB |
Output is correct |
15 |
Correct |
229 ms |
10480 KB |
Output is correct |
16 |
Correct |
286 ms |
11248 KB |
Output is correct |
17 |
Correct |
498 ms |
8172 KB |
Output is correct |
18 |
Correct |
1025 ms |
8944 KB |
Output is correct |
19 |
Correct |
278 ms |
11632 KB |
Output is correct |
20 |
Correct |
167 ms |
10996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2596 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
3 ms |
2688 KB |
Output is correct |
8 |
Correct |
2 ms |
2688 KB |
Output is correct |
9 |
Correct |
3 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
2 ms |
2816 KB |
Output is correct |
12 |
Correct |
3 ms |
2560 KB |
Output is correct |
13 |
Correct |
2 ms |
2560 KB |
Output is correct |
14 |
Correct |
2 ms |
2688 KB |
Output is correct |
15 |
Correct |
2 ms |
2688 KB |
Output is correct |
16 |
Correct |
2 ms |
2688 KB |
Output is correct |
17 |
Correct |
4 ms |
2688 KB |
Output is correct |
18 |
Correct |
9 ms |
2816 KB |
Output is correct |
19 |
Correct |
19 ms |
2816 KB |
Output is correct |
20 |
Correct |
41 ms |
2880 KB |
Output is correct |
21 |
Correct |
77 ms |
3072 KB |
Output is correct |
22 |
Correct |
68 ms |
3084 KB |
Output is correct |
23 |
Correct |
83 ms |
3064 KB |
Output is correct |
24 |
Correct |
82 ms |
3064 KB |
Output is correct |
25 |
Correct |
40 ms |
3072 KB |
Output is correct |
26 |
Correct |
41 ms |
3072 KB |
Output is correct |
27 |
Correct |
45 ms |
3072 KB |
Output is correct |
28 |
Correct |
47 ms |
3072 KB |
Output is correct |
29 |
Correct |
51 ms |
3072 KB |
Output is correct |
30 |
Correct |
47 ms |
3072 KB |
Output is correct |
31 |
Correct |
81 ms |
3072 KB |
Output is correct |
32 |
Correct |
60 ms |
3072 KB |
Output is correct |
33 |
Correct |
47 ms |
3072 KB |
Output is correct |
34 |
Correct |
37 ms |
3072 KB |
Output is correct |
35 |
Correct |
35 ms |
3072 KB |
Output is correct |
36 |
Correct |
2 ms |
2688 KB |
Output is correct |
37 |
Correct |
2 ms |
2688 KB |
Output is correct |
38 |
Correct |
2 ms |
2688 KB |
Output is correct |
39 |
Correct |
2 ms |
2688 KB |
Output is correct |
40 |
Correct |
4 ms |
2816 KB |
Output is correct |
41 |
Correct |
4 ms |
2944 KB |
Output is correct |
42 |
Correct |
6 ms |
3072 KB |
Output is correct |
43 |
Correct |
10 ms |
3456 KB |
Output is correct |
44 |
Correct |
23 ms |
4224 KB |
Output is correct |
45 |
Correct |
52 ms |
5620 KB |
Output is correct |
46 |
Correct |
1002 ms |
9452 KB |
Output is correct |
47 |
Correct |
134 ms |
8176 KB |
Output is correct |
48 |
Correct |
406 ms |
8624 KB |
Output is correct |
49 |
Correct |
450 ms |
9580 KB |
Output is correct |
50 |
Correct |
229 ms |
10480 KB |
Output is correct |
51 |
Correct |
286 ms |
11248 KB |
Output is correct |
52 |
Correct |
498 ms |
8172 KB |
Output is correct |
53 |
Correct |
1025 ms |
8944 KB |
Output is correct |
54 |
Correct |
278 ms |
11632 KB |
Output is correct |
55 |
Correct |
167 ms |
10996 KB |
Output is correct |
56 |
Correct |
181 ms |
3448 KB |
Output is correct |
57 |
Correct |
318 ms |
4216 KB |
Output is correct |
58 |
Correct |
363 ms |
5172 KB |
Output is correct |
59 |
Correct |
420 ms |
5876 KB |
Output is correct |
60 |
Correct |
521 ms |
6800 KB |
Output is correct |
61 |
Correct |
952 ms |
7092 KB |
Output is correct |
62 |
Correct |
1572 ms |
7752 KB |
Output is correct |
63 |
Correct |
1913 ms |
8636 KB |
Output is correct |
64 |
Correct |
1804 ms |
9072 KB |
Output is correct |
65 |
Correct |
1347 ms |
9500 KB |
Output is correct |
66 |
Correct |
2098 ms |
10708 KB |
Output is correct |
67 |
Correct |
2503 ms |
10476 KB |
Output is correct |
68 |
Correct |
1735 ms |
11044 KB |
Output is correct |
69 |
Correct |
1120 ms |
11228 KB |
Output is correct |
70 |
Correct |
942 ms |
11984 KB |
Output is correct |
71 |
Correct |
1027 ms |
11520 KB |
Output is correct |
72 |
Correct |
1128 ms |
11484 KB |
Output is correct |
73 |
Correct |
1109 ms |
11340 KB |
Output is correct |
74 |
Correct |
1120 ms |
11376 KB |
Output is correct |
75 |
Correct |
1171 ms |
11396 KB |
Output is correct |
76 |
Correct |
1190 ms |
11248 KB |
Output is correct |
77 |
Correct |
1250 ms |
11208 KB |
Output is correct |
78 |
Correct |
1298 ms |
11288 KB |
Output is correct |
79 |
Correct |
1270 ms |
11244 KB |
Output is correct |
80 |
Correct |
1311 ms |
11152 KB |
Output is correct |