Submission #1230694

#TimeUsernameProblemLanguageResultExecution timeMemory
1230694thdhFactories (JOI14_factories)C++20
100 / 100
2559 ms217892 KiB
#include <bits/stdc++.h> // #include "factories.h" #define ll long long #define pb push_back #define eb emplace_back #define pu push #define ins insert #define fi first #define se second #define all(a) a.begin(),a.end() #define bruh ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fu(x,a,b) for (auto x=a;x<=b;x++) #define fd(x,a,b) for (auto x=a;x>=b;x--) using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") //mt19937 mt(chrono::steady_clock::now().time_since_epoch().count()); /* Competitive Programming notes that I need to study & fix my dumbass self: 1. Coding: - Always be sure to check the memory of arrays (maybe use vectors), for loops - Always try to maximize the memory if possible, even if you are going for subtasks - Do not exploit #define int long long, it will kill you 2. Stress: - Always try generating big testcases and try if they run 3. Time management: - Don't overcommit or undercommit, always spend a certain amount of time to think a problem, don't just look at it and say I'm fucked - Do not spend too much time coding brute-force solutions, they should be easily-codable solutions that don't take up too much time Time management schedule: Offline / LAH days (4 problems - 3h): 15' thinking of solution / idea 1. no idea: skip 2. yes idea: continue thinking for <= 15' + implementing: <= 20' + brute-force: <= 5' + test generator: <= 5' I hate offline because I am dumb */ typedef pair<ll, ll> ii; const int N = 5e5+5; const int M = 20; const int mod = 1e9+7; const ll inf = 1e18; using cd = complex<double>; const long double PI = acos(-1); int power(int a,int b) {ll x = 1;if (a >= mod) a%=mod; while (b) {if (b & 1) x = x*a % mod;a = a*a % mod;b>>=1;}return x;} int n, q; vector<ii> adj[N]; vector<ii> vt[N]; ll up[N][M], h[N], d[N], par[N], tin[N], tout[N], timer = 0; ll dp[N]; ll sum = 0, ans = inf, root_h = 0; ll sz[N], mns[N], mnt[N]; bool checks[N], checkt[N]; vector<ll> nodes; stack<ll> st; void reset() { timer = sum = 0; ans = inf; nodes.clear(); for (ll i = 1; i <= n; i++) adj[i].clear(), vt[i].clear(), dp[i] = 0, d[i] = 0, h[i] = 0, tin[i] = tout[i] = sz[i] = 0, mns[i] = mnt[i] = inf, checks[i] = checkt[i] = 0; } void predfs(ll u, ll p) { tin[u] = ++timer; up[u][0] = par[u] = p; for (ll i = 1; i < M; i++) up[u][i] = up[up[u][i-1]][i-1]; for (auto i : adj[u]) { ll v = i.fi, w = i.se; if (v == p) continue; h[v] = h[u] + 1; d[v] = d[u] + w; predfs(v, u); } tout[u] = timer; } ll lca(ll u, ll v) { if (h[u] < h[v]) swap(u,v); ll diff = h[u] - h[v]; for (ll i = 0; i < M; i++) if (diff>>i & 1) u = up[u][i]; if (u == v) return u; for (ll i = M-1; i >= 0; i--) if (up[u][i] != up[v][i]) u = up[u][i], v = up[v][i]; return up[u][0]; } ll dist_anc(ll u, ll v) { return d[u] + d[v] - 2ll * d[lca(u,v)]; } bool cmp(ll u, ll v) { return tin[u] < tin[v]; } bool insub(ll u, ll v) { return tin[u] >= tin[v] && tin[u] <= tout[v]; } void dfs(ll u, ll p) { if (checks[u]) mns[u] = 0; if (checkt[u]) mnt[u] = 0; for (auto i : vt[u]) { ll v = i.fi, w = i.se; dfs(v,u); mns[u] = min(mns[u], mns[v] + w), mnt[u] = min(mnt[u], mnt[v] + w); } ans = min(ans, mns[u] + mnt[u]); } void Init(int N, int A[], int B[], int D[]) { n = N; reset(); for (ll i = 0; i < n-1; i++) { A[i]++, B[i]++; adj[A[i]].pb({B[i], D[i]}); adj[B[i]].pb({A[i], D[i]}); } for (ll i = 1; i <= n; i++) mns[i] = mnt[i] = inf; predfs(1, 0); } ll Query(int S, int X[], int T, int Y[]) { ans = inf, sum = 0; ll s, t; s = S, t = T; for (ll j = 0; j < s; j++) { ll x = X[j]; ++x; checks[x] = 1; nodes.pb(x); } for (ll j = 0; j < t; j++) { ll x = Y[j]; ++x; checkt[x] = 1; nodes.pb(x); } sort(all(nodes), cmp); // for (auto j : nodes) cout<<j<<" "; // cout<<endl; ll m = nodes.size(); for (ll j = 0; j < m-1; j++) nodes.pb(lca(nodes[j], nodes[j+1])); sort(all(nodes)); nodes.erase(unique(all(nodes)), nodes.end()); sort(all(nodes), cmp); // for (auto j : nodes) cout<<j<<" "; // cout<<endl; while (!st.empty()) st.pop(); st.push(nodes[0]); for (ll j = 1; j < nodes.size(); j++) { while (!st.empty() && !insub(nodes[j], st.top())) st.pop(); if (!st.empty()) vt[st.top()].pb({nodes[j], dist_anc(st.top(), nodes[j])}); st.push(nodes[j]); } // for (auto x : nodes) cout<<x<<" "; // cout<<endl; // cout<<sum<<endl; // pdfs(nodes[0], 0); dfs(nodes[0], 0); for (auto x : nodes) vt[x].clear(), checks[x] = checkt[x] = sz[x] = 0, mns[x] = mnt[x] = inf; nodes.clear(); return ans; } // void solve() // { // cin>>n>>q; // reset(); // for (int i = 1; i < n; i++) // { // int u,v,w; cin>>u>>v>>w; // ++u, ++v; // adj[u].pb({v, w}); adj[v].pb({u, w}); // } // for (int i = 1; i <= n; i++) mns[i] = mnt[i] = inf; // predfs(1, 0); // // cout<<lca(1, 3); // for (int i = 0; i < q; i++) // { // // cout<<endl; // } // } /* Go through the mistakes you usually make and revise your code, for god's sake... */ // signed main() // { // bruh // //freopen("input.inp","r",stdin); // //freopen("output.inp","w",stdout); // int A[] = {0, 1, 2, 2, 4, 1}, B[] = {1, 2, 3, 4, 5, 6}, D[] = {4, 4, 5, 6, 5, 3}; // Init(7, A, B, D); // int X[] = {0, 1, 3}, Y[] = {4, 6}; // cout<<Query(3, X, 2, Y); // }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...