Submission #65748

#TimeUsernameProblemLanguageResultExecution timeMemory
65748realityFactories (JOI14_factories)C++17
33 / 100
8054 ms143352 KiB
#include "factories.h" #include "bits/stdc++.h" using namespace std; #define fi first #define se second #define ll long long #define dbg(v) cerr<<#v<<" = "<<v<<'\n' #define vi vector<int> #define vl vector <ll> #define pii pair<int,int> #define mp make_pair #define db long double #define pb push_back #define all(s) s.begin(),s.end() template < class P , class Q > ostream& operator<<(ostream& stream, pair < P , Q > v){ stream << "(" << v.fi << ',' << v.se << ")"; return stream;} template < class T > ostream& operator<<(ostream& stream, const vector<T> v){ stream << "[ "; for (int i=0; i<(int)v.size(); i++) stream << v[i] << " "; stream << "]"; return stream;} template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;} template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;} int n; const int N = 2e6 + 5; vector < pii > g[N]; int SZ[N]; void dfs_sz(int v = 0) { SZ[v] = 1; for (auto & u : g[v]) { if (SZ[u.fi] != -1) continue; dfs_sz(u.fi); SZ[v] += SZ[u.fi]; if (SZ[u.fi] > SZ[g[v][0].fi]) swap(u, g[v][0]); } } int in[N]; int out[N]; int rin[N]; int nxt[N]; int pr[N]; int Time = 0; ll D[N]; void dfs_hld(int v = 0) { in[v] = ++Time; rin[in[v]] = v; for (auto u : g[v]) { if (D[u.fi] != -1) continue; nxt[u.fi] = (u == g[v][0] ? nxt[v] : u.fi); D[u.fi] = u.se + D[v]; pr[u.fi] = v; dfs_hld(u.fi); } out[v] = Time; } ll t[N]; ll T[N]; ll lz[N]; int ok[N]; void upd_lz(int l,int r,int node) { if (lz[node] != -1) { if (l != r) lz[node * 2] = lz[node * 2 + 1] = lz[node]; T[node] = t[node] + lz[node]; if (lz[node] == 0) ok[node] = 0; else ok[node] = r - l + 1; } lz[node] = -1; } void build(int l,int r,int node) { lz[node] = -1; if (l == r) return void(T[node] = t[node] = -2 * D[rin[l]]); int m = (l + r) / 2; build(l,m,node * 2); build(m+1,r,node * 2 + 1); T[node] = t[node] = min(t[node * 2],t[node * 2 + 1]); } void Upd(int l,int r,int node) { if (l == r) return; int m = (l + r) / 2; upd_lz(l,m,node * 2); upd_lz(m + 1,r,node * 2 + 1); T[node] = min(T[node * 2],T[node * 2 + 1]); ok[node] = ok[node * 2] + ok[node * 2 + 1]; } void update(int l,int r,int l1,int r1,ll v,int node) { upd_lz(l,r,node); if (l1 <= l && r <= r1) lz[node] = v; else { int m = (l + r) / 2; if (l1 <= m) update(l,m,l1,r1,v,node * 2); if (m+1<=r1) update(m+1,r,l1,r1,v,node * 2 + 1); Upd(l,r,node); } } ll query(int l,int r,int l1,int r1,int node) { upd_lz(l,r,node); if (!ok[node]) return (ll)(1e18); if (l1 <= l && r <= r1 && ok[node] == r - l + 1) return T[node]; int m = (l + r) / 2; ll ans = 1e18; if (l1 <= m) smin(ans,query(l,m,l1,r1,node * 2)); if (m+1<=r1) smin(ans,query(m+1,r,l1,r1,node * 2 + 1)); Upd(l,r,node); return ans; } void Init(int NN, int A[], int B[], int DD[]) { n = NN; for (int i = 0;i < n;++i) { g[A[i]].pb(mp(B[i],DD[i])); g[B[i]].pb(mp(A[i],DD[i])); } for (int i = 0;i < n;++i) SZ[i] = D[i] = -1; D[0] = 0; dfs_sz(); dfs_hld(); build(1,n,1); rin[0] = -1; pr[0] = -1; } void upd(int node,ll v) { while (node >= 0) { update(1,n,in[nxt[node]],in[node],v,1); node = pr[nxt[node]]; } } ll que(int node) { ll ans = 1e18; while (node >= 0) { auto Q = query(1,n,in[nxt[node]],in[node],1); smin(ans,Q); node = pr[nxt[node]]; } return ans; } long long Query(int S, int X[], int T, int Y[]) { vi a,b; for (int i = 0;i < S;++i) a.pb(X[i]); for (int i = 0;i < T;++i) b.pb(Y[i]); if (a.size() > b.size()) swap(a,b); sort(all(a),[&](int x,int y) { return D[x] > D[y]; }); for (auto it : a) upd(it,D[it]); ll mn1 = D[a[0]],mn2 = D[b[0]]; for (auto it : a) smin(mn1,D[it]); for (auto it : b) smin(mn2,D[it]); ll ans = mn1 + mn2;//vertex 0 isn't "covered" for (auto it : b) smin(ans,D[it] + que(it)); for (auto it : a) upd(it,0ll); return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...