#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
const int NS = (int)1e5 + 4, SZ = (int)1e7 + 4;
int n, q;
int B = 250;
int a[NS];
int srt[NS];
int cnt[NS];
long long ans[NS];
struct ran{
int l, r, id;
bool operator<(const ran&R)const{
return l / B < R.l / B || (l / B == R.l / B && ((r < R.r) ^ (l / B % 2)));
}
}que[NS];
long long val[SZ], id[SZ];
int hn;
long long imsi;
void push(long long x, int y){
// cout << "PUSH " << x << ' ' << y << endl;
val[++hn] = x;
id[hn] = y;
int now = hn, nxt;
while(now > 1 && val[now] > val[nxt = (now >> 1)]){
imsi = val[now], val[now] = val[nxt], val[nxt] = imsi;
imsi = id[now], id[now] = id[nxt], id[nxt] = imsi;
now = nxt;
}
}
void pop(){
// cout << "POP" << endl;
imsi = val[1], val[1] = val[hn], val[hn] = imsi;
imsi = id[1], id[1] = id[hn], id[hn] = imsi;
--hn;
int now = 1, nxt;
while((nxt = now << 1) <= hn){
if(val[nxt] > val[now] && (nxt + 1 > hn || val[nxt] > val[nxt + 1])){
imsi = val[now], val[now] = val[nxt], val[nxt] = imsi;
imsi = id[now], id[now] = id[nxt], id[nxt] = imsi;
now = nxt;
}
else if(nxt + 1 <= hn && val[nxt + 1] > val[now]){
imsi = val[now], val[now] = val[nxt + 1], val[nxt + 1] = imsi;
imsi = id[now], id[now] = id[nxt + 1], id[nxt + 1] = imsi;
now = nxt + 1;
}
else{
break;
}
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
register int i, l, r;
cin >> n >> q;
for(i = 0; i < n; ++i){
cin >> a[i];
srt[i] = a[i];
}
sort(srt, srt + n);
for(i = 0; i < n; ++i){
a[i] = lower_bound(srt, srt + n, a[i]) - srt;
}
for(i = 0; i < q; ++i){
cin >> que[i].l >> que[i].r;
--que[i].l, --que[i].r;
que[i].id = i;
}
sort(que, que + q);
r = -1, l = 0;
for(i = 0; i < q; ++i){
while(r < que[i].r){
++r;
++cnt[a[r]];
push((long long)srt[a[r]] * cnt[a[r]], a[r]);
}
while(l > que[i].l){
--l;
++cnt[a[l]];
push((long long)srt[a[l]] * cnt[a[l]], a[l]);
}
while(r > que[i].r){
--cnt[a[r]];
push((long long)srt[a[r]] * cnt[a[r]], a[r]);
--r;
}
while(l < que[i].l){
--cnt[a[l]];
push((long long)srt[a[l]] * cnt[a[l]], a[l]);
++l;
}
while(hn > 1 && (long long)srt[id[1]] * cnt[id[1]] != val[1]){
pop();
// for(int i = 1; i <= hn; ++i){
// cout << val[i] << ' ';
// }
// cout << endl;
}
ans[que[i].id] = val[1];
}
for(i = 0; i < q; ++i){
cout << ans[i] << '\n';
}
return 0;
}
Compilation message
historic.cpp: In function 'int main()':
historic.cpp:67:18: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
67 | register int i, l, r;
| ^
historic.cpp:67:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
67 | register int i, l, r;
| ^
historic.cpp:67:24: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
67 | register int i, l, r;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
456 KB |
Output is correct |
17 |
Correct |
3 ms |
596 KB |
Output is correct |
18 |
Correct |
6 ms |
904 KB |
Output is correct |
19 |
Correct |
12 ms |
1236 KB |
Output is correct |
20 |
Correct |
32 ms |
1492 KB |
Output is correct |
21 |
Correct |
49 ms |
1584 KB |
Output is correct |
22 |
Correct |
53 ms |
1884 KB |
Output is correct |
23 |
Correct |
57 ms |
1364 KB |
Output is correct |
24 |
Correct |
57 ms |
1560 KB |
Output is correct |
25 |
Correct |
33 ms |
4524 KB |
Output is correct |
26 |
Correct |
61 ms |
4296 KB |
Output is correct |
27 |
Correct |
39 ms |
3704 KB |
Output is correct |
28 |
Correct |
39 ms |
4332 KB |
Output is correct |
29 |
Correct |
47 ms |
3980 KB |
Output is correct |
30 |
Correct |
41 ms |
3328 KB |
Output is correct |
31 |
Correct |
60 ms |
1496 KB |
Output is correct |
32 |
Correct |
43 ms |
1272 KB |
Output is correct |
33 |
Correct |
46 ms |
3560 KB |
Output is correct |
34 |
Correct |
38 ms |
4956 KB |
Output is correct |
35 |
Correct |
28 ms |
5428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
464 KB |
Output is correct |
7 |
Correct |
3 ms |
724 KB |
Output is correct |
8 |
Correct |
5 ms |
980 KB |
Output is correct |
9 |
Correct |
8 ms |
1748 KB |
Output is correct |
10 |
Correct |
14 ms |
1120 KB |
Output is correct |
11 |
Correct |
98 ms |
5624 KB |
Output is correct |
12 |
Correct |
34 ms |
3824 KB |
Output is correct |
13 |
Correct |
72 ms |
7884 KB |
Output is correct |
14 |
Correct |
77 ms |
7376 KB |
Output is correct |
15 |
Correct |
99 ms |
8260 KB |
Output is correct |
16 |
Correct |
65 ms |
7600 KB |
Output is correct |
17 |
Correct |
46 ms |
5592 KB |
Output is correct |
18 |
Correct |
73 ms |
9136 KB |
Output is correct |
19 |
Correct |
64 ms |
9288 KB |
Output is correct |
20 |
Correct |
27 ms |
4908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
456 KB |
Output is correct |
17 |
Correct |
3 ms |
596 KB |
Output is correct |
18 |
Correct |
6 ms |
904 KB |
Output is correct |
19 |
Correct |
12 ms |
1236 KB |
Output is correct |
20 |
Correct |
32 ms |
1492 KB |
Output is correct |
21 |
Correct |
49 ms |
1584 KB |
Output is correct |
22 |
Correct |
53 ms |
1884 KB |
Output is correct |
23 |
Correct |
57 ms |
1364 KB |
Output is correct |
24 |
Correct |
57 ms |
1560 KB |
Output is correct |
25 |
Correct |
33 ms |
4524 KB |
Output is correct |
26 |
Correct |
61 ms |
4296 KB |
Output is correct |
27 |
Correct |
39 ms |
3704 KB |
Output is correct |
28 |
Correct |
39 ms |
4332 KB |
Output is correct |
29 |
Correct |
47 ms |
3980 KB |
Output is correct |
30 |
Correct |
41 ms |
3328 KB |
Output is correct |
31 |
Correct |
60 ms |
1496 KB |
Output is correct |
32 |
Correct |
43 ms |
1272 KB |
Output is correct |
33 |
Correct |
46 ms |
3560 KB |
Output is correct |
34 |
Correct |
38 ms |
4956 KB |
Output is correct |
35 |
Correct |
28 ms |
5428 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
0 ms |
328 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
2 ms |
468 KB |
Output is correct |
41 |
Correct |
2 ms |
464 KB |
Output is correct |
42 |
Correct |
3 ms |
724 KB |
Output is correct |
43 |
Correct |
5 ms |
980 KB |
Output is correct |
44 |
Correct |
8 ms |
1748 KB |
Output is correct |
45 |
Correct |
14 ms |
1120 KB |
Output is correct |
46 |
Correct |
98 ms |
5624 KB |
Output is correct |
47 |
Correct |
34 ms |
3824 KB |
Output is correct |
48 |
Correct |
72 ms |
7884 KB |
Output is correct |
49 |
Correct |
77 ms |
7376 KB |
Output is correct |
50 |
Correct |
99 ms |
8260 KB |
Output is correct |
51 |
Correct |
65 ms |
7600 KB |
Output is correct |
52 |
Correct |
46 ms |
5592 KB |
Output is correct |
53 |
Correct |
73 ms |
9136 KB |
Output is correct |
54 |
Correct |
64 ms |
9288 KB |
Output is correct |
55 |
Correct |
27 ms |
4908 KB |
Output is correct |
56 |
Correct |
127 ms |
1800 KB |
Output is correct |
57 |
Correct |
319 ms |
6112 KB |
Output is correct |
58 |
Correct |
532 ms |
26732 KB |
Output is correct |
59 |
Correct |
875 ms |
52896 KB |
Output is correct |
60 |
Correct |
938 ms |
75672 KB |
Output is correct |
61 |
Correct |
1571 ms |
48964 KB |
Output is correct |
62 |
Correct |
2016 ms |
14280 KB |
Output is correct |
63 |
Correct |
2224 ms |
7812 KB |
Output is correct |
64 |
Correct |
2378 ms |
7224 KB |
Output is correct |
65 |
Correct |
2221 ms |
6592 KB |
Output is correct |
66 |
Correct |
3054 ms |
7496 KB |
Output is correct |
67 |
Correct |
3691 ms |
14108 KB |
Output is correct |
68 |
Execution timed out |
4045 ms |
79580 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |