#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <deque>
#include <utility>
#include <bitset>
#include <limits.h>
#include <time.h>
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
typedef double lf;
typedef unsigned int uint;
typedef long double llf;
typedef pair<int, int> pii;
const int N_ = 150005;
const int B = 200;
int N, Q, X[N_], S[N_], SN, P[N_];
vector<int> W[N_];
ll precalc[N_/B][N_/B], C[N_];
int main() {
scanf("%d%d", &N, &Q);
for(int i = 0; i < N; i++) {
scanf("%d", X+i);
S[i] = X[i];
}
sort(S, S+N);
SN = unique(S, S+N) - S;
for(int i = 0; i < N; i++) {
P[i] = lower_bound(S, S+SN, X[i]) - S;
W[P[i]].push_back(i);
}
for(int i = 0; i < N; i += B) {
for(int j = 0; j < N; j++) C[j] = 0;
ll val = 0;
for(int j = i; j < N; j++) {
C[P[j]] += (ll)X[j];
if(val < C[P[j]]) val = C[P[j]];
if(j % B == 0) precalc[i/B][j/B] = val;
}
}
while(Q--) {
int a, b; scanf("%d%d", &a, &b); --a; --b;
int x = a, y = b;
ll ans = 0;
for(; x % B != 0; ++x) {
ll val = (ll)X[x] * (lower_bound(W[P[x]].begin(), W[P[x]].end(), b+1) - lower_bound(W[P[x]].begin(), W[P[x]].end(), a));
if(val > ans) ans = val;
}
for(; y % B != 0; --y) {
ll val = (ll)X[y] * (lower_bound(W[P[y]].begin(), W[P[y]].end(), b+1) - lower_bound(W[P[y]].begin(), W[P[y]].end(), a));
if(val > ans) ans = val;
}
ans = max(ans, precalc[x/B][y/B]);
printf("%lld\n", ans);
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
12048 KB |
Output is correct |
2 |
Correct |
0 ms |
12048 KB |
Output is correct |
3 |
Correct |
0 ms |
12048 KB |
Output is correct |
4 |
Correct |
0 ms |
12048 KB |
Output is correct |
5 |
Correct |
0 ms |
12048 KB |
Output is correct |
6 |
Correct |
0 ms |
12048 KB |
Output is correct |
7 |
Correct |
0 ms |
12048 KB |
Output is correct |
8 |
Correct |
0 ms |
12048 KB |
Output is correct |
9 |
Correct |
0 ms |
12048 KB |
Output is correct |
10 |
Correct |
0 ms |
12048 KB |
Output is correct |
11 |
Correct |
0 ms |
12048 KB |
Output is correct |
12 |
Correct |
0 ms |
12048 KB |
Output is correct |
13 |
Correct |
0 ms |
12048 KB |
Output is correct |
14 |
Correct |
0 ms |
12048 KB |
Output is correct |
15 |
Correct |
0 ms |
12048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
12048 KB |
Output is correct |
2 |
Correct |
0 ms |
12048 KB |
Output is correct |
3 |
Correct |
8 ms |
12048 KB |
Output is correct |
4 |
Correct |
20 ms |
12048 KB |
Output is correct |
5 |
Correct |
48 ms |
12048 KB |
Output is correct |
6 |
Correct |
96 ms |
12048 KB |
Output is correct |
7 |
Correct |
80 ms |
12048 KB |
Output is correct |
8 |
Correct |
100 ms |
12048 KB |
Output is correct |
9 |
Correct |
100 ms |
12048 KB |
Output is correct |
10 |
Correct |
52 ms |
12048 KB |
Output is correct |
11 |
Correct |
48 ms |
12048 KB |
Output is correct |
12 |
Correct |
52 ms |
12048 KB |
Output is correct |
13 |
Correct |
56 ms |
12048 KB |
Output is correct |
14 |
Correct |
64 ms |
12048 KB |
Output is correct |
15 |
Correct |
56 ms |
12048 KB |
Output is correct |
16 |
Correct |
104 ms |
12048 KB |
Output is correct |
17 |
Correct |
80 ms |
12048 KB |
Output is correct |
18 |
Correct |
56 ms |
12048 KB |
Output is correct |
19 |
Correct |
48 ms |
12048 KB |
Output is correct |
20 |
Correct |
44 ms |
12048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
12048 KB |
Output is correct |
2 |
Correct |
0 ms |
12048 KB |
Output is correct |
3 |
Correct |
0 ms |
12048 KB |
Output is correct |
4 |
Correct |
0 ms |
12048 KB |
Output is correct |
5 |
Correct |
8 ms |
12048 KB |
Output is correct |
6 |
Correct |
4 ms |
12048 KB |
Output is correct |
7 |
Correct |
4 ms |
12048 KB |
Output is correct |
8 |
Correct |
8 ms |
12180 KB |
Output is correct |
9 |
Correct |
16 ms |
12312 KB |
Output is correct |
10 |
Correct |
44 ms |
12608 KB |
Output is correct |
11 |
Correct |
2092 ms |
12572 KB |
Output is correct |
12 |
Correct |
140 ms |
12576 KB |
Output is correct |
13 |
Correct |
536 ms |
12708 KB |
Output is correct |
14 |
Correct |
788 ms |
13368 KB |
Output is correct |
15 |
Correct |
1216 ms |
13764 KB |
Output is correct |
16 |
Correct |
400 ms |
15084 KB |
Output is correct |
17 |
Correct |
508 ms |
12576 KB |
Output is correct |
18 |
Correct |
1008 ms |
12580 KB |
Output is correct |
19 |
Correct |
340 ms |
15084 KB |
Output is correct |
20 |
Correct |
196 ms |
15084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
220 ms |
12048 KB |
Output is correct |
2 |
Correct |
392 ms |
12180 KB |
Output is correct |
3 |
Correct |
464 ms |
12312 KB |
Output is correct |
4 |
Correct |
568 ms |
12444 KB |
Output is correct |
5 |
Correct |
704 ms |
12576 KB |
Output is correct |
6 |
Correct |
1208 ms |
12444 KB |
Output is correct |
7 |
Correct |
1992 ms |
12444 KB |
Output is correct |
8 |
Correct |
2404 ms |
12448 KB |
Output is correct |
9 |
Correct |
2280 ms |
12524 KB |
Output is correct |
10 |
Correct |
1788 ms |
12820 KB |
Output is correct |
11 |
Correct |
2648 ms |
12688 KB |
Output is correct |
12 |
Correct |
3236 ms |
12576 KB |
Output is correct |
13 |
Correct |
2216 ms |
12708 KB |
Output is correct |
14 |
Correct |
1552 ms |
13104 KB |
Output is correct |
15 |
Correct |
1304 ms |
13764 KB |
Output is correct |
16 |
Correct |
1460 ms |
13368 KB |
Output is correct |
17 |
Correct |
1540 ms |
13368 KB |
Output is correct |
18 |
Correct |
1596 ms |
13236 KB |
Output is correct |
19 |
Correct |
1588 ms |
13104 KB |
Output is correct |
20 |
Correct |
1628 ms |
13104 KB |
Output is correct |
21 |
Correct |
1688 ms |
13104 KB |
Output is correct |
22 |
Correct |
1700 ms |
12972 KB |
Output is correct |
23 |
Correct |
1720 ms |
12972 KB |
Output is correct |
24 |
Correct |
1764 ms |
12972 KB |
Output is correct |
25 |
Correct |
1764 ms |
12820 KB |
Output is correct |