Submission #224681

#TimeUsernameProblemLanguageResultExecution timeMemory
224681PeppaPigElection Campaign (JOI15_election_campaign)C++14
30 / 100
1095 ms29480 KiB
#include <bits/stdc++.h> #define long long long #define iii tuple<int, int, int> #define pii pair<int, int> #define x first #define y second using namespace std; const int N = 1e5+5; struct SegmentTree { int t[N << 1]; void update(int x, long k) { for(t[x += N] += k; x != 1; x >>= 1) t[x >> 1] = t[x] + t[x ^ 1]; } long query(int l, int r) { long ret = 0; for(l += N, r += N + 1; l < r; l >>= 1, r >>= 1) { if(l & 1) ret += t[l++]; if(r & 1) ret += t[--r]; } return ret; } } T; int n, q; vector<int> g[N]; vector<iii> Q[N]; int dep[N], par[N], rot[N], spi[N], pos[N], anc[N][18]; int dfs(int u, int p) { dep[u] = dep[p] + 1, par[u] = anc[u][0] = p; for(int i = 1; i <= 17; i++) anc[u][i] = anc[anc[u][i-1]][i-1]; int sz = 0; pii ret(0, -1); for(int &v : g[u]) if(v != p) { int z = dfs(v, u); sz += z, ret = max(ret, pii(z, v)); } spi[u] = ret.y; return sz; } void init_hld() { dfs(1, 0); for(int i = 1, idx = 0; i <= n; i++) if(spi[par[i]] != i) for(int j = i; j != -1; j = spi[j]) pos[j] = ++idx, rot[j] = i; } int lca(int a, int b) { if(dep[a] < dep[b]) swap(a, b); for(int i = 17; ~i; i--) if(dep[anc[a][i]] >= dep[b]) a = anc[a][i]; if(a == b) return a; for(int i = 17; ~i; i--) if(anc[a][i] != anc[b][i]) a = anc[a][i], b = anc[b][i]; return anc[a][0]; } int pre(int a, int b) { for(int i = 17; ~i; i--) if(dep[anc[a][i]] > dep[b]) a = anc[a][i]; return a; } long dp[N], ans; long query_path(int a, int b) { long ret = spi[a] != -1 ? dp[spi[a]] : 0; while(rot[a] != rot[b]) { ret += T.query(pos[rot[a]], pos[a]) - dp[rot[a]]; if(spi[par[rot[a]]] != -1) ret += dp[spi[par[rot[a]]]]; a = par[rot[a]]; } ret += T.query(pos[b], pos[a]); return ret; } void solve(int u, int p) { long sum = 0; for(int &v : g[u]) if(v != p) { solve(v, u); sum += dp[v]; } dp[u] = sum; for(iii &t : Q[u]) { int a, b, c; tie(a, b, c) = t; long now = sum; if(a != u) { int pa = pre(a, u); now += query_path(a, pa) - dp[pa]; } if(b != u) { int pb = pre(b, u); now += query_path(b, pb) - dp[pb]; } dp[u] = max(dp[u], now + c); } ans = max(ans, dp[u]); if(rot[u] == u) T.update(pos[par[u]], dp[u]); } int main() { scanf("%d", &n); for(int i = 1, a, b; i < n; i++) { scanf("%d %d", &a, &b); g[a].emplace_back(b); g[b].emplace_back(a); } init_hld(); scanf("%d", &q); for(int i = 1, a, b, c; i <= q; i++) { scanf("%d %d %d", &a, &b, &c); Q[lca(a, b)].emplace_back(a, b, c); } solve(1, 0); printf("%lld\n", ans); return 0; }

Compilation message (stderr)

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:106:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
election_campaign.cpp:108:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &a, &b);
         ~~~~~^~~~~~~~~~~~~~~~~
election_campaign.cpp:114:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &q);
     ~~~~~^~~~~~~~~~
election_campaign.cpp:116:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d", &a, &b, &c);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...