Submission #336662

#TimeUsernameProblemLanguageResultExecution timeMemory
336662syyCandies (JOI18_candies)C++17
100 / 100
167 ms14836 KiB
/* Transition from k to k+1 will always be * ...0101000... to ...0101010..., or * ...0101010... to ...1010101... * Thus, store the possible states that it can go in a pq */ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define FOR(i, a, b) for(ll i = (ll)a; i <= (ll)b; i++) #define DEC(i, a, b) for(ll i = (ll)a; i >= (ll)b; i--) typedef pair<ll, ll> pi; typedef pair<pi, ll> pii; typedef pair<ll, pi> ipi; typedef pair<pi, pi> pipi; #define f first #define s second typedef vector<ll> vi; typedef vector<pi> vpi; typedef vector<pii> vpii; #define pb push_back #define pf push_front #define all(v) v.begin(), v.end() #define size(v) (ll) v.size() #define disc(v) sort(all(v)); v.resize(unique(all(v)) - v.begin()); #define INF (ll) 1e9 + 100 #define LLINF (ll) 1e18 #define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define sandybridge __attribute__((optimize("Ofast"), target("arch=sandybridge"))) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng) inline ll rand(ll x, ll y) { ++y; return (rng() % (y-x)) + x; } //inclusivesss ll n, arr[200005], ss[200005], p[200005], L[200005], R[200005], ans; bool vis[200005]; priority_queue<ipi> pq; inline ll getans(ll x, ll y) { return (y % 2 ? ss[y] - ss[x-1] : ss[x-1] - ss[y]); } ll fs(ll x) { if (p[x] == x) return x; return p[x] = fs(p[x]); } int main() { fastio; cin >> n; FOR(i, 1, n) { cin >> arr[i]; ss[i] = ss[i-1] + (i % 2 ? arr[i] : -arr[i]); p[i] = L[i] = R[i] = i; pq.push(ipi(arr[i], pi(i, i))); } FOR(i, 1, (n+1)/2) { ll l, r; while (true) { l = pq.top().s.f; r = pq.top().s.s; pq.pop(); if (!vis[l-1] and !vis[r+1]) break; } ans += getans(l, r); cout << ans << "\n"; vis[l] = 1; vis[r] = 1; // because at every step you expand at most 1 // you only need to set vis of l and r as // all the intermediate nodes must have been set before ll par; if (l == r) par = fs(l); else { par = fs(l+1); p[fs(l)] = par; p[fs(r)] = par; L[par] = l, R[par] = r; } if (vis[r+2]) { R[par] = R[fs(r+2)]; p[fs(r+2)] = par; } if (l > 1 and vis[l-2]) { L[par] = L[fs(l-2)]; p[fs(l-2)] = par; } if (L[par] > 1 and R[par] < n) pq.push(ipi(getans(L[par]-1, R[par]+1), pi(L[par]-1, R[par]+1))); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...