답안 #296055

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
296055 2020-09-10T08:25:50 Z Berted Election Campaign (JOI15_election_campaign) C++14
100 / 100
259 ms 28536 KB
#include <iostream>
#include <vector>
#define ll long long
#define pii pair<int, int>
#define ppi pair<pii, int>
#define fst first
#define snd second
using namespace std;

const int LG = 17;

struct BIT
{
	int N; ll A[100001];
	BIT() {}
	BIT(int size) {N = size;}
	void init(int size) {N = size;}
	void update(int x, ll val)
	{
		for (; x <= N; x += x & (-x)) {A[x] += val;}
	}
	ll query(int x)
	{
		ll ret = 0;
		for (; x; x -= x & (-x)) {ret += A[x];}
		return ret;
	}
};

int N, Q, h[100001], L[100001], R[100001], sps[100001][LG], idx = 1;
BIT D1, D2;
ll DP[100001];
vector<int> adj[100001];
vector<ppi> solve[100001];

void prep(int u, int p)
{
	h[u] = (p == -1) ? 0 : (h[p] + 1); L[u] = R[u] = idx++;
	sps[u][0] = p;
	for (int i = 1; i < LG; i++)
	{
		if (sps[u][i - 1] != -1) sps[u][i] = sps[sps[u][i - 1]][i - 1];
		else sps[u][i] = -1;
	}
	for (auto v : adj[u])
	{
		if (v != p)
		{
			prep(v, u);
			R[u] = max(R[u], R[v]);
		}
	}
}

inline int getLCA(int u, int v)
{
	if (h[u] > h[v]) {swap(u, v);}
	for (int i = 0; h[v] - h[u]; i++)
	{
		if ((h[v] - h[u]) & (1 << i)) {v = sps[v][i];}
	}
	if (u == v) {return u;}
	for (int i = LG - 1; i >= 0; i--)
	{
		if (sps[u][i] != sps[v][i]) {u = sps[u][i]; v = sps[v][i];}
	}
	return sps[u][0];
}

void DFS(int u, int p)
{
	ll childSum = 0;
	for (auto v : adj[u])
	{
		if (v != p) {DFS(v, u); childSum += DP[v];}
	}

	DP[u] = childSum;
	for (auto v : solve[u])
	{
		ll v1 = D1.query(L[v.fst.fst]) + D1.query(L[v.fst.snd]);
		ll v2 = D2.query(L[v.fst.fst]) + D2.query(L[v.fst.snd]);
		DP[u] = max(DP[u], v1 - v2 + childSum + v.snd);
	}

	D1.update(L[u], childSum);
	D1.update(R[u] + 1, -childSum);
	D2.update(L[u], DP[u]);
	D2.update(R[u] + 1, -DP[u]);
}

int main()
{
	ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> N; D1.init(N); D2.init(N);
	for (int i = 1; i < N; i++)
	{
		int u, v; cin >> u >> v;
		adj[u - 1].push_back(v - 1);
		adj[v - 1].push_back(u - 1);
	}
	prep(0, -1);
	cin >> Q;
	for (int i = 0; i < Q; i++)
	{
		int u, v, w; cin >> u >> v >> w; u--; v--;
		solve[getLCA(u, v)].push_back({{u, v}, w});
	}
	DFS(0, -1);
	cout << DP[0] << "\n";
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5248 KB Output is correct
5 Correct 121 ms 18808 KB Output is correct
6 Correct 59 ms 26488 KB Output is correct
7 Correct 125 ms 23672 KB Output is correct
8 Correct 84 ms 19064 KB Output is correct
9 Correct 116 ms 22136 KB Output is correct
10 Correct 84 ms 19064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5120 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5248 KB Output is correct
4 Correct 131 ms 28408 KB Output is correct
5 Correct 140 ms 28408 KB Output is correct
6 Correct 118 ms 28412 KB Output is correct
7 Correct 136 ms 28408 KB Output is correct
8 Correct 134 ms 28408 KB Output is correct
9 Correct 122 ms 28412 KB Output is correct
10 Correct 132 ms 28408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5120 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5248 KB Output is correct
4 Correct 131 ms 28408 KB Output is correct
5 Correct 140 ms 28408 KB Output is correct
6 Correct 118 ms 28412 KB Output is correct
7 Correct 136 ms 28408 KB Output is correct
8 Correct 134 ms 28408 KB Output is correct
9 Correct 122 ms 28412 KB Output is correct
10 Correct 132 ms 28408 KB Output is correct
11 Correct 14 ms 6016 KB Output is correct
12 Correct 137 ms 28444 KB Output is correct
13 Correct 137 ms 28408 KB Output is correct
14 Correct 121 ms 28408 KB Output is correct
15 Correct 140 ms 28408 KB Output is correct
16 Correct 122 ms 28408 KB Output is correct
17 Correct 142 ms 28408 KB Output is correct
18 Correct 143 ms 28416 KB Output is correct
19 Correct 122 ms 28408 KB Output is correct
20 Correct 142 ms 28408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 194 ms 20268 KB Output is correct
2 Correct 129 ms 28408 KB Output is correct
3 Correct 250 ms 25336 KB Output is correct
4 Correct 136 ms 20652 KB Output is correct
5 Correct 220 ms 24824 KB Output is correct
6 Correct 134 ms 20588 KB Output is correct
7 Correct 256 ms 24788 KB Output is correct
8 Correct 188 ms 20472 KB Output is correct
9 Correct 126 ms 28536 KB Output is correct
10 Correct 251 ms 23740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5248 KB Output is correct
5 Correct 121 ms 18808 KB Output is correct
6 Correct 59 ms 26488 KB Output is correct
7 Correct 125 ms 23672 KB Output is correct
8 Correct 84 ms 19064 KB Output is correct
9 Correct 116 ms 22136 KB Output is correct
10 Correct 84 ms 19064 KB Output is correct
11 Correct 5 ms 5248 KB Output is correct
12 Correct 5 ms 5376 KB Output is correct
13 Correct 5 ms 5248 KB Output is correct
14 Correct 6 ms 5248 KB Output is correct
15 Correct 5 ms 5248 KB Output is correct
16 Correct 5 ms 5248 KB Output is correct
17 Correct 5 ms 5248 KB Output is correct
18 Correct 5 ms 5248 KB Output is correct
19 Correct 4 ms 5248 KB Output is correct
20 Correct 5 ms 5376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5248 KB Output is correct
5 Correct 121 ms 18808 KB Output is correct
6 Correct 59 ms 26488 KB Output is correct
7 Correct 125 ms 23672 KB Output is correct
8 Correct 84 ms 19064 KB Output is correct
9 Correct 116 ms 22136 KB Output is correct
10 Correct 84 ms 19064 KB Output is correct
11 Correct 3 ms 5120 KB Output is correct
12 Correct 4 ms 5120 KB Output is correct
13 Correct 4 ms 5248 KB Output is correct
14 Correct 131 ms 28408 KB Output is correct
15 Correct 140 ms 28408 KB Output is correct
16 Correct 118 ms 28412 KB Output is correct
17 Correct 136 ms 28408 KB Output is correct
18 Correct 134 ms 28408 KB Output is correct
19 Correct 122 ms 28412 KB Output is correct
20 Correct 132 ms 28408 KB Output is correct
21 Correct 14 ms 6016 KB Output is correct
22 Correct 137 ms 28444 KB Output is correct
23 Correct 137 ms 28408 KB Output is correct
24 Correct 121 ms 28408 KB Output is correct
25 Correct 140 ms 28408 KB Output is correct
26 Correct 122 ms 28408 KB Output is correct
27 Correct 142 ms 28408 KB Output is correct
28 Correct 143 ms 28416 KB Output is correct
29 Correct 122 ms 28408 KB Output is correct
30 Correct 142 ms 28408 KB Output is correct
31 Correct 194 ms 20268 KB Output is correct
32 Correct 129 ms 28408 KB Output is correct
33 Correct 250 ms 25336 KB Output is correct
34 Correct 136 ms 20652 KB Output is correct
35 Correct 220 ms 24824 KB Output is correct
36 Correct 134 ms 20588 KB Output is correct
37 Correct 256 ms 24788 KB Output is correct
38 Correct 188 ms 20472 KB Output is correct
39 Correct 126 ms 28536 KB Output is correct
40 Correct 251 ms 23740 KB Output is correct
41 Correct 5 ms 5248 KB Output is correct
42 Correct 5 ms 5376 KB Output is correct
43 Correct 5 ms 5248 KB Output is correct
44 Correct 6 ms 5248 KB Output is correct
45 Correct 5 ms 5248 KB Output is correct
46 Correct 5 ms 5248 KB Output is correct
47 Correct 5 ms 5248 KB Output is correct
48 Correct 5 ms 5248 KB Output is correct
49 Correct 4 ms 5248 KB Output is correct
50 Correct 5 ms 5376 KB Output is correct
51 Correct 199 ms 20472 KB Output is correct
52 Correct 153 ms 28536 KB Output is correct
53 Correct 259 ms 23724 KB Output is correct
54 Correct 142 ms 20660 KB Output is correct
55 Correct 185 ms 20376 KB Output is correct
56 Correct 144 ms 28444 KB Output is correct
57 Correct 215 ms 24312 KB Output is correct
58 Correct 147 ms 20532 KB Output is correct
59 Correct 218 ms 20424 KB Output is correct
60 Correct 153 ms 28408 KB Output is correct
61 Correct 218 ms 24568 KB Output is correct
62 Correct 135 ms 20428 KB Output is correct
63 Correct 218 ms 20188 KB Output is correct
64 Correct 136 ms 28536 KB Output is correct
65 Correct 248 ms 24364 KB Output is correct
66 Correct 131 ms 20788 KB Output is correct
67 Correct 181 ms 20168 KB Output is correct
68 Correct 132 ms 28408 KB Output is correct
69 Correct 236 ms 23288 KB Output is correct
70 Correct 139 ms 20828 KB Output is correct