#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int NMAX = 2e5;
struct node_t{
long long lazy;
long long ma_val;
int ma_pos;
node_t operator + (const node_t &other)const{
node_t ans;
ans.lazy = false;
if(this->ma_val < other.ma_val){
ans.ma_val = other.ma_val;
ans.ma_pos = other.ma_pos;
}
else{
ans.ma_val = this->ma_val;
ans.ma_pos = this->ma_pos;
}
return ans;
}
}aint[4 * NMAX + 5];
void propag(int nod,int st,int dr){
if(st == dr || aint[nod].lazy == 0){
return ;
}
aint[nod * 2].lazy += aint[nod].lazy;
aint[nod * 2].ma_val += aint[nod].lazy;
aint[nod * 2 + 1].lazy += aint[nod].lazy;
aint[nod * 2 + 1].ma_val += aint[nod].lazy;
aint[nod].lazy = 0;
}
void build(int nod,int st,int dr){
if(st == dr){
aint[nod].lazy = 0;
aint[nod].ma_val = 0;
aint[nod].ma_pos = st;
return ;
}
int mid = (st + dr) / 2;
build(nod * 2,st,mid);
build(nod * 2 + 1,mid + 1,dr);
aint[nod] = aint[nod * 2] + aint[nod * 2 + 1];
}
void update(int nod,int st,int dr,int l,int r,long long val){
propag(nod,st,dr);
if(r < st || l > dr){
return ;
}
if(l <= st && dr <= r){
aint[nod].lazy += val;
aint[nod].ma_val += val;
return ;
}
int mid = (st + dr) / 2;
update(nod * 2,st,mid,l,r,val);
update(nod * 2 + 1,mid + 1,dr,l,r,val);
aint[nod] = aint[nod * 2] + aint[nod * 2 + 1];
}
int n;
vector< pair<int,pair<int,int> > > graph[NMAX + 5];
long long subtree_cost[NMAX + 5];
long long singular_cost[NMAX + 5];
void predfs(int nod,int tata){
for(auto it:graph[nod]){
if(it.first == tata){
continue;
}
predfs(it.first,nod);
subtree_cost[nod] += subtree_cost[it.first] + it.second.second;
}
}
long long dfs_singular(int nod,int tata,long long cost){
long long ans = singular_cost[nod] = cost;
for(auto it:graph[nod]){
if(it.first == tata){
continue;
}
ans = max(ans,dfs_singular(it.first,nod,cost - it.second.second + it.second.first));
}
return ans;
}
pair<long long,int> dfs_far(int nod,int tata,long long cost){
pair<long long,int> bst = {cost + singular_cost[nod],nod};
for(auto it:graph[nod]){
if(it.first == tata){
continue;
}
bst = max(bst,dfs_far(it.first,nod,cost + it.second.first + it.second.second));
}
return bst;
}
int q;
long long ans[NMAX + 5];
int father[NMAX + 5];
int fst[NMAX + 5];
int lst[NMAX + 5];
int euler;
bool added[NMAX + 5];
int lin[NMAX + 5];
int father_edge[NMAX + 5];
void afis(int nod,int st,int dr){
propag(nod,st,dr);
if(st == dr){
return ;
}
int mid = (st + dr) / 2;
afis(nod * 2,st,mid);
afis(nod * 2 + 1,mid + 1,dr);
}
void root_tree(int nod,int tata){
father[nod] = tata;
lin[++euler] = nod;
fst[nod] = euler;
for(auto it:graph[nod]){
if(it.first == tata){
continue;
}
father_edge[it.first] = it.second.first;
root_tree(it.first,nod);
}
lst[nod] = euler;
}
void do_costs(int nod,long long father_cost){
update(1,1,n,fst[nod],fst[nod],father_cost);
for(auto it:graph[nod]){
if(it.first == father[nod]){
continue;
}
do_costs(it.first,father_cost + it.second.first);
}
}
void add_node(int nod){
while(added[nod] == false){
update(1,1,n,fst[nod],lst[nod],-father_edge[nod]);
added[nod] = true;
nod = father[nod];
}
}
node_t get_max(){
propag(1,1,n);
return aint[1];
}
int main(){
scanf("%d",&n);
build(1,1,n);
long long total_sum = 0;
for(int i = 1;i < n;i++){
int a,b,c,d;
scanf("%d %d %d %d",&a,&b,&c,&d);
total_sum += c + d;
graph[a].push_back({b,{c,d}});
graph[b].push_back({a,{d,c}});
}
predfs(1,0);
ans[1] = dfs_singular(1,0,subtree_cost[1]);
if(n > 1){
pair<long long,int> tmp1 = dfs_far(1,0,0);
pair<long long,int> tmp2 = dfs_far(tmp1.second,0,singular_cost[tmp1.second]);
ans[2] = tmp2.first / 2;
added[tmp1.second] = true;
root_tree(tmp1.second,0);
do_costs(tmp1.second,0);
add_node(tmp2.second);
afis(1,1,n);
for(int i = 3;i <= n;i++){
node_t tmp = get_max();
ans[i] = ans[i - 1] + tmp.ma_val;
add_node(lin[tmp.ma_pos]);
}
}
scanf("%d",&q);
while(q--){
int a;
scanf("%d",&a);
printf("%lld\n",total_sum - ans[a]);
}
return 0;
}
Compilation message
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:175:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
designated_cities.cpp:182:14: 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:209:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&q);
~~~~~^~~~~~~~~
designated_cities.cpp:213:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&a);
~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
4984 KB |
Output is correct |
4 |
Correct |
6 ms |
4984 KB |
Output is correct |
5 |
Correct |
7 ms |
4984 KB |
Output is correct |
6 |
Correct |
7 ms |
4984 KB |
Output is correct |
7 |
Correct |
6 ms |
4984 KB |
Output is correct |
8 |
Correct |
6 ms |
5112 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
7 ms |
5112 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
710 ms |
40852 KB |
Output is correct |
3 |
Correct |
791 ms |
57716 KB |
Output is correct |
4 |
Correct |
631 ms |
39516 KB |
Output is correct |
5 |
Correct |
657 ms |
40756 KB |
Output is correct |
6 |
Correct |
742 ms |
43032 KB |
Output is correct |
7 |
Correct |
603 ms |
40864 KB |
Output is correct |
8 |
Correct |
831 ms |
56976 KB |
Output is correct |
9 |
Correct |
536 ms |
39844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4988 KB |
Output is correct |
2 |
Correct |
748 ms |
41008 KB |
Output is correct |
3 |
Correct |
1058 ms |
57640 KB |
Output is correct |
4 |
Correct |
686 ms |
39424 KB |
Output is correct |
5 |
Correct |
706 ms |
40744 KB |
Output is correct |
6 |
Correct |
739 ms |
43772 KB |
Output is correct |
7 |
Correct |
550 ms |
40868 KB |
Output is correct |
8 |
Correct |
779 ms |
52044 KB |
Output is correct |
9 |
Correct |
564 ms |
39412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
4984 KB |
Output is correct |
4 |
Correct |
6 ms |
4984 KB |
Output is correct |
5 |
Correct |
7 ms |
4984 KB |
Output is correct |
6 |
Correct |
7 ms |
4984 KB |
Output is correct |
7 |
Correct |
6 ms |
4984 KB |
Output is correct |
8 |
Correct |
6 ms |
5112 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
7 ms |
5112 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
12 |
Correct |
7 ms |
4984 KB |
Output is correct |
13 |
Correct |
10 ms |
5368 KB |
Output is correct |
14 |
Correct |
10 ms |
5496 KB |
Output is correct |
15 |
Correct |
10 ms |
5368 KB |
Output is correct |
16 |
Correct |
10 ms |
5368 KB |
Output is correct |
17 |
Correct |
10 ms |
5368 KB |
Output is correct |
18 |
Correct |
10 ms |
5368 KB |
Output is correct |
19 |
Correct |
10 ms |
5368 KB |
Output is correct |
20 |
Correct |
10 ms |
5496 KB |
Output is correct |
21 |
Correct |
10 ms |
5496 KB |
Output is correct |
22 |
Correct |
11 ms |
5368 KB |
Output is correct |
23 |
Correct |
10 ms |
5368 KB |
Output is correct |
24 |
Correct |
10 ms |
5468 KB |
Output is correct |
25 |
Correct |
10 ms |
5496 KB |
Output is correct |
26 |
Correct |
11 ms |
5624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
710 ms |
40852 KB |
Output is correct |
3 |
Correct |
791 ms |
57716 KB |
Output is correct |
4 |
Correct |
631 ms |
39516 KB |
Output is correct |
5 |
Correct |
657 ms |
40756 KB |
Output is correct |
6 |
Correct |
742 ms |
43032 KB |
Output is correct |
7 |
Correct |
603 ms |
40864 KB |
Output is correct |
8 |
Correct |
831 ms |
56976 KB |
Output is correct |
9 |
Correct |
536 ms |
39844 KB |
Output is correct |
10 |
Correct |
6 ms |
4988 KB |
Output is correct |
11 |
Correct |
748 ms |
41008 KB |
Output is correct |
12 |
Correct |
1058 ms |
57640 KB |
Output is correct |
13 |
Correct |
686 ms |
39424 KB |
Output is correct |
14 |
Correct |
706 ms |
40744 KB |
Output is correct |
15 |
Correct |
739 ms |
43772 KB |
Output is correct |
16 |
Correct |
550 ms |
40868 KB |
Output is correct |
17 |
Correct |
779 ms |
52044 KB |
Output is correct |
18 |
Correct |
564 ms |
39412 KB |
Output is correct |
19 |
Correct |
6 ms |
5112 KB |
Output is correct |
20 |
Correct |
720 ms |
40780 KB |
Output is correct |
21 |
Correct |
729 ms |
57580 KB |
Output is correct |
22 |
Correct |
625 ms |
39544 KB |
Output is correct |
23 |
Correct |
697 ms |
40824 KB |
Output is correct |
24 |
Correct |
683 ms |
39800 KB |
Output is correct |
25 |
Correct |
720 ms |
40996 KB |
Output is correct |
26 |
Correct |
665 ms |
39772 KB |
Output is correct |
27 |
Correct |
696 ms |
40740 KB |
Output is correct |
28 |
Correct |
696 ms |
43356 KB |
Output is correct |
29 |
Correct |
730 ms |
40852 KB |
Output is correct |
30 |
Correct |
662 ms |
39880 KB |
Output is correct |
31 |
Correct |
622 ms |
40872 KB |
Output is correct |
32 |
Correct |
784 ms |
52552 KB |
Output is correct |
33 |
Correct |
531 ms |
39828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
4984 KB |
Output is correct |
4 |
Correct |
6 ms |
4984 KB |
Output is correct |
5 |
Correct |
7 ms |
4984 KB |
Output is correct |
6 |
Correct |
7 ms |
4984 KB |
Output is correct |
7 |
Correct |
6 ms |
4984 KB |
Output is correct |
8 |
Correct |
6 ms |
5112 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
7 ms |
5112 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
5112 KB |
Output is correct |
13 |
Correct |
710 ms |
40852 KB |
Output is correct |
14 |
Correct |
791 ms |
57716 KB |
Output is correct |
15 |
Correct |
631 ms |
39516 KB |
Output is correct |
16 |
Correct |
657 ms |
40756 KB |
Output is correct |
17 |
Correct |
742 ms |
43032 KB |
Output is correct |
18 |
Correct |
603 ms |
40864 KB |
Output is correct |
19 |
Correct |
831 ms |
56976 KB |
Output is correct |
20 |
Correct |
536 ms |
39844 KB |
Output is correct |
21 |
Correct |
6 ms |
4988 KB |
Output is correct |
22 |
Correct |
748 ms |
41008 KB |
Output is correct |
23 |
Correct |
1058 ms |
57640 KB |
Output is correct |
24 |
Correct |
686 ms |
39424 KB |
Output is correct |
25 |
Correct |
706 ms |
40744 KB |
Output is correct |
26 |
Correct |
739 ms |
43772 KB |
Output is correct |
27 |
Correct |
550 ms |
40868 KB |
Output is correct |
28 |
Correct |
779 ms |
52044 KB |
Output is correct |
29 |
Correct |
564 ms |
39412 KB |
Output is correct |
30 |
Correct |
7 ms |
4984 KB |
Output is correct |
31 |
Correct |
10 ms |
5368 KB |
Output is correct |
32 |
Correct |
10 ms |
5496 KB |
Output is correct |
33 |
Correct |
10 ms |
5368 KB |
Output is correct |
34 |
Correct |
10 ms |
5368 KB |
Output is correct |
35 |
Correct |
10 ms |
5368 KB |
Output is correct |
36 |
Correct |
10 ms |
5368 KB |
Output is correct |
37 |
Correct |
10 ms |
5368 KB |
Output is correct |
38 |
Correct |
10 ms |
5496 KB |
Output is correct |
39 |
Correct |
10 ms |
5496 KB |
Output is correct |
40 |
Correct |
11 ms |
5368 KB |
Output is correct |
41 |
Correct |
10 ms |
5368 KB |
Output is correct |
42 |
Correct |
10 ms |
5468 KB |
Output is correct |
43 |
Correct |
10 ms |
5496 KB |
Output is correct |
44 |
Correct |
11 ms |
5624 KB |
Output is correct |
45 |
Correct |
6 ms |
5112 KB |
Output is correct |
46 |
Correct |
720 ms |
40780 KB |
Output is correct |
47 |
Correct |
729 ms |
57580 KB |
Output is correct |
48 |
Correct |
625 ms |
39544 KB |
Output is correct |
49 |
Correct |
697 ms |
40824 KB |
Output is correct |
50 |
Correct |
683 ms |
39800 KB |
Output is correct |
51 |
Correct |
720 ms |
40996 KB |
Output is correct |
52 |
Correct |
665 ms |
39772 KB |
Output is correct |
53 |
Correct |
696 ms |
40740 KB |
Output is correct |
54 |
Correct |
696 ms |
43356 KB |
Output is correct |
55 |
Correct |
730 ms |
40852 KB |
Output is correct |
56 |
Correct |
662 ms |
39880 KB |
Output is correct |
57 |
Correct |
622 ms |
40872 KB |
Output is correct |
58 |
Correct |
784 ms |
52552 KB |
Output is correct |
59 |
Correct |
531 ms |
39828 KB |
Output is correct |
60 |
Correct |
7 ms |
5112 KB |
Output is correct |
61 |
Correct |
840 ms |
43560 KB |
Output is correct |
62 |
Correct |
826 ms |
59512 KB |
Output is correct |
63 |
Correct |
735 ms |
42176 KB |
Output is correct |
64 |
Correct |
730 ms |
43728 KB |
Output is correct |
65 |
Correct |
735 ms |
42216 KB |
Output is correct |
66 |
Correct |
698 ms |
43512 KB |
Output is correct |
67 |
Correct |
744 ms |
42120 KB |
Output is correct |
68 |
Correct |
729 ms |
43556 KB |
Output is correct |
69 |
Correct |
789 ms |
45944 KB |
Output is correct |
70 |
Correct |
709 ms |
43428 KB |
Output is correct |
71 |
Correct |
657 ms |
42800 KB |
Output is correct |
72 |
Correct |
643 ms |
44204 KB |
Output is correct |
73 |
Correct |
829 ms |
54904 KB |
Output is correct |
74 |
Correct |
590 ms |
43952 KB |
Output is correct |