//https://oj.uz/problem/view/JOI14_historical
#include<bits/stdc++.h>
const int N = 1e5 + 5;
const int MAGIC = 300;
using namespace std;
vector <int> Query[N/MAGIC + 5];
int n, q, a[N], l[N], r[N], p[N], x[N], cnt[N];
long long ans[N];
bool cmp(int a, int b){return r[a] < r[b];}
signed main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> q;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) x[i] = i;
sort(x, x+n, [](int xx, int yy){return a[xx] < a[yy];});
for (int i = 0; i < n; i++){
if (i == 0) p[x[i]] = 1;
else p[x[i]] = p[x[i-1]] + (a[x[i]] != a[x[i-1]]);
}
for (int i = 1; i <= q; i++){
cin >> l[i] >> r[i]; l[i]--; r[i]--;
Query[l[i]/MAGIC].push_back(i);
}
for (int i = 0; i <= n/MAGIC; i++) sort(Query[i].begin(), Query[i].end(), cmp);
for (int i = 0; i <= n/MAGIC; i++){
int R = (i+1)*MAGIC-1;
long long Max = 0;
memset(cnt, 0, sizeof cnt);
for (int j = 0; j < Query[i].size(); j++){
int id = Query[i][j], x = l[id], y = r[id];
if (y < R){
for (int k = y; k >= x; k--) cnt[p[k]]++, ans[id] = max(ans[id], 1LL * cnt[p[k]] * a[k]);
for (int k = y; k >= x; k--) cnt[p[k]]--;
continue;
}
while (R < y) R++, cnt[p[R]]++, Max = max(Max, 1LL * cnt[p[R]] * a[R]); ans[id] = Max;
for (int k = (i+1)*MAGIC-1; k >= x; k--) cnt[p[k]]++, ans[id] = max(ans[id], 1LL * cnt[p[k]] * a[k]);
for (int k = (i+1)*MAGIC-1; k >= x; k--) cnt[p[k]]--;
}
}
for (int i = 1; i <= q; i++) cout << ans[i] << "\n";
}
Compilation message
historic.cpp: In function 'int main()':
historic.cpp:34:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < Query[i].size(); j++){
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
760 KB |
Output is correct |
2 |
Correct |
2 ms |
760 KB |
Output is correct |
3 |
Correct |
2 ms |
816 KB |
Output is correct |
4 |
Correct |
2 ms |
840 KB |
Output is correct |
5 |
Correct |
2 ms |
892 KB |
Output is correct |
6 |
Correct |
2 ms |
908 KB |
Output is correct |
7 |
Correct |
2 ms |
944 KB |
Output is correct |
8 |
Correct |
2 ms |
944 KB |
Output is correct |
9 |
Correct |
2 ms |
1028 KB |
Output is correct |
10 |
Correct |
2 ms |
1028 KB |
Output is correct |
11 |
Correct |
2 ms |
1028 KB |
Output is correct |
12 |
Correct |
2 ms |
1028 KB |
Output is correct |
13 |
Correct |
2 ms |
1028 KB |
Output is correct |
14 |
Correct |
2 ms |
1028 KB |
Output is correct |
15 |
Correct |
2 ms |
1028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1028 KB |
Output is correct |
2 |
Correct |
2 ms |
1028 KB |
Output is correct |
3 |
Correct |
3 ms |
1028 KB |
Output is correct |
4 |
Correct |
3 ms |
1144 KB |
Output is correct |
5 |
Correct |
5 ms |
1160 KB |
Output is correct |
6 |
Correct |
6 ms |
1304 KB |
Output is correct |
7 |
Correct |
7 ms |
1304 KB |
Output is correct |
8 |
Correct |
7 ms |
1304 KB |
Output is correct |
9 |
Correct |
6 ms |
1304 KB |
Output is correct |
10 |
Correct |
7 ms |
1320 KB |
Output is correct |
11 |
Correct |
7 ms |
1336 KB |
Output is correct |
12 |
Correct |
7 ms |
1336 KB |
Output is correct |
13 |
Correct |
7 ms |
1432 KB |
Output is correct |
14 |
Correct |
7 ms |
1432 KB |
Output is correct |
15 |
Correct |
8 ms |
1432 KB |
Output is correct |
16 |
Correct |
9 ms |
1432 KB |
Output is correct |
17 |
Correct |
7 ms |
1432 KB |
Output is correct |
18 |
Correct |
7 ms |
1432 KB |
Output is correct |
19 |
Correct |
11 ms |
1432 KB |
Output is correct |
20 |
Correct |
7 ms |
1432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1432 KB |
Output is correct |
2 |
Correct |
2 ms |
1432 KB |
Output is correct |
3 |
Correct |
2 ms |
1432 KB |
Output is correct |
4 |
Correct |
2 ms |
1432 KB |
Output is correct |
5 |
Correct |
3 ms |
1432 KB |
Output is correct |
6 |
Correct |
2 ms |
1432 KB |
Output is correct |
7 |
Correct |
4 ms |
1432 KB |
Output is correct |
8 |
Correct |
5 ms |
1432 KB |
Output is correct |
9 |
Correct |
9 ms |
1432 KB |
Output is correct |
10 |
Correct |
12 ms |
1636 KB |
Output is correct |
11 |
Correct |
76 ms |
4536 KB |
Output is correct |
12 |
Correct |
28 ms |
4536 KB |
Output is correct |
13 |
Correct |
45 ms |
4536 KB |
Output is correct |
14 |
Correct |
71 ms |
4536 KB |
Output is correct |
15 |
Correct |
70 ms |
5436 KB |
Output is correct |
16 |
Correct |
101 ms |
5436 KB |
Output is correct |
17 |
Correct |
56 ms |
5436 KB |
Output is correct |
18 |
Correct |
103 ms |
5436 KB |
Output is correct |
19 |
Correct |
92 ms |
5436 KB |
Output is correct |
20 |
Correct |
55 ms |
5436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
5436 KB |
Output is correct |
2 |
Correct |
23 ms |
5436 KB |
Output is correct |
3 |
Correct |
37 ms |
5436 KB |
Output is correct |
4 |
Correct |
55 ms |
5436 KB |
Output is correct |
5 |
Correct |
69 ms |
5436 KB |
Output is correct |
6 |
Correct |
83 ms |
5436 KB |
Output is correct |
7 |
Correct |
93 ms |
5436 KB |
Output is correct |
8 |
Correct |
108 ms |
5436 KB |
Output is correct |
9 |
Correct |
132 ms |
5436 KB |
Output is correct |
10 |
Correct |
194 ms |
5436 KB |
Output is correct |
11 |
Correct |
143 ms |
5436 KB |
Output is correct |
12 |
Correct |
140 ms |
5496 KB |
Output is correct |
13 |
Correct |
148 ms |
5584 KB |
Output is correct |
14 |
Correct |
160 ms |
5584 KB |
Output is correct |
15 |
Correct |
173 ms |
5584 KB |
Output is correct |
16 |
Correct |
176 ms |
5608 KB |
Output is correct |
17 |
Correct |
167 ms |
5608 KB |
Output is correct |
18 |
Correct |
169 ms |
5608 KB |
Output is correct |
19 |
Correct |
172 ms |
5608 KB |
Output is correct |
20 |
Correct |
175 ms |
5608 KB |
Output is correct |
21 |
Correct |
156 ms |
5608 KB |
Output is correct |
22 |
Correct |
160 ms |
5608 KB |
Output is correct |
23 |
Correct |
158 ms |
5608 KB |
Output is correct |
24 |
Correct |
158 ms |
5608 KB |
Output is correct |
25 |
Correct |
196 ms |
5900 KB |
Output is correct |