#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int N, tot, A[200005], P[200005][2];
set<iii> s1, r1;
struct node {
node *left, *right;
int S, E;
bool ip;
ii val;
node(int _s, int _e) : S(_s), E(_e), ip(0) {
if (S == E) {
val = mp(A[S], S);
return;
}
int M = (S + E) >> 1;
left = new node(S, M);
right = new node(M + 1, E);
val = max(left->val, right->val);
}
void prop() {
if (S == E || !ip) return;
left->val = right->val = mp(LLONG_MIN, LLONG_MIN);
left->ip = right->ip = 1;
}
void set(int l, int r) {
if (l > E || r < S) return;
if (l <= S && E <= r) {
val = mp(LLONG_MIN, LLONG_MIN);
ip = 1;
return;
}
prop();
left->set(l, r);
right->set(l, r);
val = max(left->val, right->val);
}
} *root;
void add(int l, int r) {
vector<set<iii>::iterator> td;
int newl = l, newr = r;
auto it = r1.upper_bound(mt(r, LLONG_MAX, LLONG_MAX));
if (it != r1.end() && g0(*it) == newr + 2) {
newr = g1(*it);
td.pb(it);
if (s1.find(mt(g2(*it), g0(*it), g1(*it))) != s1.end()) s1.erase(mt(g2(*it), g0(*it), g1(*it)));
}
if (it != r1.begin()) {
--it;
if (g1(*it) == newl - 2) {
newl = g0(*it);
td.pb(it);
if (s1.find(mt(g2(*it), g0(*it), g1(*it))) != s1.end()) s1.erase(mt(g2(*it), g0(*it), g1(*it)));
}
++it;
}
for (auto i : td) r1.erase(i);
assert((newl & 1) == (newr & 1));
if (newl == 1 || newr == N) {
r1.emplace(newl, newr, -1ll);
} else {
int nv = (P[newr + 1][(newr + 1) & 1] - P[newl - 2][(newr + 1) & 1]) - (P[newr + 1][newr & 1] - P[newl - 2][newr & 1]);
s1.emplace(nv, newl, newr);
r1.emplace(newl, newr, nv);
}
root->set(max(1ll, newl - 1), min(N, newr + 1));
}
void tks1() {
assert(!s1.empty());
auto [v, l, r] = *s1.rbegin();
assert(l != 1 && r != N);
s1.erase(--s1.end());
r1.erase(r1.find(mt(l, r, v)));
tot += v;
add(l - 1, r + 1);
}
void tks2() {
auto v = root->val;
tot += v.first;
add(v.second, v.second);
}
main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> A[i];
P[i][0] = (i & 1 ? 0 : A[i]) + P[i - 1][0];
P[i][1] = (i & 1 ? A[i] : 0) + P[i - 1][1];
}
root = new node(1, N);
for (int i = 1; i <= (N + 1) / 2; i++) {
if (s1.empty()) {
tks2();
} else {
if (g0(*s1.rbegin()) > root->val.first) {
tks1();
} else {
tks2();
}
}
cout << tot << '\n';
}
}
Compilation message
candies.cpp:111:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
111 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
588 KB |
Output is correct |
2 |
Correct |
2 ms |
716 KB |
Output is correct |
3 |
Correct |
2 ms |
588 KB |
Output is correct |
4 |
Correct |
2 ms |
588 KB |
Output is correct |
5 |
Correct |
2 ms |
716 KB |
Output is correct |
6 |
Correct |
2 ms |
716 KB |
Output is correct |
7 |
Correct |
2 ms |
588 KB |
Output is correct |
8 |
Correct |
2 ms |
716 KB |
Output is correct |
9 |
Correct |
2 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
588 KB |
Output is correct |
11 |
Correct |
2 ms |
716 KB |
Output is correct |
12 |
Correct |
3 ms |
588 KB |
Output is correct |
13 |
Correct |
2 ms |
716 KB |
Output is correct |
14 |
Correct |
2 ms |
580 KB |
Output is correct |
15 |
Correct |
2 ms |
716 KB |
Output is correct |
16 |
Correct |
2 ms |
592 KB |
Output is correct |
17 |
Correct |
2 ms |
716 KB |
Output is correct |
18 |
Correct |
2 ms |
588 KB |
Output is correct |
19 |
Correct |
2 ms |
588 KB |
Output is correct |
20 |
Correct |
2 ms |
588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
588 KB |
Output is correct |
2 |
Correct |
2 ms |
716 KB |
Output is correct |
3 |
Correct |
2 ms |
588 KB |
Output is correct |
4 |
Correct |
2 ms |
588 KB |
Output is correct |
5 |
Correct |
2 ms |
716 KB |
Output is correct |
6 |
Correct |
2 ms |
716 KB |
Output is correct |
7 |
Correct |
2 ms |
588 KB |
Output is correct |
8 |
Correct |
2 ms |
716 KB |
Output is correct |
9 |
Correct |
2 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
588 KB |
Output is correct |
11 |
Correct |
2 ms |
716 KB |
Output is correct |
12 |
Correct |
3 ms |
588 KB |
Output is correct |
13 |
Correct |
2 ms |
716 KB |
Output is correct |
14 |
Correct |
2 ms |
580 KB |
Output is correct |
15 |
Correct |
2 ms |
716 KB |
Output is correct |
16 |
Correct |
2 ms |
592 KB |
Output is correct |
17 |
Correct |
2 ms |
716 KB |
Output is correct |
18 |
Correct |
2 ms |
588 KB |
Output is correct |
19 |
Correct |
2 ms |
588 KB |
Output is correct |
20 |
Correct |
2 ms |
588 KB |
Output is correct |
21 |
Correct |
347 ms |
37864 KB |
Output is correct |
22 |
Correct |
376 ms |
37804 KB |
Output is correct |
23 |
Correct |
347 ms |
37956 KB |
Output is correct |
24 |
Correct |
178 ms |
35180 KB |
Output is correct |
25 |
Correct |
184 ms |
35216 KB |
Output is correct |
26 |
Correct |
176 ms |
35176 KB |
Output is correct |
27 |
Correct |
230 ms |
37932 KB |
Output is correct |
28 |
Correct |
222 ms |
37872 KB |
Output is correct |
29 |
Correct |
228 ms |
37952 KB |
Output is correct |
30 |
Correct |
240 ms |
37920 KB |
Output is correct |
31 |
Correct |
225 ms |
37952 KB |
Output is correct |
32 |
Correct |
225 ms |
37828 KB |
Output is correct |
33 |
Correct |
280 ms |
37684 KB |
Output is correct |
34 |
Correct |
288 ms |
37828 KB |
Output is correct |
35 |
Correct |
294 ms |
37848 KB |
Output is correct |
36 |
Correct |
403 ms |
37884 KB |
Output is correct |
37 |
Correct |
374 ms |
37964 KB |
Output is correct |
38 |
Correct |
351 ms |
37896 KB |
Output is correct |
39 |
Correct |
175 ms |
35200 KB |
Output is correct |
40 |
Correct |
178 ms |
35232 KB |
Output is correct |
41 |
Correct |
175 ms |
35268 KB |
Output is correct |
42 |
Correct |
223 ms |
37908 KB |
Output is correct |
43 |
Correct |
229 ms |
37944 KB |
Output is correct |
44 |
Correct |
225 ms |
37856 KB |
Output is correct |
45 |
Correct |
222 ms |
37876 KB |
Output is correct |
46 |
Correct |
223 ms |
37844 KB |
Output is correct |
47 |
Correct |
227 ms |
38072 KB |
Output is correct |
48 |
Correct |
289 ms |
37828 KB |
Output is correct |
49 |
Correct |
278 ms |
37808 KB |
Output is correct |
50 |
Correct |
279 ms |
37700 KB |
Output is correct |