Submission #71052

# Submission time Handle Problem Language Result Execution time Memory
71052 2018-08-24T04:47:24 Z MatheusLealV Election Campaign (JOI15_election_campaign) C++17
100 / 100
662 ms 40028 KB
#include <bits/stdc++.h>
#define N 100050
#define l (2*nod)
#define r (2*nod + 1)
#define mid ((a + b)/2)
#define logn 20
using namespace std;

struct qr{
	int a, b, c;
};

vector<qr> Q[N];

int n, q, anc[N][logn], deep[N], ini[N], fim[N], t;

struct T
{
	int tree[4*N], lazy[4*N];

	void clear()
	{
		memset(tree, 0, sizeof tree);

		memset(lazy, 0, sizeof lazy);
	}

	inline void prop(int nod, int a, int b)
	{
		if(!lazy[nod]) return;

		tree[nod] += (b - a + 1)*lazy[nod];

		if(a != b)
		{
			lazy[l] += lazy[nod];

			lazy[r] += lazy[nod];
		}

		lazy[nod] = 0;
	}

	void upd(int nod, int a, int b, int i, int j, int x)
	{
		prop(nod, a, b);

		if(j < a or i > b) return;

		if(i <= a and j >= b)
		{
			tree[nod] += (b - a + 1)*x;

			if(a != b)
			{
				lazy[l] += x;

				lazy[r] += x;
			}

			return;
		}

		upd(l, a, mid, i, j, x), upd(r, mid + 1, b, i, j, x);

		tree[nod] = tree[l] + tree[r];
	}

	int query(int nod, int a, int b, int i, int j)
	{
		prop(nod, a, b);

		if(j < a or i > b) return 0;

		if(i <= a and j >= b) return tree[nod];

		return query(l, a, mid, i, j) + query(r, mid + 1, b, i, j);
	}

} Tree[2];

vector<int> grafo[N];

inline int LCA(int u, int v)
{
	if(deep[u] < deep[v]) swap(u, v);

	for(int i = logn - 1; i >= 0; i--)
		if(deep[u] - (1<<i) >= deep[v]) u = anc[u][i];

	if(u == v) return u;

	for(int i = logn - 1; i >= 0; i--)
		if(anc[u][i] != -1 and anc[v][i] != -1 and anc[u][i] != anc[v][i])
			u = anc[u][i], v = anc[v][i];

	return anc[u][0];
}

void dfs(int x, int p)
{
	anc[x][0] = p;

	ini[x] = ++t;

	for(auto v: grafo[x])
	{
		if(v == p) continue;

		deep[v] = deep[x] + 1;

		dfs(v, x);
	}

	fim[x] = t;
}

int dp[N][2], ok[N][2];

int solve(int x, int f)
{
	ok[x][f] = 1;

	if(dp[x][f] != -1) return dp[x][f];

	int soma = 0;

	for(auto v: grafo[x])
	{
		if(v == anc[x][0]) continue;

		soma += max(solve(v, 1), solve(v, 0));
	}

	dp[x][f] = soma;

	if(!f)
	{
		for(auto q: Q[x])
		{
			int a = q.a, b = q.b, c = q.c;

			int A = Tree[0].query(1, 1, n, ini[x], ini[x]);

			int B = Tree[1].query(1, 1, n, ini[x], ini[x]);

			int somamax = Tree[0].query(1, 1, n, ini[a], ini[a]) - A;

			int soma1 = Tree[1].query(1, 1, n, ini[a], ini[a]) - B;

			somamax += Tree[0].query(1, 1, n, ini[b], ini[b]) - A;

			soma1 += Tree[1].query(1, 1, n, ini[b], ini[b]) - B;

			int ans = soma + c - somamax + soma1;

			dp[x][f] = max(dp[x][f], ans);
		}
	}

	if(f) Tree[1].upd(1, 1, n, ini[x], fim[x], dp[x][1]);

	else Tree[0].upd(1, 1, n, ini[x], fim[x], max(dp[x][0], dp[x][1]));

	return dp[x][f];
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(0);

	cin>>n;

	Tree[0].clear(), Tree[1].clear();

	for(int i = 1, a, b; i < n; i++)
	{
		cin>>a>>b;

		grafo[a].push_back(b);

		grafo[b].push_back(a);
	}

	memset(anc, -1, sizeof anc);

	dfs(1, 1);

	for(int j = 1; j < logn; j++)
		for(int i = 1; i <= n; i++) anc[i][j] = anc[anc[i][j - 1]][j - 1];

	cin>>q;

	for(int i = 1, a, b, c; i <= q; i++)
	{
		cin>>a>>b>>c;

		int L = LCA(a, b);

		Q[L].push_back({a, b, c});
	}

	memset(dp, -1, sizeof dp);

	cout<<solve(1, 0)<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 18 ms 19964 KB Output is correct
2 Correct 18 ms 19964 KB Output is correct
3 Correct 21 ms 20020 KB Output is correct
4 Correct 21 ms 20052 KB Output is correct
5 Correct 207 ms 25472 KB Output is correct
6 Correct 145 ms 37740 KB Output is correct
7 Correct 255 ms 37740 KB Output is correct
8 Correct 205 ms 37740 KB Output is correct
9 Correct 276 ms 37740 KB Output is correct
10 Correct 218 ms 37740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 37740 KB Output is correct
2 Correct 19 ms 37740 KB Output is correct
3 Correct 22 ms 37740 KB Output is correct
4 Correct 460 ms 39688 KB Output is correct
5 Correct 473 ms 39844 KB Output is correct
6 Correct 332 ms 39956 KB Output is correct
7 Correct 387 ms 39956 KB Output is correct
8 Correct 396 ms 39956 KB Output is correct
9 Correct 305 ms 39956 KB Output is correct
10 Correct 466 ms 39956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 37740 KB Output is correct
2 Correct 19 ms 37740 KB Output is correct
3 Correct 22 ms 37740 KB Output is correct
4 Correct 460 ms 39688 KB Output is correct
5 Correct 473 ms 39844 KB Output is correct
6 Correct 332 ms 39956 KB Output is correct
7 Correct 387 ms 39956 KB Output is correct
8 Correct 396 ms 39956 KB Output is correct
9 Correct 305 ms 39956 KB Output is correct
10 Correct 466 ms 39956 KB Output is correct
11 Correct 57 ms 39956 KB Output is correct
12 Correct 369 ms 39956 KB Output is correct
13 Correct 378 ms 39956 KB Output is correct
14 Correct 362 ms 39956 KB Output is correct
15 Correct 451 ms 39956 KB Output is correct
16 Correct 393 ms 39956 KB Output is correct
17 Correct 407 ms 40028 KB Output is correct
18 Correct 398 ms 40028 KB Output is correct
19 Correct 374 ms 40028 KB Output is correct
20 Correct 433 ms 40028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 504 ms 40028 KB Output is correct
2 Correct 386 ms 40028 KB Output is correct
3 Correct 645 ms 40028 KB Output is correct
4 Correct 392 ms 40028 KB Output is correct
5 Correct 441 ms 40028 KB Output is correct
6 Correct 452 ms 40028 KB Output is correct
7 Correct 662 ms 40028 KB Output is correct
8 Correct 505 ms 40028 KB Output is correct
9 Correct 352 ms 40028 KB Output is correct
10 Correct 602 ms 40028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 19964 KB Output is correct
2 Correct 18 ms 19964 KB Output is correct
3 Correct 21 ms 20020 KB Output is correct
4 Correct 21 ms 20052 KB Output is correct
5 Correct 207 ms 25472 KB Output is correct
6 Correct 145 ms 37740 KB Output is correct
7 Correct 255 ms 37740 KB Output is correct
8 Correct 205 ms 37740 KB Output is correct
9 Correct 276 ms 37740 KB Output is correct
10 Correct 218 ms 37740 KB Output is correct
11 Correct 22 ms 40028 KB Output is correct
12 Correct 24 ms 40028 KB Output is correct
13 Correct 24 ms 40028 KB Output is correct
14 Correct 21 ms 40028 KB Output is correct
15 Correct 21 ms 40028 KB Output is correct
16 Correct 27 ms 40028 KB Output is correct
17 Correct 21 ms 40028 KB Output is correct
18 Correct 21 ms 40028 KB Output is correct
19 Correct 22 ms 40028 KB Output is correct
20 Correct 20 ms 40028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 19964 KB Output is correct
2 Correct 18 ms 19964 KB Output is correct
3 Correct 21 ms 20020 KB Output is correct
4 Correct 21 ms 20052 KB Output is correct
5 Correct 207 ms 25472 KB Output is correct
6 Correct 145 ms 37740 KB Output is correct
7 Correct 255 ms 37740 KB Output is correct
8 Correct 205 ms 37740 KB Output is correct
9 Correct 276 ms 37740 KB Output is correct
10 Correct 218 ms 37740 KB Output is correct
11 Correct 19 ms 37740 KB Output is correct
12 Correct 19 ms 37740 KB Output is correct
13 Correct 22 ms 37740 KB Output is correct
14 Correct 460 ms 39688 KB Output is correct
15 Correct 473 ms 39844 KB Output is correct
16 Correct 332 ms 39956 KB Output is correct
17 Correct 387 ms 39956 KB Output is correct
18 Correct 396 ms 39956 KB Output is correct
19 Correct 305 ms 39956 KB Output is correct
20 Correct 466 ms 39956 KB Output is correct
21 Correct 57 ms 39956 KB Output is correct
22 Correct 369 ms 39956 KB Output is correct
23 Correct 378 ms 39956 KB Output is correct
24 Correct 362 ms 39956 KB Output is correct
25 Correct 451 ms 39956 KB Output is correct
26 Correct 393 ms 39956 KB Output is correct
27 Correct 407 ms 40028 KB Output is correct
28 Correct 398 ms 40028 KB Output is correct
29 Correct 374 ms 40028 KB Output is correct
30 Correct 433 ms 40028 KB Output is correct
31 Correct 504 ms 40028 KB Output is correct
32 Correct 386 ms 40028 KB Output is correct
33 Correct 645 ms 40028 KB Output is correct
34 Correct 392 ms 40028 KB Output is correct
35 Correct 441 ms 40028 KB Output is correct
36 Correct 452 ms 40028 KB Output is correct
37 Correct 662 ms 40028 KB Output is correct
38 Correct 505 ms 40028 KB Output is correct
39 Correct 352 ms 40028 KB Output is correct
40 Correct 602 ms 40028 KB Output is correct
41 Correct 22 ms 40028 KB Output is correct
42 Correct 24 ms 40028 KB Output is correct
43 Correct 24 ms 40028 KB Output is correct
44 Correct 21 ms 40028 KB Output is correct
45 Correct 21 ms 40028 KB Output is correct
46 Correct 27 ms 40028 KB Output is correct
47 Correct 21 ms 40028 KB Output is correct
48 Correct 21 ms 40028 KB Output is correct
49 Correct 22 ms 40028 KB Output is correct
50 Correct 20 ms 40028 KB Output is correct
51 Correct 406 ms 40028 KB Output is correct
52 Correct 428 ms 40028 KB Output is correct
53 Correct 626 ms 40028 KB Output is correct
54 Correct 423 ms 40028 KB Output is correct
55 Correct 524 ms 40028 KB Output is correct
56 Correct 374 ms 40028 KB Output is correct
57 Correct 504 ms 40028 KB Output is correct
58 Correct 483 ms 40028 KB Output is correct
59 Correct 452 ms 40028 KB Output is correct
60 Correct 390 ms 40028 KB Output is correct
61 Correct 473 ms 40028 KB Output is correct
62 Correct 399 ms 40028 KB Output is correct
63 Correct 474 ms 40028 KB Output is correct
64 Correct 358 ms 40028 KB Output is correct
65 Correct 540 ms 40028 KB Output is correct
66 Correct 355 ms 40028 KB Output is correct
67 Correct 441 ms 40028 KB Output is correct
68 Correct 416 ms 40028 KB Output is correct
69 Correct 477 ms 40028 KB Output is correct
70 Correct 407 ms 40028 KB Output is correct