#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>
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;
//cout<<"ROOT "<<root<<'\n';
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;
//cout<<i<<" "<<rev[st.st[1].se]<<'\n';
continue;
}
sum-=st.st[1].fi;
ans[i]=sum;
//cout<<rev[st.st[1].se]<<" ";
del(rev[st.st[1].se]);
}
int q;
cin>>q;
for1(i,1,q){
int x;
cin>>x;
cout<<ans[x]<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Incorrect |
2 ms |
5076 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
269 ms |
45100 KB |
Output is correct |
3 |
Correct |
393 ms |
60560 KB |
Output is correct |
4 |
Correct |
312 ms |
45176 KB |
Output is correct |
5 |
Correct |
263 ms |
45044 KB |
Output is correct |
6 |
Correct |
263 ms |
47436 KB |
Output is correct |
7 |
Correct |
325 ms |
44504 KB |
Output is correct |
8 |
Correct |
354 ms |
61660 KB |
Output is correct |
9 |
Correct |
290 ms |
42728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Incorrect |
258 ms |
45116 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Incorrect |
2 ms |
5076 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
269 ms |
45100 KB |
Output is correct |
3 |
Correct |
393 ms |
60560 KB |
Output is correct |
4 |
Correct |
312 ms |
45176 KB |
Output is correct |
5 |
Correct |
263 ms |
45044 KB |
Output is correct |
6 |
Correct |
263 ms |
47436 KB |
Output is correct |
7 |
Correct |
325 ms |
44504 KB |
Output is correct |
8 |
Correct |
354 ms |
61660 KB |
Output is correct |
9 |
Correct |
290 ms |
42728 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Incorrect |
258 ms |
45116 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Incorrect |
2 ms |
5076 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |