#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int B = 300;
int n,q;
int a[100005];
long long buck[350][350];
vector<int> v;
vector<int> show[100005];
inline int range_count(int p, int q, int o){
return (int)(upper_bound(show[o].begin(),show[o].end(),q) - lower_bound(show[o].begin(),show[o].end(),p));
}
void query(){
int p,q;
scanf("%d %d",&p,&q);
p--;
q--;
if(p/B == q/B){
long long mv = 0;
for (int i=p; i<=q; i++) {
mv = max(mv,1ll * v[a[i]] * range_count(p,q,a[i]));
}
printf("%lld\n",mv);
return;
}
else{
long long mv = 0;
int i;
for (i=p; i%B != 0; i++) {
mv = max(mv,1ll * v[a[i]] * range_count(p,q,a[i]));
}
mv = max(mv,buck[i/B][q/B]);
for (int i=q/B*B+1; i<=q; i++) {
mv = max(mv,1ll * v[a[i]] * range_count(p,q,a[i]));
}
printf("%lld\n",mv);
}
}
int main(){
scanf("%d %d",&n,&q);
for (int i=0; i<n; i++) {
scanf("%d",&a[i]);
v.push_back(a[i]);
}
sort(v.begin(),v.end());
v.resize(unique(v.begin(),v.end()) - v.begin());
for (int i=0; i<n; i++) {
a[i] = (int)(lower_bound(v.begin(),v.end(),a[i]) - v.begin());
show[a[i]].push_back(i);
}
// buck[i][j] = i * B ~ j * B
for (int i=0; i<n; i+=B) {
long long cnt[100005] = {};
long long maxv = 0;
for (int j=i; j<n; j++) {
cnt[a[j]] += v[a[j]];
maxv = max(maxv,cnt[a[j]]);
if(j % B== 0){
buck[i/B][j/B] = maxv;
}
}
}
while (q--) {
query();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5588 KB |
Output is correct |
2 |
Correct |
0 ms |
5588 KB |
Output is correct |
3 |
Correct |
0 ms |
5592 KB |
Output is correct |
4 |
Correct |
0 ms |
5588 KB |
Output is correct |
5 |
Correct |
0 ms |
5588 KB |
Output is correct |
6 |
Correct |
0 ms |
5592 KB |
Output is correct |
7 |
Correct |
0 ms |
5592 KB |
Output is correct |
8 |
Correct |
0 ms |
5592 KB |
Output is correct |
9 |
Correct |
0 ms |
5592 KB |
Output is correct |
10 |
Correct |
0 ms |
5592 KB |
Output is correct |
11 |
Correct |
0 ms |
5588 KB |
Output is correct |
12 |
Correct |
0 ms |
5592 KB |
Output is correct |
13 |
Correct |
0 ms |
5592 KB |
Output is correct |
14 |
Correct |
0 ms |
5592 KB |
Output is correct |
15 |
Correct |
0 ms |
5588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5592 KB |
Output is correct |
2 |
Correct |
4 ms |
5592 KB |
Output is correct |
3 |
Correct |
8 ms |
5588 KB |
Output is correct |
4 |
Correct |
24 ms |
5592 KB |
Output is correct |
5 |
Correct |
64 ms |
5592 KB |
Output is correct |
6 |
Correct |
120 ms |
5592 KB |
Output is correct |
7 |
Correct |
108 ms |
5592 KB |
Output is correct |
8 |
Correct |
136 ms |
5588 KB |
Output is correct |
9 |
Correct |
128 ms |
5592 KB |
Output is correct |
10 |
Correct |
60 ms |
5588 KB |
Output is correct |
11 |
Correct |
64 ms |
5588 KB |
Output is correct |
12 |
Correct |
72 ms |
5588 KB |
Output is correct |
13 |
Correct |
72 ms |
5592 KB |
Output is correct |
14 |
Correct |
80 ms |
5592 KB |
Output is correct |
15 |
Correct |
76 ms |
5592 KB |
Output is correct |
16 |
Correct |
136 ms |
5592 KB |
Output is correct |
17 |
Correct |
96 ms |
5588 KB |
Output is correct |
18 |
Correct |
76 ms |
5592 KB |
Output is correct |
19 |
Correct |
56 ms |
5592 KB |
Output is correct |
20 |
Correct |
52 ms |
5592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5588 KB |
Output is correct |
2 |
Correct |
0 ms |
5588 KB |
Output is correct |
3 |
Correct |
0 ms |
5596 KB |
Output is correct |
4 |
Correct |
0 ms |
5592 KB |
Output is correct |
5 |
Correct |
4 ms |
5592 KB |
Output is correct |
6 |
Correct |
0 ms |
5592 KB |
Output is correct |
7 |
Correct |
4 ms |
5588 KB |
Output is correct |
8 |
Correct |
12 ms |
5720 KB |
Output is correct |
9 |
Correct |
12 ms |
5984 KB |
Output is correct |
10 |
Correct |
40 ms |
6536 KB |
Output is correct |
11 |
Correct |
1044 ms |
6628 KB |
Output is correct |
12 |
Correct |
104 ms |
6636 KB |
Output is correct |
13 |
Correct |
540 ms |
6764 KB |
Output is correct |
14 |
Correct |
652 ms |
7428 KB |
Output is correct |
15 |
Correct |
244 ms |
7820 KB |
Output is correct |
16 |
Correct |
424 ms |
9140 KB |
Output is correct |
17 |
Correct |
672 ms |
6632 KB |
Output is correct |
18 |
Correct |
1368 ms |
6636 KB |
Output is correct |
19 |
Correct |
336 ms |
9144 KB |
Output is correct |
20 |
Correct |
160 ms |
9140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
296 ms |
5588 KB |
Output is correct |
2 |
Correct |
540 ms |
5856 KB |
Output is correct |
3 |
Correct |
648 ms |
5988 KB |
Output is correct |
4 |
Correct |
756 ms |
6248 KB |
Output is correct |
5 |
Correct |
944 ms |
6380 KB |
Output is correct |
6 |
Correct |
1628 ms |
6244 KB |
Output is correct |
7 |
Correct |
2712 ms |
6504 KB |
Output is correct |
8 |
Correct |
3184 ms |
6504 KB |
Output is correct |
9 |
Correct |
2840 ms |
6584 KB |
Output is correct |
10 |
Correct |
2348 ms |
7128 KB |
Output is correct |
11 |
Correct |
3292 ms |
6872 KB |
Output is correct |
12 |
Execution timed out |
4000 ms |
6632 KB |
Program timed out |
13 |
Halted |
0 ms |
0 KB |
- |