Submission #1086351

#TimeUsernameProblemLanguageResultExecution timeMemory
1086351TrinhKhanhDungFactories (JOI14_factories)C++14
100 / 100
5045 ms174932 KiB
#include <bits/stdc++.h> #define ll long long #define fi first #define se second #define sz(x) (int)x.size() #define ALL(v) v.begin(),v.end() #define MASK(k) (1LL << (k)) #define BIT(x, i) (((x) >> (i)) & 1) #define oo (ll)1e18 #define INF (ll)1e9 #define MOD (ll)(998244353) #include "factories.h" using namespace std; template<class T1, class T2> bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;} template<class T1, class T2> bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;} template<class T1, class T2> void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;} template<class T1, class T2> void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;} template<class T> void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());} struct seg{ vector<ll> mi; int n; seg(int _n = 0){ n = _n; mi.resize(n * 4 + 3, oo); } void upd(int p, ll c){ int i = 1, l = 0, r = n; while(l < r){ int m = (l + r) >> 1; if(p <= m){ i = i * 2; r = m; } else{ i = i * 2 + 1; l = m + 1; } } mi[i] = c; while(i > 1){ i >>= 1; mi[i] = min(mi[i * 2], mi[i * 2 + 1]); } } ll get(int i, int l, int r, int u, int v){ if(r < u || v < l) return oo; if(u <= l && r <= v) return mi[i]; int m = (l + r) >> 1; return min(get(i * 2, l, m, u, v), get(i * 2 + 1, m + 1, r, u, v)); } ll get(int u, int v){ return get(1, 0, n, u, v); } } it1((int)5e5), it2((int)5e5); const int MAX = 5e5 + 10, LOG = 19; int N, Q; vector<pair<int, int>> adj[MAX]; int h[MAX], d[MAX][LOG], in[MAX], out[MAX], timer; ll dist[MAX]; void dfs(int u, int p){ in[u] = ++timer; for(auto o: adj[u]){ int v = o.fi; int c = o.se; if(v == p) continue; h[v] = h[u] + 1; d[v][0] = u; for(int i=1; i<LOG; i++){ d[v][i] = d[d[v][i - 1]][i - 1]; } dist[v] = dist[u] + c; dfs(v, u); } out[u] = timer; // cout << u << ' ' << in[u] << ' ' << out[u] << ' ' << h[u] << ' ' << dist[u] << '\n'; } int lca(int u, int v){ if(h[u] < h[v]) swap(u, v); int delta = h[u] - h[v]; for(int i=0; i<LOG; i++) if(BIT(delta, i)){ u = d[u][i]; } if(u == v) return u; for(int i=LOG-1; i>=0; i--) if(d[u][i] != d[v][i]){ u = d[u][i]; v = d[v][i]; } return d[u][0]; } void Init(int n, int A[], int B[], int D[]){ N = n; for(int i=0; i<N-1; i++){ int u = A[i], v = B[i], c = D[i]; adj[u].push_back(make_pair(v, c)); adj[v].push_back(make_pair(u, c)); } dfs(0, -1); } long long Query(int S, int X[], int T, int Y[]){ int n = S, m = T; vector<int> a; for(int i=0; i<n; i++){ int x = X[i]; a.push_back(x); it1.upd(in[x], dist[x]); } for(int i=0; i<m; i++){ int x = Y[i]; a.push_back(x); it2.upd(in[x], dist[x]); } sort(ALL(a), [&](int u, int v){return in[u] < in[v];}); for(int i=1; i<n+m; i++){ a.push_back(lca(a[i], a[i - 1])); } cps(a); sort(ALL(a), [&](int u, int v){return in[u] < in[v];}); ll ans = oo; for(int x: a){ minimize(ans, it1.get(in[x], out[x]) + it2.get(in[x], out[x]) - 2 * dist[x]); } for(int x: a){ it1.upd(in[x], oo); it2.upd(in[x], oo); } return ans; } //void solve(){ // cin >> N >> Q; // for(int i=1; i<N; i++){ // int u, v, c; // cin >> u >> v >> c; // adj[u].push_back(make_pair(v, c)); // adj[v].push_back(make_pair(u, c)); // } // dfs(0, -1); // seg it1(N), it2(N); // while(Q--){ // int n, m; cin >> n >> m; // vector<int> a; // for(int i=0; i<n; i++){ // int x; cin >> x; // a.push_back(x); // it1.upd(in[x], dist[x]); // } // for(int i=0; i<m; i++){ // int x; cin >> x; // a.push_back(x); // it2.upd(in[x], dist[x]); // } // sort(ALL(a), [&](int u, int v){return in[u] < in[v];}); // for(int i=1; i<n+m; i++){ // a.push_back(lca(a[i], a[i - 1])); // } // cps(a); // sort(ALL(a), [&](int u, int v){return in[u] < in[v];}); // ll ans = oo; // for(int x: a){ //// cout << x << ' ' << // minimize(ans, it1.get(in[x], out[x]) + it2.get(in[x], out[x]) - 2 * dist[x]); // } // for(int x: a){ // it1.upd(in[x], oo); // it2.upd(in[x], oo); // } // cout << ans << '\n'; // } //} // //int main(){ // ios_base::sync_with_stdio(0); cin.tie(0); //// freopen("numtable.inp","r",stdin); //// freopen("numtable.out","w",stdout); // // solve(); // // return 0; //}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...