Submission #1086530

#TimeUsernameProblemLanguageResultExecution timeMemory
1086530steveonalexElection Campaign (JOI15_election_campaign)C++17
10 / 100
84 ms31056 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define MASK(i) (1LL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) #define ALL(v) (v).begin(), (v).end() #define block_of_code if(true) ll max(ll a, ll b){return (a > b) ? a : b;} ll min(ll a, ll b){return (a < b) ? a : b;} ll gcd(ll a, ll b){return __gcd(a, b);} ll lcm(ll a, ll b){return a / gcd(a, b) * b;} ll LASTBIT(ll mask){return (mask) & (-mask);} int pop_cnt(ll mask){return __builtin_popcountll(mask);} int ctz(ull mask){return __builtin_ctzll(mask);} int logOf(ull mask){return 63 - __builtin_clzll(mask);} mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);} double rngesus_d(double l, double r){ double cur = rngesus(0, MASK(60) - 1); cur /= MASK(60) - 1; return l + cur * (r - l); } 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 T> void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){ for(auto item: container) out << item << separator; out << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); } const int N = 1e5 + 69, LOG_N = 17; struct FenwickTree{ int n; vector<ll> a; FenwickTree(int _n){ n = _n; a.resize(n+1); } void update(int i, ll v){ while(i <= n){ a[i] += v; i += LASTBIT(i); } } void update(int l, int r, ll v){ update(l, v); update(r + 1, -v); } ll get(int i){ ll ans = 0; while(i > 0){ ans += a[i]; i -= LASTBIT(i); } return ans; } }; int n, q; vector<int> graph[N]; int parent[N][LOG_N], h[N]; int in[N], out[N], dfs_cnt = 0; void dfs(int u, int p){ in[u] = ++dfs_cnt; h[u] = h[p] + 1; for(int j = 1; MASK(j) < h[u]; ++j) parent[u][j] = parent[parent[u][j-1]][j-1]; for(int v: graph[u]) if (v != p){ parent[v][0] = u; dfs(v, u); out[u] = out[v]; } } int jump(int u, int d){ for(int j = 0; j < LOG_N; ++j) if (GETBIT(d, j)) u = parent[u][j]; return u; } int LCA(int u, int v){ if (h[u] < h[v]) swap(u, v); u = jump(u, h[u] - h[v]); if (u == v) return u; for(int j = LOG_N - 1; j>=0; --j) if (parent[u][j] != parent[v][j]){ u = parent[u][j]; v = parent[v][j]; } return parent[u][0]; } vector<array<int, 3>> queries[N]; ll dp[N], sum[N]; FenwickTree bit(N); void go(int u, int p){ for(int v: graph[u]) if (v != p){ go(v, u); sum[u] += dp[v]; dp[u] += dp[v]; } for(array<int, 3> i: queries[u]){ int x = i[0], y = i[1], w = i[2]; ll ans = sum[u] + w; ans += bit.get(in[x]) + bit.get(in[y]) - 2 * bit.get(in[u]); maximize(dp[u], ans); } bit.update(in[u], out[u], sum[u] - dp[u]); } int main(void){ ios::sync_with_stdio(0);cin.tie(0); cout.tie(0); clock_t start = clock(); cin >> n; for(int i = 1; i<n; ++i){ int u,v; cin >> u >> v; graph[u].push_back(v); graph[v].push_back(u); } dfs(1, 0); cin >> q; while(q--){ int u, v, w; cin >> u >> v >> w; queries[LCA(u, v)].push_back({{u, v, w}}); } go(1, 0); cout << dp[1] << "\n"; cerr << "Time elapsed: " << clock() - start << " ms\n"; return 0; }
#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...