Submission #519543

# Submission time Handle Problem Language Result Execution time Memory
519543 2022-01-26T15:31:09 Z valerikk Election Campaign (JOI15_election_campaign) C++17
100 / 100
174 ms 52648 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 5e5 + 10;
const int K = 20;

struct {
	ll t[(int) 1e6 + 10];

	void upd(int ind, ll val) {
		for (int i = ind + 1; i < N; i += i & -i) {
			t[i] += val;
		}
	}

	void upd(int l, int r, ll val) {
		upd(l, val);
		upd(r, -val);
	}

	ll get(int ind) {
		ll sum = 0;
		for (int i = ind + 1; i > 0; i -= i & -i) {
			sum += t[i];
		}
		return sum;
	}
} fsum, fdp;

int n;
vector<int> g[N];
int m;
int a[N], b[N];
ll c[N];

int tin[N], tout[N];
int par[N];
int up[K][N];

void dfs(int v, int p) {
	static int t = 0;
	tin[v] = t++;
	par[v] = p;

	if (p != v) {
		g[v].erase(find(g[v].begin(), g[v].end(), p));
	}

	for (int u : g[v]) {
		dfs(u, v);
	}
	
	tout[v] = t;
}

bool anc(int v, int u) {
	return tin[v] <= tin[u] && tout[v] >= tout[u];
}

int getlca(int v, int u) {
	if (anc(v, u)) {
		return v;
	}
	if (anc(u, v)) {
		return u;
	}

	for (int i = K - 1; i >= 0; i--) {
		if (!anc(up[i][v], u)) {
			v = up[i][v];
		}
	}
	return par[v];
}

vector<int> by_lca[N];
ll dp[N];
ll sum[N];

void dfsdp(int v) {
	for (int u : g[v]) {
		dfsdp(u);
		sum[v] += dp[u];
	}

	dp[v] = sum[v];
	for (int i : by_lca[v]) {
		ll cur = fsum.get(tin[a[i]]) + fsum.get(tin[b[i]]) + sum[v] - fdp.get(tin[a[i]]) - fdp.get(tin[b[i]]) + c[i];
		dp[v] = max(dp[v], cur);
	}

	fsum.upd(tin[v], tout[v], sum[v]);
	fdp.upd(tin[v], tout[v], dp[v]);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for (int i = 0; i < n - 1; i++) {
		int x, y;
		cin >> x >> y;
		x--;
		y--;
		g[x].push_back(y);
		g[y].push_back(x);
	}

	cin >> m;
	for (int i = 0; i < m; i++) {
		cin >> a[i] >> b[i] >> c[i];
		a[i]--;
		b[i]--;
	}

	dfs(0, 0);

	for (int i = 0; i < n; i++) {
		up[0][i] = par[i];
	}
	for (int i = 1; i < K; i++) {
		for (int j = 0; j < n; j++) {
			up[i][j] = up[i - 1][up[i - 1][j]];
		}
	}

	for (int i = 0; i < m; i++) {
		by_lca[getlca(a[i], b[i])].push_back(i);
	}

	dfsdp(0);

	cout << dp[0] << "\n";

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 24012 KB Output is correct
2 Correct 14 ms 24076 KB Output is correct
3 Correct 13 ms 24012 KB Output is correct
4 Correct 13 ms 24268 KB Output is correct
5 Correct 85 ms 39804 KB Output is correct
6 Correct 65 ms 47288 KB Output is correct
7 Correct 72 ms 44684 KB Output is correct
8 Correct 65 ms 39228 KB Output is correct
9 Correct 78 ms 43084 KB Output is correct
10 Correct 60 ms 39328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 24012 KB Output is correct
2 Correct 13 ms 24068 KB Output is correct
3 Correct 13 ms 24336 KB Output is correct
4 Correct 98 ms 50184 KB Output is correct
5 Correct 93 ms 50144 KB Output is correct
6 Correct 85 ms 50120 KB Output is correct
7 Correct 91 ms 50120 KB Output is correct
8 Correct 106 ms 50160 KB Output is correct
9 Correct 88 ms 50176 KB Output is correct
10 Correct 97 ms 50168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 24012 KB Output is correct
2 Correct 13 ms 24068 KB Output is correct
3 Correct 13 ms 24336 KB Output is correct
4 Correct 98 ms 50184 KB Output is correct
5 Correct 93 ms 50144 KB Output is correct
6 Correct 85 ms 50120 KB Output is correct
7 Correct 91 ms 50120 KB Output is correct
8 Correct 106 ms 50160 KB Output is correct
9 Correct 88 ms 50176 KB Output is correct
10 Correct 97 ms 50168 KB Output is correct
11 Correct 20 ms 25024 KB Output is correct
12 Correct 93 ms 50108 KB Output is correct
13 Correct 105 ms 52516 KB Output is correct
14 Correct 89 ms 52564 KB Output is correct
15 Correct 101 ms 52516 KB Output is correct
16 Correct 108 ms 52548 KB Output is correct
17 Correct 106 ms 52440 KB Output is correct
18 Correct 93 ms 52512 KB Output is correct
19 Correct 91 ms 52456 KB Output is correct
20 Correct 100 ms 52520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 146 ms 41900 KB Output is correct
2 Correct 103 ms 50204 KB Output is correct
3 Correct 160 ms 47016 KB Output is correct
4 Correct 136 ms 41428 KB Output is correct
5 Correct 148 ms 46732 KB Output is correct
6 Correct 131 ms 41600 KB Output is correct
7 Correct 174 ms 46416 KB Output is correct
8 Correct 146 ms 42304 KB Output is correct
9 Correct 88 ms 50120 KB Output is correct
10 Correct 162 ms 45504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 24012 KB Output is correct
2 Correct 14 ms 24076 KB Output is correct
3 Correct 13 ms 24012 KB Output is correct
4 Correct 13 ms 24268 KB Output is correct
5 Correct 85 ms 39804 KB Output is correct
6 Correct 65 ms 47288 KB Output is correct
7 Correct 72 ms 44684 KB Output is correct
8 Correct 65 ms 39228 KB Output is correct
9 Correct 78 ms 43084 KB Output is correct
10 Correct 60 ms 39328 KB Output is correct
11 Correct 13 ms 24268 KB Output is correct
12 Correct 13 ms 24320 KB Output is correct
13 Correct 13 ms 24344 KB Output is correct
14 Correct 13 ms 24268 KB Output is correct
15 Correct 15 ms 24244 KB Output is correct
16 Correct 13 ms 24248 KB Output is correct
17 Correct 13 ms 24304 KB Output is correct
18 Correct 15 ms 24320 KB Output is correct
19 Correct 13 ms 24228 KB Output is correct
20 Correct 13 ms 24360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 24012 KB Output is correct
2 Correct 14 ms 24076 KB Output is correct
3 Correct 13 ms 24012 KB Output is correct
4 Correct 13 ms 24268 KB Output is correct
5 Correct 85 ms 39804 KB Output is correct
6 Correct 65 ms 47288 KB Output is correct
7 Correct 72 ms 44684 KB Output is correct
8 Correct 65 ms 39228 KB Output is correct
9 Correct 78 ms 43084 KB Output is correct
10 Correct 60 ms 39328 KB Output is correct
11 Correct 13 ms 24012 KB Output is correct
12 Correct 13 ms 24068 KB Output is correct
13 Correct 13 ms 24336 KB Output is correct
14 Correct 98 ms 50184 KB Output is correct
15 Correct 93 ms 50144 KB Output is correct
16 Correct 85 ms 50120 KB Output is correct
17 Correct 91 ms 50120 KB Output is correct
18 Correct 106 ms 50160 KB Output is correct
19 Correct 88 ms 50176 KB Output is correct
20 Correct 97 ms 50168 KB Output is correct
21 Correct 20 ms 25024 KB Output is correct
22 Correct 93 ms 50108 KB Output is correct
23 Correct 105 ms 52516 KB Output is correct
24 Correct 89 ms 52564 KB Output is correct
25 Correct 101 ms 52516 KB Output is correct
26 Correct 108 ms 52548 KB Output is correct
27 Correct 106 ms 52440 KB Output is correct
28 Correct 93 ms 52512 KB Output is correct
29 Correct 91 ms 52456 KB Output is correct
30 Correct 100 ms 52520 KB Output is correct
31 Correct 146 ms 41900 KB Output is correct
32 Correct 103 ms 50204 KB Output is correct
33 Correct 160 ms 47016 KB Output is correct
34 Correct 136 ms 41428 KB Output is correct
35 Correct 148 ms 46732 KB Output is correct
36 Correct 131 ms 41600 KB Output is correct
37 Correct 174 ms 46416 KB Output is correct
38 Correct 146 ms 42304 KB Output is correct
39 Correct 88 ms 50120 KB Output is correct
40 Correct 162 ms 45504 KB Output is correct
41 Correct 13 ms 24268 KB Output is correct
42 Correct 13 ms 24320 KB Output is correct
43 Correct 13 ms 24344 KB Output is correct
44 Correct 13 ms 24268 KB Output is correct
45 Correct 15 ms 24244 KB Output is correct
46 Correct 13 ms 24248 KB Output is correct
47 Correct 13 ms 24304 KB Output is correct
48 Correct 15 ms 24320 KB Output is correct
49 Correct 13 ms 24228 KB Output is correct
50 Correct 13 ms 24360 KB Output is correct
51 Correct 134 ms 44636 KB Output is correct
52 Correct 93 ms 52648 KB Output is correct
53 Correct 162 ms 47860 KB Output is correct
54 Correct 104 ms 43916 KB Output is correct
55 Correct 160 ms 44248 KB Output is correct
56 Correct 94 ms 52420 KB Output is correct
57 Correct 144 ms 48776 KB Output is correct
58 Correct 129 ms 43844 KB Output is correct
59 Correct 163 ms 44792 KB Output is correct
60 Correct 96 ms 52464 KB Output is correct
61 Correct 137 ms 48720 KB Output is correct
62 Correct 130 ms 43856 KB Output is correct
63 Correct 145 ms 44272 KB Output is correct
64 Correct 96 ms 52476 KB Output is correct
65 Correct 171 ms 48524 KB Output is correct
66 Correct 109 ms 43992 KB Output is correct
67 Correct 147 ms 44372 KB Output is correct
68 Correct 96 ms 52524 KB Output is correct
69 Correct 167 ms 47552 KB Output is correct
70 Correct 125 ms 43904 KB Output is correct