#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 10;
const ll INF = 1e16;
int n;
ll a[N];
ll pr[N];
ll seg_sum(int l, int r) {
if(l <= 0 || r > n) return INF;
return pr[r - 1] - pr[l - 1];
}
ll inv_sum(int l, int r) {
if(l <= 0 || r > n) return -INF;
return pr[r] - pr[l] + a[l];
}
ll func(int l, int r) {
return inv_sum(l, r) - seg_sum(l, r);
}
set<pair<ll, int>> Afree;
set<pair<int, int>> segments;
set<pair<ll, pair<int, int>>> inv_choices;
ll cur_ans = 0;
void inv_seg(int l, int r) {
segments.erase({l, r});
inv_choices.erase({func(l, r), {l, r}});
cur_ans += func(l, r);
l--, r++;
if(!segments.empty()) {
auto rseg = segments.upper_bound({l, r}), lseg = rseg; lseg--;
if(rseg != segments.begin()) {
if(lseg->second == l) {
l = lseg->first;
inv_choices.erase({func(lseg->first, lseg->second), {lseg->first, lseg->second}});
segments.erase(lseg);
}
}
if(rseg != segments.end()) {
if(rseg->first == r) {
r = rseg->second;
inv_choices.erase({func(rseg->first, rseg->second), {rseg->first, rseg->second}});
segments.erase(rseg);
}
}
}
segments.insert({l, r});
inv_choices.insert({func(l, r), {l, r}});
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
for(int i = 1; i <= n + 1; i++)
pr[i] = a[i] + (i - 2 >= 0 ? pr[i - 2] : 0);
for(int i = 1; i <= n; i++)
segments.insert({i, i}),
inv_choices.insert({func(i, i), {i, i}});
for(int _ = 1; _ <= (n + 1) / 2; _++) {
auto [l, r] = (--inv_choices.end())->second;
// cout << l << ' ' << r << '\n';
inv_seg(l, r);
cout << cur_ans << '\n';
// for(auto [l, r] : segments) {
// cout << '{' << l << ' ' << r << '}' << ' ';
// }
// cout << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
484 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
592 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
3 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
484 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
592 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
3 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
433 ms |
28720 KB |
Output is correct |
22 |
Correct |
468 ms |
28720 KB |
Output is correct |
23 |
Correct |
513 ms |
28752 KB |
Output is correct |
24 |
Correct |
173 ms |
28616 KB |
Output is correct |
25 |
Correct |
197 ms |
28564 KB |
Output is correct |
26 |
Correct |
202 ms |
28528 KB |
Output is correct |
27 |
Correct |
184 ms |
28752 KB |
Output is correct |
28 |
Correct |
210 ms |
28780 KB |
Output is correct |
29 |
Correct |
185 ms |
28676 KB |
Output is correct |
30 |
Correct |
192 ms |
28736 KB |
Output is correct |
31 |
Correct |
196 ms |
28764 KB |
Output is correct |
32 |
Correct |
202 ms |
28800 KB |
Output is correct |
33 |
Correct |
319 ms |
28492 KB |
Output is correct |
34 |
Correct |
270 ms |
28600 KB |
Output is correct |
35 |
Correct |
265 ms |
28512 KB |
Output is correct |
36 |
Correct |
365 ms |
28724 KB |
Output is correct |
37 |
Correct |
433 ms |
28776 KB |
Output is correct |
38 |
Correct |
419 ms |
28748 KB |
Output is correct |
39 |
Correct |
172 ms |
28604 KB |
Output is correct |
40 |
Correct |
180 ms |
28516 KB |
Output is correct |
41 |
Correct |
191 ms |
28560 KB |
Output is correct |
42 |
Correct |
183 ms |
28788 KB |
Output is correct |
43 |
Correct |
180 ms |
28768 KB |
Output is correct |
44 |
Correct |
189 ms |
28768 KB |
Output is correct |
45 |
Correct |
215 ms |
28780 KB |
Output is correct |
46 |
Correct |
198 ms |
28804 KB |
Output is correct |
47 |
Correct |
230 ms |
28748 KB |
Output is correct |
48 |
Correct |
255 ms |
28480 KB |
Output is correct |
49 |
Correct |
295 ms |
28612 KB |
Output is correct |
50 |
Correct |
323 ms |
28456 KB |
Output is correct |