#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <set>
using namespace std;
vector < pair < long long , long long > > Next[200005];
map < long long , long long > all[200005];
set < long long > vis[200005];
long long ans[200005];
long long all2[200005];
long long xx[200005];
long long how[200005];
long long who[200005];
long long Father[200005];
long long l,r,now2=0;
long long ll[200005],rr[200005];
long long N=0,big=1e18;
long long x,y,root,now=1;
pair < long long , long long > A,B;
struct A
{
long long here,l,r,add;
long long nxl,nxr;
pair < long long , long long > ans;
}Node[1000005];
void build(long long l,long long r,long long here)
{
Node[here].l=l;
Node[here].r=r;
Node[here].add=0;
if(l==r)
{
//printf("%lld\n",all2[l]);
//printf("tt %lld %lld %lld\n",l,all2[l],who[l]);
Node[here].ans=make_pair(all2[l],who[l]);
return;
}
Node[here].nxl=now++;
Node[here].nxr=now++;
build(l,(l+r)/2,Node[here].nxl);
build((l+r)/2+1,r,Node[here].nxr);
Node[here].ans=max(Node[Node[here].nxl].ans,Node[Node[here].nxr].ans);
}
void UPD(long long here)
{
Node[Node[here].nxl].ans.first+=Node[here].add;
Node[Node[here].nxr].ans.first+=Node[here].add;
Node[Node[here].nxl].add+=Node[here].add;
Node[Node[here].nxr].add+=Node[here].add;
Node[here].add=0;
return;
}
void cha(long long l,long long r,long long con,long long here)
{
if(l==Node[here].l&&r==Node[here].r)
{
//printf("%lld %lld %lld\n",l,r,con);
Node[here].add+=con;
Node[here].ans.first+=con;
return;
}
UPD(here);
if(r<=(Node[here].l+Node[here].r)/2) cha(l,r,con,Node[here].nxl);
else if(l>(Node[here].l+Node[here].r)/2) cha(l,r,con,Node[here].nxr);
else
{
cha(l,(Node[here].l+Node[here].r)/2,con,Node[here].nxl);
cha((Node[here].l+Node[here].r)/2+1,r,con,Node[here].nxr);
}
Node[here].ans=max(Node[Node[here].nxl].ans,Node[Node[here].nxr].ans);
}
void F(long long here,long long fa)
{
for(auto i:Next[here])
{
if(i.first!=fa)
{
ans[1]+=i.second;
F(i.first,here);
}
}
}
void F2(long long here,long long fa)
{
for(auto i:Next[here])
{
if(i.first!=fa)
{
ans[i.first]=ans[here]-all[here][i.first]+all[i.first][here];
F2(i.first,here);
}
}
}
pair < long long , long long > F4(long long here,long long fa,long long con)
{
pair < long long , long long > a=make_pair(con,here),b=make_pair(con,here),tt;
for(auto i:Next[here])
{
if(i.first!=fa)
{
tt=F4(i.first,here,con+i.second);
if(tt>a)
{
b=a;
a=tt;
}
else if(tt>b) b=tt;
}
}
big=min(big,ans[here]-(a.first+b.first-2*con));
if(big==ans[here]-(a.first+b.first-2*con))
{
root=here;
x=a.second;
y=b.second;
}
return a;
}
void F3(long long here,long long fa,long long con)
{
pair < long long , long long > a=make_pair(con,here),tt;
for(auto i:Next[here]) if(i.first!=fa) F3(i.first,here,con+i.second);
big=max(big,con);
if(big==con) x=here;
}
void F5(long long here)
{
long long ok=0;
//printf("%lld %lld\n",here,Father[here]);
if(Father[here]==-1||all[Father[here]].find(here)==all[Father[here]].end()) return;
cha(ll[here],rr[here],0-all[Father[here]][here],0);
//printf("%lld %lld\n",ll[here],rr[here]);
//printf("%lld %lld %lld %lld\n",here,0-all[Father[here]][here],ll[here],rr[here]);
all[Father[here]].erase(here);
F5(Father[here]);
}
void F6(long long here,long long fa,long long con)
{
all2[now2]=con;
ll[here]=now2;
Father[here]=fa;
who[now2]=here;
for(auto i:Next[here])
{
if(i.first==fa) continue;
now2++;
F6(i.first,here,con+i.second);
}
rr[here]=now2;
//printf("aa %lld\n",here);
}
int main()
{
//freopen("01-04.txt","rt",stdin);
long long M,i,j,k,a,b,c,d,where;
scanf("%lld",&N);
for(i=1;i<N;i++)
{
scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
Next[a].push_back(make_pair(b,c));
Next[b].push_back(make_pair(a,d));
all[a][b]=c;
all[b][a]=d;
}
for(i=1;i<=N;i++) xx[i]=1e18;
F(1,-1);
F2(1,-1);
how[1]=1e18;
for(i=1;i<=N;i++)
{
how[1]=min(how[1],ans[i]);
if(how[1]==ans[i]) where=i;
}
F4(1,-1,0);
F6(root,-1,0);
build(0,N-1,0);
//printf("aa %lld %lld %lld\n",root,x,y);
//printf("%lld\n",Node[0].ans.first);
F5(x);
//printf("%lld\n",Node[0].ans.first);
F5(y);
//printf("%lld\n",Node[0].ans.first);
how[2]=big;
for(i=3;i<=N;i++)
{
x=Node[0].ans.second;
//printf("bb %lld %lld %lld\n",x,ll[x],Node[0].ans.first);
how[i]=how[i-1]-Node[0].ans.first;
F5(x);
}
scanf("%lld",&M);
while(M--)
{
scanf("%lld",&a);
printf("%lld\n",how[a]);
}
return 0;
}
Compilation message
designated_cities.cpp: In function 'void F3(long long int, long long int, long long int)':
designated_cities.cpp:124:36: warning: variable 'a' set but not used [-Wunused-but-set-variable]
124 | pair < long long , long long > a=make_pair(con,here),tt;
| ^
designated_cities.cpp: In function 'void F5(long long int)':
designated_cities.cpp:131:15: warning: unused variable 'ok' [-Wunused-variable]
131 | long long ok=0;
| ^~
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:159:19: warning: unused variable 'j' [-Wunused-variable]
159 | long long M,i,j,k,a,b,c,d,where;
| ^
designated_cities.cpp:159:21: warning: unused variable 'k' [-Wunused-variable]
159 | long long M,i,j,k,a,b,c,d,where;
| ^
designated_cities.cpp:159:31: warning: variable 'where' set but not used [-Wunused-but-set-variable]
159 | long long M,i,j,k,a,b,c,d,where;
| ^~~~~
designated_cities.cpp:160:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
160 | scanf("%lld",&N);
| ~~~~~^~~~~~~~~~~
designated_cities.cpp:163:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
163 | scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:196:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
196 | scanf("%lld",&M);
| ~~~~~^~~~~~~~~~~
designated_cities.cpp:199:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
199 | scanf("%lld",&a);
| ~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
86508 KB |
Output is correct |
2 |
Correct |
48 ms |
86508 KB |
Output is correct |
3 |
Correct |
50 ms |
86508 KB |
Output is correct |
4 |
Correct |
48 ms |
86636 KB |
Output is correct |
5 |
Correct |
48 ms |
86508 KB |
Output is correct |
6 |
Correct |
47 ms |
86508 KB |
Output is correct |
7 |
Correct |
47 ms |
86636 KB |
Output is correct |
8 |
Correct |
47 ms |
86508 KB |
Output is correct |
9 |
Correct |
48 ms |
86508 KB |
Output is correct |
10 |
Correct |
47 ms |
86508 KB |
Output is correct |
11 |
Correct |
49 ms |
86628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
86508 KB |
Output is correct |
2 |
Correct |
865 ms |
134508 KB |
Output is correct |
3 |
Correct |
846 ms |
157560 KB |
Output is correct |
4 |
Correct |
839 ms |
134704 KB |
Output is correct |
5 |
Correct |
964 ms |
134752 KB |
Output is correct |
6 |
Correct |
872 ms |
138220 KB |
Output is correct |
7 |
Correct |
1059 ms |
134108 KB |
Output is correct |
8 |
Correct |
884 ms |
158444 KB |
Output is correct |
9 |
Correct |
1294 ms |
134128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
86508 KB |
Output is correct |
2 |
Correct |
909 ms |
134788 KB |
Output is correct |
3 |
Correct |
841 ms |
161260 KB |
Output is correct |
4 |
Correct |
815 ms |
134508 KB |
Output is correct |
5 |
Correct |
937 ms |
134496 KB |
Output is correct |
6 |
Correct |
902 ms |
138388 KB |
Output is correct |
7 |
Correct |
1194 ms |
133720 KB |
Output is correct |
8 |
Correct |
878 ms |
149356 KB |
Output is correct |
9 |
Correct |
1253 ms |
133588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
86508 KB |
Output is correct |
2 |
Correct |
48 ms |
86508 KB |
Output is correct |
3 |
Correct |
50 ms |
86508 KB |
Output is correct |
4 |
Correct |
48 ms |
86636 KB |
Output is correct |
5 |
Correct |
48 ms |
86508 KB |
Output is correct |
6 |
Correct |
47 ms |
86508 KB |
Output is correct |
7 |
Correct |
47 ms |
86636 KB |
Output is correct |
8 |
Correct |
47 ms |
86508 KB |
Output is correct |
9 |
Correct |
48 ms |
86508 KB |
Output is correct |
10 |
Correct |
47 ms |
86508 KB |
Output is correct |
11 |
Correct |
49 ms |
86628 KB |
Output is correct |
12 |
Correct |
49 ms |
86508 KB |
Output is correct |
13 |
Correct |
53 ms |
87020 KB |
Output is correct |
14 |
Correct |
55 ms |
87148 KB |
Output is correct |
15 |
Correct |
62 ms |
87020 KB |
Output is correct |
16 |
Correct |
52 ms |
87020 KB |
Output is correct |
17 |
Correct |
52 ms |
87020 KB |
Output is correct |
18 |
Correct |
53 ms |
87020 KB |
Output is correct |
19 |
Correct |
52 ms |
87020 KB |
Output is correct |
20 |
Correct |
53 ms |
87020 KB |
Output is correct |
21 |
Correct |
52 ms |
87020 KB |
Output is correct |
22 |
Correct |
53 ms |
87020 KB |
Output is correct |
23 |
Correct |
54 ms |
87020 KB |
Output is correct |
24 |
Correct |
53 ms |
87148 KB |
Output is correct |
25 |
Correct |
52 ms |
87404 KB |
Output is correct |
26 |
Correct |
52 ms |
87020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
86508 KB |
Output is correct |
2 |
Correct |
865 ms |
134508 KB |
Output is correct |
3 |
Correct |
846 ms |
157560 KB |
Output is correct |
4 |
Correct |
839 ms |
134704 KB |
Output is correct |
5 |
Correct |
964 ms |
134752 KB |
Output is correct |
6 |
Correct |
872 ms |
138220 KB |
Output is correct |
7 |
Correct |
1059 ms |
134108 KB |
Output is correct |
8 |
Correct |
884 ms |
158444 KB |
Output is correct |
9 |
Correct |
1294 ms |
134128 KB |
Output is correct |
10 |
Correct |
49 ms |
86508 KB |
Output is correct |
11 |
Correct |
909 ms |
134788 KB |
Output is correct |
12 |
Correct |
841 ms |
161260 KB |
Output is correct |
13 |
Correct |
815 ms |
134508 KB |
Output is correct |
14 |
Correct |
937 ms |
134496 KB |
Output is correct |
15 |
Correct |
902 ms |
138388 KB |
Output is correct |
16 |
Correct |
1194 ms |
133720 KB |
Output is correct |
17 |
Correct |
878 ms |
149356 KB |
Output is correct |
18 |
Correct |
1253 ms |
133588 KB |
Output is correct |
19 |
Correct |
48 ms |
86508 KB |
Output is correct |
20 |
Correct |
859 ms |
134516 KB |
Output is correct |
21 |
Correct |
883 ms |
167716 KB |
Output is correct |
22 |
Correct |
811 ms |
139288 KB |
Output is correct |
23 |
Correct |
875 ms |
140968 KB |
Output is correct |
24 |
Correct |
896 ms |
139756 KB |
Output is correct |
25 |
Correct |
851 ms |
141036 KB |
Output is correct |
26 |
Correct |
915 ms |
139884 KB |
Output is correct |
27 |
Correct |
933 ms |
140648 KB |
Output is correct |
28 |
Correct |
910 ms |
143988 KB |
Output is correct |
29 |
Correct |
885 ms |
141292 KB |
Output is correct |
30 |
Correct |
944 ms |
139624 KB |
Output is correct |
31 |
Correct |
1086 ms |
139996 KB |
Output is correct |
32 |
Correct |
913 ms |
156652 KB |
Output is correct |
33 |
Correct |
1206 ms |
139988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
86508 KB |
Output is correct |
2 |
Correct |
48 ms |
86508 KB |
Output is correct |
3 |
Correct |
50 ms |
86508 KB |
Output is correct |
4 |
Correct |
48 ms |
86636 KB |
Output is correct |
5 |
Correct |
48 ms |
86508 KB |
Output is correct |
6 |
Correct |
47 ms |
86508 KB |
Output is correct |
7 |
Correct |
47 ms |
86636 KB |
Output is correct |
8 |
Correct |
47 ms |
86508 KB |
Output is correct |
9 |
Correct |
48 ms |
86508 KB |
Output is correct |
10 |
Correct |
47 ms |
86508 KB |
Output is correct |
11 |
Correct |
49 ms |
86628 KB |
Output is correct |
12 |
Correct |
54 ms |
86508 KB |
Output is correct |
13 |
Correct |
865 ms |
134508 KB |
Output is correct |
14 |
Correct |
846 ms |
157560 KB |
Output is correct |
15 |
Correct |
839 ms |
134704 KB |
Output is correct |
16 |
Correct |
964 ms |
134752 KB |
Output is correct |
17 |
Correct |
872 ms |
138220 KB |
Output is correct |
18 |
Correct |
1059 ms |
134108 KB |
Output is correct |
19 |
Correct |
884 ms |
158444 KB |
Output is correct |
20 |
Correct |
1294 ms |
134128 KB |
Output is correct |
21 |
Correct |
49 ms |
86508 KB |
Output is correct |
22 |
Correct |
909 ms |
134788 KB |
Output is correct |
23 |
Correct |
841 ms |
161260 KB |
Output is correct |
24 |
Correct |
815 ms |
134508 KB |
Output is correct |
25 |
Correct |
937 ms |
134496 KB |
Output is correct |
26 |
Correct |
902 ms |
138388 KB |
Output is correct |
27 |
Correct |
1194 ms |
133720 KB |
Output is correct |
28 |
Correct |
878 ms |
149356 KB |
Output is correct |
29 |
Correct |
1253 ms |
133588 KB |
Output is correct |
30 |
Correct |
49 ms |
86508 KB |
Output is correct |
31 |
Correct |
53 ms |
87020 KB |
Output is correct |
32 |
Correct |
55 ms |
87148 KB |
Output is correct |
33 |
Correct |
62 ms |
87020 KB |
Output is correct |
34 |
Correct |
52 ms |
87020 KB |
Output is correct |
35 |
Correct |
52 ms |
87020 KB |
Output is correct |
36 |
Correct |
53 ms |
87020 KB |
Output is correct |
37 |
Correct |
52 ms |
87020 KB |
Output is correct |
38 |
Correct |
53 ms |
87020 KB |
Output is correct |
39 |
Correct |
52 ms |
87020 KB |
Output is correct |
40 |
Correct |
53 ms |
87020 KB |
Output is correct |
41 |
Correct |
54 ms |
87020 KB |
Output is correct |
42 |
Correct |
53 ms |
87148 KB |
Output is correct |
43 |
Correct |
52 ms |
87404 KB |
Output is correct |
44 |
Correct |
52 ms |
87020 KB |
Output is correct |
45 |
Correct |
48 ms |
86508 KB |
Output is correct |
46 |
Correct |
859 ms |
134516 KB |
Output is correct |
47 |
Correct |
883 ms |
167716 KB |
Output is correct |
48 |
Correct |
811 ms |
139288 KB |
Output is correct |
49 |
Correct |
875 ms |
140968 KB |
Output is correct |
50 |
Correct |
896 ms |
139756 KB |
Output is correct |
51 |
Correct |
851 ms |
141036 KB |
Output is correct |
52 |
Correct |
915 ms |
139884 KB |
Output is correct |
53 |
Correct |
933 ms |
140648 KB |
Output is correct |
54 |
Correct |
910 ms |
143988 KB |
Output is correct |
55 |
Correct |
885 ms |
141292 KB |
Output is correct |
56 |
Correct |
944 ms |
139624 KB |
Output is correct |
57 |
Correct |
1086 ms |
139996 KB |
Output is correct |
58 |
Correct |
913 ms |
156652 KB |
Output is correct |
59 |
Correct |
1206 ms |
139988 KB |
Output is correct |
60 |
Correct |
48 ms |
86508 KB |
Output is correct |
61 |
Correct |
927 ms |
143508 KB |
Output is correct |
62 |
Correct |
896 ms |
165432 KB |
Output is correct |
63 |
Correct |
859 ms |
141932 KB |
Output is correct |
64 |
Correct |
927 ms |
143852 KB |
Output is correct |
65 |
Correct |
924 ms |
142228 KB |
Output is correct |
66 |
Correct |
939 ms |
143596 KB |
Output is correct |
67 |
Correct |
932 ms |
142324 KB |
Output is correct |
68 |
Correct |
967 ms |
143484 KB |
Output is correct |
69 |
Correct |
941 ms |
146284 KB |
Output is correct |
70 |
Correct |
920 ms |
144064 KB |
Output is correct |
71 |
Correct |
1014 ms |
142368 KB |
Output is correct |
72 |
Correct |
1196 ms |
143460 KB |
Output is correct |
73 |
Correct |
1010 ms |
156780 KB |
Output is correct |
74 |
Correct |
1373 ms |
144116 KB |
Output is correct |