#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 |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
3 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
3 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 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 |
616 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
600 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
3 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
3 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 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 |
616 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
600 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
600 KB |
Output is correct |
21 |
Correct |
396 ms |
28736 KB |
Output is correct |
22 |
Correct |
462 ms |
28732 KB |
Output is correct |
23 |
Correct |
448 ms |
28712 KB |
Output is correct |
24 |
Correct |
185 ms |
28564 KB |
Output is correct |
25 |
Correct |
185 ms |
28512 KB |
Output is correct |
26 |
Correct |
166 ms |
28604 KB |
Output is correct |
27 |
Correct |
189 ms |
28736 KB |
Output is correct |
28 |
Correct |
190 ms |
28748 KB |
Output is correct |
29 |
Correct |
183 ms |
28756 KB |
Output is correct |
30 |
Correct |
213 ms |
28680 KB |
Output is correct |
31 |
Correct |
212 ms |
28752 KB |
Output is correct |
32 |
Correct |
199 ms |
28728 KB |
Output is correct |
33 |
Correct |
279 ms |
28540 KB |
Output is correct |
34 |
Correct |
277 ms |
28456 KB |
Output is correct |
35 |
Correct |
287 ms |
28556 KB |
Output is correct |
36 |
Correct |
432 ms |
28768 KB |
Output is correct |
37 |
Correct |
382 ms |
28732 KB |
Output is correct |
38 |
Correct |
324 ms |
28704 KB |
Output is correct |
39 |
Correct |
188 ms |
28620 KB |
Output is correct |
40 |
Correct |
172 ms |
28544 KB |
Output is correct |
41 |
Correct |
178 ms |
28592 KB |
Output is correct |
42 |
Correct |
187 ms |
28704 KB |
Output is correct |
43 |
Correct |
194 ms |
28740 KB |
Output is correct |
44 |
Correct |
207 ms |
28760 KB |
Output is correct |
45 |
Correct |
232 ms |
28760 KB |
Output is correct |
46 |
Correct |
197 ms |
28792 KB |
Output is correct |
47 |
Correct |
196 ms |
28748 KB |
Output is correct |
48 |
Correct |
287 ms |
28444 KB |
Output is correct |
49 |
Correct |
284 ms |
28544 KB |
Output is correct |
50 |
Correct |
292 ms |
28596 KB |
Output is correct |