답안 #71048

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
71048 2018-08-24T04:45:25 Z MatheusLealV Election Campaign (JOI15_election_campaign) C++17
100 / 100
708 ms 43556 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];

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 nivel = deep[x], u = a;

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

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

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

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

			/*while(u != x)
			{
				soma1 += dp[u][1];

				somamax += max(dp[u][1], dp[u][0]); 

				u = anc[u][0];
			}

			u = b;

			while(u != x)
			{
				soma1 += dp[u][1];

				somamax += max(dp[u][1], dp[u][0]); 

				u = anc[u][0];
			}*/

			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";
}

Compilation message

election_campaign.cpp: In function 'int solve(int, int)':
election_campaign.cpp:143:8: warning: unused variable 'nivel' [-Wunused-variable]
    int nivel = deep[x], u = a;
        ^~~~~
election_campaign.cpp:143:25: warning: unused variable 'u' [-Wunused-variable]
    int nivel = deep[x], u = a;
                         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 19960 KB Output is correct
2 Correct 18 ms 19960 KB Output is correct
3 Correct 18 ms 20124 KB Output is correct
4 Correct 18 ms 20124 KB Output is correct
5 Correct 200 ms 26420 KB Output is correct
6 Correct 123 ms 40076 KB Output is correct
7 Correct 294 ms 40076 KB Output is correct
8 Correct 166 ms 40076 KB Output is correct
9 Correct 215 ms 40076 KB Output is correct
10 Correct 172 ms 40076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 40076 KB Output is correct
2 Correct 20 ms 40076 KB Output is correct
3 Correct 19 ms 40076 KB Output is correct
4 Correct 423 ms 42556 KB Output is correct
5 Correct 373 ms 43376 KB Output is correct
6 Correct 314 ms 43456 KB Output is correct
7 Correct 418 ms 43456 KB Output is correct
8 Correct 472 ms 43540 KB Output is correct
9 Correct 356 ms 43540 KB Output is correct
10 Correct 329 ms 43540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 40076 KB Output is correct
2 Correct 20 ms 40076 KB Output is correct
3 Correct 19 ms 40076 KB Output is correct
4 Correct 423 ms 42556 KB Output is correct
5 Correct 373 ms 43376 KB Output is correct
6 Correct 314 ms 43456 KB Output is correct
7 Correct 418 ms 43456 KB Output is correct
8 Correct 472 ms 43540 KB Output is correct
9 Correct 356 ms 43540 KB Output is correct
10 Correct 329 ms 43540 KB Output is correct
11 Correct 54 ms 43540 KB Output is correct
12 Correct 377 ms 43540 KB Output is correct
13 Correct 335 ms 43540 KB Output is correct
14 Correct 430 ms 43540 KB Output is correct
15 Correct 431 ms 43556 KB Output is correct
16 Correct 304 ms 43556 KB Output is correct
17 Correct 410 ms 43556 KB Output is correct
18 Correct 389 ms 43556 KB Output is correct
19 Correct 309 ms 43556 KB Output is correct
20 Correct 337 ms 43556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 418 ms 43556 KB Output is correct
2 Correct 341 ms 43556 KB Output is correct
3 Correct 616 ms 43556 KB Output is correct
4 Correct 423 ms 43556 KB Output is correct
5 Correct 477 ms 43556 KB Output is correct
6 Correct 477 ms 43556 KB Output is correct
7 Correct 708 ms 43556 KB Output is correct
8 Correct 476 ms 43556 KB Output is correct
9 Correct 451 ms 43556 KB Output is correct
10 Correct 663 ms 43556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 19960 KB Output is correct
2 Correct 18 ms 19960 KB Output is correct
3 Correct 18 ms 20124 KB Output is correct
4 Correct 18 ms 20124 KB Output is correct
5 Correct 200 ms 26420 KB Output is correct
6 Correct 123 ms 40076 KB Output is correct
7 Correct 294 ms 40076 KB Output is correct
8 Correct 166 ms 40076 KB Output is correct
9 Correct 215 ms 40076 KB Output is correct
10 Correct 172 ms 40076 KB Output is correct
11 Correct 22 ms 43556 KB Output is correct
12 Correct 20 ms 43556 KB Output is correct
13 Correct 23 ms 43556 KB Output is correct
14 Correct 23 ms 43556 KB Output is correct
15 Correct 21 ms 43556 KB Output is correct
16 Correct 20 ms 43556 KB Output is correct
17 Correct 19 ms 43556 KB Output is correct
18 Correct 26 ms 43556 KB Output is correct
19 Correct 22 ms 43556 KB Output is correct
20 Correct 23 ms 43556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 19960 KB Output is correct
2 Correct 18 ms 19960 KB Output is correct
3 Correct 18 ms 20124 KB Output is correct
4 Correct 18 ms 20124 KB Output is correct
5 Correct 200 ms 26420 KB Output is correct
6 Correct 123 ms 40076 KB Output is correct
7 Correct 294 ms 40076 KB Output is correct
8 Correct 166 ms 40076 KB Output is correct
9 Correct 215 ms 40076 KB Output is correct
10 Correct 172 ms 40076 KB Output is correct
11 Correct 18 ms 40076 KB Output is correct
12 Correct 20 ms 40076 KB Output is correct
13 Correct 19 ms 40076 KB Output is correct
14 Correct 423 ms 42556 KB Output is correct
15 Correct 373 ms 43376 KB Output is correct
16 Correct 314 ms 43456 KB Output is correct
17 Correct 418 ms 43456 KB Output is correct
18 Correct 472 ms 43540 KB Output is correct
19 Correct 356 ms 43540 KB Output is correct
20 Correct 329 ms 43540 KB Output is correct
21 Correct 54 ms 43540 KB Output is correct
22 Correct 377 ms 43540 KB Output is correct
23 Correct 335 ms 43540 KB Output is correct
24 Correct 430 ms 43540 KB Output is correct
25 Correct 431 ms 43556 KB Output is correct
26 Correct 304 ms 43556 KB Output is correct
27 Correct 410 ms 43556 KB Output is correct
28 Correct 389 ms 43556 KB Output is correct
29 Correct 309 ms 43556 KB Output is correct
30 Correct 337 ms 43556 KB Output is correct
31 Correct 418 ms 43556 KB Output is correct
32 Correct 341 ms 43556 KB Output is correct
33 Correct 616 ms 43556 KB Output is correct
34 Correct 423 ms 43556 KB Output is correct
35 Correct 477 ms 43556 KB Output is correct
36 Correct 477 ms 43556 KB Output is correct
37 Correct 708 ms 43556 KB Output is correct
38 Correct 476 ms 43556 KB Output is correct
39 Correct 451 ms 43556 KB Output is correct
40 Correct 663 ms 43556 KB Output is correct
41 Correct 22 ms 43556 KB Output is correct
42 Correct 20 ms 43556 KB Output is correct
43 Correct 23 ms 43556 KB Output is correct
44 Correct 23 ms 43556 KB Output is correct
45 Correct 21 ms 43556 KB Output is correct
46 Correct 20 ms 43556 KB Output is correct
47 Correct 19 ms 43556 KB Output is correct
48 Correct 26 ms 43556 KB Output is correct
49 Correct 22 ms 43556 KB Output is correct
50 Correct 23 ms 43556 KB Output is correct
51 Correct 413 ms 43556 KB Output is correct
52 Correct 429 ms 43556 KB Output is correct
53 Correct 525 ms 43556 KB Output is correct
54 Correct 337 ms 43556 KB Output is correct
55 Correct 489 ms 43556 KB Output is correct
56 Correct 442 ms 43556 KB Output is correct
57 Correct 500 ms 43556 KB Output is correct
58 Correct 440 ms 43556 KB Output is correct
59 Correct 420 ms 43556 KB Output is correct
60 Correct 487 ms 43556 KB Output is correct
61 Correct 553 ms 43556 KB Output is correct
62 Correct 604 ms 43556 KB Output is correct
63 Correct 510 ms 43556 KB Output is correct
64 Correct 511 ms 43556 KB Output is correct
65 Correct 555 ms 43556 KB Output is correct
66 Correct 343 ms 43556 KB Output is correct
67 Correct 427 ms 43556 KB Output is correct
68 Correct 382 ms 43556 KB Output is correct
69 Correct 546 ms 43556 KB Output is correct
70 Correct 496 ms 43556 KB Output is correct