//1L1YA
#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3,unrool-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
#define Pb push_back
#define F first
#define S second
#define endl '\n'
#define sep ' '
#define all(x) x.begin(),x.end()
#define al(x,n) x+1,x+n+1
#define SZ(x) ((int)x.size())
#define lc (id<<1)
#define rc (lc|1)
#define mid (l+r>>1)
#define dokme(x) cout << x << endl, exit(0)
#define sik(x) cout << x << endl;continue;
#define FastIO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define FileIO freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll oo=1e18;
const int mod=1e9+7;
const int inf=1e9+111;
const int N=2e5+11;
const int lg=23;
ll n,s,q,t=1,mx=1,a[N],d[N],dp[N],st[N],et[N],val[N],par[N],ans[N],lz[N<<2];
pll seg[N<<2];
vector<pii> G1[N],G2[N];
bool mark[N];
void dfs1(int v,int p=0){
if(d[v]>d[mx]) mx=v;
for(auto [u,w]: G1[v])
if(u!=p){
d[u]=d[v]+w;
dfs1(u,v);
dp[v]+=dp[u]+w;
}
}
void dfs2(int v,int p=0){
ans[1]=min(ans[1],dp[v]);
for(auto [u,w]: G2[v])
if(u!=p){
dp[u]=dp[v]+w;
dfs2(u,v);
}
}
void dfs3(int v,int p=0){
a[t]=v;st[v]=t++;
for(auto [u,w]: G1[v])
if(u!=p){
val[u]=w;
par[u]=v;
d[u]=d[v]+w;
dfs3(u,v);
s+=w;
}
et[v]=t;
}
void build(int id=1,int l=1,int r=n+1){
if(r-l==1){
seg[id]={d[a[l]],a[l]};
return;
}
build(lc,l,mid);
build(rc,mid,r);
seg[id]=max(seg[lc],seg[rc]);
}
void shift(int id){
seg[lc].F+=lz[id];lz[lc]+=lz[id];
seg[rc].F+=lz[id];lz[rc]+=lz[id];
lz[id]=0;
}
void update(int ql,int qr,int val,int id=1,int l=1,int r=n+1){
if(qr<=l || ql>=r) return;
if(ql<=l && r<=qr){
seg[id].F+=val;
lz[id]+=val;
return;
}
shift(id);
update(ql,qr,val,lc,l,mid);
update(ql,qr,val,rc,mid,r);
seg[id]=max(seg[lc],seg[rc]);
}
int main(){
FastIO
cin >> n;
for(int i=1;i<n;i++){
int u,v,a,b;
cin >> u >> v >> a >> b;
G1[u].Pb({v,a});
G1[v].Pb({u,b});
G2[u].Pb({v,b-a});
G2[v].Pb({u,a-b});
}
dfs1(1);
ans[1]=oo;
dfs2(1);
mark[mx]=1;d[mx]=0;
dfs3(mx);
build();
for(int i=2;i<=n;i++){
int v=seg[1].S;
s-=seg[1].F;
while(!mark[v]){
mark[v]=1;
update(st[v],et[v],-val[v]);
v=par[v];
}
ans[i]=s;
}
cin >> q;
while(q--){
int i;cin >> i;
cout << ans[i] << endl;
}
return 0;
}
Compilation message
designated_cities.cpp: In function 'void build(int, int, int)':
designated_cities.cpp:23:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
23 | #define mid (l+r>>1)
| ~^~
designated_cities.cpp:79:13: note: in expansion of macro 'mid'
79 | build(lc,l,mid);
| ^~~
designated_cities.cpp:23:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
23 | #define mid (l+r>>1)
| ~^~
designated_cities.cpp:80:11: note: in expansion of macro 'mid'
80 | build(rc,mid,r);
| ^~~
designated_cities.cpp: In function 'void update(int, int, int, int, int, int)':
designated_cities.cpp:23:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
23 | #define mid (l+r>>1)
| ~^~
designated_cities.cpp:98:24: note: in expansion of macro 'mid'
98 | update(ql,qr,val,lc,l,mid);
| ^~~
designated_cities.cpp:23:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
23 | #define mid (l+r>>1)
| ~^~
designated_cities.cpp:99:22: note: in expansion of macro 'mid'
99 | update(ql,qr,val,rc,mid,r);
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9696 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
3 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
5 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
277 ms |
55776 KB |
Output is correct |
3 |
Correct |
266 ms |
69460 KB |
Output is correct |
4 |
Correct |
259 ms |
54352 KB |
Output is correct |
5 |
Correct |
236 ms |
55876 KB |
Output is correct |
6 |
Correct |
264 ms |
57940 KB |
Output is correct |
7 |
Correct |
246 ms |
56000 KB |
Output is correct |
8 |
Correct |
309 ms |
69716 KB |
Output is correct |
9 |
Correct |
208 ms |
57052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
257 ms |
55872 KB |
Output is correct |
3 |
Correct |
258 ms |
71504 KB |
Output is correct |
4 |
Correct |
240 ms |
54352 KB |
Output is correct |
5 |
Correct |
246 ms |
55620 KB |
Output is correct |
6 |
Correct |
248 ms |
58196 KB |
Output is correct |
7 |
Correct |
197 ms |
56380 KB |
Output is correct |
8 |
Correct |
299 ms |
65016 KB |
Output is correct |
9 |
Correct |
197 ms |
56624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9696 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
3 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
5 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
4 ms |
9816 KB |
Output is correct |
13 |
Correct |
6 ms |
10332 KB |
Output is correct |
14 |
Correct |
5 ms |
10328 KB |
Output is correct |
15 |
Correct |
6 ms |
10332 KB |
Output is correct |
16 |
Correct |
5 ms |
10252 KB |
Output is correct |
17 |
Correct |
6 ms |
10332 KB |
Output is correct |
18 |
Correct |
6 ms |
10332 KB |
Output is correct |
19 |
Correct |
5 ms |
10292 KB |
Output is correct |
20 |
Correct |
5 ms |
10292 KB |
Output is correct |
21 |
Correct |
5 ms |
10256 KB |
Output is correct |
22 |
Correct |
5 ms |
10160 KB |
Output is correct |
23 |
Correct |
6 ms |
10328 KB |
Output is correct |
24 |
Correct |
5 ms |
10392 KB |
Output is correct |
25 |
Correct |
5 ms |
10328 KB |
Output is correct |
26 |
Correct |
6 ms |
10332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
277 ms |
55776 KB |
Output is correct |
3 |
Correct |
266 ms |
69460 KB |
Output is correct |
4 |
Correct |
259 ms |
54352 KB |
Output is correct |
5 |
Correct |
236 ms |
55876 KB |
Output is correct |
6 |
Correct |
264 ms |
57940 KB |
Output is correct |
7 |
Correct |
246 ms |
56000 KB |
Output is correct |
8 |
Correct |
309 ms |
69716 KB |
Output is correct |
9 |
Correct |
208 ms |
57052 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
257 ms |
55872 KB |
Output is correct |
12 |
Correct |
258 ms |
71504 KB |
Output is correct |
13 |
Correct |
240 ms |
54352 KB |
Output is correct |
14 |
Correct |
246 ms |
55620 KB |
Output is correct |
15 |
Correct |
248 ms |
58196 KB |
Output is correct |
16 |
Correct |
197 ms |
56380 KB |
Output is correct |
17 |
Correct |
299 ms |
65016 KB |
Output is correct |
18 |
Correct |
197 ms |
56624 KB |
Output is correct |
19 |
Correct |
4 ms |
9820 KB |
Output is correct |
20 |
Correct |
246 ms |
55844 KB |
Output is correct |
21 |
Correct |
285 ms |
71764 KB |
Output is correct |
22 |
Correct |
250 ms |
54300 KB |
Output is correct |
23 |
Correct |
241 ms |
55888 KB |
Output is correct |
24 |
Correct |
223 ms |
54712 KB |
Output is correct |
25 |
Correct |
258 ms |
55892 KB |
Output is correct |
26 |
Correct |
249 ms |
54616 KB |
Output is correct |
27 |
Correct |
217 ms |
55692 KB |
Output is correct |
28 |
Correct |
242 ms |
57920 KB |
Output is correct |
29 |
Correct |
276 ms |
55636 KB |
Output is correct |
30 |
Correct |
214 ms |
55104 KB |
Output is correct |
31 |
Correct |
280 ms |
56112 KB |
Output is correct |
32 |
Correct |
268 ms |
65540 KB |
Output is correct |
33 |
Correct |
181 ms |
56880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9696 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
3 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
5 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
4 ms |
9820 KB |
Output is correct |
13 |
Correct |
277 ms |
55776 KB |
Output is correct |
14 |
Correct |
266 ms |
69460 KB |
Output is correct |
15 |
Correct |
259 ms |
54352 KB |
Output is correct |
16 |
Correct |
236 ms |
55876 KB |
Output is correct |
17 |
Correct |
264 ms |
57940 KB |
Output is correct |
18 |
Correct |
246 ms |
56000 KB |
Output is correct |
19 |
Correct |
309 ms |
69716 KB |
Output is correct |
20 |
Correct |
208 ms |
57052 KB |
Output is correct |
21 |
Correct |
4 ms |
9820 KB |
Output is correct |
22 |
Correct |
257 ms |
55872 KB |
Output is correct |
23 |
Correct |
258 ms |
71504 KB |
Output is correct |
24 |
Correct |
240 ms |
54352 KB |
Output is correct |
25 |
Correct |
246 ms |
55620 KB |
Output is correct |
26 |
Correct |
248 ms |
58196 KB |
Output is correct |
27 |
Correct |
197 ms |
56380 KB |
Output is correct |
28 |
Correct |
299 ms |
65016 KB |
Output is correct |
29 |
Correct |
197 ms |
56624 KB |
Output is correct |
30 |
Correct |
4 ms |
9816 KB |
Output is correct |
31 |
Correct |
6 ms |
10332 KB |
Output is correct |
32 |
Correct |
5 ms |
10328 KB |
Output is correct |
33 |
Correct |
6 ms |
10332 KB |
Output is correct |
34 |
Correct |
5 ms |
10252 KB |
Output is correct |
35 |
Correct |
6 ms |
10332 KB |
Output is correct |
36 |
Correct |
6 ms |
10332 KB |
Output is correct |
37 |
Correct |
5 ms |
10292 KB |
Output is correct |
38 |
Correct |
5 ms |
10292 KB |
Output is correct |
39 |
Correct |
5 ms |
10256 KB |
Output is correct |
40 |
Correct |
5 ms |
10160 KB |
Output is correct |
41 |
Correct |
6 ms |
10328 KB |
Output is correct |
42 |
Correct |
5 ms |
10392 KB |
Output is correct |
43 |
Correct |
5 ms |
10328 KB |
Output is correct |
44 |
Correct |
6 ms |
10332 KB |
Output is correct |
45 |
Correct |
4 ms |
9820 KB |
Output is correct |
46 |
Correct |
246 ms |
55844 KB |
Output is correct |
47 |
Correct |
285 ms |
71764 KB |
Output is correct |
48 |
Correct |
250 ms |
54300 KB |
Output is correct |
49 |
Correct |
241 ms |
55888 KB |
Output is correct |
50 |
Correct |
223 ms |
54712 KB |
Output is correct |
51 |
Correct |
258 ms |
55892 KB |
Output is correct |
52 |
Correct |
249 ms |
54616 KB |
Output is correct |
53 |
Correct |
217 ms |
55692 KB |
Output is correct |
54 |
Correct |
242 ms |
57920 KB |
Output is correct |
55 |
Correct |
276 ms |
55636 KB |
Output is correct |
56 |
Correct |
214 ms |
55104 KB |
Output is correct |
57 |
Correct |
280 ms |
56112 KB |
Output is correct |
58 |
Correct |
268 ms |
65540 KB |
Output is correct |
59 |
Correct |
181 ms |
56880 KB |
Output is correct |
60 |
Correct |
4 ms |
9820 KB |
Output is correct |
61 |
Correct |
292 ms |
58392 KB |
Output is correct |
62 |
Correct |
301 ms |
70996 KB |
Output is correct |
63 |
Correct |
242 ms |
57172 KB |
Output is correct |
64 |
Correct |
279 ms |
58504 KB |
Output is correct |
65 |
Correct |
268 ms |
57172 KB |
Output is correct |
66 |
Correct |
279 ms |
58448 KB |
Output is correct |
67 |
Correct |
250 ms |
57172 KB |
Output is correct |
68 |
Correct |
301 ms |
58396 KB |
Output is correct |
69 |
Correct |
268 ms |
60496 KB |
Output is correct |
70 |
Correct |
273 ms |
58192 KB |
Output is correct |
71 |
Correct |
243 ms |
57864 KB |
Output is correct |
72 |
Correct |
246 ms |
59264 KB |
Output is correct |
73 |
Correct |
293 ms |
67664 KB |
Output is correct |
74 |
Correct |
246 ms |
61224 KB |
Output is correct |