#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define sc second
#define endl "\n"
#define pii pair<int,int>
using namespace std;
const int MAXN = 1e5+5;
const int mod7 = 1e9+7;
const long long inf = 1e18;
const int lg = 18;
struct put{int a,b,w;};
int t = 1;
int n,m;
vector<vector<int>> graf(MAXN);
vector<vector<put>> sviputevi(MAXN);
int up[MAXN][lg];
int dist[MAXN];
int tin[MAXN];
int tout[MAXN];
int dp[MAXN];
vector<int>seg(MAXN<<3);
vector<int>lazy(MAXN<<3);
void push(int nod, int tl, int tr)
{
if(!lazy[nod])return;
if(tl!=tr)
{
lazy[nod<<1] += lazy[nod];
lazy[nod<<1|1] += lazy[nod];
}
seg[nod] += (tr-tl+1)*lazy[nod];
lazy[nod] = 0;
}
void update(int nod, int tl, int tr, int l, int r, int v)
{
push(nod, tl, tr);
if(tl > r || tr<l || tl>tr || l > r)return;
if(tl>=l && tr<=r)
{
lazy[nod]+=v;
push(nod, tl, tr);
return;
}
int mid = tl+tr >> 1;
update(nod<<1, tl, mid, l, r, v);
update(nod<<1|1, mid+1, tr, l, r, v);
seg[nod] = seg[nod<<1] + seg[nod<<1|1];
}
int query(int nod, int tl, int tr, int l, int r)
{
push(nod, tl, tr);
if(tl > r || tr<l || tl>tr || l > r)return 0;
if(tl>=l && tr<=r)return seg[nod];
int mid = tl+tr >> 1;
return query(nod<<1, tl, mid, l, r) + query(nod<<1|1, mid+1, tr, l, r);
}
void dfs(int nod, int p, int d)
{
tin[nod] = t++;
dist[nod] = d;
up[nod][0] = p;
for(auto x: graf[nod])if(x!=p)dfs(x,nod, d+1);
tout[nod] = t++;
}
void fillUp()
{
for(int i=1; i<lg; i++)
{
for(int j=1; j<=n; j++)
{
up[j][i] = up[up[j][i-1]][i-1];
}
}
}
int lca(int u, int v)
{
if(u==v)return u;
if(tin[u] < tin[v] && tout[u] > tout[v])return u;
if(tin[v] < tin[u] && tout[v] > tout[u])return v;
for(int i=lg-1; i>=0; i--)
{
int guess = up[v][i];
if(!(tin[guess] < tin[u] && tout[guess] > tout[u]))v = guess;
}
return up[v][0];
}
void solve(int nod, int p)
{
for(auto x: graf[nod])
{
if(x!=p)solve(x, nod);
dp[nod]+=dp[x];
}
int rez = dp[nod];
for(auto x: sviputevi[nod])
{
int a = x.a;
int b = x.b;
int w = x.w;
if(dist[a] > dist[b])swap(a,b);
if(a == nod)rez = max(rez, dp[nod] - query(1,1,2*MAXN, b,b) + w);
else
{
rez = max(rez, dp[nod] - query(1,1,2*MAXN, a,a) - query(1,1,2*MAXN, b,b) + w);
}
}
dp[nod] = rez;
update(1,1, 2*MAXN, tin[nod], tout[nod], dp[nod]);
}
signed main()
{
ios_base::sync_with_stdio(false),cin.tie(0), cout.tie(0);
int tt=1;
//cin >> tt;
while(tt--)
{
cin >> n;
for(int i=0; i<n-1; i++)
{
int a,b;cin >> a >> b;
graf[a].pb(b);
graf[b].pb(a);
}
dfs(1,1,0);
fillUp();
cin >> m;
vector<pii> dubine;
for(int i=0; i<m; i++)
{
int a,b,w;cin >> a >> b >> w;
sviputevi[lca(a,b)].pb({a,b,w});
}
for(int i=1; i<=n; i++)dubine.pb({dist[i], i});
sort(all(dubine));
reverse(all(dubine));
for(int i=0; i<n; i++)
{
int nod = dubine[i].sc;
int p = up[nod][0];
for(auto x: graf[nod])
{
if(x==p)continue;
dp[nod]+=dp[x];
}
int rez = dp[nod];
for(auto x: sviputevi[nod])
{
int a = x.a;
int b = x.b;
int w = x.w;
if(dist[a] > dist[b])swap(a,b);
if(a == nod)
{
int q = query(1,1,2*MAXN, tin[b],tin[b]);
rez = max(rez, dp[nod] - query(1,1,2*MAXN, tin[b],tin[b]) + w);
}
else
{
rez = max(rez, dp[nod] - query(1,1,2*MAXN, tin[a],tin[a])- query(1,1,2*MAXN, tin[b],tin[b]) + w);
}
}
update(1,1, 2*MAXN, tin[nod], tout[nod], rez - dp[nod]);
dp[nod] = rez;
}
//solve(1,1);
cout << dp[1] << endl;
}
}
Compilation message
election_campaign.cpp: In function 'void update(long long int, long long int, long long int, long long int, long long int, long long int)':
election_campaign.cpp:56:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int mid = tl+tr >> 1;
| ~~^~~
election_campaign.cpp: In function 'long long int query(long long int, long long int, long long int, long long int, long long int)':
election_campaign.cpp:67:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
67 | int mid = tl+tr >> 1;
| ~~^~~
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:177:25: warning: unused variable 'q' [-Wunused-variable]
177 | int q = query(1,1,2*MAXN, tin[b],tin[b]);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21584 KB |
Output is correct |
2 |
Correct |
5 ms |
21584 KB |
Output is correct |
3 |
Correct |
4 ms |
21584 KB |
Output is correct |
4 |
Correct |
4 ms |
21840 KB |
Output is correct |
5 |
Correct |
105 ms |
42868 KB |
Output is correct |
6 |
Correct |
85 ms |
46780 KB |
Output is correct |
7 |
Correct |
129 ms |
45620 KB |
Output is correct |
8 |
Correct |
115 ms |
42996 KB |
Output is correct |
9 |
Correct |
115 ms |
44592 KB |
Output is correct |
10 |
Correct |
94 ms |
43064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21584 KB |
Output is correct |
2 |
Correct |
5 ms |
21584 KB |
Output is correct |
3 |
Correct |
5 ms |
21840 KB |
Output is correct |
4 |
Correct |
141 ms |
51756 KB |
Output is correct |
5 |
Correct |
180 ms |
51568 KB |
Output is correct |
6 |
Correct |
156 ms |
51644 KB |
Output is correct |
7 |
Correct |
190 ms |
51656 KB |
Output is correct |
8 |
Correct |
161 ms |
51644 KB |
Output is correct |
9 |
Correct |
141 ms |
51744 KB |
Output is correct |
10 |
Correct |
147 ms |
51748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21584 KB |
Output is correct |
2 |
Correct |
5 ms |
21584 KB |
Output is correct |
3 |
Correct |
5 ms |
21840 KB |
Output is correct |
4 |
Correct |
141 ms |
51756 KB |
Output is correct |
5 |
Correct |
180 ms |
51568 KB |
Output is correct |
6 |
Correct |
156 ms |
51644 KB |
Output is correct |
7 |
Correct |
190 ms |
51656 KB |
Output is correct |
8 |
Correct |
161 ms |
51644 KB |
Output is correct |
9 |
Correct |
141 ms |
51744 KB |
Output is correct |
10 |
Correct |
147 ms |
51748 KB |
Output is correct |
11 |
Correct |
17 ms |
23120 KB |
Output is correct |
12 |
Correct |
156 ms |
51912 KB |
Output is correct |
13 |
Correct |
163 ms |
51900 KB |
Output is correct |
14 |
Correct |
146 ms |
52008 KB |
Output is correct |
15 |
Correct |
150 ms |
51912 KB |
Output is correct |
16 |
Correct |
140 ms |
52048 KB |
Output is correct |
17 |
Correct |
159 ms |
51900 KB |
Output is correct |
18 |
Correct |
143 ms |
51912 KB |
Output is correct |
19 |
Correct |
127 ms |
51908 KB |
Output is correct |
20 |
Correct |
135 ms |
51912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
172 ms |
47868 KB |
Output is correct |
2 |
Correct |
126 ms |
51644 KB |
Output is correct |
3 |
Correct |
222 ms |
50308 KB |
Output is correct |
4 |
Correct |
148 ms |
47868 KB |
Output is correct |
5 |
Correct |
168 ms |
49792 KB |
Output is correct |
6 |
Correct |
150 ms |
48052 KB |
Output is correct |
7 |
Correct |
202 ms |
49916 KB |
Output is correct |
8 |
Correct |
174 ms |
47296 KB |
Output is correct |
9 |
Correct |
142 ms |
51588 KB |
Output is correct |
10 |
Correct |
203 ms |
49720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21584 KB |
Output is correct |
2 |
Correct |
5 ms |
21584 KB |
Output is correct |
3 |
Correct |
4 ms |
21584 KB |
Output is correct |
4 |
Correct |
4 ms |
21840 KB |
Output is correct |
5 |
Correct |
105 ms |
42868 KB |
Output is correct |
6 |
Correct |
85 ms |
46780 KB |
Output is correct |
7 |
Correct |
129 ms |
45620 KB |
Output is correct |
8 |
Correct |
115 ms |
42996 KB |
Output is correct |
9 |
Correct |
115 ms |
44592 KB |
Output is correct |
10 |
Correct |
94 ms |
43064 KB |
Output is correct |
11 |
Correct |
6 ms |
21840 KB |
Output is correct |
12 |
Correct |
6 ms |
21848 KB |
Output is correct |
13 |
Correct |
5 ms |
21840 KB |
Output is correct |
14 |
Correct |
5 ms |
21852 KB |
Output is correct |
15 |
Correct |
5 ms |
21840 KB |
Output is correct |
16 |
Correct |
7 ms |
21840 KB |
Output is correct |
17 |
Correct |
6 ms |
21840 KB |
Output is correct |
18 |
Correct |
6 ms |
22008 KB |
Output is correct |
19 |
Correct |
5 ms |
21840 KB |
Output is correct |
20 |
Correct |
5 ms |
21840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21584 KB |
Output is correct |
2 |
Correct |
5 ms |
21584 KB |
Output is correct |
3 |
Correct |
4 ms |
21584 KB |
Output is correct |
4 |
Correct |
4 ms |
21840 KB |
Output is correct |
5 |
Correct |
105 ms |
42868 KB |
Output is correct |
6 |
Correct |
85 ms |
46780 KB |
Output is correct |
7 |
Correct |
129 ms |
45620 KB |
Output is correct |
8 |
Correct |
115 ms |
42996 KB |
Output is correct |
9 |
Correct |
115 ms |
44592 KB |
Output is correct |
10 |
Correct |
94 ms |
43064 KB |
Output is correct |
11 |
Correct |
4 ms |
21584 KB |
Output is correct |
12 |
Correct |
5 ms |
21584 KB |
Output is correct |
13 |
Correct |
5 ms |
21840 KB |
Output is correct |
14 |
Correct |
141 ms |
51756 KB |
Output is correct |
15 |
Correct |
180 ms |
51568 KB |
Output is correct |
16 |
Correct |
156 ms |
51644 KB |
Output is correct |
17 |
Correct |
190 ms |
51656 KB |
Output is correct |
18 |
Correct |
161 ms |
51644 KB |
Output is correct |
19 |
Correct |
141 ms |
51744 KB |
Output is correct |
20 |
Correct |
147 ms |
51748 KB |
Output is correct |
21 |
Correct |
17 ms |
23120 KB |
Output is correct |
22 |
Correct |
156 ms |
51912 KB |
Output is correct |
23 |
Correct |
163 ms |
51900 KB |
Output is correct |
24 |
Correct |
146 ms |
52008 KB |
Output is correct |
25 |
Correct |
150 ms |
51912 KB |
Output is correct |
26 |
Correct |
140 ms |
52048 KB |
Output is correct |
27 |
Correct |
159 ms |
51900 KB |
Output is correct |
28 |
Correct |
143 ms |
51912 KB |
Output is correct |
29 |
Correct |
127 ms |
51908 KB |
Output is correct |
30 |
Correct |
135 ms |
51912 KB |
Output is correct |
31 |
Correct |
172 ms |
47868 KB |
Output is correct |
32 |
Correct |
126 ms |
51644 KB |
Output is correct |
33 |
Correct |
222 ms |
50308 KB |
Output is correct |
34 |
Correct |
148 ms |
47868 KB |
Output is correct |
35 |
Correct |
168 ms |
49792 KB |
Output is correct |
36 |
Correct |
150 ms |
48052 KB |
Output is correct |
37 |
Correct |
202 ms |
49916 KB |
Output is correct |
38 |
Correct |
174 ms |
47296 KB |
Output is correct |
39 |
Correct |
142 ms |
51588 KB |
Output is correct |
40 |
Correct |
203 ms |
49720 KB |
Output is correct |
41 |
Correct |
6 ms |
21840 KB |
Output is correct |
42 |
Correct |
6 ms |
21848 KB |
Output is correct |
43 |
Correct |
5 ms |
21840 KB |
Output is correct |
44 |
Correct |
5 ms |
21852 KB |
Output is correct |
45 |
Correct |
5 ms |
21840 KB |
Output is correct |
46 |
Correct |
7 ms |
21840 KB |
Output is correct |
47 |
Correct |
6 ms |
21840 KB |
Output is correct |
48 |
Correct |
6 ms |
22008 KB |
Output is correct |
49 |
Correct |
5 ms |
21840 KB |
Output is correct |
50 |
Correct |
5 ms |
21840 KB |
Output is correct |
51 |
Correct |
181 ms |
47740 KB |
Output is correct |
52 |
Correct |
152 ms |
51904 KB |
Output is correct |
53 |
Correct |
205 ms |
49880 KB |
Output is correct |
54 |
Correct |
172 ms |
47676 KB |
Output is correct |
55 |
Correct |
185 ms |
48128 KB |
Output is correct |
56 |
Correct |
160 ms |
51980 KB |
Output is correct |
57 |
Correct |
185 ms |
49788 KB |
Output is correct |
58 |
Correct |
167 ms |
48248 KB |
Output is correct |
59 |
Correct |
177 ms |
47544 KB |
Output is correct |
60 |
Correct |
161 ms |
52020 KB |
Output is correct |
61 |
Correct |
168 ms |
49788 KB |
Output is correct |
62 |
Correct |
170 ms |
47900 KB |
Output is correct |
63 |
Correct |
184 ms |
47976 KB |
Output is correct |
64 |
Correct |
161 ms |
51848 KB |
Output is correct |
65 |
Correct |
192 ms |
50428 KB |
Output is correct |
66 |
Correct |
151 ms |
47540 KB |
Output is correct |
67 |
Correct |
194 ms |
48052 KB |
Output is correct |
68 |
Correct |
155 ms |
51900 KB |
Output is correct |
69 |
Correct |
185 ms |
49020 KB |
Output is correct |
70 |
Correct |
166 ms |
47616 KB |
Output is correct |