#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime>
#include <bitset>
#include <complex>
#include <chrono>
#include <random>
#include <functional>
using namespace std;
#define int long long
const int N = 2e5 + 7;
const int INF = 1e18 + 129;
int n;
int a[N];
int s1[N];
int s2[N];
int summ(int l, int r) {
int tans = 0;
if (l % 2 == 1) {
tans = s2[r] - s2[l - 1];
} else {
tans = s1[r] - s1[l - 1];
}
return tans;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
{
for (int i = 1; i <= n; i++) {
int v = a[i];
if (i & 1) v *= -1;
s1[i] = s1[i - 1] + v;
}
for (int i = 1; i <= n; i++) {
int v = -a[i];
if (i & 1) v *= -1;
s2[i] = s2[i - 1] + v;
}
}
set<pair<int, int>> seg;
set<pair<int, pair<int, int>>> sss;
auto real_erase = [&](pair<int, int> x) {
seg.erase(x);
sss.erase({summ(x.first, x.second), x});
};
auto real_insert = [&](pair<int, int> x) {
seg.insert(x);
sss.insert({summ(x.first, x.second), x});
};
for (int i = 1; i <= n; i++) {
real_insert({i, i});
}
auto ers = [&](int l, int r) {
auto it = seg.lower_bound({l, r});
auto prv = it;
if (it != seg.begin()) prv--;
auto nxt = it;
if (it != seg.end()) nxt++;
if (seg.size() == 1) {
seg.erase({l, r});
return;
}
if (it == seg.begin()) {
pair<int, int> we = *it;
pair<int, int> next = *(nxt);
real_erase(we);
real_erase(next);
return;
}
if (it == --seg.end()) {
pair<int, int> we = *it;
pair<int, int> prev = *(prv);
real_erase(we);
real_erase(prev);
return;
}
pair<int, int> we = *it;
pair<int, int> next = *(nxt);
pair<int, int> prev = *(prv);
real_erase(we);
real_erase(next);
real_erase(prev);
real_insert({prev.first, next.second});
};
int curr = 0;
vector<int> ans;
while (!seg.empty()) {
int mx = -INF;
int lm = -1;
int rm = -1;
auto uu = *sss.rbegin();
mx = uu.first;
tie(lm, rm) = uu.second;
ers(lm, rm);
curr += mx;
ans.push_back(curr);
}
for (auto t : ans) {
cout << t << ' ';
}
cout << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
768 KB |
Output is correct |
2 |
Correct |
7 ms |
640 KB |
Output is correct |
3 |
Correct |
7 ms |
640 KB |
Output is correct |
4 |
Correct |
7 ms |
640 KB |
Output is correct |
5 |
Correct |
7 ms |
640 KB |
Output is correct |
6 |
Correct |
7 ms |
672 KB |
Output is correct |
7 |
Correct |
7 ms |
640 KB |
Output is correct |
8 |
Correct |
7 ms |
640 KB |
Output is correct |
9 |
Correct |
6 ms |
640 KB |
Output is correct |
10 |
Correct |
6 ms |
640 KB |
Output is correct |
11 |
Correct |
7 ms |
640 KB |
Output is correct |
12 |
Correct |
7 ms |
640 KB |
Output is correct |
13 |
Correct |
7 ms |
640 KB |
Output is correct |
14 |
Correct |
7 ms |
640 KB |
Output is correct |
15 |
Correct |
7 ms |
640 KB |
Output is correct |
16 |
Correct |
7 ms |
640 KB |
Output is correct |
17 |
Correct |
7 ms |
640 KB |
Output is correct |
18 |
Correct |
7 ms |
640 KB |
Output is correct |
19 |
Correct |
7 ms |
640 KB |
Output is correct |
20 |
Correct |
7 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
768 KB |
Output is correct |
2 |
Correct |
7 ms |
640 KB |
Output is correct |
3 |
Correct |
7 ms |
640 KB |
Output is correct |
4 |
Correct |
7 ms |
640 KB |
Output is correct |
5 |
Correct |
7 ms |
640 KB |
Output is correct |
6 |
Correct |
7 ms |
672 KB |
Output is correct |
7 |
Correct |
7 ms |
640 KB |
Output is correct |
8 |
Correct |
7 ms |
640 KB |
Output is correct |
9 |
Correct |
6 ms |
640 KB |
Output is correct |
10 |
Correct |
6 ms |
640 KB |
Output is correct |
11 |
Correct |
7 ms |
640 KB |
Output is correct |
12 |
Correct |
7 ms |
640 KB |
Output is correct |
13 |
Correct |
7 ms |
640 KB |
Output is correct |
14 |
Correct |
7 ms |
640 KB |
Output is correct |
15 |
Correct |
7 ms |
640 KB |
Output is correct |
16 |
Correct |
7 ms |
640 KB |
Output is correct |
17 |
Correct |
7 ms |
640 KB |
Output is correct |
18 |
Correct |
7 ms |
640 KB |
Output is correct |
19 |
Correct |
7 ms |
640 KB |
Output is correct |
20 |
Correct |
7 ms |
768 KB |
Output is correct |
21 |
Correct |
659 ms |
34276 KB |
Output is correct |
22 |
Correct |
682 ms |
34524 KB |
Output is correct |
23 |
Correct |
682 ms |
34616 KB |
Output is correct |
24 |
Correct |
307 ms |
34252 KB |
Output is correct |
25 |
Correct |
302 ms |
34284 KB |
Output is correct |
26 |
Correct |
299 ms |
34284 KB |
Output is correct |
27 |
Correct |
327 ms |
34412 KB |
Output is correct |
28 |
Correct |
327 ms |
34408 KB |
Output is correct |
29 |
Correct |
328 ms |
34540 KB |
Output is correct |
30 |
Correct |
350 ms |
34412 KB |
Output is correct |
31 |
Correct |
339 ms |
34540 KB |
Output is correct |
32 |
Correct |
349 ms |
34408 KB |
Output is correct |
33 |
Correct |
491 ms |
34280 KB |
Output is correct |
34 |
Correct |
477 ms |
34452 KB |
Output is correct |
35 |
Correct |
479 ms |
34280 KB |
Output is correct |
36 |
Correct |
670 ms |
34588 KB |
Output is correct |
37 |
Correct |
707 ms |
34592 KB |
Output is correct |
38 |
Correct |
691 ms |
34588 KB |
Output is correct |
39 |
Correct |
302 ms |
34280 KB |
Output is correct |
40 |
Correct |
304 ms |
34284 KB |
Output is correct |
41 |
Correct |
310 ms |
34280 KB |
Output is correct |
42 |
Correct |
326 ms |
34412 KB |
Output is correct |
43 |
Correct |
320 ms |
34408 KB |
Output is correct |
44 |
Correct |
330 ms |
34416 KB |
Output is correct |
45 |
Correct |
338 ms |
34412 KB |
Output is correct |
46 |
Correct |
343 ms |
34408 KB |
Output is correct |
47 |
Correct |
341 ms |
34408 KB |
Output is correct |
48 |
Correct |
476 ms |
34284 KB |
Output is correct |
49 |
Correct |
485 ms |
34408 KB |
Output is correct |
50 |
Correct |
469 ms |
34292 KB |
Output is correct |