#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC optimize("conserve-stack")
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define down cout<<'\n';
#define debug cout<<" cucuucucuuu",down
#define NHP ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define modwwe int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".ans","w",stdout)
#define pb push_back
#define mask(i) (1<<i)
#define checktime cerr << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms";
using namespace std;
#define getchar_unlocked getchar
inline int scan()
{
char c = getchar_unlocked();
int x = 0;
while (c < '0' || c > '9')
{
c = getchar_unlocked();
}
while (c >= '0' && c <= '9')
{
x = (x << 1) + (x << 3) + c - '0';
c = getchar_unlocked();
}
return x;
};
void phongbeo();
const int inf = 1e18;
const int mod2 = -1e9+7;
const int mod1 = 998244353;
const int base=67;
int add(int x,int y)
{
if(x+y>=mod2) x-=mod2;
if(x+y<0) x+=mod2;
return x+y;
}
struct icd
{
int c,d;
};
struct ib
{
int a;
int b;
};
struct ic
{
int a, b, c;
};
struct id
{
int a,b,c,d;
};
struct ie
{
int a, b, c, d, e;
};
int n, m, s1, s2, s4, s3, sf, k, s5, s6, mx, s7, s8, s9, mx2, dem2 = 0, dem = 0, s33, dem3, dem4, mid, l2, r2, center;
int i, s10, s12,k1,k2,k3,s11,limit,w,l,r,last,root,cost;
int kk;
int el = 19;
main()
{
if(fopen(task".inp","r"))
{
fin(task);
// fou(task);
}
NHP
/// cin>>s1;
// modwwe
phongbeo(),down
// checktime
}
vector<int> v[200001];
int par[200001];
id a[200001];
ic dp[200001][3][2];
icd c[200001];
bool vis[200001];
int in[200001];
int ou[200001];
int res[200001];
int kkk[200001];
void dfs(int x,int y)
{
in[x]=++dem;
kkk[dem]=x;
par[x]=y;
for(auto f:v[x])
if(f!=y)
dfs(f,x);
ou[x]=dem;
}
///ta nhan xet la dinh cua canh x con lon nhat =dinh cua canh x+1 con lon nhat
struct IT
{
ib t[800001];
int t2[800001];
ib mer(ib a,ib b)
{
if(a.a>=b.a) return a;
return b;
}
void ff(int x)
{
for(int i=x*2; i<=x*2+1; i++)
{
t[i].a+=t2[x];
t2[i]+=t2[x];
}
t2[x]=0;
}
void build(int node,int l,int r)
{
if(l==r)
{
t[node].b=kkk[l];
return;
}
int mid=l+r>>1;
build(node<<1,l,mid);
build(node<<1|1,mid+1,r);
t[node]=mer(t[node<<1],t[node<<1|1]);
}
void upd(int node,int l,int r,int l1,int r1,int x)/// find minimum cost
{
if(l>r1||r<l1) return;
if(l>=l1&&r<=r1)
{
t[node].a+=x;
t2[node]+=x;
return;
}
int mid=l+r>>1;
if(t2[node]!=0) ff(node);
upd(node<<1,l,mid,l1,r1,x);
upd(node<<1|1,mid+1,r,l1,r1,x);
t[node]=mer(t[node<<1],t[node<<1|1]);
}
} st; ///->t1=minimum new cost
ic mer(ic a,ic b)
{
if(a.b<b.b) swap(a,b);
return {a.a+b.a,a.b,max({a.c,b.c,b.b})};
}
void dfs2(int x,int y)
{
dp[x][0][0]= {0,0,0};
for(auto f:v[x])
{
if(f!=y)
{
dfs2(f,x);
///.b .c luu 2 con dc lay
///.a la luu cost
for(int i=0; i<=2; i++)
for(int j=0; j<2; j++)
dp[x][i][j].a+=dp[f][0][0].a+c[f].c;
for(int i=1; i<=2; i++)
{
if(dp[f][i][1].a+c[f].d+dp[x][0][0].a-dp[f][0][0].a-c[f].c<dp[x][i][1].a)
dp[x][i][1]=dp[f][i][1],
dp[x][i][1].a+=c[f].d+dp[x][0][0].a-dp[f][0][0].a-c[f].c;
}
for(int i=2; i>=0; --i)
for(int j=0; j<=2; j++)
if(i+j<=2)
{
if(dp[x][i+j][0].a>dp[x][i][0].a+dp[f][j][0].a+(j==0)*c[f].c-c[f].c-dp[f][0][0].a)
dp[x][i+j][0]=mer(dp[x][i][0],dp[f][j][0]),
dp[x][i+j][0].a+=(j==0)*c[f].c-c[f].c-dp[f][0][0].a;
if(i>0&&j>0)
if(dp[x][i+j][1].a>dp[x][i][0].a+dp[f][j][0].a+(j==0)*c[f].c-c[f].c-dp[f][0][0].a)
dp[x][i+j][1]=mer(dp[x][i][0],dp[f][j][0]),
dp[x][i+j][1].a+=(j==0)*c[f].c-c[f].c-dp[f][0][0].a;
}
}
}
for(int i=1; i>=0; i--)
{
if(dp[x][i+1][0].a>dp[x][i][0].a)dp[x][i+1][0]=mer({0,x,0},dp[x][i][0]);
if(dp[x][i+1][1].a>dp[x][i][0].a)dp[x][i+1][1]=mer({0,x,0},dp[x][i][0]);
}
}
bool check(int x,int y)
{
if(in[x]<=in[y]&&in[y]<=ou[x]) return 1;
return 0;
}
void go(int x)
{
while(!vis[x])
{
st.upd(1,1,n,in[x],ou[x],-c[x].c);
x=par[x];
}
}
void go2(int y)
{
int x=l;
while(!check(x,y))
{
vis[x]=1;
st.upd(1,1,n,in[x],ou[x],c[s2].d);
st.upd(1,1,n,1,n,-c[s2].d);
x=par[x];
}
l=x;
}
int get()
{
int s=max(0ll,st.t[1].a);
go2(st.t[1].b);
go(st.t[1].b);
return s;
}
void solve()
{
for(int i=1; i<n; i++)
cin>>a[i].a>>a[i].b>>a[i].c>>a[i].d,
v[a[i].a].pb(a[i].b),
v[a[i].b].pb(a[i].a);
dfs(1,0);
vis[1]=1;
ou[0]=dem;
for(int i=1; i<n; i++)
{
if(par[a[i].a]==a[i].b)
swap(a[i].c,a[i].d),swap(a[i].a,a[i].b);
c[a[i].b]= {a[i].c,a[i].d};
}
for(int i=1; i<=n; i++)
for(int j=0; j<=2; j++)
for(int f=0; f<2; f++)
dp[i][j][f]= {inf,0,0};
st.build(1,1,n);
dfs2(1,0);
res[1]=dp[1][1][1].a;
res[2]=dp[1][2][1].a;
s2=dp[1][2][1].b;
l=s2;
for(int i=2; i<=n; i++)
if(check(i,s2))
{
st.upd(1,1,n,1,n,c[i].d);
st.upd(1,1,n,in[i],ou[i],-c[i].d);
vis[i]=1;
}
else
{
st.upd(1,1,n,in[i],ou[i],c[i].c);
}
get();
for(int i=3; i<=n; i++) res[i]=res[i-1]-get();
cin>>m;
for(int i=1; i<=m; i++)
{
cin>>l;
cout<<res[l],down
}
}
void phongbeo()
{
cin>>n;
solve();
}
Compilation message
designated_cities.cpp:75:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
75 | main()
| ^~~~
designated_cities.cpp: In member function 'void IT::build(long long int, long long int, long long int)':
designated_cities.cpp:134:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
134 | int mid=l+r>>1;
| ~^~
designated_cities.cpp: In member function 'void IT::upd(long long int, long long int, long long int, long long int, long long int, long long int)':
designated_cities.cpp:148:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
148 | int mid=l+r>>1;
| ~^~
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:13:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | #define fin(x) freopen(x".inp","r",stdin)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
designated_cities.cpp:79:9: note: in expansion of macro 'fin'
79 | fin(task);
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
20048 KB |
Output is correct |
2 |
Incorrect |
5 ms |
20048 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
20048 KB |
Output is correct |
2 |
Correct |
299 ms |
77060 KB |
Output is correct |
3 |
Correct |
393 ms |
95816 KB |
Output is correct |
4 |
Correct |
1205 ms |
75644 KB |
Output is correct |
5 |
Correct |
328 ms |
76992 KB |
Output is correct |
6 |
Correct |
253 ms |
79444 KB |
Output is correct |
7 |
Correct |
257 ms |
76996 KB |
Output is correct |
8 |
Correct |
397 ms |
96392 KB |
Output is correct |
9 |
Correct |
286 ms |
78008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20048 KB |
Output is correct |
2 |
Correct |
269 ms |
77108 KB |
Output is correct |
3 |
Correct |
276 ms |
98632 KB |
Output is correct |
4 |
Correct |
221 ms |
75460 KB |
Output is correct |
5 |
Correct |
263 ms |
76984 KB |
Output is correct |
6 |
Correct |
214 ms |
81612 KB |
Output is correct |
7 |
Correct |
201 ms |
78008 KB |
Output is correct |
8 |
Correct |
304 ms |
89932 KB |
Output is correct |
9 |
Correct |
209 ms |
77596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
20048 KB |
Output is correct |
2 |
Incorrect |
5 ms |
20048 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
20048 KB |
Output is correct |
2 |
Correct |
299 ms |
77060 KB |
Output is correct |
3 |
Correct |
393 ms |
95816 KB |
Output is correct |
4 |
Correct |
1205 ms |
75644 KB |
Output is correct |
5 |
Correct |
328 ms |
76992 KB |
Output is correct |
6 |
Correct |
253 ms |
79444 KB |
Output is correct |
7 |
Correct |
257 ms |
76996 KB |
Output is correct |
8 |
Correct |
397 ms |
96392 KB |
Output is correct |
9 |
Correct |
286 ms |
78008 KB |
Output is correct |
10 |
Correct |
4 ms |
20048 KB |
Output is correct |
11 |
Correct |
269 ms |
77108 KB |
Output is correct |
12 |
Correct |
276 ms |
98632 KB |
Output is correct |
13 |
Correct |
221 ms |
75460 KB |
Output is correct |
14 |
Correct |
263 ms |
76984 KB |
Output is correct |
15 |
Correct |
214 ms |
81612 KB |
Output is correct |
16 |
Correct |
201 ms |
78008 KB |
Output is correct |
17 |
Correct |
304 ms |
89932 KB |
Output is correct |
18 |
Correct |
209 ms |
77596 KB |
Output is correct |
19 |
Correct |
4 ms |
20220 KB |
Output is correct |
20 |
Incorrect |
223 ms |
76876 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
20048 KB |
Output is correct |
2 |
Incorrect |
5 ms |
20048 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |