Submission #1110381

#TimeUsernameProblemLanguageResultExecution timeMemory
1110381NonozeJobs (BOI24_jobs)C++17
100 / 100
1251 ms112440 KiB
/* * Author: Nonoze * Created: Thursday 31/10/2024 */ #include <bits/stdc++.h> using namespace std; #ifndef _IN_LOCAL #define dbg(...) #endif #define endl '\n' #define endlfl '\n' << flush #define quit(x) return (void)(cout << x << endl) template<typename T> void read(T& x) { cin >> x;} template<typename T1, typename T2> void read(pair<T1, T2>& p) { read(p.first), read(p.second);} template<typename T> void read(vector<T>& v) { for (auto& x : v) read(x); } template<typename T1, typename T2> void read(T1& x, T2& y) { read(x), read(y); } template<typename T1, typename T2, typename T3> void read(T1& x, T2& y, T3& z) { read(x), read(y), read(z); } template<typename T1, typename T2, typename T3, typename T4> void read(T1& x, T2& y, T3& z, T4& zz) { read(x), read(y), read(z), read(zz); } template<typename T> void print(vector<T>& v) { for (auto& x : v) cout << x << ' '; cout << endl; } #define sz(x) (int)(x.size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define pb push_back #define mp(a, b) make_pair(a, b) #define fi first #define se second #define cmin(a, b) a = min(a, b) #define cmax(a, b) a = max(a, b) #define YES cout << "YES" << endl #define NO cout << "NO" << endl #define QYES quit("YES") #define QNO quit("NO") #define int long long #define double long double const int inf = numeric_limits<int>::max() / 4; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MOD = 1e9+7, LOG=20; void solve(); signed main() { ios::sync_with_stdio(0); cin.tie(0); int tt=1; // cin >> tt; while(tt--) solve(); return 0; } struct segtree { int n; vector<pair<int, int>> t, lazy; segtree(int _n) : n(_n), t(4*n), lazy(4*n) {} void push(int v, int l, int r) { if (lazy[v]==mp(0LL, 0LL)) return; t[v].fi+=lazy[v].fi; t[v].se+=lazy[v].se; if (l!=r) { lazy[2*v+1].fi+=lazy[v].fi, lazy[2*v+1].se+=lazy[v].se; lazy[2*v+2].fi+=lazy[v].fi, lazy[2*v+2].se+=lazy[v].se; } lazy[v]={0, 0}; } void update(int v, int l, int r, int ql, int qr, pair<int, int> val) { push(v, l, r); if (l>qr || r<ql) return; if (l>=ql && r<=qr) { lazy[v]=val; push(v, l, r); return; } int m=(l+r)/2; update(2*v+1, l, m, ql, qr, val); update(2*v+2, m+1, r, ql, qr, val); if ((t[2*v+1].fi==t[2*v+2].fi && t[2*v+1].se>t[2*v+2].se) || t[2*v+1].fi>t[2*v+2].fi) t[v]=t[2*v+1]; else t[v]=t[2*v+2]; } pair<int, pair<int, int>> query(int v, int l, int r) { push(v, l, r); if (l==r) return {l, t[v]}; int m=(l+r)/2; if (t[2*v+1].fi>t[2*v+2].fi || (t[2*v+1].fi==t[2*v+2].fi && t[2*v+1].se>t[2*v+2].se)) return query(2*v+1, l, m); return query(2*v+2, m+1, r); } void update(int l, int r, pair<int, int> val) { update(0, 0, n-1, l, r, val); } pair<int, pair<int, int>> query() { return query(0, 0, n-1); } }; int n, k, m; vector<int> a, p, in, out, is; vector<vector<int>> adj; set<int> ok, isin; segtree seg(0); int cnt=-1; void dfs(int u, int val=0, int mn=0) { if (u) { cnt++; in[u]=cnt, is[cnt]=u; val+=a[u], cmin(mn, val); seg.update(in[u], in[u], {mn, a[u]}); } for (auto v: adj[u]) dfs(v, val, mn); if (u) out[u]=cnt; } int find(int u) { if (u==-1 || p[u]==u || ok.count(u)) return u; return p[u]=find(p[u]); } void solve() { read(n, k); n++; seg=segtree(n-1); int base=k; adj.clear(), adj.resize(n), a.clear(), a.resize(n), p.clear(), p.resize(n); in.clear(), in.resize(n), out.clear(), out.resize(n), is.clear(), is.resize(n); for (int i=1; i<n; i++) { read(a[i], p[i]); adj[p[i]].pb(i); } for (int i=0; i<n; i++) ok.insert(i), isin.insert(i); dfs(0); auto q=seg.query(); while (k+q.se.fi>=0) { int u=is[q.fi]; seg.update(in[u], in[u], {-inf, -inf}); isin.erase(u); if (a[u]<0) { q=seg.query(); continue; } ok.erase(u); int par=find(p[u]); if (par==0) { seg.update(in[u], out[u], {-a[u], -a[u]}); k+=a[u]; a[u]=0; q=seg.query(); continue; } if (!isin.count(par)) seg.update(in[par], in[par], {inf, inf}), isin.insert(par); seg.update(in[par], out[par], {a[u], a[u]}); seg.update(in[u], out[u], {-a[u], -a[u]}); a[par]+=a[u]; a[u]=0; q=seg.query(); } cout << k-base << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...