Submission #41002

#TimeUsernameProblemLanguageResultExecution timeMemory
41002DoanPhuDucFactories (JOI14_factories)C++98
33 / 100
6045 ms188588 KiB
#include <bits/stdc++.h> #define FOR(x, a, b) for (int x = a; x <= b; ++x) #define FOD(x, a, b) for (int x = a; x >= b; --x) #define REP(x, a, b) for (int x = a; x < b; ++x) #define DEBUG(X) { cout << #X << " = " << X << endl; } #define PR(A, n) { cout << #A << " = "; FOR(_, 1, n) cout << A[_] << " "; cout << endl; } #define PR0(A, n) { cout << #A << " = "; REP(_, 0, n) cout << A[_] << " "; cout << endl; } using namespace std; typedef long long LL; typedef pair <int, int> II; const int N = 5e5 + 10; const LL INF = (LL)1e18; const int LG = 19; int n, s, t, q, timer; int h[N], a[N], b[N], e[N], V[2 * N], lg[2 * N], x[N]; int spT[2 * N][LG + 5]; LL f[N], d[N]; vector <II> adj[N]; struct CD { int cPar[N], c[N]; void Init() { memset(cPar, -1, sizeof cPar); DFS(1); Centroid(1, -1, c[1], -2); } void DFS(int u, int par = -1) { c[u] = 1; REP(k, 0, adj[u].size()) { int v = adj[u][k].first; if (v == par || cPar[v] != -1) continue; DFS(v, u); c[u] += c[v]; } } void Centroid(int u, int par, int sz, int preC) { REP(k, 0, adj[u].size()) { int v = adj[u][k].first; if (v == par || cPar[v] != -1) continue; if (c[v] * 2 > sz) { Centroid(v, u, sz, preC); return; } } cPar[u] = preC; REP(k, 0, adj[u].size()) { int v = adj[u][k].first; if (cPar[v] != -1) continue; DFS(v, u); Centroid(v, u, c[v], u); } } } CD; void DFS(int u, int par = -1) { V[++timer] = u; x[u] = timer; REP(k, 0, adj[u].size()) { int v = adj[u][k].first, w = adj[u][k].second; if (v == par) continue; d[v] = d[u] + w; h[v] = h[u] + 1; DFS(v, u); V[++timer] = u; } } void InitLCA() { FOR(i, 1, timer) spT[i][0] = V[i]; for (int j = 1; 1 << j <= timer; ++j) for (int i = 1; i + (1 << j) - 1 <= timer; ++i) { int x = spT[i][j - 1], y = spT[i + (1 << (j - 1))][j - 1]; if (h[x] < h[y]) spT[i][j] = x; else spT[i][j] = y; } for (int i = 0; 1 << i <= timer; ++i) lg[1 << i] = i; FOR(i, 1, timer) if (!lg[i]) lg[i] = lg[i - 1]; } int Query(int l, int r) { if (l > r) swap(l, r); int k = lg[r - l + 1]; int x = spT[l][k], y = spT[r - (1 << k) + 1][k]; if (h[x] < h[y]) return x; else return y; } int LCA(int u, int v) { return Query(x[u], x[v]); } LL Dist(int u, int v) { int w = LCA(u, v); return d[u] + d[v] - 2LL * d[w]; } void Init(int N, int A[], int B[], int D[]) { n = N; REP(i, 0, n - 1) { int u = A[i], v = B[i], w = D[i]; ++u; ++v; adj[u].push_back(II(v, w)); adj[v].push_back(II(u, w)); } FOR(i, 1, n) f[i] = INF; DFS(1); InitLCA(); CD.Init(); } void Update(int u) { int x = u; while (x != -2) { f[x] = min(f[x], Dist(x, u)); x = CD.cPar[x]; } } void Assign(int u, LL v) { int x = u; while (x != -2) { f[x] = v; x = CD.cPar[x]; } } LL Query(int u) { int x = u; LL ans = INF; while (x != -2) { ans = min(ans, f[x] + Dist(x, u)); x = CD.cPar[x]; } return ans; } long long Query(int S, int X[], int T, int Y[]) { s = S; t = T; LL ans = INF; REP(i, 0, s) ++X[i]; REP(i, 0, t) ++Y[i]; REP(i, 0, s) Update(X[i]); REP(i, 0, t) ans = min(ans, Query(Y[i])); REP(i, 0, s) Assign(X[i], INF); return ans; }

Compilation message (stderr)

factories.cpp: In member function 'void CD::DFS(int, int)':
factories.cpp:5:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(x, a, b) for (int x = a; x < b; ++x)
                                        ^
factories.cpp:36:9: note: in expansion of macro 'REP'
         REP(k, 0, adj[u].size()) {
         ^
factories.cpp: In member function 'void CD::Centroid(int, int, int, int)':
factories.cpp:5:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(x, a, b) for (int x = a; x < b; ++x)
                                        ^
factories.cpp:43:9: note: in expansion of macro 'REP'
         REP(k, 0, adj[u].size()) {
         ^
factories.cpp:5:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(x, a, b) for (int x = a; x < b; ++x)
                                        ^
factories.cpp:51:9: note: in expansion of macro 'REP'
         REP(k, 0, adj[u].size()) {
         ^
factories.cpp: In function 'void DFS(int, int)':
factories.cpp:5:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(x, a, b) for (int x = a; x < b; ++x)
                                        ^
factories.cpp:61:5: note: in expansion of macro 'REP'
     REP(k, 0, adj[u].size()) {
     ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...