Submission #222708

#TimeUsernameProblemLanguageResultExecution timeMemory
222708opukittpceno_hhrCandies (JOI18_candies)C++17
100 / 100
707 ms34616 KiB
#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; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...