Submission #56846

# Submission time Handle Problem Language Result Execution time Memory
56846 2018-07-12T20:19:28 Z Mamnoon_Siam Election Campaign (JOI15_election_campaign) C++17
100 / 100
661 ms 40260 KB
//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
using namespace std;

#define debug(s) cout<< #s <<" = "<< s <<endl
#define all(v) (v).begin(), (v).end()
#define KeepUnique(v) (v).erase( unique(all(v)), v.end() )
#define MEMSET(a,val) memset(a, val, sizeof a)
#define PB push_back
#define endl '\n'
typedef long long ll;

inline int myrand(int l, int r) {
	int ret = rand(); ret <<= 15; ret ^= rand();
	if(ret < 0) ret = -ret; ret %= (r-l+1); ret += l;
	return ret;
}
template <typename F, typename S>
ostream& operator << (ostream& os, const pair< F, S>& p) {
    return os<<"(" <<p.first<<", "<<p.second<<")"; }

typedef pair<int, int> ii;
template<typename T> using min_pq =
	priority_queue<T, vector<T>, greater<T> >;

//int dx[] = {-1, +0, +1, +0};
//int dy[] = {+0, +1, +0, -1};

const int maxn = 100005;

int n, m;
vector<int> g[maxn];
int lvl[maxn];
vector<int> paths[maxn];
vector<int> A, B, C;
vector<int> shitz[maxn], pathnodes[maxn];
int t[18][maxn];
int dp[maxn];
int S[maxn];
int sz[maxn];
int tym = 0;
int where[maxn];

int dfs(int u, int par) {
	sz[u] = 1;
	where[u] = ++tym;
	t[0][u] = par;
	lvl[u] = par == -1 ? 0 : lvl[par] + 1;
	for(int v : g[u]) {
		if(par - v) {
			dfs(v, u);
			sz[u] += sz[v];
		}
	}
}

int __lca(int u, int v) {
	if(lvl[u] > lvl[v]) swap(u, v);
	int d = lvl[v] - lvl[u];
	for(int i = 16; i >= 0; i--) {
		if(d & (1 << i)) {
			v = t[i][v];
		}
	}
	if(u == v) return u;
	for(int i = 16; i >= 0; i--) {
		if(t[i][u] != t[i][v]) {
			u = t[i][u];
			v = t[i][v];
		}
	} return t[0][u];
}

void update(int u, int val) {
	int l = where[u], r = where[u] + sz[u];
	for(; l <= n; l += l & -l) S[l] += val;
	for(; r <= n; r += r & -r) S[r] -= val;
}
int query(int u, int ret = 0) {
	for(int p = where[u]; p > 0; p -= p & -p)
		ret += S[p];
	return ret;
}

int pathcost(int i) { // query path S[u] sum
	int u = A[i], v = B[i];
	int lca = __lca(u, v);
	return query(u) + query(v) - query(lca) - (lca == 1 ? 0 : query(t[0][lca]));
}

void prep(int u, int par) {
	for(int v : g[u]) {
		if(v - par) {
			prep(v, u);
			// S[u] += dp[v]; // update S[u]
			update(u, dp[v]);
		}
	}
	// dp[u] = S[u]; // access S[u]
	dp[u] = query(u) - (u != 1 ? query(t[0][u]) : 0);
	for(int p : paths[u]) {
		int ret = C[p] + pathcost(p);
		dp[u] = max(dp[u], ret);
	}
	// S[u] -= dp[u]; // update S[u]
	update(u, -dp[u]);
}

int32_t main () {
	// freopen("in", "r", stdin);
	cin >> n;
	for(int i = 1; i < n; i++) {
		int u, v; cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	MEMSET(t, -1);
	dfs(1, -1);
	for(int p = 1; p <= 16; p++) {
		for(int i = 1; i <= n; i++) {
			int mid = t[p - 1][i];
			if(mid != -1) t[p][i] = t[p - 1][mid];
		}
	}
	cin >> m;
	A.resize(m), B.resize(m), C.resize(m);
	for(int i = 0; i < m; i++) {
		cin >> A[i] >> B[i] >> C[i];
		int lca = __lca(A[i], B[i]);
		paths[lca].push_back(i);
	}
	prep(1, -1);
	cout << dp[1] << endl;
}

Compilation message

election_campaign.cpp: In function 'int myrand(int, int)':
election_campaign.cpp:17:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
  if(ret < 0) ret = -ret; ret %= (r-l+1); ret += l;
  ^~
election_campaign.cpp:17:26: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
  if(ret < 0) ret = -ret; ret %= (r-l+1); ret += l;
                          ^~~
election_campaign.cpp: In function 'int dfs(int, int)':
election_campaign.cpp:57:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
# Verdict Execution time Memory Grader output
1 Correct 17 ms 16760 KB Output is correct
2 Correct 19 ms 16760 KB Output is correct
3 Correct 17 ms 16904 KB Output is correct
4 Correct 20 ms 16972 KB Output is correct
5 Correct 258 ms 22172 KB Output is correct
6 Correct 134 ms 29864 KB Output is correct
7 Correct 160 ms 29864 KB Output is correct
8 Correct 148 ms 29864 KB Output is correct
9 Correct 261 ms 29864 KB Output is correct
10 Correct 207 ms 29864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 29864 KB Output is correct
2 Correct 17 ms 29864 KB Output is correct
3 Correct 21 ms 29864 KB Output is correct
4 Correct 433 ms 32260 KB Output is correct
5 Correct 366 ms 34624 KB Output is correct
6 Correct 432 ms 37088 KB Output is correct
7 Correct 382 ms 39532 KB Output is correct
8 Correct 371 ms 40164 KB Output is correct
9 Correct 343 ms 40164 KB Output is correct
10 Correct 424 ms 40260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 29864 KB Output is correct
2 Correct 17 ms 29864 KB Output is correct
3 Correct 21 ms 29864 KB Output is correct
4 Correct 433 ms 32260 KB Output is correct
5 Correct 366 ms 34624 KB Output is correct
6 Correct 432 ms 37088 KB Output is correct
7 Correct 382 ms 39532 KB Output is correct
8 Correct 371 ms 40164 KB Output is correct
9 Correct 343 ms 40164 KB Output is correct
10 Correct 424 ms 40260 KB Output is correct
11 Correct 65 ms 40260 KB Output is correct
12 Correct 458 ms 40260 KB Output is correct
13 Correct 377 ms 40260 KB Output is correct
14 Correct 347 ms 40260 KB Output is correct
15 Correct 381 ms 40260 KB Output is correct
16 Correct 384 ms 40260 KB Output is correct
17 Correct 385 ms 40260 KB Output is correct
18 Correct 452 ms 40260 KB Output is correct
19 Correct 323 ms 40260 KB Output is correct
20 Correct 396 ms 40260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 327 ms 40260 KB Output is correct
2 Correct 316 ms 40260 KB Output is correct
3 Correct 614 ms 40260 KB Output is correct
4 Correct 374 ms 40260 KB Output is correct
5 Correct 553 ms 40260 KB Output is correct
6 Correct 369 ms 40260 KB Output is correct
7 Correct 614 ms 40260 KB Output is correct
8 Correct 463 ms 40260 KB Output is correct
9 Correct 352 ms 40260 KB Output is correct
10 Correct 661 ms 40260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 16760 KB Output is correct
2 Correct 19 ms 16760 KB Output is correct
3 Correct 17 ms 16904 KB Output is correct
4 Correct 20 ms 16972 KB Output is correct
5 Correct 258 ms 22172 KB Output is correct
6 Correct 134 ms 29864 KB Output is correct
7 Correct 160 ms 29864 KB Output is correct
8 Correct 148 ms 29864 KB Output is correct
9 Correct 261 ms 29864 KB Output is correct
10 Correct 207 ms 29864 KB Output is correct
11 Correct 20 ms 40260 KB Output is correct
12 Correct 22 ms 40260 KB Output is correct
13 Correct 24 ms 40260 KB Output is correct
14 Correct 25 ms 40260 KB Output is correct
15 Correct 19 ms 40260 KB Output is correct
16 Correct 20 ms 40260 KB Output is correct
17 Correct 20 ms 40260 KB Output is correct
18 Correct 20 ms 40260 KB Output is correct
19 Correct 21 ms 40260 KB Output is correct
20 Correct 23 ms 40260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 16760 KB Output is correct
2 Correct 19 ms 16760 KB Output is correct
3 Correct 17 ms 16904 KB Output is correct
4 Correct 20 ms 16972 KB Output is correct
5 Correct 258 ms 22172 KB Output is correct
6 Correct 134 ms 29864 KB Output is correct
7 Correct 160 ms 29864 KB Output is correct
8 Correct 148 ms 29864 KB Output is correct
9 Correct 261 ms 29864 KB Output is correct
10 Correct 207 ms 29864 KB Output is correct
11 Correct 17 ms 29864 KB Output is correct
12 Correct 17 ms 29864 KB Output is correct
13 Correct 21 ms 29864 KB Output is correct
14 Correct 433 ms 32260 KB Output is correct
15 Correct 366 ms 34624 KB Output is correct
16 Correct 432 ms 37088 KB Output is correct
17 Correct 382 ms 39532 KB Output is correct
18 Correct 371 ms 40164 KB Output is correct
19 Correct 343 ms 40164 KB Output is correct
20 Correct 424 ms 40260 KB Output is correct
21 Correct 65 ms 40260 KB Output is correct
22 Correct 458 ms 40260 KB Output is correct
23 Correct 377 ms 40260 KB Output is correct
24 Correct 347 ms 40260 KB Output is correct
25 Correct 381 ms 40260 KB Output is correct
26 Correct 384 ms 40260 KB Output is correct
27 Correct 385 ms 40260 KB Output is correct
28 Correct 452 ms 40260 KB Output is correct
29 Correct 323 ms 40260 KB Output is correct
30 Correct 396 ms 40260 KB Output is correct
31 Correct 327 ms 40260 KB Output is correct
32 Correct 316 ms 40260 KB Output is correct
33 Correct 614 ms 40260 KB Output is correct
34 Correct 374 ms 40260 KB Output is correct
35 Correct 553 ms 40260 KB Output is correct
36 Correct 369 ms 40260 KB Output is correct
37 Correct 614 ms 40260 KB Output is correct
38 Correct 463 ms 40260 KB Output is correct
39 Correct 352 ms 40260 KB Output is correct
40 Correct 661 ms 40260 KB Output is correct
41 Correct 20 ms 40260 KB Output is correct
42 Correct 22 ms 40260 KB Output is correct
43 Correct 24 ms 40260 KB Output is correct
44 Correct 25 ms 40260 KB Output is correct
45 Correct 19 ms 40260 KB Output is correct
46 Correct 20 ms 40260 KB Output is correct
47 Correct 20 ms 40260 KB Output is correct
48 Correct 20 ms 40260 KB Output is correct
49 Correct 21 ms 40260 KB Output is correct
50 Correct 23 ms 40260 KB Output is correct
51 Correct 453 ms 40260 KB Output is correct
52 Correct 355 ms 40260 KB Output is correct
53 Correct 621 ms 40260 KB Output is correct
54 Correct 410 ms 40260 KB Output is correct
55 Correct 554 ms 40260 KB Output is correct
56 Correct 369 ms 40260 KB Output is correct
57 Correct 502 ms 40260 KB Output is correct
58 Correct 402 ms 40260 KB Output is correct
59 Correct 453 ms 40260 KB Output is correct
60 Correct 391 ms 40260 KB Output is correct
61 Correct 613 ms 40260 KB Output is correct
62 Correct 466 ms 40260 KB Output is correct
63 Correct 655 ms 40260 KB Output is correct
64 Correct 455 ms 40260 KB Output is correct
65 Correct 607 ms 40260 KB Output is correct
66 Correct 421 ms 40260 KB Output is correct
67 Correct 553 ms 40260 KB Output is correct
68 Correct 413 ms 40260 KB Output is correct
69 Correct 504 ms 40260 KB Output is correct
70 Correct 283 ms 40260 KB Output is correct