Submission #385227

#TimeUsernameProblemLanguageResultExecution timeMemory
385227Return_0Election Campaign (JOI15_election_campaign)C++17
100 / 100
255 ms45932 KiB
#pragma GCC optimize("Ofast,unroll-loops") // #pragma comment(linker, "/stack:200000000") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native") #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; typedef int ll; typedef long double ld; typedef pair <ll, ll> pll; #ifdef SINA #define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f (const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); } #define dbg2(x, j, n) cout<< #x << " : "; output((j), (n), x, 1); cout.flush(); #else #define dbg(...) 0 #define dbg2(x, j, n) 0 #endif #define SZ(x) ((ll)((x).size())) #define File(s, t) freopen(s ".txt", "r", stdin); freopen(t ".txt", "w", stdout); #define input(j, n, a) for (int _i = (j); _i < (n)+(j); _i++) cin>> a[_i]; #define output(j, n, a, t) for (int _i = (j); _i < (n)+(j); _i++) cout<< a[_i] << (((t) && _i != (n)+(j)-1)? ' ' : '\n'); #define kill(x) return cout<< (x) << endl, 0 #define cl const ll #define fr first #define sc second #define lc (v << 1) #define rc (lc | 1) #define mid ((l + r) >> 1) #define All(x) (x).begin(), (x).end() cl inf = sizeof(ll) == 4 ? (1e9 + 10) : (3e18), mod = 1e9 + 7, MOD = 998244353; template <class A,class B> ostream& operator << (ostream& out,const pair<A,B>&a){return out<<'('<<a.first<<", "<<a.second<<')';} template <class A> ostream& operator << (ostream& out, const vector<A> &a) { out<< '['; for (int i = -1; ++i < int(a.size());) out<< a[i] << (i + 1 < int(a.size()) ? ", " : ""); return out<<']'; } template <class T, typename _t = less <T> > using Tree = tree <T, null_type, _t, rb_tree_tag, tree_order_statistics_node_update>; pll operator + (const pll &x, const pll &y) { return pll(x.fr + y.fr, x.sc + y.sc); } cl N = 1e5 + 7; struct segtree { ll l, r, sum, sum2; segtree *lt, *rt; inline segtree (cl &L = 1, cl &R = 0) : l(L), r(R), sum(0), sum2(0) {} void build () { if (l == r) return; lt = new segtree (l, mid); rt = new segtree (mid + 1, r); lt->build(); rt->build(); } void upd (cl &pos, cl &x, cl &y) { if (l == r) { sum = x; sum2 = y; return; } if (pos <= lt->r) lt->upd(pos, x, y); else rt->upd(pos, x, y); sum = lt->sum + rt->sum; sum2 = lt->sum2 + rt->sum2; } pll ask (cl &ql, cl &qr) { if (r < ql || qr < l) return pll(0, 0); if (ql <= l && r <= qr) return pll(sum, sum2); return lt->ask(ql, qr) + rt->ask(ql, qr); } } seg [N]; ll sz [N], bc [N], h [N], pos [N], head [N], par [N], dp [N], val [N], n, q; vector <pair <pll, ll>> path [N]; vector <ll> adj [N]; ll DFS (cl &v = 1, cl &pr = 0) { par[v] = pr; h[v] = h[pr] + 1; for (auto &u : adj[v]) if (u != pr) sz[v] += DFS(u, v), bc[v] = (sz[u] > sz[bc[v]] ? u : bc[v]); return ++sz[v]; } void HLD (cl &v = 1, cl &hd = 1) { head[v] = hd; pos[v] = h[v] - h[head[v]] + 1; if (bc[v]) HLD(bc[v], hd); for (auto &u : adj[v]) if (u != par[v] && u != bc[v]) HLD(u, u); } ll LCA (ll v, ll u) { while (head[v] != head[u]) { if (h[par[head[v]]] < h[par[head[u]]]) u = par[head[u]]; else v = par[head[v]]; } return (h[v] < h[u] ? v : u); } ll ask (ll v, ll u, ll lca) { pll ret = {0, 0}; for (; head[v] != head[lca]; v = par[head[v]]) ret = ret + seg[head[v]].ask(1, pos[v]); ret = ret + seg[head[v]].ask(pos[lca] + 1, pos[v]); for (; head[u] != head[lca]; u = par[head[u]]) ret = ret + seg[head[u]].ask(1, pos[u]); ret = ret + seg[head[u]].ask(pos[lca] + 1, pos[u]); ret.fr += val[lca]; return ret.fr - ret.sc; } void dfs (cl &v = 1) { for (auto &u : adj[v]) if (u != par[v]) dfs(u); dp[v] = val[v]; for (auto &x : path[v]) dp[v] = max(dp[v], ask(x.fr.fr, x.fr.sc, v) + x.sc); seg[head[v]].upd(pos[v], val[v], dp[v]); val[par[v]] += dp[v]; } int main () { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>> n; for (ll i = 1, v, u; i < n; i++) { cin>> v >> u; adj[v].push_back(u); adj[u].push_back(v); } DFS(); HLD(); for (ll v = 1; v <= n; v++) seg[head[v]].r++; for (ll i = 1; i <= n; i++) if (seg[i].r) seg[i].build(); cin>> q; for (ll i = 0, v, u, c; i < q; i++) { cin>> v >> u >> c; path[LCA(v, u)].push_back({{v, u}, c}); } dfs(); cout<< *max_element(dp, dp + N) << endl; cerr<< "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; return 0; } /* 5 1 2 1 3 2 4 2 5 2 4 5 3 3 2 4 7 3 4 6 5 2 7 1 5 7 5 4 5 5 4 3 10 5 6 5 2 6 9 7 2 2 1 3 8 8 1 2 2 3 3 4 4 5 5 6 6 7 7 8 5 7 5 4 5 8 9 4 3 9 1 3 3 2 8 11 10 10 6 2 7 1 9 9 8 3 8 6 4 7 8 5 4 4 8 7 1 3 1 4 10 1 2 8 1 5 3 1 3 7 1 8 5 1 1 9 1 20 17 10 11 4 8 3 3 16 1 14 15 18 5 4 6 18 10 18 19 4 16 7 2 13 4 12 12 20 9 20 18 13 20 14 14 7 13 7 15 19 9 2341 13 8 6974 8 3 3339 15 17 6515 10 13 4370 1 7 8376 18 2 9272 6 7 4595 1 20 505 10 9 308 6 19 8937 2 15 5072 5 4 4217 2 4 4170 19 12 8204 */
#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...