#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".out","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);
vis[x]=1;
x=par[x];
}
}
void go2(int y)
{
int x=l;
while(!check(x,y))
{
st.upd(1,1,n,in[x],ou[x],c[x].d);
st.upd(1,1,n,1,n,-c[x].d);
x=par[x];
vis[x]=1;
}
l=x;
go(y);
}
int get()
{
int s=max(0ll,st.t[1].a);
//cout<<st.t[1].a<<" "<<st.t[1].b,down
go2(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;
vis[l]=1;
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);
}
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);
| ^~~
designated_cities.cpp:14:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | #define fou(x) freopen(x".out","w",stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:80:9: note: in expansion of macro 'fou'
80 | fou(task);
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20048 KB |
Output is correct |
2 |
Correct |
4 ms |
20048 KB |
Output is correct |
3 |
Correct |
4 ms |
20048 KB |
Output is correct |
4 |
Correct |
4 ms |
20048 KB |
Output is correct |
5 |
Correct |
4 ms |
20048 KB |
Output is correct |
6 |
Correct |
4 ms |
20220 KB |
Output is correct |
7 |
Correct |
4 ms |
20216 KB |
Output is correct |
8 |
Correct |
5 ms |
20048 KB |
Output is correct |
9 |
Correct |
5 ms |
20060 KB |
Output is correct |
10 |
Correct |
4 ms |
20124 KB |
Output is correct |
11 |
Correct |
4 ms |
20216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20220 KB |
Output is correct |
2 |
Correct |
303 ms |
76948 KB |
Output is correct |
3 |
Correct |
275 ms |
95816 KB |
Output is correct |
4 |
Correct |
234 ms |
75592 KB |
Output is correct |
5 |
Correct |
258 ms |
76836 KB |
Output is correct |
6 |
Correct |
267 ms |
79444 KB |
Output is correct |
7 |
Correct |
250 ms |
76988 KB |
Output is correct |
8 |
Correct |
292 ms |
96160 KB |
Output is correct |
9 |
Correct |
216 ms |
78008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
20120 KB |
Output is correct |
2 |
Correct |
267 ms |
76968 KB |
Output is correct |
3 |
Correct |
300 ms |
98632 KB |
Output is correct |
4 |
Correct |
249 ms |
75688 KB |
Output is correct |
5 |
Correct |
276 ms |
76880 KB |
Output is correct |
6 |
Correct |
266 ms |
81804 KB |
Output is correct |
7 |
Correct |
254 ms |
78012 KB |
Output is correct |
8 |
Correct |
291 ms |
89932 KB |
Output is correct |
9 |
Correct |
223 ms |
77752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20048 KB |
Output is correct |
2 |
Correct |
4 ms |
20048 KB |
Output is correct |
3 |
Correct |
4 ms |
20048 KB |
Output is correct |
4 |
Correct |
4 ms |
20048 KB |
Output is correct |
5 |
Correct |
4 ms |
20048 KB |
Output is correct |
6 |
Correct |
4 ms |
20220 KB |
Output is correct |
7 |
Correct |
4 ms |
20216 KB |
Output is correct |
8 |
Correct |
5 ms |
20048 KB |
Output is correct |
9 |
Correct |
5 ms |
20060 KB |
Output is correct |
10 |
Correct |
4 ms |
20124 KB |
Output is correct |
11 |
Correct |
4 ms |
20216 KB |
Output is correct |
12 |
Correct |
4 ms |
20048 KB |
Output is correct |
13 |
Correct |
5 ms |
20412 KB |
Output is correct |
14 |
Correct |
6 ms |
20472 KB |
Output is correct |
15 |
Correct |
5 ms |
20304 KB |
Output is correct |
16 |
Correct |
6 ms |
20408 KB |
Output is correct |
17 |
Correct |
5 ms |
20388 KB |
Output is correct |
18 |
Correct |
6 ms |
20304 KB |
Output is correct |
19 |
Correct |
5 ms |
20304 KB |
Output is correct |
20 |
Correct |
5 ms |
20428 KB |
Output is correct |
21 |
Correct |
6 ms |
20304 KB |
Output is correct |
22 |
Correct |
7 ms |
20304 KB |
Output is correct |
23 |
Correct |
6 ms |
20304 KB |
Output is correct |
24 |
Correct |
5 ms |
20304 KB |
Output is correct |
25 |
Correct |
5 ms |
20560 KB |
Output is correct |
26 |
Correct |
5 ms |
20304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20220 KB |
Output is correct |
2 |
Correct |
303 ms |
76948 KB |
Output is correct |
3 |
Correct |
275 ms |
95816 KB |
Output is correct |
4 |
Correct |
234 ms |
75592 KB |
Output is correct |
5 |
Correct |
258 ms |
76836 KB |
Output is correct |
6 |
Correct |
267 ms |
79444 KB |
Output is correct |
7 |
Correct |
250 ms |
76988 KB |
Output is correct |
8 |
Correct |
292 ms |
96160 KB |
Output is correct |
9 |
Correct |
216 ms |
78008 KB |
Output is correct |
10 |
Correct |
5 ms |
20120 KB |
Output is correct |
11 |
Correct |
267 ms |
76968 KB |
Output is correct |
12 |
Correct |
300 ms |
98632 KB |
Output is correct |
13 |
Correct |
249 ms |
75688 KB |
Output is correct |
14 |
Correct |
276 ms |
76880 KB |
Output is correct |
15 |
Correct |
266 ms |
81804 KB |
Output is correct |
16 |
Correct |
254 ms |
78012 KB |
Output is correct |
17 |
Correct |
291 ms |
89932 KB |
Output is correct |
18 |
Correct |
223 ms |
77752 KB |
Output is correct |
19 |
Correct |
5 ms |
20064 KB |
Output is correct |
20 |
Correct |
246 ms |
76872 KB |
Output is correct |
21 |
Correct |
285 ms |
98888 KB |
Output is correct |
22 |
Correct |
243 ms |
75740 KB |
Output is correct |
23 |
Correct |
301 ms |
77128 KB |
Output is correct |
24 |
Correct |
289 ms |
75848 KB |
Output is correct |
25 |
Correct |
324 ms |
77004 KB |
Output is correct |
26 |
Correct |
274 ms |
75816 KB |
Output is correct |
27 |
Correct |
336 ms |
76996 KB |
Output is correct |
28 |
Correct |
301 ms |
81344 KB |
Output is correct |
29 |
Correct |
282 ms |
77200 KB |
Output is correct |
30 |
Correct |
278 ms |
76064 KB |
Output is correct |
31 |
Correct |
268 ms |
77500 KB |
Output is correct |
32 |
Correct |
302 ms |
88684 KB |
Output is correct |
33 |
Correct |
242 ms |
77952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20048 KB |
Output is correct |
2 |
Correct |
4 ms |
20048 KB |
Output is correct |
3 |
Correct |
4 ms |
20048 KB |
Output is correct |
4 |
Correct |
4 ms |
20048 KB |
Output is correct |
5 |
Correct |
4 ms |
20048 KB |
Output is correct |
6 |
Correct |
4 ms |
20220 KB |
Output is correct |
7 |
Correct |
4 ms |
20216 KB |
Output is correct |
8 |
Correct |
5 ms |
20048 KB |
Output is correct |
9 |
Correct |
5 ms |
20060 KB |
Output is correct |
10 |
Correct |
4 ms |
20124 KB |
Output is correct |
11 |
Correct |
4 ms |
20216 KB |
Output is correct |
12 |
Correct |
4 ms |
20220 KB |
Output is correct |
13 |
Correct |
303 ms |
76948 KB |
Output is correct |
14 |
Correct |
275 ms |
95816 KB |
Output is correct |
15 |
Correct |
234 ms |
75592 KB |
Output is correct |
16 |
Correct |
258 ms |
76836 KB |
Output is correct |
17 |
Correct |
267 ms |
79444 KB |
Output is correct |
18 |
Correct |
250 ms |
76988 KB |
Output is correct |
19 |
Correct |
292 ms |
96160 KB |
Output is correct |
20 |
Correct |
216 ms |
78008 KB |
Output is correct |
21 |
Correct |
5 ms |
20120 KB |
Output is correct |
22 |
Correct |
267 ms |
76968 KB |
Output is correct |
23 |
Correct |
300 ms |
98632 KB |
Output is correct |
24 |
Correct |
249 ms |
75688 KB |
Output is correct |
25 |
Correct |
276 ms |
76880 KB |
Output is correct |
26 |
Correct |
266 ms |
81804 KB |
Output is correct |
27 |
Correct |
254 ms |
78012 KB |
Output is correct |
28 |
Correct |
291 ms |
89932 KB |
Output is correct |
29 |
Correct |
223 ms |
77752 KB |
Output is correct |
30 |
Correct |
4 ms |
20048 KB |
Output is correct |
31 |
Correct |
5 ms |
20412 KB |
Output is correct |
32 |
Correct |
6 ms |
20472 KB |
Output is correct |
33 |
Correct |
5 ms |
20304 KB |
Output is correct |
34 |
Correct |
6 ms |
20408 KB |
Output is correct |
35 |
Correct |
5 ms |
20388 KB |
Output is correct |
36 |
Correct |
6 ms |
20304 KB |
Output is correct |
37 |
Correct |
5 ms |
20304 KB |
Output is correct |
38 |
Correct |
5 ms |
20428 KB |
Output is correct |
39 |
Correct |
6 ms |
20304 KB |
Output is correct |
40 |
Correct |
7 ms |
20304 KB |
Output is correct |
41 |
Correct |
6 ms |
20304 KB |
Output is correct |
42 |
Correct |
5 ms |
20304 KB |
Output is correct |
43 |
Correct |
5 ms |
20560 KB |
Output is correct |
44 |
Correct |
5 ms |
20304 KB |
Output is correct |
45 |
Correct |
5 ms |
20064 KB |
Output is correct |
46 |
Correct |
246 ms |
76872 KB |
Output is correct |
47 |
Correct |
285 ms |
98888 KB |
Output is correct |
48 |
Correct |
243 ms |
75740 KB |
Output is correct |
49 |
Correct |
301 ms |
77128 KB |
Output is correct |
50 |
Correct |
289 ms |
75848 KB |
Output is correct |
51 |
Correct |
324 ms |
77004 KB |
Output is correct |
52 |
Correct |
274 ms |
75816 KB |
Output is correct |
53 |
Correct |
336 ms |
76996 KB |
Output is correct |
54 |
Correct |
301 ms |
81344 KB |
Output is correct |
55 |
Correct |
282 ms |
77200 KB |
Output is correct |
56 |
Correct |
278 ms |
76064 KB |
Output is correct |
57 |
Correct |
268 ms |
77500 KB |
Output is correct |
58 |
Correct |
302 ms |
88684 KB |
Output is correct |
59 |
Correct |
242 ms |
77952 KB |
Output is correct |
60 |
Correct |
4 ms |
20048 KB |
Output is correct |
61 |
Correct |
395 ms |
79624 KB |
Output is correct |
62 |
Correct |
293 ms |
97608 KB |
Output is correct |
63 |
Correct |
270 ms |
80392 KB |
Output is correct |
64 |
Correct |
314 ms |
79668 KB |
Output is correct |
65 |
Correct |
280 ms |
78152 KB |
Output is correct |
66 |
Correct |
312 ms |
79776 KB |
Output is correct |
67 |
Correct |
281 ms |
78416 KB |
Output is correct |
68 |
Correct |
288 ms |
79812 KB |
Output is correct |
69 |
Correct |
315 ms |
81836 KB |
Output is correct |
70 |
Correct |
260 ms |
79688 KB |
Output is correct |
71 |
Correct |
275 ms |
78784 KB |
Output is correct |
72 |
Correct |
287 ms |
80320 KB |
Output is correct |
73 |
Correct |
275 ms |
91228 KB |
Output is correct |
74 |
Correct |
219 ms |
82256 KB |
Output is correct |