답안 #226482

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
226482 2020-04-23T23:43:27 Z AQT Election Campaign (JOI15_election_campaign) C++14
100 / 100
248 ms 41208 KB
#include <bits/stdc++.h>

using namespace std;

struct node{
	int l, r;
	long long tot, lzy;
};

int N, M;
vector<int> graph[100005];
vector<pair<long long, pair<int, int>>> v[100005];
int par[20][100005];
int lft[100005], rht[100005], t=0;
node seg[1000000];
long long dp[100005];
long long tot[100005];

void pd(int idx){
	if(seg[idx].lzy){
		seg[2*idx].lzy += seg[idx].lzy;
		seg[2*idx+1].lzy += seg[idx].lzy;
		seg[2*idx].tot += seg[idx].lzy;
		seg[2*idx+1].tot += seg[idx].lzy;
		seg[idx].lzy = 0;
	}
}

void build(int l, int r, int idx){
	seg[idx].l = l, seg[idx].r = r;
	if(l == r){
		return;
	}
	int mid = l+r>>1;
	build(l, mid, 2*idx);
	build(mid+1, r, 2*idx+1);
}

void upd(int l, int r, long long v, int idx){
	if(seg[idx].l == l && seg[idx].r == r){
		seg[idx].lzy += v;
		seg[idx].tot += v;
		return;
	}
	pd(idx);
	int mid = seg[idx].l + seg[idx].r >> 1;
	if(r <= mid){
		upd(l, r, v, 2*idx);
	}
	else if(l > mid){
		upd(l, r, v, 2*idx+1);
	}
	else{
		upd(l, mid, v, 2*idx);
		upd(mid+1, r, v, 2*idx+1);
	}
}

long long get(int p, int idx = 1){
	if(seg[idx].l == seg[idx].r){
		return seg[idx].tot;
	}
	pd(idx);
	get(p, 2*idx + (p > seg[idx].l + seg[idx].r >> 1));
}

int lca(int u, int v){
	if(lft[v] <= lft[u] && rht[u] <= rht[v]){
		return v;
	}
	if(lft[u] <= lft[v] && rht[v] <= rht[u]){
		return u;
	}
	for(int d = 16; d>=0; d--){
		int n = par[d][u];
		if(!n || (lft[n] <= lft[v] && rht[v] <= rht[n])){
			
		}
		else{
			u = n;
		}
	}
	return par[0][u];
}

void dfs1(int n){
	//cout << n << " " << par[0][n] << endl;
	lft[n] = ++t;
	for(int e : graph[n]){
		if(e != par[0][n]){
			par[0][e] = n;
			dfs1(e);
		}
	}
	rht[n] = ++t;
}

void dfs2(int n){
	for(int e : graph[n]){
		if(e != par[0][n]){
			dfs2(e);
			dp[n] += dp[e];
		}
	}
	tot[n] = dp[n];
	for(auto q : v[n]){
		if(q.second.first != n && q.second.second != n){
			dp[n] = max(dp[n], get(lft[q.second.first]) + get(lft[q.second.second]) + tot[n] + q.first);
		}
		else if(q.second.first != n || q.second.second != n){
			//cout << get(q.second.first == n ? lft[q.second.second] : lft[q.second.first]) << " " << tot[n] + q.first << "\n";
			dp[n] = max(dp[n], get(q.second.first == n ? lft[q.second.second] : lft[q.second.first]) + tot[n] + q.first);
		}
		else{
			dp[n] = max(dp[n], tot[n] + q.first);
		}
	}
	upd(lft[n], rht[n], -dp[n]+tot[n], 1);
}

int main(){
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> N;
	for(int i = 1; i<N; i++){
		int a, b;
		cin >> a >> b;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	dfs1(1);
	for(int d = 1; d<=16; d++){
		for(int n = 1; n<=N; n++){
			par[d][n] = par[d-1][par[d-1][n]];
		}
	}
	build(1, 2*N, 1);
	cin >> M;
	while(M--){
		int a, b, c;
		cin >> a >> b >> c;
		v[lca(a, b)].push_back({c,{a, b}});
		//cout << lca(a, b) << endl;
	}
	dfs2(1);
	for(int i = 1; i<=N; i++){
		//cout << dp[i] << " " << tot[i] << "\n";
	}
	cout << dp[1] << "\n";
}

Compilation message

election_campaign.cpp: In function 'void build(int, int, int)':
election_campaign.cpp:34:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid = l+r>>1;
            ~^~
election_campaign.cpp: In function 'void upd(int, int, long long int, int)':
election_campaign.cpp:46:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid = seg[idx].l + seg[idx].r >> 1;
            ~~~~~~~~~~~^~~~~~~~~~~~
election_campaign.cpp: In function 'long long int get(int, int)':
election_campaign.cpp:64:33: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  get(p, 2*idx + (p > seg[idx].l + seg[idx].r >> 1));
                      ~~~~~~~~~~~^~~~~~~~~~~~
election_campaign.cpp:65:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5100 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 7 ms 5376 KB Output is correct
5 Correct 105 ms 30840 KB Output is correct
6 Correct 89 ms 36984 KB Output is correct
7 Correct 106 ms 34756 KB Output is correct
8 Correct 81 ms 30456 KB Output is correct
9 Correct 108 ms 33528 KB Output is correct
10 Correct 80 ms 30328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 8 ms 5376 KB Output is correct
4 Correct 152 ms 40824 KB Output is correct
5 Correct 156 ms 40824 KB Output is correct
6 Correct 150 ms 40696 KB Output is correct
7 Correct 151 ms 40824 KB Output is correct
8 Correct 160 ms 40696 KB Output is correct
9 Correct 144 ms 40824 KB Output is correct
10 Correct 152 ms 40824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 8 ms 5376 KB Output is correct
4 Correct 152 ms 40824 KB Output is correct
5 Correct 156 ms 40824 KB Output is correct
6 Correct 150 ms 40696 KB Output is correct
7 Correct 151 ms 40824 KB Output is correct
8 Correct 160 ms 40696 KB Output is correct
9 Correct 144 ms 40824 KB Output is correct
10 Correct 152 ms 40824 KB Output is correct
11 Correct 17 ms 6272 KB Output is correct
12 Correct 153 ms 41080 KB Output is correct
13 Correct 153 ms 41080 KB Output is correct
14 Correct 149 ms 41208 KB Output is correct
15 Correct 156 ms 41080 KB Output is correct
16 Correct 151 ms 41080 KB Output is correct
17 Correct 154 ms 41112 KB Output is correct
18 Correct 157 ms 41080 KB Output is correct
19 Correct 150 ms 41080 KB Output is correct
20 Correct 158 ms 41080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 216 ms 34032 KB Output is correct
2 Correct 147 ms 40824 KB Output is correct
3 Correct 234 ms 38264 KB Output is correct
4 Correct 189 ms 33872 KB Output is correct
5 Correct 206 ms 37876 KB Output is correct
6 Correct 180 ms 33624 KB Output is correct
7 Correct 248 ms 37788 KB Output is correct
8 Correct 200 ms 34424 KB Output is correct
9 Correct 150 ms 40824 KB Output is correct
10 Correct 242 ms 36848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5100 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 7 ms 5376 KB Output is correct
5 Correct 105 ms 30840 KB Output is correct
6 Correct 89 ms 36984 KB Output is correct
7 Correct 106 ms 34756 KB Output is correct
8 Correct 81 ms 30456 KB Output is correct
9 Correct 108 ms 33528 KB Output is correct
10 Correct 80 ms 30328 KB Output is correct
11 Correct 8 ms 5376 KB Output is correct
12 Correct 8 ms 5504 KB Output is correct
13 Correct 8 ms 5248 KB Output is correct
14 Correct 8 ms 5376 KB Output is correct
15 Correct 8 ms 5376 KB Output is correct
16 Correct 8 ms 5376 KB Output is correct
17 Correct 8 ms 5376 KB Output is correct
18 Correct 8 ms 5376 KB Output is correct
19 Correct 8 ms 5504 KB Output is correct
20 Correct 8 ms 5504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5100 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 7 ms 5376 KB Output is correct
5 Correct 105 ms 30840 KB Output is correct
6 Correct 89 ms 36984 KB Output is correct
7 Correct 106 ms 34756 KB Output is correct
8 Correct 81 ms 30456 KB Output is correct
9 Correct 108 ms 33528 KB Output is correct
10 Correct 80 ms 30328 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 7 ms 5120 KB Output is correct
13 Correct 8 ms 5376 KB Output is correct
14 Correct 152 ms 40824 KB Output is correct
15 Correct 156 ms 40824 KB Output is correct
16 Correct 150 ms 40696 KB Output is correct
17 Correct 151 ms 40824 KB Output is correct
18 Correct 160 ms 40696 KB Output is correct
19 Correct 144 ms 40824 KB Output is correct
20 Correct 152 ms 40824 KB Output is correct
21 Correct 17 ms 6272 KB Output is correct
22 Correct 153 ms 41080 KB Output is correct
23 Correct 153 ms 41080 KB Output is correct
24 Correct 149 ms 41208 KB Output is correct
25 Correct 156 ms 41080 KB Output is correct
26 Correct 151 ms 41080 KB Output is correct
27 Correct 154 ms 41112 KB Output is correct
28 Correct 157 ms 41080 KB Output is correct
29 Correct 150 ms 41080 KB Output is correct
30 Correct 158 ms 41080 KB Output is correct
31 Correct 216 ms 34032 KB Output is correct
32 Correct 147 ms 40824 KB Output is correct
33 Correct 234 ms 38264 KB Output is correct
34 Correct 189 ms 33872 KB Output is correct
35 Correct 206 ms 37876 KB Output is correct
36 Correct 180 ms 33624 KB Output is correct
37 Correct 248 ms 37788 KB Output is correct
38 Correct 200 ms 34424 KB Output is correct
39 Correct 150 ms 40824 KB Output is correct
40 Correct 242 ms 36848 KB Output is correct
41 Correct 8 ms 5376 KB Output is correct
42 Correct 8 ms 5504 KB Output is correct
43 Correct 8 ms 5248 KB Output is correct
44 Correct 8 ms 5376 KB Output is correct
45 Correct 8 ms 5376 KB Output is correct
46 Correct 8 ms 5376 KB Output is correct
47 Correct 8 ms 5376 KB Output is correct
48 Correct 8 ms 5376 KB Output is correct
49 Correct 8 ms 5504 KB Output is correct
50 Correct 8 ms 5504 KB Output is correct
51 Correct 208 ms 34804 KB Output is correct
52 Correct 153 ms 41096 KB Output is correct
53 Correct 246 ms 37172 KB Output is correct
54 Correct 155 ms 34164 KB Output is correct
55 Correct 213 ms 34448 KB Output is correct
56 Correct 155 ms 41080 KB Output is correct
57 Correct 202 ms 37876 KB Output is correct
58 Correct 193 ms 34024 KB Output is correct
59 Correct 185 ms 34672 KB Output is correct
60 Correct 155 ms 41080 KB Output is correct
61 Correct 202 ms 37876 KB Output is correct
62 Correct 184 ms 33700 KB Output is correct
63 Correct 222 ms 34308 KB Output is correct
64 Correct 154 ms 41080 KB Output is correct
65 Correct 239 ms 37740 KB Output is correct
66 Correct 158 ms 34156 KB Output is correct
67 Correct 209 ms 34364 KB Output is correct
68 Correct 162 ms 41084 KB Output is correct
69 Correct 198 ms 36852 KB Output is correct
70 Correct 193 ms 34020 KB Output is correct