#include<bits/stdc++.h>
#define STIZE(x) fprintf(stderr, "STIZE%d\n", x);
#define PRINT(x) fprintf(stderr, "%s = %d\n", #x, x);
#define NL(x) printf("%c", " \n"[(x)]);
#define lld long long
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define mid (l+r)/2
#define endl '\n'
#define all(a) begin(a),end(a)
#define sz(a) int((a).size())
#define LINF 1000000000000000LL
#define INF 1000000000
#define EPS 1e-9
using namespace std;
#define MAXN 100010
#define MAXL 20
struct query{
int A, B, C;
} q;
vector<query> qs[MAXN];
vector<int> adj[MAXN];
int N, siz[MAXN], idx[MAXN], seg[4*MAXN], anc[MAXN][MAXL], timer, idxx, in[MAXN], out[MAXN], dp[MAXN], dp1[MAXN];
int dfsLCA(int node, int prev) {
idx[node] = ++timer; in[node] = ++idxx;
anc[node][0] = prev; siz[node] = 1;
for(auto x : adj[node]) {
if(x != prev) {
siz[node] += dfsLCA(x, node);
}
}
out[node] = ++idxx;
return siz[node];
}
void init() {
dfsLCA(1, 1);
for(int d = 1; d < MAXL; d++) {
for(int i = 1; i <= N; i++) {
anc[i][d] = anc[anc[i][d-1]][d-1];
}
}
}
bool inSubtree(int x, int y) {
return (in[y] <= in[x] && out[x] <= out[y]);
}
int LCA(int x, int y) {
if(inSubtree(x, y)) return y;
if(inSubtree(y, x)) return x;
for(int d = MAXL-1; d >= 0; d--) {
if(!inSubtree(y, anc[x][d])) x = anc[x][d];
}
return anc[x][0];
}
int query(int node1, int l, int r, int idx) {
if(l == r && l == idx) return seg[node1];
if(r < l || idx < l || r < idx) return 0;
return seg[node1] + query(2*node1, l, mid, idx) + query(2*node1+1, mid+1, r, idx);
}
int update(int node1, int l, int r, int L, int R, int val) {
if(l > r || L > r || l > R || L > R) return 0;
if(L <= l && r <= R) {
return seg[node1] += val;
}
update(2*node1, l, mid, L, R, val); update(2*node1+1, mid+1, r, L, R, val);
}
int DFS(int node, int prev) {
for(auto x : adj[node]) {
if(x != prev) dp1[node] += DFS(x, node);
}
dp[node] = dp1[node];
for(auto q : qs[node]) {
dp[node] = max(dp[node], q.C + query(1, 1, N, idx[q.A]) + query(1, 1, N, idx[q.B]) + dp1[node]);
}
update(1, 1, N, idx[node], idx[node]+siz[node]-1, dp1[node]-dp[node]);
return dp[node];
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);
cin >> N;
for(int i = 1; i < N; i++) {
int x, y; cin >> x >> y;
adj[x].pb(y); adj[y].pb(x);
}
init();
int Q; cin >> Q;
for(int i = 1; i <= Q; i++) {
cin >> q.A >> q.B >> q.C;
qs[LCA(q.A, q.B)].pb(q);
}
DFS(1, 1);
cout << dp[1] << endl;
return 0;
}
Compilation message
election_campaign.cpp: In function 'int update(int, int, int, int, int, int)':
election_campaign.cpp:67:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5200 KB |
Output is correct |
3 |
Correct |
6 ms |
5200 KB |
Output is correct |
4 |
Correct |
6 ms |
5408 KB |
Output is correct |
5 |
Correct |
135 ms |
19680 KB |
Output is correct |
6 |
Correct |
75 ms |
25696 KB |
Output is correct |
7 |
Correct |
128 ms |
25696 KB |
Output is correct |
8 |
Correct |
87 ms |
25696 KB |
Output is correct |
9 |
Correct |
108 ms |
25696 KB |
Output is correct |
10 |
Correct |
85 ms |
25696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
25696 KB |
Output is correct |
2 |
Correct |
6 ms |
25696 KB |
Output is correct |
3 |
Correct |
7 ms |
25696 KB |
Output is correct |
4 |
Correct |
180 ms |
27844 KB |
Output is correct |
5 |
Correct |
151 ms |
27972 KB |
Output is correct |
6 |
Correct |
146 ms |
28020 KB |
Output is correct |
7 |
Correct |
154 ms |
28020 KB |
Output is correct |
8 |
Correct |
154 ms |
28020 KB |
Output is correct |
9 |
Correct |
149 ms |
28020 KB |
Output is correct |
10 |
Correct |
159 ms |
28028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
25696 KB |
Output is correct |
2 |
Correct |
6 ms |
25696 KB |
Output is correct |
3 |
Correct |
7 ms |
25696 KB |
Output is correct |
4 |
Correct |
180 ms |
27844 KB |
Output is correct |
5 |
Correct |
151 ms |
27972 KB |
Output is correct |
6 |
Correct |
146 ms |
28020 KB |
Output is correct |
7 |
Correct |
154 ms |
28020 KB |
Output is correct |
8 |
Correct |
154 ms |
28020 KB |
Output is correct |
9 |
Correct |
149 ms |
28020 KB |
Output is correct |
10 |
Correct |
159 ms |
28028 KB |
Output is correct |
11 |
Correct |
19 ms |
28028 KB |
Output is correct |
12 |
Correct |
195 ms |
28028 KB |
Output is correct |
13 |
Correct |
155 ms |
28028 KB |
Output is correct |
14 |
Correct |
156 ms |
28028 KB |
Output is correct |
15 |
Correct |
171 ms |
28056 KB |
Output is correct |
16 |
Correct |
150 ms |
28056 KB |
Output is correct |
17 |
Correct |
162 ms |
28056 KB |
Output is correct |
18 |
Correct |
156 ms |
28056 KB |
Output is correct |
19 |
Correct |
156 ms |
28132 KB |
Output is correct |
20 |
Correct |
155 ms |
28132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
229 ms |
28132 KB |
Output is correct |
2 |
Correct |
148 ms |
28132 KB |
Output is correct |
3 |
Correct |
243 ms |
28132 KB |
Output is correct |
4 |
Correct |
217 ms |
28132 KB |
Output is correct |
5 |
Correct |
207 ms |
28132 KB |
Output is correct |
6 |
Correct |
237 ms |
28132 KB |
Output is correct |
7 |
Correct |
248 ms |
28132 KB |
Output is correct |
8 |
Correct |
197 ms |
28132 KB |
Output is correct |
9 |
Correct |
160 ms |
28132 KB |
Output is correct |
10 |
Correct |
249 ms |
28132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5200 KB |
Output is correct |
3 |
Correct |
6 ms |
5200 KB |
Output is correct |
4 |
Correct |
6 ms |
5408 KB |
Output is correct |
5 |
Correct |
135 ms |
19680 KB |
Output is correct |
6 |
Correct |
75 ms |
25696 KB |
Output is correct |
7 |
Correct |
128 ms |
25696 KB |
Output is correct |
8 |
Correct |
87 ms |
25696 KB |
Output is correct |
9 |
Correct |
108 ms |
25696 KB |
Output is correct |
10 |
Correct |
85 ms |
25696 KB |
Output is correct |
11 |
Correct |
7 ms |
28132 KB |
Output is correct |
12 |
Correct |
7 ms |
28132 KB |
Output is correct |
13 |
Correct |
7 ms |
28132 KB |
Output is correct |
14 |
Correct |
8 ms |
28132 KB |
Output is correct |
15 |
Correct |
9 ms |
28132 KB |
Output is correct |
16 |
Correct |
7 ms |
28132 KB |
Output is correct |
17 |
Correct |
7 ms |
28132 KB |
Output is correct |
18 |
Correct |
7 ms |
28132 KB |
Output is correct |
19 |
Correct |
7 ms |
28132 KB |
Output is correct |
20 |
Correct |
7 ms |
28132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5200 KB |
Output is correct |
3 |
Correct |
6 ms |
5200 KB |
Output is correct |
4 |
Correct |
6 ms |
5408 KB |
Output is correct |
5 |
Correct |
135 ms |
19680 KB |
Output is correct |
6 |
Correct |
75 ms |
25696 KB |
Output is correct |
7 |
Correct |
128 ms |
25696 KB |
Output is correct |
8 |
Correct |
87 ms |
25696 KB |
Output is correct |
9 |
Correct |
108 ms |
25696 KB |
Output is correct |
10 |
Correct |
85 ms |
25696 KB |
Output is correct |
11 |
Correct |
5 ms |
25696 KB |
Output is correct |
12 |
Correct |
6 ms |
25696 KB |
Output is correct |
13 |
Correct |
7 ms |
25696 KB |
Output is correct |
14 |
Correct |
180 ms |
27844 KB |
Output is correct |
15 |
Correct |
151 ms |
27972 KB |
Output is correct |
16 |
Correct |
146 ms |
28020 KB |
Output is correct |
17 |
Correct |
154 ms |
28020 KB |
Output is correct |
18 |
Correct |
154 ms |
28020 KB |
Output is correct |
19 |
Correct |
149 ms |
28020 KB |
Output is correct |
20 |
Correct |
159 ms |
28028 KB |
Output is correct |
21 |
Correct |
19 ms |
28028 KB |
Output is correct |
22 |
Correct |
195 ms |
28028 KB |
Output is correct |
23 |
Correct |
155 ms |
28028 KB |
Output is correct |
24 |
Correct |
156 ms |
28028 KB |
Output is correct |
25 |
Correct |
171 ms |
28056 KB |
Output is correct |
26 |
Correct |
150 ms |
28056 KB |
Output is correct |
27 |
Correct |
162 ms |
28056 KB |
Output is correct |
28 |
Correct |
156 ms |
28056 KB |
Output is correct |
29 |
Correct |
156 ms |
28132 KB |
Output is correct |
30 |
Correct |
155 ms |
28132 KB |
Output is correct |
31 |
Correct |
229 ms |
28132 KB |
Output is correct |
32 |
Correct |
148 ms |
28132 KB |
Output is correct |
33 |
Correct |
243 ms |
28132 KB |
Output is correct |
34 |
Correct |
217 ms |
28132 KB |
Output is correct |
35 |
Correct |
207 ms |
28132 KB |
Output is correct |
36 |
Correct |
237 ms |
28132 KB |
Output is correct |
37 |
Correct |
248 ms |
28132 KB |
Output is correct |
38 |
Correct |
197 ms |
28132 KB |
Output is correct |
39 |
Correct |
160 ms |
28132 KB |
Output is correct |
40 |
Correct |
249 ms |
28132 KB |
Output is correct |
41 |
Correct |
7 ms |
28132 KB |
Output is correct |
42 |
Correct |
7 ms |
28132 KB |
Output is correct |
43 |
Correct |
7 ms |
28132 KB |
Output is correct |
44 |
Correct |
8 ms |
28132 KB |
Output is correct |
45 |
Correct |
9 ms |
28132 KB |
Output is correct |
46 |
Correct |
7 ms |
28132 KB |
Output is correct |
47 |
Correct |
7 ms |
28132 KB |
Output is correct |
48 |
Correct |
7 ms |
28132 KB |
Output is correct |
49 |
Correct |
7 ms |
28132 KB |
Output is correct |
50 |
Correct |
7 ms |
28132 KB |
Output is correct |
51 |
Correct |
212 ms |
28132 KB |
Output is correct |
52 |
Correct |
156 ms |
28132 KB |
Output is correct |
53 |
Correct |
247 ms |
28132 KB |
Output is correct |
54 |
Correct |
172 ms |
28132 KB |
Output is correct |
55 |
Correct |
216 ms |
28132 KB |
Output is correct |
56 |
Correct |
155 ms |
28132 KB |
Output is correct |
57 |
Correct |
227 ms |
28132 KB |
Output is correct |
58 |
Correct |
191 ms |
28132 KB |
Output is correct |
59 |
Correct |
207 ms |
28132 KB |
Output is correct |
60 |
Correct |
162 ms |
28132 KB |
Output is correct |
61 |
Correct |
208 ms |
28132 KB |
Output is correct |
62 |
Correct |
202 ms |
28132 KB |
Output is correct |
63 |
Correct |
239 ms |
28132 KB |
Output is correct |
64 |
Correct |
172 ms |
28132 KB |
Output is correct |
65 |
Correct |
251 ms |
28132 KB |
Output is correct |
66 |
Correct |
192 ms |
28132 KB |
Output is correct |
67 |
Correct |
246 ms |
28132 KB |
Output is correct |
68 |
Correct |
153 ms |
28132 KB |
Output is correct |
69 |
Correct |
205 ms |
28132 KB |
Output is correct |
70 |
Correct |
189 ms |
28132 KB |
Output is correct |