#include<bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define ppb pop_back
#define ii pair<int,int>
#define ll long long
#define orta ((bas+son)>>1)
#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define umin(x,y) x=min(x,y)
#define umax(x,y) x=max(x,y)
#define inf 2000000000000000
#define N 200005
#define MOD 998244353
#define KOK 700
using namespace std;
int tut21,tut22,n;
ll sub[N],mn,mn2;
vector<int> v[N];
ii e[N*2];
ll ans[N];
vector<pair<ll,int>> deep[N];
struct solve {
int root;
int t;
int u[N],be[N],en[N],tut[N],dae[N],dad[N];
ll dep[N];
ll lazy[N*4];
pair<ll,int> S[N*4];
void shift(int node,ll val) {
S[node].st+=val;
lazy[node]+=val;
}
void push(int node) {
shift(node<<1,lazy[node]);
shift(node<<1|1,lazy[node]);
lazy[node]=0;
}
void up(int node,int bas,int son,int l,int r,ll val) {
if(bas>r || son<l) return ;
if(bas>=l && son<=r) {
shift(node,val);
return ;
}
push(node);
up(node<<1,bas,orta,l,r,val);
up(node<<1|1,orta+1,son,l,r,val);
S[node]=max(S[node<<1],S[node<<1|1]);
}
void build(int node,int bas,int son) {
if(bas==son) {
S[node]={dep[tut[bas]],tut[bas]};
return ;
}
build(node<<1,bas,orta);
build(node<<1|1,orta+1,son);
S[node]=max(S[node<<1],S[node<<1|1]);
}
void dfs(int node,int ata) {
be[node]=++t;
tut[t]=node;
for(int i:v[node]) {
if(e[i].st==ata) continue ;
dep[e[i].st]=dep[node]+e[i].nd;
dae[e[i].st]=e[i].nd;
dad[e[i].st]=node;
dfs(e[i].st,node);
}
en[node]=t;
}
void build() {
memset(u,0,sizeof(u));
memset(lazy,0,sizeof(lazy));
t=dep[root]=0;
dfs(root,0);
dep[root]=-inf;
u[root]=1;
build(1,1,n);
}
void update(int node) {
while(!u[node]) {
// cerr<<node<<"\n";
up(1,1,n,be[node],be[node],-inf);
up(1,1,n,be[node]+1,en[node],-dae[node]);
u[node]=1;
node=dad[node];
}
//cerr<<"done\n";
//exit(0);
}
pair<ll,int> getnext() {
return S[1];
}
bool check() {
//if(S[1].st>0) cerr<<S[1].nd<<"\n";
return S[1].st>0;
}
} solver;
void up(int node,pair<ll,int> val,int add) {
val.st+=add;
deep[node].pb(val);
for(int i=sz(deep[node])-1;i>0;i--) {
if(deep[node][i]>deep[node][i-1]) swap(deep[node][i],deep[node][i-1]);
}
if(sz(deep[node])>2) deep[node].ppb();
}
void dfs(int node,int ata,ll u) {
if(sub[node]+u<mn) {
mn=sub[node]+u;
}
if(sz(deep[node])>1) {
if(sub[node]+u-deep[node][0].st-deep[node][1].st<mn2) {
mn2=sub[node]+u-deep[node][0].st-deep[node][1].st;
tut21=deep[node][0].nd;
tut22=deep[node][1].nd;
}
}
if(sub[node]+u-deep[node][0].st<mn2) {
mn2=sub[node]+u-deep[node][0].st;
tut21=deep[node][0].nd;
tut22=node;
}
for(int i:v[node]) {
if(e[i].st==ata) continue ;
dfs(e[i].st,node,u+sub[node]-(sub[e[i].st]+e[i].nd)+e[i^1].nd);
}
}
void dfs(int node,int ata) {
for(int i:v[node]) {
if(e[i].st==ata) continue ;
dfs(e[i].st,node);
up(node,deep[e[i].st][0],e[i].nd);
sub[node]+=sub[e[i].st]+e[i].nd;
}
up(node,{0,node},0);
}
int main() {
scanf("%d",&n);
int ecnt=0;
for(int i=0;i<n-1;i++) {
int a,b,c,d;
scanf("%d %d %d %d",&a,&b,&c,&d);
e[ecnt]={b,c};
e[ecnt^1]={a,d};
v[a].pb(ecnt);
v[b].pb(ecnt^1);
ecnt+=2;
}
dfs(1,0);
mn=mn2=inf;
dfs(1,0,0);
ans[1]=mn;
ans[2]=mn2;
solver.root=tut21;
solver.build();
solver.update(tut22);
int cnt=2;
while(solver.check()) {
pair<ll,int> res=solver.getnext();
ans[cnt+1]=ans[cnt]-res.st;
++cnt;
solver.update(res.nd);
}
ll last=ans[cnt];
while(++cnt<=n) {
ans[cnt]=last;
}
int q;
scanf("%d",&q);
while(q--) {
int x;
scanf("%d",&x);
printf("%lld\n",ans[x]);
}
}
Compilation message
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:237:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
designated_cities.cpp:245:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d %d",&a,&b,&c,&d);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:293:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&q);
~~~~~^~~~~~~~~
designated_cities.cpp:299:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&x);
~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
29304 KB |
Output is correct |
2 |
Correct |
28 ms |
29304 KB |
Output is correct |
3 |
Correct |
27 ms |
29304 KB |
Output is correct |
4 |
Correct |
28 ms |
29304 KB |
Output is correct |
5 |
Correct |
27 ms |
29304 KB |
Output is correct |
6 |
Correct |
27 ms |
29304 KB |
Output is correct |
7 |
Correct |
27 ms |
29308 KB |
Output is correct |
8 |
Correct |
27 ms |
29432 KB |
Output is correct |
9 |
Correct |
32 ms |
29304 KB |
Output is correct |
10 |
Correct |
27 ms |
29304 KB |
Output is correct |
11 |
Correct |
27 ms |
29304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
29304 KB |
Output is correct |
2 |
Correct |
767 ms |
63696 KB |
Output is correct |
3 |
Correct |
786 ms |
73176 KB |
Output is correct |
4 |
Correct |
680 ms |
62328 KB |
Output is correct |
5 |
Correct |
720 ms |
63216 KB |
Output is correct |
6 |
Correct |
748 ms |
64888 KB |
Output is correct |
7 |
Correct |
661 ms |
62192 KB |
Output is correct |
8 |
Correct |
992 ms |
73608 KB |
Output is correct |
9 |
Correct |
624 ms |
59368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
29304 KB |
Output is correct |
2 |
Correct |
746 ms |
63624 KB |
Output is correct |
3 |
Correct |
765 ms |
75004 KB |
Output is correct |
4 |
Correct |
667 ms |
62328 KB |
Output is correct |
5 |
Correct |
717 ms |
63216 KB |
Output is correct |
6 |
Correct |
749 ms |
65256 KB |
Output is correct |
7 |
Correct |
590 ms |
61040 KB |
Output is correct |
8 |
Correct |
785 ms |
70172 KB |
Output is correct |
9 |
Correct |
662 ms |
58900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
29304 KB |
Output is correct |
2 |
Correct |
28 ms |
29304 KB |
Output is correct |
3 |
Correct |
27 ms |
29304 KB |
Output is correct |
4 |
Correct |
28 ms |
29304 KB |
Output is correct |
5 |
Correct |
27 ms |
29304 KB |
Output is correct |
6 |
Correct |
27 ms |
29304 KB |
Output is correct |
7 |
Correct |
27 ms |
29308 KB |
Output is correct |
8 |
Correct |
27 ms |
29432 KB |
Output is correct |
9 |
Correct |
32 ms |
29304 KB |
Output is correct |
10 |
Correct |
27 ms |
29304 KB |
Output is correct |
11 |
Correct |
27 ms |
29304 KB |
Output is correct |
12 |
Correct |
27 ms |
29304 KB |
Output is correct |
13 |
Correct |
30 ms |
29688 KB |
Output is correct |
14 |
Correct |
32 ms |
29760 KB |
Output is correct |
15 |
Correct |
31 ms |
29688 KB |
Output is correct |
16 |
Correct |
31 ms |
29688 KB |
Output is correct |
17 |
Correct |
33 ms |
29728 KB |
Output is correct |
18 |
Correct |
32 ms |
29816 KB |
Output is correct |
19 |
Correct |
38 ms |
29688 KB |
Output is correct |
20 |
Correct |
30 ms |
29816 KB |
Output is correct |
21 |
Correct |
30 ms |
29692 KB |
Output is correct |
22 |
Correct |
30 ms |
29688 KB |
Output is correct |
23 |
Correct |
28 ms |
29816 KB |
Output is correct |
24 |
Correct |
30 ms |
29688 KB |
Output is correct |
25 |
Correct |
30 ms |
29816 KB |
Output is correct |
26 |
Correct |
30 ms |
29688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
29304 KB |
Output is correct |
2 |
Correct |
767 ms |
63696 KB |
Output is correct |
3 |
Correct |
786 ms |
73176 KB |
Output is correct |
4 |
Correct |
680 ms |
62328 KB |
Output is correct |
5 |
Correct |
720 ms |
63216 KB |
Output is correct |
6 |
Correct |
748 ms |
64888 KB |
Output is correct |
7 |
Correct |
661 ms |
62192 KB |
Output is correct |
8 |
Correct |
992 ms |
73608 KB |
Output is correct |
9 |
Correct |
624 ms |
59368 KB |
Output is correct |
10 |
Correct |
27 ms |
29304 KB |
Output is correct |
11 |
Correct |
746 ms |
63624 KB |
Output is correct |
12 |
Correct |
765 ms |
75004 KB |
Output is correct |
13 |
Correct |
667 ms |
62328 KB |
Output is correct |
14 |
Correct |
717 ms |
63216 KB |
Output is correct |
15 |
Correct |
749 ms |
65256 KB |
Output is correct |
16 |
Correct |
590 ms |
61040 KB |
Output is correct |
17 |
Correct |
785 ms |
70172 KB |
Output is correct |
18 |
Correct |
662 ms |
58900 KB |
Output is correct |
19 |
Correct |
26 ms |
29304 KB |
Output is correct |
20 |
Correct |
816 ms |
63604 KB |
Output is correct |
21 |
Correct |
748 ms |
75192 KB |
Output is correct |
22 |
Correct |
675 ms |
62456 KB |
Output is correct |
23 |
Correct |
728 ms |
63736 KB |
Output is correct |
24 |
Correct |
707 ms |
62456 KB |
Output is correct |
25 |
Correct |
741 ms |
63684 KB |
Output is correct |
26 |
Correct |
696 ms |
62616 KB |
Output is correct |
27 |
Correct |
709 ms |
63320 KB |
Output is correct |
28 |
Correct |
769 ms |
65036 KB |
Output is correct |
29 |
Correct |
734 ms |
63864 KB |
Output is correct |
30 |
Correct |
686 ms |
62200 KB |
Output is correct |
31 |
Correct |
632 ms |
61808 KB |
Output is correct |
32 |
Correct |
804 ms |
70648 KB |
Output is correct |
33 |
Correct |
560 ms |
59340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
29304 KB |
Output is correct |
2 |
Correct |
28 ms |
29304 KB |
Output is correct |
3 |
Correct |
27 ms |
29304 KB |
Output is correct |
4 |
Correct |
28 ms |
29304 KB |
Output is correct |
5 |
Correct |
27 ms |
29304 KB |
Output is correct |
6 |
Correct |
27 ms |
29304 KB |
Output is correct |
7 |
Correct |
27 ms |
29308 KB |
Output is correct |
8 |
Correct |
27 ms |
29432 KB |
Output is correct |
9 |
Correct |
32 ms |
29304 KB |
Output is correct |
10 |
Correct |
27 ms |
29304 KB |
Output is correct |
11 |
Correct |
27 ms |
29304 KB |
Output is correct |
12 |
Correct |
28 ms |
29304 KB |
Output is correct |
13 |
Correct |
767 ms |
63696 KB |
Output is correct |
14 |
Correct |
786 ms |
73176 KB |
Output is correct |
15 |
Correct |
680 ms |
62328 KB |
Output is correct |
16 |
Correct |
720 ms |
63216 KB |
Output is correct |
17 |
Correct |
748 ms |
64888 KB |
Output is correct |
18 |
Correct |
661 ms |
62192 KB |
Output is correct |
19 |
Correct |
992 ms |
73608 KB |
Output is correct |
20 |
Correct |
624 ms |
59368 KB |
Output is correct |
21 |
Correct |
27 ms |
29304 KB |
Output is correct |
22 |
Correct |
746 ms |
63624 KB |
Output is correct |
23 |
Correct |
765 ms |
75004 KB |
Output is correct |
24 |
Correct |
667 ms |
62328 KB |
Output is correct |
25 |
Correct |
717 ms |
63216 KB |
Output is correct |
26 |
Correct |
749 ms |
65256 KB |
Output is correct |
27 |
Correct |
590 ms |
61040 KB |
Output is correct |
28 |
Correct |
785 ms |
70172 KB |
Output is correct |
29 |
Correct |
662 ms |
58900 KB |
Output is correct |
30 |
Correct |
27 ms |
29304 KB |
Output is correct |
31 |
Correct |
30 ms |
29688 KB |
Output is correct |
32 |
Correct |
32 ms |
29760 KB |
Output is correct |
33 |
Correct |
31 ms |
29688 KB |
Output is correct |
34 |
Correct |
31 ms |
29688 KB |
Output is correct |
35 |
Correct |
33 ms |
29728 KB |
Output is correct |
36 |
Correct |
32 ms |
29816 KB |
Output is correct |
37 |
Correct |
38 ms |
29688 KB |
Output is correct |
38 |
Correct |
30 ms |
29816 KB |
Output is correct |
39 |
Correct |
30 ms |
29692 KB |
Output is correct |
40 |
Correct |
30 ms |
29688 KB |
Output is correct |
41 |
Correct |
28 ms |
29816 KB |
Output is correct |
42 |
Correct |
30 ms |
29688 KB |
Output is correct |
43 |
Correct |
30 ms |
29816 KB |
Output is correct |
44 |
Correct |
30 ms |
29688 KB |
Output is correct |
45 |
Correct |
26 ms |
29304 KB |
Output is correct |
46 |
Correct |
816 ms |
63604 KB |
Output is correct |
47 |
Correct |
748 ms |
75192 KB |
Output is correct |
48 |
Correct |
675 ms |
62456 KB |
Output is correct |
49 |
Correct |
728 ms |
63736 KB |
Output is correct |
50 |
Correct |
707 ms |
62456 KB |
Output is correct |
51 |
Correct |
741 ms |
63684 KB |
Output is correct |
52 |
Correct |
696 ms |
62616 KB |
Output is correct |
53 |
Correct |
709 ms |
63320 KB |
Output is correct |
54 |
Correct |
769 ms |
65036 KB |
Output is correct |
55 |
Correct |
734 ms |
63864 KB |
Output is correct |
56 |
Correct |
686 ms |
62200 KB |
Output is correct |
57 |
Correct |
632 ms |
61808 KB |
Output is correct |
58 |
Correct |
804 ms |
70648 KB |
Output is correct |
59 |
Correct |
560 ms |
59340 KB |
Output is correct |
60 |
Correct |
27 ms |
29304 KB |
Output is correct |
61 |
Correct |
790 ms |
66480 KB |
Output is correct |
62 |
Correct |
834 ms |
75132 KB |
Output is correct |
63 |
Correct |
715 ms |
65072 KB |
Output is correct |
64 |
Correct |
1041 ms |
66324 KB |
Output is correct |
65 |
Correct |
848 ms |
64956 KB |
Output is correct |
66 |
Correct |
802 ms |
66372 KB |
Output is correct |
67 |
Correct |
807 ms |
64868 KB |
Output is correct |
68 |
Correct |
762 ms |
66204 KB |
Output is correct |
69 |
Correct |
825 ms |
67472 KB |
Output is correct |
70 |
Correct |
754 ms |
66176 KB |
Output is correct |
71 |
Correct |
736 ms |
64888 KB |
Output is correct |
72 |
Correct |
685 ms |
65496 KB |
Output is correct |
73 |
Correct |
836 ms |
71832 KB |
Output is correct |
74 |
Correct |
620 ms |
63504 KB |
Output is correct |