#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
#define pll pair<long long,long long>
#define eb emplace_back
#define pli pair<long long,int>
#define int long long
pii cmp(pli &p, pli &q){
if (p.fi>q.fi)return p;
return q;
}
struct IT{
vector<pli>st;
vector<long long>lazy;
void resz(int n){
st.resize(4*n+9);
lazy.resize(4*n+9);
}
void push(int id){
if (lazy[id]){
lazy[id*2]+=lazy[id];
lazy[id*2+1]+=lazy[id];
st[id*2].fi+=lazy[id];
st[id*2+1].fi+=lazy[id];
lazy[id]=0;
}
}
void update(int id,int l,int r,int u,int v,long long val){
if (l>v||r<u||u>v)return;
if (u<=l&&r<=v){
st[id].fi+=val;
lazy[id]+=val;
if (l==r)st[id].se=l;
return;
}
push(id);
int mid=(l+r)/2;
update(id*2,l,mid,u,v,val);
update(id*2+1,mid+1,r,u,v,val);
st[id]=cmp(st[id*2],st[id*2+1]);
}
};
const int maxn=2e5+9;
vector<pil>a[maxn];
long long f[maxn];//subtree
long long g[maxn];//g[i] if i root
pli dfs1(int u,int par){
pli test={0,u};
for (auto v:a[u]){
if (v.fi==par)continue;
auto tmp=dfs1(v.fi,u);
tmp.fi+=v.se;
test=cmp(test,tmp);
f[u]+=v.se+f[v.fi];
}
return test;
}
void dfs2(int u,int par){
for (auto v:a[u]){
if (v.fi==par){
g[u]+=v.se;
}
}
for (auto v:a[u]){
if (v.fi==par)continue;
g[v.fi]=g[u]-v.se;
dfs2(v.fi,u);
}
}
long long dep[maxn],ans[maxn],w[maxn];
int in[maxn],out[maxn],rev[maxn],tme=0,p[maxn];
bool vis[maxn];
void dfs3(int u,int par){
in[u]=++tme;
rev[tme]=u;
for (auto v:a[u]){
if (v.fi==par)continue;
dep[v.fi]=dep[u]+v.se;
w[v.fi]=v.se;
p[v.fi]=u;
dfs3(v.fi,u);
}
out[u]=tme;
}
IT st;
int n;
void del(int u){
while (u){
if (vis[u])return;
st.update(1,1,n,in[u],out[u],-w[u]);
vis[u]=1;
u=p[u];
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
cin>>n;
for1(i,1,n-1){
int u,v;
long long x,y;
cin>>u>>v>>x>>y;
a[u].pb({v,x});
a[v].pb({u,y});
}
dfs1(1,0);
g[1]=f[1];
dfs2(1,0);
int r1=min_element(g+1,g+1+n)-g;
ans[1]=g[r1];
int root=dfs1(r1,0).se;
vis[root]=1;
dfs3(root,0);
st.resz(n);
for1(i,1,n){
st.update(1,1,n,in[i],in[i],dep[i]);
}
long long sum=g[root];
for1(i,2,n){
if (st.st[1].fi==0){
ans[i]=sum;
continue;
}
sum-=st.st[1].fi;
ans[i]=sum;
del(rev[st.st[1].se]);
}
int q;
cin>>q;
for1(i,1,q){
int x;
cin>>x;
cout<<ans[x]<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5008 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5080 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
362 ms |
48324 KB |
Output is correct |
3 |
Correct |
360 ms |
63756 KB |
Output is correct |
4 |
Correct |
320 ms |
48240 KB |
Output is correct |
5 |
Correct |
340 ms |
48192 KB |
Output is correct |
6 |
Correct |
346 ms |
50552 KB |
Output is correct |
7 |
Correct |
314 ms |
47708 KB |
Output is correct |
8 |
Correct |
397 ms |
64812 KB |
Output is correct |
9 |
Correct |
332 ms |
45880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
388 ms |
48288 KB |
Output is correct |
3 |
Correct |
453 ms |
67076 KB |
Output is correct |
4 |
Correct |
316 ms |
48300 KB |
Output is correct |
5 |
Correct |
416 ms |
48204 KB |
Output is correct |
6 |
Correct |
390 ms |
51324 KB |
Output is correct |
7 |
Correct |
350 ms |
47448 KB |
Output is correct |
8 |
Correct |
470 ms |
58304 KB |
Output is correct |
9 |
Correct |
328 ms |
45876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5008 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5080 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
4 ms |
5460 KB |
Output is correct |
14 |
Correct |
4 ms |
5716 KB |
Output is correct |
15 |
Correct |
4 ms |
5460 KB |
Output is correct |
16 |
Correct |
5 ms |
5460 KB |
Output is correct |
17 |
Correct |
4 ms |
5460 KB |
Output is correct |
18 |
Correct |
4 ms |
5460 KB |
Output is correct |
19 |
Correct |
5 ms |
5460 KB |
Output is correct |
20 |
Correct |
5 ms |
5508 KB |
Output is correct |
21 |
Correct |
5 ms |
5468 KB |
Output is correct |
22 |
Correct |
5 ms |
5516 KB |
Output is correct |
23 |
Correct |
4 ms |
5460 KB |
Output is correct |
24 |
Correct |
6 ms |
5464 KB |
Output is correct |
25 |
Correct |
5 ms |
5588 KB |
Output is correct |
26 |
Correct |
4 ms |
5460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
362 ms |
48324 KB |
Output is correct |
3 |
Correct |
360 ms |
63756 KB |
Output is correct |
4 |
Correct |
320 ms |
48240 KB |
Output is correct |
5 |
Correct |
340 ms |
48192 KB |
Output is correct |
6 |
Correct |
346 ms |
50552 KB |
Output is correct |
7 |
Correct |
314 ms |
47708 KB |
Output is correct |
8 |
Correct |
397 ms |
64812 KB |
Output is correct |
9 |
Correct |
332 ms |
45880 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
388 ms |
48288 KB |
Output is correct |
12 |
Correct |
453 ms |
67076 KB |
Output is correct |
13 |
Correct |
316 ms |
48300 KB |
Output is correct |
14 |
Correct |
416 ms |
48204 KB |
Output is correct |
15 |
Correct |
390 ms |
51324 KB |
Output is correct |
16 |
Correct |
350 ms |
47448 KB |
Output is correct |
17 |
Correct |
470 ms |
58304 KB |
Output is correct |
18 |
Correct |
328 ms |
45876 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
404 ms |
48228 KB |
Output is correct |
21 |
Correct |
398 ms |
66512 KB |
Output is correct |
22 |
Correct |
340 ms |
48236 KB |
Output is correct |
23 |
Correct |
411 ms |
48572 KB |
Output is correct |
24 |
Correct |
416 ms |
48528 KB |
Output is correct |
25 |
Correct |
408 ms |
48624 KB |
Output is correct |
26 |
Correct |
357 ms |
48488 KB |
Output is correct |
27 |
Correct |
377 ms |
48236 KB |
Output is correct |
28 |
Correct |
379 ms |
50472 KB |
Output is correct |
29 |
Correct |
404 ms |
48824 KB |
Output is correct |
30 |
Correct |
340 ms |
48088 KB |
Output is correct |
31 |
Correct |
327 ms |
47548 KB |
Output is correct |
32 |
Correct |
427 ms |
59052 KB |
Output is correct |
33 |
Correct |
313 ms |
45856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5008 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5080 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
362 ms |
48324 KB |
Output is correct |
14 |
Correct |
360 ms |
63756 KB |
Output is correct |
15 |
Correct |
320 ms |
48240 KB |
Output is correct |
16 |
Correct |
340 ms |
48192 KB |
Output is correct |
17 |
Correct |
346 ms |
50552 KB |
Output is correct |
18 |
Correct |
314 ms |
47708 KB |
Output is correct |
19 |
Correct |
397 ms |
64812 KB |
Output is correct |
20 |
Correct |
332 ms |
45880 KB |
Output is correct |
21 |
Correct |
2 ms |
5076 KB |
Output is correct |
22 |
Correct |
388 ms |
48288 KB |
Output is correct |
23 |
Correct |
453 ms |
67076 KB |
Output is correct |
24 |
Correct |
316 ms |
48300 KB |
Output is correct |
25 |
Correct |
416 ms |
48204 KB |
Output is correct |
26 |
Correct |
390 ms |
51324 KB |
Output is correct |
27 |
Correct |
350 ms |
47448 KB |
Output is correct |
28 |
Correct |
470 ms |
58304 KB |
Output is correct |
29 |
Correct |
328 ms |
45876 KB |
Output is correct |
30 |
Correct |
3 ms |
5076 KB |
Output is correct |
31 |
Correct |
4 ms |
5460 KB |
Output is correct |
32 |
Correct |
4 ms |
5716 KB |
Output is correct |
33 |
Correct |
4 ms |
5460 KB |
Output is correct |
34 |
Correct |
5 ms |
5460 KB |
Output is correct |
35 |
Correct |
4 ms |
5460 KB |
Output is correct |
36 |
Correct |
4 ms |
5460 KB |
Output is correct |
37 |
Correct |
5 ms |
5460 KB |
Output is correct |
38 |
Correct |
5 ms |
5508 KB |
Output is correct |
39 |
Correct |
5 ms |
5468 KB |
Output is correct |
40 |
Correct |
5 ms |
5516 KB |
Output is correct |
41 |
Correct |
4 ms |
5460 KB |
Output is correct |
42 |
Correct |
6 ms |
5464 KB |
Output is correct |
43 |
Correct |
5 ms |
5588 KB |
Output is correct |
44 |
Correct |
4 ms |
5460 KB |
Output is correct |
45 |
Correct |
3 ms |
5076 KB |
Output is correct |
46 |
Correct |
404 ms |
48228 KB |
Output is correct |
47 |
Correct |
398 ms |
66512 KB |
Output is correct |
48 |
Correct |
340 ms |
48236 KB |
Output is correct |
49 |
Correct |
411 ms |
48572 KB |
Output is correct |
50 |
Correct |
416 ms |
48528 KB |
Output is correct |
51 |
Correct |
408 ms |
48624 KB |
Output is correct |
52 |
Correct |
357 ms |
48488 KB |
Output is correct |
53 |
Correct |
377 ms |
48236 KB |
Output is correct |
54 |
Correct |
379 ms |
50472 KB |
Output is correct |
55 |
Correct |
404 ms |
48824 KB |
Output is correct |
56 |
Correct |
340 ms |
48088 KB |
Output is correct |
57 |
Correct |
327 ms |
47548 KB |
Output is correct |
58 |
Correct |
427 ms |
59052 KB |
Output is correct |
59 |
Correct |
313 ms |
45856 KB |
Output is correct |
60 |
Correct |
2 ms |
5076 KB |
Output is correct |
61 |
Correct |
391 ms |
49612 KB |
Output is correct |
62 |
Correct |
444 ms |
66528 KB |
Output is correct |
63 |
Correct |
373 ms |
49672 KB |
Output is correct |
64 |
Correct |
406 ms |
49940 KB |
Output is correct |
65 |
Correct |
451 ms |
49772 KB |
Output is correct |
66 |
Correct |
376 ms |
49940 KB |
Output is correct |
67 |
Correct |
388 ms |
49732 KB |
Output is correct |
68 |
Correct |
402 ms |
49860 KB |
Output is correct |
69 |
Correct |
503 ms |
52356 KB |
Output is correct |
70 |
Correct |
416 ms |
50232 KB |
Output is correct |
71 |
Correct |
388 ms |
49592 KB |
Output is correct |
72 |
Correct |
412 ms |
49712 KB |
Output is correct |
73 |
Correct |
438 ms |
61736 KB |
Output is correct |
74 |
Correct |
384 ms |
48764 KB |
Output is correct |