Submission #536811

#TimeUsernameProblemLanguageResultExecution timeMemory
536811pavementElection Campaign (JOI15_election_campaign)C++17
100 / 100
304 ms46228 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #ifdef _WIN32 #define getchar_unlocked _getchar_nolock #endif #define int long long #define mp make_pair #define mt make_tuple #define pb push_back #define ppb pop_back #define eb emplace_back #define g0(a) get<0>(a) #define g1(a) get<1>(a) #define g2(a) get<2>(a) #define g3(a) get<3>(a) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef double db; typedef long long ll; typedef long double ld; typedef pair<int, int> ii; typedef tuple<int, int, int> iii; typedef tuple<int, int, int, int> iiii; typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; int N, M, idx, a[100005], b[100005], c[100005], sz[100005], pre[100005], ft1[100005], ft2[100005], anc[100005][25], dep[100005]; vector<int> adj[100005], vec[100005]; int dfs(int n, int e = -1) { anc[n][0] = e; pre[n] = ++idx; for (int i = 1; i <= 20; i++) if (anc[n][i - 1] != -1) anc[n][i] = anc[anc[n][i - 1]][i - 1]; for (auto u : adj[n]) if (u != e) { dep[u] = dep[n] + 1; sz[n] += dfs(u, n); } return ++sz[n]; } int lca(int u, int v) { if (dep[u] < dep[v]) swap(u, v); for (int i = 20; i >= 0; i--) if (dep[u] - (1 << i) >= dep[v]) u = anc[u][i]; if (u == v) return u; for (int i = 20; i >= 0; i--) if (anc[u][i] != anc[v][i]) { u = anc[u][i]; v = anc[v][i]; } return anc[u][0]; } inline int ls(int x) { return x & -x; } void upd1(int l, int r, int v) { for (; l <= N; l += ls(l)) ft1[l] += v; for (r++; r <= N; r += ls(r)) ft1[r] -= v; } void upd2(int l, int r, int v) { for (; l <= N; l += ls(l)) ft2[l] += v; for (r++; r <= N; r += ls(r)) ft2[r] -= v; } int qry1(int p) { p = pre[p]; int r = 0; for (; p; p -= ls(p)) r += ft1[p]; return r; } int qry2(int p) { p = pre[p]; int r = 0; for (; p; p -= ls(p)) r += ft2[p]; return r; } int dp(int n, int e = -1) { int ret = 0; for (auto u : adj[n]) if (u != e) ret += dp(u, n); upd1(pre[n], pre[n] + sz[n] - 1, ret); for (auto i : vec[n]) ret = max(ret, qry1(a[i]) + qry1(b[i]) - qry1(n) - (qry2(a[i]) + qry2(b[i]) - qry2(n)) + c[i]); upd2(pre[n], pre[n] + sz[n] - 1, ret); return ret; } main() { memset(anc, -1, sizeof anc); ios::sync_with_stdio(0); cin.tie(0); cin >> N; for (int i = 1, u, v; i < N; i++) { cin >> u >> v; adj[u].pb(v); adj[v].pb(u); } dfs(1); cin >> M; for (int i = 1; i <= M; i++) { cin >> a[i] >> b[i] >> c[i]; vec[lca(a[i], b[i])].pb(i); } cout << dp(1) << '\n'; }

Compilation message (stderr)

election_campaign.cpp:95:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   95 | main() {
      | ^~~~
#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...