#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
#define N 200005
#define mp make_pair
using namespace std;
ll n,ch[N],ans[N],al,mx[N],ze[N],q,ind[N],e,f;
ll up[N],d[N],save[6*N],pown=1,fix[N],root,p[N],timer,in[N],out[N],sum,l,r,ad;
pair<ll,ll>t[6*N];
vector<pair<ll,pair<ll,ll> > >g[N];
vector<pair<pair<ll,ll> , pair<ll,ll> > > ed;
void calc1(ll x,ll par){
for(int i=0; i<g[x].size(); i++)
if(g[x][i].f != par){
calc1(g[x][i].f , x);
ch[x] += g[x][i].s.s + ch[g[x][i].f];
}
}
void solve1(ll x,ll par,ll zed){
ans[1] = max(ans[1] , zed + ch[x]);
ze[x] = zed;
for(int i=0; i<g[x].size(); i++)
if(g[x][i].f != par)
solve1(g[x][i].f , x , zed + ch[x] - ch[g[x][i].f] - g[x][i].s.s + g[x][i].s.f);
}
void solve2(ll x,ll par,ll dis){
ll mx1 = 0, mx2 = 0,ind1 = x , ind2 = x;
for(int i=0; i<g[x].size(); i++)
if(g[x][i].f != par){
solve2(g[x][i].f , x , dis + g[x][i].s.f);
if(mx[g[x][i].f] > mx1)mx2 = mx1,ind2 = ind1 , mx1 = mx[g[x][i].f] , ind1 = ind[g[x][i].f];
else if(mx[g[x][i].f] > mx2)mx2 = mx[g[x][i].f] , ind2 = ind[g[x][i].f];
}
ll cur = ze[x] + mx1 + mx2 - 2 * dis + ch[x];
if(cur > ans[2]){
root = x;
ans[2] = cur;
e = ind1;
f = ind2;
}
if(mx1 > dis){
ind[x] = ind1;
mx[x] = mx1;
}
else {
ind[x] = x;
mx[x] = dis;
}
}
void go(ll x,ll par){
p[x] = par;
timer++;
in[x] = timer;
for(int i=0; i<g[x].size(); i++)
if(g[x][i].f != par){
up[g[x][i].f] = g[x][i].s.f;
d[g[x][i].f] = d[x] + g[x][i].s.f;
go(g[x][i].f , x);
}
out[x] = timer;
}
void upd(ll x){
if(!x)return;
t[x] = max(t[2 * x] , t[2 * x + 1]);
upd(x / 2);
}
void update(ll x,ll L,ll R){
if(L > r || R < l)return;
if(L >= l && R <= r){
t[x].f += ad;
save[x] += ad;
return;
}
if(save[x]){
save[2 * x] += save[x];
save[2 * x + 1] += save[x];
t[2 * x].f += save[x];
t[2 * x + 1].f += save[x];
save[x] = 0;
}
update(2 * x , L , (L + R) / 2);
update(2 * x + 1 , (L + R) / 2 + 1 , R);
t[x] = max(t[2 * x] , t[2 * x + 1]);
}
void delet(ll x){
while(!fix[x]){
fix[x] = 1;
l = in[x];
r = out[x];
ad = -up[x];
update(1 , 1 , pown);
x = p[x];
}
}
int main(){
ios::sync_with_stdio(false);
cin >> n;
while(pown <= n)
pown *= 2;
for(int i=1; i<n; i++){
ll a,b,c,d;
cin >> a >> b >> c >> d;
al += c + d;
g[a].pb(mp(b , mp(c , d)));
g[b].pb(mp(a , mp(d , c)));
ed.pb(mp(mp(a , b) , mp(c , d)));
}
calc1(1 , 0);
solve1(1 , 0 , 0);
solve2(1 , 0 , 0);
go(root , 0);
fix[root] = 1;
for(int i=1; i<=n; i++){
t[pown + in[i] - 1] = mp(d[i] , i);
upd((pown + in[i] - 1) / 2);
}
delet(e);
delet(f);
for(int i=3; i<=n; i++){
ans[i] = ans[i - 1] + t[1].f;
delet(t[1].s);
}
cin >> q;
while(q--){
ll x;
cin >> x;
cout << al - ans[x] << '\n';
}
return 0;
}
Compilation message
designated_cities.cpp: In function 'void calc1(long long int, long long int)':
designated_cities.cpp:15:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<g[x].size(); i++)
~^~~~~~~~~~~~
designated_cities.cpp: In function 'void solve1(long long int, long long int, long long int)':
designated_cities.cpp:24:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<g[x].size(); i++)
~^~~~~~~~~~~~
designated_cities.cpp: In function 'void solve2(long long int, long long int, long long int)':
designated_cities.cpp:30:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<g[x].size(); i++)
~^~~~~~~~~~~~
designated_cities.cpp: In function 'void go(long long int, long long int)':
designated_cities.cpp:56:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<g[x].size(); i++)
~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5112 KB |
Output is correct |
2 |
Correct |
8 ms |
5116 KB |
Output is correct |
3 |
Correct |
8 ms |
5112 KB |
Output is correct |
4 |
Correct |
8 ms |
5112 KB |
Output is correct |
5 |
Correct |
7 ms |
5112 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
7 ms |
5112 KB |
Output is correct |
8 |
Correct |
7 ms |
5112 KB |
Output is correct |
9 |
Correct |
8 ms |
5112 KB |
Output is correct |
10 |
Correct |
8 ms |
5112 KB |
Output is correct |
11 |
Correct |
8 ms |
5112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
563 ms |
53540 KB |
Output is correct |
3 |
Correct |
579 ms |
71504 KB |
Output is correct |
4 |
Correct |
514 ms |
53072 KB |
Output is correct |
5 |
Correct |
538 ms |
53328 KB |
Output is correct |
6 |
Correct |
560 ms |
56144 KB |
Output is correct |
7 |
Correct |
473 ms |
52088 KB |
Output is correct |
8 |
Correct |
598 ms |
72144 KB |
Output is correct |
9 |
Correct |
369 ms |
48184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
539 ms |
53720 KB |
Output is correct |
3 |
Correct |
590 ms |
74584 KB |
Output is correct |
4 |
Correct |
506 ms |
53840 KB |
Output is correct |
5 |
Correct |
549 ms |
54092 KB |
Output is correct |
6 |
Correct |
564 ms |
56752 KB |
Output is correct |
7 |
Correct |
406 ms |
51152 KB |
Output is correct |
8 |
Correct |
590 ms |
65488 KB |
Output is correct |
9 |
Correct |
370 ms |
48564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5112 KB |
Output is correct |
2 |
Correct |
8 ms |
5116 KB |
Output is correct |
3 |
Correct |
8 ms |
5112 KB |
Output is correct |
4 |
Correct |
8 ms |
5112 KB |
Output is correct |
5 |
Correct |
7 ms |
5112 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
7 ms |
5112 KB |
Output is correct |
8 |
Correct |
7 ms |
5112 KB |
Output is correct |
9 |
Correct |
8 ms |
5112 KB |
Output is correct |
10 |
Correct |
8 ms |
5112 KB |
Output is correct |
11 |
Correct |
8 ms |
5112 KB |
Output is correct |
12 |
Correct |
7 ms |
5116 KB |
Output is correct |
13 |
Correct |
14 ms |
5624 KB |
Output is correct |
14 |
Correct |
14 ms |
5868 KB |
Output is correct |
15 |
Correct |
14 ms |
5624 KB |
Output is correct |
16 |
Correct |
14 ms |
5624 KB |
Output is correct |
17 |
Correct |
14 ms |
5624 KB |
Output is correct |
18 |
Correct |
15 ms |
5624 KB |
Output is correct |
19 |
Correct |
14 ms |
5624 KB |
Output is correct |
20 |
Correct |
14 ms |
5624 KB |
Output is correct |
21 |
Correct |
15 ms |
5752 KB |
Output is correct |
22 |
Correct |
15 ms |
5624 KB |
Output is correct |
23 |
Correct |
14 ms |
5624 KB |
Output is correct |
24 |
Correct |
14 ms |
5756 KB |
Output is correct |
25 |
Correct |
15 ms |
5880 KB |
Output is correct |
26 |
Correct |
14 ms |
5752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
563 ms |
53540 KB |
Output is correct |
3 |
Correct |
579 ms |
71504 KB |
Output is correct |
4 |
Correct |
514 ms |
53072 KB |
Output is correct |
5 |
Correct |
538 ms |
53328 KB |
Output is correct |
6 |
Correct |
560 ms |
56144 KB |
Output is correct |
7 |
Correct |
473 ms |
52088 KB |
Output is correct |
8 |
Correct |
598 ms |
72144 KB |
Output is correct |
9 |
Correct |
369 ms |
48184 KB |
Output is correct |
10 |
Correct |
7 ms |
5112 KB |
Output is correct |
11 |
Correct |
539 ms |
53720 KB |
Output is correct |
12 |
Correct |
590 ms |
74584 KB |
Output is correct |
13 |
Correct |
506 ms |
53840 KB |
Output is correct |
14 |
Correct |
549 ms |
54092 KB |
Output is correct |
15 |
Correct |
564 ms |
56752 KB |
Output is correct |
16 |
Correct |
406 ms |
51152 KB |
Output is correct |
17 |
Correct |
590 ms |
65488 KB |
Output is correct |
18 |
Correct |
370 ms |
48564 KB |
Output is correct |
19 |
Correct |
7 ms |
5112 KB |
Output is correct |
20 |
Correct |
555 ms |
53848 KB |
Output is correct |
21 |
Correct |
596 ms |
78672 KB |
Output is correct |
22 |
Correct |
509 ms |
56144 KB |
Output is correct |
23 |
Correct |
566 ms |
57936 KB |
Output is correct |
24 |
Correct |
548 ms |
56784 KB |
Output is correct |
25 |
Correct |
560 ms |
58064 KB |
Output is correct |
26 |
Correct |
552 ms |
56656 KB |
Output is correct |
27 |
Correct |
533 ms |
57300 KB |
Output is correct |
28 |
Correct |
569 ms |
60092 KB |
Output is correct |
29 |
Correct |
556 ms |
58192 KB |
Output is correct |
30 |
Correct |
525 ms |
56016 KB |
Output is correct |
31 |
Correct |
442 ms |
55812 KB |
Output is correct |
32 |
Correct |
595 ms |
70172 KB |
Output is correct |
33 |
Correct |
373 ms |
52280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5112 KB |
Output is correct |
2 |
Correct |
8 ms |
5116 KB |
Output is correct |
3 |
Correct |
8 ms |
5112 KB |
Output is correct |
4 |
Correct |
8 ms |
5112 KB |
Output is correct |
5 |
Correct |
7 ms |
5112 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
7 ms |
5112 KB |
Output is correct |
8 |
Correct |
7 ms |
5112 KB |
Output is correct |
9 |
Correct |
8 ms |
5112 KB |
Output is correct |
10 |
Correct |
8 ms |
5112 KB |
Output is correct |
11 |
Correct |
8 ms |
5112 KB |
Output is correct |
12 |
Correct |
7 ms |
5112 KB |
Output is correct |
13 |
Correct |
563 ms |
53540 KB |
Output is correct |
14 |
Correct |
579 ms |
71504 KB |
Output is correct |
15 |
Correct |
514 ms |
53072 KB |
Output is correct |
16 |
Correct |
538 ms |
53328 KB |
Output is correct |
17 |
Correct |
560 ms |
56144 KB |
Output is correct |
18 |
Correct |
473 ms |
52088 KB |
Output is correct |
19 |
Correct |
598 ms |
72144 KB |
Output is correct |
20 |
Correct |
369 ms |
48184 KB |
Output is correct |
21 |
Correct |
7 ms |
5112 KB |
Output is correct |
22 |
Correct |
539 ms |
53720 KB |
Output is correct |
23 |
Correct |
590 ms |
74584 KB |
Output is correct |
24 |
Correct |
506 ms |
53840 KB |
Output is correct |
25 |
Correct |
549 ms |
54092 KB |
Output is correct |
26 |
Correct |
564 ms |
56752 KB |
Output is correct |
27 |
Correct |
406 ms |
51152 KB |
Output is correct |
28 |
Correct |
590 ms |
65488 KB |
Output is correct |
29 |
Correct |
370 ms |
48564 KB |
Output is correct |
30 |
Correct |
7 ms |
5116 KB |
Output is correct |
31 |
Correct |
14 ms |
5624 KB |
Output is correct |
32 |
Correct |
14 ms |
5868 KB |
Output is correct |
33 |
Correct |
14 ms |
5624 KB |
Output is correct |
34 |
Correct |
14 ms |
5624 KB |
Output is correct |
35 |
Correct |
14 ms |
5624 KB |
Output is correct |
36 |
Correct |
15 ms |
5624 KB |
Output is correct |
37 |
Correct |
14 ms |
5624 KB |
Output is correct |
38 |
Correct |
14 ms |
5624 KB |
Output is correct |
39 |
Correct |
15 ms |
5752 KB |
Output is correct |
40 |
Correct |
15 ms |
5624 KB |
Output is correct |
41 |
Correct |
14 ms |
5624 KB |
Output is correct |
42 |
Correct |
14 ms |
5756 KB |
Output is correct |
43 |
Correct |
15 ms |
5880 KB |
Output is correct |
44 |
Correct |
14 ms |
5752 KB |
Output is correct |
45 |
Correct |
7 ms |
5112 KB |
Output is correct |
46 |
Correct |
555 ms |
53848 KB |
Output is correct |
47 |
Correct |
596 ms |
78672 KB |
Output is correct |
48 |
Correct |
509 ms |
56144 KB |
Output is correct |
49 |
Correct |
566 ms |
57936 KB |
Output is correct |
50 |
Correct |
548 ms |
56784 KB |
Output is correct |
51 |
Correct |
560 ms |
58064 KB |
Output is correct |
52 |
Correct |
552 ms |
56656 KB |
Output is correct |
53 |
Correct |
533 ms |
57300 KB |
Output is correct |
54 |
Correct |
569 ms |
60092 KB |
Output is correct |
55 |
Correct |
556 ms |
58192 KB |
Output is correct |
56 |
Correct |
525 ms |
56016 KB |
Output is correct |
57 |
Correct |
442 ms |
55812 KB |
Output is correct |
58 |
Correct |
595 ms |
70172 KB |
Output is correct |
59 |
Correct |
373 ms |
52280 KB |
Output is correct |
60 |
Correct |
7 ms |
5112 KB |
Output is correct |
61 |
Correct |
969 ms |
60240 KB |
Output is correct |
62 |
Correct |
995 ms |
77376 KB |
Output is correct |
63 |
Correct |
954 ms |
59032 KB |
Output is correct |
64 |
Correct |
966 ms |
61024 KB |
Output is correct |
65 |
Correct |
996 ms |
59220 KB |
Output is correct |
66 |
Correct |
972 ms |
60700 KB |
Output is correct |
67 |
Correct |
957 ms |
59252 KB |
Output is correct |
68 |
Correct |
975 ms |
60240 KB |
Output is correct |
69 |
Correct |
962 ms |
62544 KB |
Output is correct |
70 |
Correct |
961 ms |
60888 KB |
Output is correct |
71 |
Correct |
929 ms |
58648 KB |
Output is correct |
72 |
Correct |
882 ms |
59528 KB |
Output is correct |
73 |
Correct |
1036 ms |
70748 KB |
Output is correct |
74 |
Correct |
822 ms |
56752 KB |
Output is correct |