Submission #914103

# Submission time Handle Problem Language Result Execution time Memory
914103 2024-01-21T04:40:25 Z penguin133 Election Campaign (JOI15_election_campaign) C++17
100 / 100
313 ms 42488 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

int ft[500005], n, S[100005], E[100005], dp[100005], par[20][100005], dep[100005];

void upd(int l, int r, int v){
	r++;
	for(;l<=n;l+=(l & -l))ft[l] += v;
	for(;r <= n; r += (r & -r))ft[r] -= v;
}

int qry(int p){
	int res = 0;
	for(;p;p-=(p & -p))res += ft[p];
	return res;
}

vector <int> adj[100005];
vector <pii> stuf[100005];
int cnt = 1;
void dfs(int x, int p, int d){
	S[x] = cnt++;
	par[0][x] = p;
	for(int i = 1; i <= 19; i++)par[i][x] = par[i-1][par[i-1][x]];
	dep[x] = d;
	for(auto i : adj[x])if(i != p)dfs(i, x, d + 1);
	E[x] = cnt - 1;
}

int lca(int u, int v){
	if(dep[u] > dep[v])swap(u, v);
	int df = dep[v] - dep[u];
	for(int i=0;i<=19;i++)if(df >> i & 1)v = par[i][v];
	if(u == v)return u;
	for(int i=19;i>=0;i--)if(par[i][u] != par[i][v])u = par[i][u], v = par[i][v];
	return par[0][u];
}

void proc(int x, int p){
	for(auto i : adj[x]){
		if(i != p){
			proc(i, x);
		}
	}
	int sm = 0;
	for(auto i : adj[x]){
		if(i != p){
			
			upd(S[x], E[x], dp[i]);
			upd(S[i], E[i], -dp[i]);
			sm += dp[i];
		}
	}
	int ans = sm;
	for(auto i : stuf[x]){
		//cout << x << ' ' << i.se.fi << ' ' << i.se.se << ' ' << qry(S[i.se.fi]) << ' ' << qry(S[i.se.se]) << '\n';
		ans = max(ans, i.fi + qry(S[i.se.fi]) + qry(S[i.se.se]) - sm);
	}
	
	//cout << x << ' ' << sm << ' ' << ans << '\n';
	dp[x] = ans;
}

void solve(){
	cin >> n;
	for(int i=1;i<n;i++){
		int a, b; cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	dfs(1, -1, 1);
	int m; cin >> m;
	while(m--){
		int a, b, c; cin >> a >> b >> c;
		stuf[lca(a, b)].push_back({c, {a, b}});
	}
	proc(1, -1);
	cout << dp[1];
}

main(){
	ios::sync_with_stdio(0);cin.tie(0);
	int tc = 1;
	//cin >> tc;
	for(int tc1=1;tc1<=tc;tc1++){
		// cout << "Case #" << tc1 << ": ";
		solve();
	}
}

Compilation message

election_campaign.cpp:91:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   91 | main(){
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 25176 KB Output is correct
2 Correct 5 ms 25180 KB Output is correct
3 Correct 5 ms 25180 KB Output is correct
4 Correct 6 ms 25176 KB Output is correct
5 Correct 119 ms 30208 KB Output is correct
6 Correct 38 ms 37156 KB Output is correct
7 Correct 95 ms 34900 KB Output is correct
8 Correct 72 ms 30348 KB Output is correct
9 Correct 76 ms 33488 KB Output is correct
10 Correct 81 ms 30464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 25180 KB Output is correct
2 Correct 5 ms 25180 KB Output is correct
3 Correct 6 ms 25176 KB Output is correct
4 Correct 81 ms 42008 KB Output is correct
5 Correct 93 ms 42132 KB Output is correct
6 Correct 78 ms 42068 KB Output is correct
7 Correct 84 ms 42064 KB Output is correct
8 Correct 98 ms 42024 KB Output is correct
9 Correct 85 ms 41984 KB Output is correct
10 Correct 85 ms 41996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 25180 KB Output is correct
2 Correct 5 ms 25180 KB Output is correct
3 Correct 6 ms 25176 KB Output is correct
4 Correct 81 ms 42008 KB Output is correct
5 Correct 93 ms 42132 KB Output is correct
6 Correct 78 ms 42068 KB Output is correct
7 Correct 84 ms 42064 KB Output is correct
8 Correct 98 ms 42024 KB Output is correct
9 Correct 85 ms 41984 KB Output is correct
10 Correct 85 ms 41996 KB Output is correct
11 Correct 13 ms 26460 KB Output is correct
12 Correct 95 ms 42256 KB Output is correct
13 Correct 86 ms 42292 KB Output is correct
14 Correct 81 ms 42320 KB Output is correct
15 Correct 85 ms 42320 KB Output is correct
16 Correct 87 ms 42488 KB Output is correct
17 Correct 85 ms 42264 KB Output is correct
18 Correct 87 ms 42404 KB Output is correct
19 Correct 79 ms 42320 KB Output is correct
20 Correct 86 ms 42312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 189 ms 34508 KB Output is correct
2 Correct 79 ms 42152 KB Output is correct
3 Correct 263 ms 39348 KB Output is correct
4 Correct 157 ms 34968 KB Output is correct
5 Correct 177 ms 38800 KB Output is correct
6 Correct 129 ms 35336 KB Output is correct
7 Correct 313 ms 38416 KB Output is correct
8 Correct 242 ms 34388 KB Output is correct
9 Correct 79 ms 42068 KB Output is correct
10 Correct 214 ms 37508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 25176 KB Output is correct
2 Correct 5 ms 25180 KB Output is correct
3 Correct 5 ms 25180 KB Output is correct
4 Correct 6 ms 25176 KB Output is correct
5 Correct 119 ms 30208 KB Output is correct
6 Correct 38 ms 37156 KB Output is correct
7 Correct 95 ms 34900 KB Output is correct
8 Correct 72 ms 30348 KB Output is correct
9 Correct 76 ms 33488 KB Output is correct
10 Correct 81 ms 30464 KB Output is correct
11 Correct 7 ms 25180 KB Output is correct
12 Correct 5 ms 25180 KB Output is correct
13 Correct 6 ms 25180 KB Output is correct
14 Correct 6 ms 25244 KB Output is correct
15 Correct 6 ms 25180 KB Output is correct
16 Correct 6 ms 25244 KB Output is correct
17 Correct 6 ms 25180 KB Output is correct
18 Correct 5 ms 25180 KB Output is correct
19 Correct 6 ms 25180 KB Output is correct
20 Correct 6 ms 25388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 25176 KB Output is correct
2 Correct 5 ms 25180 KB Output is correct
3 Correct 5 ms 25180 KB Output is correct
4 Correct 6 ms 25176 KB Output is correct
5 Correct 119 ms 30208 KB Output is correct
6 Correct 38 ms 37156 KB Output is correct
7 Correct 95 ms 34900 KB Output is correct
8 Correct 72 ms 30348 KB Output is correct
9 Correct 76 ms 33488 KB Output is correct
10 Correct 81 ms 30464 KB Output is correct
11 Correct 6 ms 25180 KB Output is correct
12 Correct 5 ms 25180 KB Output is correct
13 Correct 6 ms 25176 KB Output is correct
14 Correct 81 ms 42008 KB Output is correct
15 Correct 93 ms 42132 KB Output is correct
16 Correct 78 ms 42068 KB Output is correct
17 Correct 84 ms 42064 KB Output is correct
18 Correct 98 ms 42024 KB Output is correct
19 Correct 85 ms 41984 KB Output is correct
20 Correct 85 ms 41996 KB Output is correct
21 Correct 13 ms 26460 KB Output is correct
22 Correct 95 ms 42256 KB Output is correct
23 Correct 86 ms 42292 KB Output is correct
24 Correct 81 ms 42320 KB Output is correct
25 Correct 85 ms 42320 KB Output is correct
26 Correct 87 ms 42488 KB Output is correct
27 Correct 85 ms 42264 KB Output is correct
28 Correct 87 ms 42404 KB Output is correct
29 Correct 79 ms 42320 KB Output is correct
30 Correct 86 ms 42312 KB Output is correct
31 Correct 189 ms 34508 KB Output is correct
32 Correct 79 ms 42152 KB Output is correct
33 Correct 263 ms 39348 KB Output is correct
34 Correct 157 ms 34968 KB Output is correct
35 Correct 177 ms 38800 KB Output is correct
36 Correct 129 ms 35336 KB Output is correct
37 Correct 313 ms 38416 KB Output is correct
38 Correct 242 ms 34388 KB Output is correct
39 Correct 79 ms 42068 KB Output is correct
40 Correct 214 ms 37508 KB Output is correct
41 Correct 7 ms 25180 KB Output is correct
42 Correct 5 ms 25180 KB Output is correct
43 Correct 6 ms 25180 KB Output is correct
44 Correct 6 ms 25244 KB Output is correct
45 Correct 6 ms 25180 KB Output is correct
46 Correct 6 ms 25244 KB Output is correct
47 Correct 6 ms 25180 KB Output is correct
48 Correct 5 ms 25180 KB Output is correct
49 Correct 6 ms 25180 KB Output is correct
50 Correct 6 ms 25388 KB Output is correct
51 Correct 187 ms 34676 KB Output is correct
52 Correct 92 ms 42372 KB Output is correct
53 Correct 206 ms 37892 KB Output is correct
54 Correct 107 ms 34980 KB Output is correct
55 Correct 283 ms 34568 KB Output is correct
56 Correct 86 ms 42320 KB Output is correct
57 Correct 195 ms 38544 KB Output is correct
58 Correct 154 ms 35008 KB Output is correct
59 Correct 204 ms 34784 KB Output is correct
60 Correct 88 ms 42360 KB Output is correct
61 Correct 270 ms 38544 KB Output is correct
62 Correct 165 ms 35260 KB Output is correct
63 Correct 206 ms 34624 KB Output is correct
64 Correct 90 ms 42324 KB Output is correct
65 Correct 205 ms 38404 KB Output is correct
66 Correct 126 ms 34780 KB Output is correct
67 Correct 171 ms 35292 KB Output is correct
68 Correct 97 ms 42420 KB Output is correct
69 Correct 180 ms 37444 KB Output is correct
70 Correct 143 ms 35196 KB Output is correct