#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int B = 200;
int n,q;
int a[100005];
long long buck[501][501];
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();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
6592 KB |
Output is correct |
2 |
Correct |
0 ms |
6596 KB |
Output is correct |
3 |
Correct |
0 ms |
6596 KB |
Output is correct |
4 |
Correct |
0 ms |
6592 KB |
Output is correct |
5 |
Correct |
0 ms |
6596 KB |
Output is correct |
6 |
Correct |
0 ms |
6592 KB |
Output is correct |
7 |
Correct |
0 ms |
6592 KB |
Output is correct |
8 |
Correct |
0 ms |
6592 KB |
Output is correct |
9 |
Correct |
0 ms |
6592 KB |
Output is correct |
10 |
Correct |
0 ms |
6592 KB |
Output is correct |
11 |
Correct |
0 ms |
6592 KB |
Output is correct |
12 |
Correct |
0 ms |
6592 KB |
Output is correct |
13 |
Correct |
0 ms |
6596 KB |
Output is correct |
14 |
Correct |
0 ms |
6592 KB |
Output is correct |
15 |
Correct |
0 ms |
6596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
6592 KB |
Output is correct |
2 |
Correct |
0 ms |
6596 KB |
Output is correct |
3 |
Correct |
8 ms |
6596 KB |
Output is correct |
4 |
Correct |
20 ms |
6596 KB |
Output is correct |
5 |
Correct |
48 ms |
6596 KB |
Output is correct |
6 |
Correct |
84 ms |
6596 KB |
Output is correct |
7 |
Correct |
80 ms |
6596 KB |
Output is correct |
8 |
Correct |
92 ms |
6596 KB |
Output is correct |
9 |
Correct |
92 ms |
6592 KB |
Output is correct |
10 |
Correct |
40 ms |
6596 KB |
Output is correct |
11 |
Correct |
48 ms |
6596 KB |
Output is correct |
12 |
Correct |
56 ms |
6592 KB |
Output is correct |
13 |
Correct |
52 ms |
6596 KB |
Output is correct |
14 |
Correct |
52 ms |
6592 KB |
Output is correct |
15 |
Correct |
56 ms |
6596 KB |
Output is correct |
16 |
Correct |
96 ms |
6592 KB |
Output is correct |
17 |
Correct |
64 ms |
6592 KB |
Output is correct |
18 |
Correct |
56 ms |
6592 KB |
Output is correct |
19 |
Correct |
44 ms |
6592 KB |
Output is correct |
20 |
Correct |
44 ms |
6596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
6592 KB |
Output is correct |
2 |
Correct |
0 ms |
6596 KB |
Output is correct |
3 |
Correct |
0 ms |
6596 KB |
Output is correct |
4 |
Correct |
0 ms |
6596 KB |
Output is correct |
5 |
Correct |
4 ms |
6592 KB |
Output is correct |
6 |
Correct |
0 ms |
6592 KB |
Output is correct |
7 |
Correct |
8 ms |
6596 KB |
Output is correct |
8 |
Correct |
8 ms |
6724 KB |
Output is correct |
9 |
Correct |
28 ms |
6988 KB |
Output is correct |
10 |
Correct |
52 ms |
7544 KB |
Output is correct |
11 |
Correct |
1108 ms |
7632 KB |
Output is correct |
12 |
Correct |
140 ms |
7636 KB |
Output is correct |
13 |
Correct |
484 ms |
7772 KB |
Output is correct |
14 |
Correct |
628 ms |
8432 KB |
Output is correct |
15 |
Correct |
312 ms |
8824 KB |
Output is correct |
16 |
Correct |
420 ms |
10152 KB |
Output is correct |
17 |
Correct |
492 ms |
7636 KB |
Output is correct |
18 |
Correct |
944 ms |
7640 KB |
Output is correct |
19 |
Correct |
320 ms |
10148 KB |
Output is correct |
20 |
Correct |
184 ms |
10148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
208 ms |
6592 KB |
Output is correct |
2 |
Correct |
376 ms |
6856 KB |
Output is correct |
3 |
Correct |
456 ms |
6988 KB |
Output is correct |
4 |
Correct |
536 ms |
7252 KB |
Output is correct |
5 |
Correct |
684 ms |
7384 KB |
Output is correct |
6 |
Correct |
1148 ms |
7252 KB |
Output is correct |
7 |
Correct |
1920 ms |
7508 KB |
Output is correct |
8 |
Correct |
2296 ms |
7512 KB |
Output is correct |
9 |
Correct |
2144 ms |
7588 KB |
Output is correct |
10 |
Correct |
1696 ms |
8128 KB |
Output is correct |
11 |
Correct |
2464 ms |
7872 KB |
Output is correct |
12 |
Correct |
3056 ms |
7640 KB |
Output is correct |
13 |
Correct |
2144 ms |
7768 KB |
Output is correct |
14 |
Correct |
1524 ms |
8168 KB |
Output is correct |
15 |
Correct |
1312 ms |
8828 KB |
Output is correct |
16 |
Correct |
1424 ms |
8428 KB |
Output is correct |
17 |
Correct |
1488 ms |
8432 KB |
Output is correct |
18 |
Correct |
1512 ms |
8300 KB |
Output is correct |
19 |
Correct |
1552 ms |
8168 KB |
Output is correct |
20 |
Correct |
1604 ms |
8164 KB |
Output is correct |
21 |
Correct |
1604 ms |
8172 KB |
Output is correct |
22 |
Correct |
1644 ms |
8036 KB |
Output is correct |
23 |
Correct |
1648 ms |
8032 KB |
Output is correct |
24 |
Correct |
1688 ms |
8032 KB |
Output is correct |
25 |
Correct |
1688 ms |
8132 KB |
Output is correct |