Submission #947963

#TimeUsernameProblemLanguageResultExecution timeMemory
947963vqpahmadElection Campaign (JOI15_election_campaign)C++17
100 / 100
212 ms111876 KiB
#include<bits/stdc++.h> using namespace std; #ifdef ONPC #include"debug.h" #else #define debug(...) 42 #endif #define endl '\n' #define ll long long #define pii pair<int,int> #define F first #define S second #define pb push_back #define sz(a) (int)a.size() #define all(a) a.begin(),a.end() #define int long long template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } const int mod = 1e9 + 7; const int MAXN = 1e6 + 15; const int inf = 0x3f3f3f3f; const ll INF = 0x3f3f3f3f3f3f3f3f; const int off = (1<<18); struct sgt { #define ls (idx<<1) #define rs (idx<<1|1) ll t[off<<1], lazy[MAXN]; int unit = 0; void pull(int idx){ t[idx] = t[ls]+t[rs]; } void push(int idx){ if (!lazy[idx]) return; t[idx] += lazy[idx]; if (idx < off){ lazy[ls] += lazy[idx]; lazy[rs] += lazy[idx]; } lazy[idx] = 0; } void update(int idx, int u){ t[idx+=off] = u; while (idx/=2) pull(idx); } ll get(int idx){ ll res = t[idx += off]; while (idx/=2) res += t[idx]; return res; } int get(int l, int r, int idx=1, int lo=0, int hi=off){ if (r <= lo || hi <= l) return unit; if (l <= lo && hi <= r) return t[idx]; int mi = (lo+hi)>>1; return get(l, r, ls, lo, mi)+get(l, r, rs, mi, hi); } void update(int l, int r, int v, int idx=1, int lo=0, int hi=off){ if (r <= lo || hi <= l) return; if (l <= lo && hi <= r){ t[idx] += v; return; } int mi = (lo+hi)>>1; update(l, r, v, ls, lo, mi); update(l, r, v, rs, mi, hi); return; } } seg1, seg2; vector<int> adj[MAXN]; int fa[MAXN][20], dep[MAXN], dt[MAXN], ft[MAXN], timer = 1; void dfs(int node, int par){ dep[node] = dep[par] + 1; fa[node][0] = par; dt[node] = timer++; for (auto to : adj[node]){ if (to != par) dfs(to, node); } ft[node] = timer++; } int lca(int u, int v){ if (dep[u] > dep[v]) swap(u, v); for (int j = 19; j >= 0; j--){ if (dep[fa[v][j]] >= dep[u]) v = fa[v][j]; } if (u == v) return u; for (int j = 19; j >= 0; j--){ if (fa[v][j] != fa[u][j]) v = fa[v][j], u = fa[u][j]; } return fa[u][0]; } vector<array<int, 3>> queries[MAXN]; ll dp1[MAXN], dp2[MAXN]; // dp1 for best answer // dp2 for sum of childern bool contain(int u, int v){ return dt[u] <= dt[v] && ft[v] <= ft[u]; } void solve(int node, int par){ for (int to : adj[node]){ if (to != par) { solve(to, node); dp1[node] += dp1[to]; } } dp2[node] = dp1[node]; seg2.update(dt[node], ft[node], dp2[node]); for (auto [u, v, c] : queries[node]){ debug(node, u, v, c); debug(seg2.get(dt[u]), seg2.get(dt[v]), seg2.get(dt[node]), seg2.get(dt[fa[node][0]])); ll tm = c + seg2.get(dt[u]) + seg2.get(dt[v]) - seg2.get(dt[node]) - seg2.get(dt[fa[node][0]]); tm -= seg1.get(dt[u]) + seg1.get(dt[v]); ckmax(dp1[node], tm); } seg1.update(dt[node], ft[node], dp1[node]); debug(node, dp1[node], dp2[node]); } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 0; i < n - 1; i++){ int u, v; cin >> u >> v; adj[u].pb(v); adj[v].pb(u); } dfs(1, 0); for (int j = 1; j < 20; j++){ for (int i = 1; i <= n; i++){ fa[i][j] = fa[fa[i][j - 1]][j - 1]; } } for (int i = 1; i <= n; i++){ debug(i, dt[i], ft[i]); } int m; cin >> m; for (int i = 0; i < m; i++){ int u, v, c; cin >> u >> v >> c; queries[lca(u, v)].pb({u, v, c}); } solve(1, 0); cout << dp1[1] << endl; }

Compilation message (stderr)

election_campaign.cpp: In function 'void solve(long long int, long long int)':
election_campaign.cpp:6:20: warning: statement has no effect [-Wunused-value]
    6 | #define debug(...) 42
      |                    ^~
election_campaign.cpp:112:3: note: in expansion of macro 'debug'
  112 |   debug(node, u, v, c);
      |   ^~~~~
election_campaign.cpp:6:20: warning: statement has no effect [-Wunused-value]
    6 | #define debug(...) 42
      |                    ^~
election_campaign.cpp:113:3: note: in expansion of macro 'debug'
  113 |   debug(seg2.get(dt[u]), seg2.get(dt[v]), seg2.get(dt[node]), seg2.get(dt[fa[node][0]]));
      |   ^~~~~
election_campaign.cpp:6:20: warning: statement has no effect [-Wunused-value]
    6 | #define debug(...) 42
      |                    ^~
election_campaign.cpp:119:2: note: in expansion of macro 'debug'
  119 |  debug(node, dp1[node], dp2[node]);
      |  ^~~~~
election_campaign.cpp: In function 'int32_t main()':
election_campaign.cpp:6:20: warning: statement has no effect [-Wunused-value]
    6 | #define debug(...) 42
      |                    ^~
election_campaign.cpp:139:3: note: in expansion of macro 'debug'
  139 |   debug(i, dt[i], ft[i]);
      |   ^~~~~
#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...