#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+2;
const int inf=1e16+2;
int n,ans[N],dis[N],idx=0,idx1=0;
vector<pair<pair<int,int>,int> > adj[N];
int it_max[4*N],lazy[4*N],tin[N],tout[N],now=0,par[N],sum1=0,wei[N],pos[N];
bool used[N];
void build(int idx,int l,int r){
lazy[idx]=0;
if(l==r){
it_max[idx]=dis[pos[l]];
return;
}
build(2*idx,l,(l+r)/2);
build(2*idx+1,(l+r)/2+1,r);
it_max[idx]=max(it_max[2*idx],it_max[2*idx+1]);
}
void push(int idx,int l,int r){
if(l<r){
it_max[2*idx]+=lazy[idx];
it_max[2*idx+1]+=lazy[idx];
lazy[2*idx]+=lazy[idx];
lazy[2*idx+1]+=lazy[idx];
}
lazy[idx]=0;
return;
}
void add(int idx,int l,int r,int lef,int rig,int val){
if(lef>rig){
return;
}
if(l>rig||r<lef){
return;
}
if(l>=lef&&r<=rig){
it_max[idx]+=val;
lazy[idx]+=val;
return;
}
push(idx,l,r);
add(2*idx,l,(l+r)/2,lef,rig,val);
add(2*idx+1,(l+r)/2+1,r,lef,rig,val);
it_max[idx]=max(it_max[2*idx],it_max[2*idx+1]);
}
int trace(int idx,int l,int r){
if(l==r){
return pos[l];
}
push(idx,l,r);
if(it_max[2*idx]==it_max[idx]){
return trace(2*idx,l,(l+r)/2);
}
assert(it_max[2*idx+1]==it_max[idx]);
return trace(2*idx+1,(l+r)/2+1,r);
}
void dfs1(int x,int p){
now++;
tin[x]=now;
pos[now]=x;
for(auto i:adj[x]){
if(i.first.first!=p){
dis[i.first.first]=dis[x]+i.first.second;
dfs1(i.first.first,x);
sum1+=i.first.second;
}
}
tout[x]=now;
}
void dfs(int x,int p){
ans[1]=min(ans[1],sum1);
if(ans[2]>sum1-it_max[1]){
ans[2]=sum1-it_max[1];
idx=x;
idx1=trace(1,1,n);
}
for(auto i:adj[x]){
if(i.first.first!=p){
sum1-=i.first.second;
sum1+=i.second;
add(1,1,n,tin[i.first.first],tout[i.first.first],-i.first.second);
add(1,1,n,1,tin[i.first.first]-1,i.second);
add(1,1,n,tout[i.first.first]+1,n,i.second);
dfs(i.first.first,x);
add(1,1,n,tin[i.first.first],tout[i.first.first],i.first.second);
add(1,1,n,1,tin[i.first.first]-1,-i.second);
add(1,1,n,tout[i.first.first]+1,n,-i.second);
sum1+=i.first.second;
sum1-=i.second;
}
}
}
void dfs2(int x,int p){
now++;
tin[x]=now;
pos[now]=x;
for(auto i:adj[x]){
if(i.first.first!=p){
par[i.first.first]=x;
dis[i.first.first]=dis[x]+i.first.second;
sum1+=i.first.second;
wei[i.first.first]=i.first.second;
dfs2(i.first.first,x);
}
}
tout[x]=now;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int i,j,k,l,q,m;
cin>>n;
for(i=1;i<n;i++){
cin>>j>>k>>l>>m;
adj[j].push_back({{k,l},m});
adj[k].push_back({{j,m},l});
ans[i]=inf;
}
ans[n]=inf;
dfs1(1,1);
build(1,1,n);
dfs(1,1);
dis[idx]=sum1=now=0;
dfs2(idx,idx);
build(1,1,n);
used[idx]=true;
for(i=2;i<=n;i++){
ans[i]=sum1-it_max[1];
sum1-=it_max[1];
j=trace(1,1,n);
while(!used[j]){
used[j]=true;
add(1,1,n,tin[j],tout[j],-wei[j]);
j=par[j];
}
}
cin>>q;
for(i=1;i<=q;i++){
cin>>j;
cout<<ans[j]<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5240 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
7 ms |
5112 KB |
Output is correct |
4 |
Correct |
7 ms |
5112 KB |
Output is correct |
5 |
Correct |
7 ms |
5112 KB |
Output is correct |
6 |
Correct |
6 ms |
5116 KB |
Output is correct |
7 |
Correct |
7 ms |
5112 KB |
Output is correct |
8 |
Correct |
7 ms |
5112 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
7 ms |
5112 KB |
Output is correct |
11 |
Correct |
7 ms |
5116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
908 ms |
44048 KB |
Output is correct |
3 |
Correct |
947 ms |
56696 KB |
Output is correct |
4 |
Correct |
877 ms |
42616 KB |
Output is correct |
5 |
Correct |
922 ms |
43688 KB |
Output is correct |
6 |
Correct |
965 ms |
45944 KB |
Output is correct |
7 |
Correct |
864 ms |
42784 KB |
Output is correct |
8 |
Correct |
1051 ms |
57168 KB |
Output is correct |
9 |
Correct |
846 ms |
41880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
926 ms |
44068 KB |
Output is correct |
3 |
Correct |
929 ms |
58872 KB |
Output is correct |
4 |
Correct |
892 ms |
42616 KB |
Output is correct |
5 |
Correct |
889 ms |
43688 KB |
Output is correct |
6 |
Correct |
987 ms |
46328 KB |
Output is correct |
7 |
Correct |
844 ms |
41880 KB |
Output is correct |
8 |
Correct |
1027 ms |
52600 KB |
Output is correct |
9 |
Correct |
828 ms |
41492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5240 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
7 ms |
5112 KB |
Output is correct |
4 |
Correct |
7 ms |
5112 KB |
Output is correct |
5 |
Correct |
7 ms |
5112 KB |
Output is correct |
6 |
Correct |
6 ms |
5116 KB |
Output is correct |
7 |
Correct |
7 ms |
5112 KB |
Output is correct |
8 |
Correct |
7 ms |
5112 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
7 ms |
5112 KB |
Output is correct |
11 |
Correct |
7 ms |
5116 KB |
Output is correct |
12 |
Correct |
6 ms |
5112 KB |
Output is correct |
13 |
Correct |
11 ms |
5496 KB |
Output is correct |
14 |
Correct |
11 ms |
5624 KB |
Output is correct |
15 |
Correct |
11 ms |
5496 KB |
Output is correct |
16 |
Correct |
11 ms |
5496 KB |
Output is correct |
17 |
Correct |
11 ms |
5496 KB |
Output is correct |
18 |
Correct |
11 ms |
5496 KB |
Output is correct |
19 |
Correct |
15 ms |
5496 KB |
Output is correct |
20 |
Correct |
11 ms |
5496 KB |
Output is correct |
21 |
Correct |
11 ms |
5468 KB |
Output is correct |
22 |
Correct |
11 ms |
5496 KB |
Output is correct |
23 |
Correct |
11 ms |
5496 KB |
Output is correct |
24 |
Correct |
11 ms |
5496 KB |
Output is correct |
25 |
Correct |
22 ms |
5596 KB |
Output is correct |
26 |
Correct |
11 ms |
5496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
908 ms |
44048 KB |
Output is correct |
3 |
Correct |
947 ms |
56696 KB |
Output is correct |
4 |
Correct |
877 ms |
42616 KB |
Output is correct |
5 |
Correct |
922 ms |
43688 KB |
Output is correct |
6 |
Correct |
965 ms |
45944 KB |
Output is correct |
7 |
Correct |
864 ms |
42784 KB |
Output is correct |
8 |
Correct |
1051 ms |
57168 KB |
Output is correct |
9 |
Correct |
846 ms |
41880 KB |
Output is correct |
10 |
Correct |
7 ms |
5112 KB |
Output is correct |
11 |
Correct |
926 ms |
44068 KB |
Output is correct |
12 |
Correct |
929 ms |
58872 KB |
Output is correct |
13 |
Correct |
892 ms |
42616 KB |
Output is correct |
14 |
Correct |
889 ms |
43688 KB |
Output is correct |
15 |
Correct |
987 ms |
46328 KB |
Output is correct |
16 |
Correct |
844 ms |
41880 KB |
Output is correct |
17 |
Correct |
1027 ms |
52600 KB |
Output is correct |
18 |
Correct |
828 ms |
41492 KB |
Output is correct |
19 |
Correct |
6 ms |
5112 KB |
Output is correct |
20 |
Correct |
917 ms |
43948 KB |
Output is correct |
21 |
Correct |
998 ms |
59000 KB |
Output is correct |
22 |
Correct |
873 ms |
42536 KB |
Output is correct |
23 |
Correct |
948 ms |
44420 KB |
Output is correct |
24 |
Correct |
941 ms |
43104 KB |
Output is correct |
25 |
Correct |
918 ms |
44280 KB |
Output is correct |
26 |
Correct |
936 ms |
43000 KB |
Output is correct |
27 |
Correct |
926 ms |
43856 KB |
Output is correct |
28 |
Correct |
970 ms |
45908 KB |
Output is correct |
29 |
Correct |
955 ms |
44536 KB |
Output is correct |
30 |
Correct |
896 ms |
42636 KB |
Output is correct |
31 |
Correct |
896 ms |
42532 KB |
Output is correct |
32 |
Correct |
1051 ms |
53112 KB |
Output is correct |
33 |
Correct |
932 ms |
41988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5240 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
7 ms |
5112 KB |
Output is correct |
4 |
Correct |
7 ms |
5112 KB |
Output is correct |
5 |
Correct |
7 ms |
5112 KB |
Output is correct |
6 |
Correct |
6 ms |
5116 KB |
Output is correct |
7 |
Correct |
7 ms |
5112 KB |
Output is correct |
8 |
Correct |
7 ms |
5112 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
7 ms |
5112 KB |
Output is correct |
11 |
Correct |
7 ms |
5116 KB |
Output is correct |
12 |
Correct |
6 ms |
5112 KB |
Output is correct |
13 |
Correct |
908 ms |
44048 KB |
Output is correct |
14 |
Correct |
947 ms |
56696 KB |
Output is correct |
15 |
Correct |
877 ms |
42616 KB |
Output is correct |
16 |
Correct |
922 ms |
43688 KB |
Output is correct |
17 |
Correct |
965 ms |
45944 KB |
Output is correct |
18 |
Correct |
864 ms |
42784 KB |
Output is correct |
19 |
Correct |
1051 ms |
57168 KB |
Output is correct |
20 |
Correct |
846 ms |
41880 KB |
Output is correct |
21 |
Correct |
7 ms |
5112 KB |
Output is correct |
22 |
Correct |
926 ms |
44068 KB |
Output is correct |
23 |
Correct |
929 ms |
58872 KB |
Output is correct |
24 |
Correct |
892 ms |
42616 KB |
Output is correct |
25 |
Correct |
889 ms |
43688 KB |
Output is correct |
26 |
Correct |
987 ms |
46328 KB |
Output is correct |
27 |
Correct |
844 ms |
41880 KB |
Output is correct |
28 |
Correct |
1027 ms |
52600 KB |
Output is correct |
29 |
Correct |
828 ms |
41492 KB |
Output is correct |
30 |
Correct |
6 ms |
5112 KB |
Output is correct |
31 |
Correct |
11 ms |
5496 KB |
Output is correct |
32 |
Correct |
11 ms |
5624 KB |
Output is correct |
33 |
Correct |
11 ms |
5496 KB |
Output is correct |
34 |
Correct |
11 ms |
5496 KB |
Output is correct |
35 |
Correct |
11 ms |
5496 KB |
Output is correct |
36 |
Correct |
11 ms |
5496 KB |
Output is correct |
37 |
Correct |
15 ms |
5496 KB |
Output is correct |
38 |
Correct |
11 ms |
5496 KB |
Output is correct |
39 |
Correct |
11 ms |
5468 KB |
Output is correct |
40 |
Correct |
11 ms |
5496 KB |
Output is correct |
41 |
Correct |
11 ms |
5496 KB |
Output is correct |
42 |
Correct |
11 ms |
5496 KB |
Output is correct |
43 |
Correct |
22 ms |
5596 KB |
Output is correct |
44 |
Correct |
11 ms |
5496 KB |
Output is correct |
45 |
Correct |
6 ms |
5112 KB |
Output is correct |
46 |
Correct |
917 ms |
43948 KB |
Output is correct |
47 |
Correct |
998 ms |
59000 KB |
Output is correct |
48 |
Correct |
873 ms |
42536 KB |
Output is correct |
49 |
Correct |
948 ms |
44420 KB |
Output is correct |
50 |
Correct |
941 ms |
43104 KB |
Output is correct |
51 |
Correct |
918 ms |
44280 KB |
Output is correct |
52 |
Correct |
936 ms |
43000 KB |
Output is correct |
53 |
Correct |
926 ms |
43856 KB |
Output is correct |
54 |
Correct |
970 ms |
45908 KB |
Output is correct |
55 |
Correct |
955 ms |
44536 KB |
Output is correct |
56 |
Correct |
896 ms |
42636 KB |
Output is correct |
57 |
Correct |
896 ms |
42532 KB |
Output is correct |
58 |
Correct |
1051 ms |
53112 KB |
Output is correct |
59 |
Correct |
932 ms |
41988 KB |
Output is correct |
60 |
Correct |
6 ms |
5112 KB |
Output is correct |
61 |
Correct |
1074 ms |
46632 KB |
Output is correct |
62 |
Correct |
918 ms |
58492 KB |
Output is correct |
63 |
Correct |
890 ms |
45304 KB |
Output is correct |
64 |
Correct |
947 ms |
46968 KB |
Output is correct |
65 |
Correct |
943 ms |
45508 KB |
Output is correct |
66 |
Correct |
955 ms |
47020 KB |
Output is correct |
67 |
Correct |
933 ms |
45500 KB |
Output is correct |
68 |
Correct |
943 ms |
46700 KB |
Output is correct |
69 |
Correct |
971 ms |
48308 KB |
Output is correct |
70 |
Correct |
980 ms |
47068 KB |
Output is correct |
71 |
Correct |
939 ms |
45360 KB |
Output is correct |
72 |
Correct |
898 ms |
45944 KB |
Output is correct |
73 |
Correct |
1041 ms |
54264 KB |
Output is correct |
74 |
Correct |
864 ms |
46100 KB |
Output is correct |