#include <bits/stdc++.h>
using namespace std;
set<pair<int,int> > mst[505];
int A[100005];
int B[100005];
int W[100005];
int find_min(int node, int dest, int pa){
if (node==dest) return -1;
for (auto x : mst[node]){
if (x.first==pa) continue;
int res = find_min(x.first,dest,node);
if (res!=-2){
if (res==-1) return x.second;
if (W[res]<W[x.second]) return res;
else return x.second;
}
}
return -2;
}
int n;
vector<pair<int,int> > add,del;
const long long INF = 1000000010;
long long fenw[2][1300005];
void upd(int pos, int val, int id){
pos++;
while (pos<1300005){
fenw[id][pos]+=val;
pos+=(pos&-pos);
}
}
long long qu(int pos, int id){
long long ans = 0;
pos++;
while (pos>0){
ans += fenw[id][pos];
pos -= pos&-pos;
}
return ans;
}
long long ans[1000005];
vector<int> discr;
void upd2(int pos, int val, int id){
int t = lower_bound(discr.begin(),discr.end(),pos)-discr.begin();
upd(t,val,id);
}
long long qu2(int pos, int id){
int t = lower_bound(discr.begin(),discr.end(),pos)-discr.begin();
return qu(t,id);
}
int main(){
scanf("%d",&n);
int m;
scanf("%d",&m);
vector<pair<int,pair<int,int>>> edgel;
for (int x = 0; x<m; x++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
edgel.push_back({c,{a,b}});
}
sort(edgel.begin(),edgel.end());
for (int x = 0; x<m; x++){
A[x] = edgel[x].second.first;
B[x] = edgel[x].second.second;
W[x] = edgel[x].first;
discr.push_back(W[x]);
}
for (int x = 0; x<m; x++){
int t = find_min(A[x],B[x],-1);
if (t==-2){
add.push_back({-1,x});
mst[A[x]].insert({B[x],x});
mst[B[x]].insert({A[x],x});
continue;
}
//printf("swap %d to %d at q %d\n",t,x,(W[x]+W[t])/2);
del.push_back({(W[x]+W[t])/2,t});
mst[A[t]].erase(mst[A[t]].lower_bound({B[t],t}));
mst[B[t]].erase(mst[B[t]].lower_bound({A[t],t}));
add.push_back({(W[x]+W[t])/2,x});
mst[A[x]].insert({B[x],x});
mst[B[x]].insert({A[x],x});
}
sort(add.begin(),add.end());
sort(del.begin(),del.end());
int q;
scanf("%d",&q);
vector<pair<int,int> > queries;
for (int x = 0; x<q; x++){
int a;
scanf("%d",&a);
queries.push_back({a,x});
discr.push_back(a);
}
sort(discr.begin(),discr.end());
sort(queries.begin(),queries.end());
int i1 = 0;
int i2 = 0;
for (auto x : queries){
while (i1<add.size() && x.first>add[i1].first){
auto t = add[i1];
upd2(W[t.second],W[t.second],0);
upd2(W[t.second],1,1);
//printf("add %d\n",t.second);
i1++;
}
while (i2<del.size() && x.first>del[i2].first){
auto t = del[i2];
upd2(W[t.second],-W[t.second],0);
upd2(W[t.second],-1,1);
//printf("del %d\n",t.second);
i2++;
}
//printf("qu %d\n",x.first);
long long sum1 = qu2(x.first,0);
long long sumtot = qu2(INF-5,0);
long long num1 = qu2(x.first,1);
long long numtot = n-1;
ans[x.second] = num1*x.first-sum1 + (sumtot-sum1)-x.first*(numtot-num1);
}
for (int x = 0; x<q; x++){
printf("%lld\n",ans[x]);
}
}
Compilation message
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:109:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | while (i1<add.size() && x.first>add[i1].first){
| ~~^~~~~~~~~~~
reconstruction.cpp:116:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
116 | while (i2<del.size() && x.first>del[i2].first){
| ~~^~~~~~~~~~~
reconstruction.cpp:60:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
reconstruction.cpp:62:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf("%d",&m);
| ~~~~~^~~~~~~~~
reconstruction.cpp:66:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | scanf("%d%d%d",&a,&b,&c);
| ~~~~~^~~~~~~~~~~~~~~~~~~
reconstruction.cpp:96:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | scanf("%d",&q);
| ~~~~~^~~~~~~~~
reconstruction.cpp:100:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | scanf("%d",&a);
| ~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
392 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
392 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
660 ms |
8008 KB |
Output is correct |
21 |
Correct |
337 ms |
8004 KB |
Output is correct |
22 |
Correct |
446 ms |
8036 KB |
Output is correct |
23 |
Correct |
480 ms |
8080 KB |
Output is correct |
24 |
Correct |
520 ms |
8084 KB |
Output is correct |
25 |
Correct |
592 ms |
8064 KB |
Output is correct |
26 |
Correct |
604 ms |
8072 KB |
Output is correct |
27 |
Correct |
606 ms |
8060 KB |
Output is correct |
28 |
Correct |
598 ms |
8020 KB |
Output is correct |
29 |
Correct |
368 ms |
6672 KB |
Output is correct |
30 |
Correct |
606 ms |
7784 KB |
Output is correct |
31 |
Correct |
604 ms |
7880 KB |
Output is correct |
32 |
Correct |
601 ms |
8100 KB |
Output is correct |
33 |
Correct |
592 ms |
7284 KB |
Output is correct |
34 |
Correct |
244 ms |
6496 KB |
Output is correct |
35 |
Correct |
612 ms |
7380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
1065 ms |
52892 KB |
Output is correct |
5 |
Correct |
1030 ms |
53560 KB |
Output is correct |
6 |
Correct |
1037 ms |
53456 KB |
Output is correct |
7 |
Correct |
617 ms |
56960 KB |
Output is correct |
8 |
Correct |
569 ms |
57592 KB |
Output is correct |
9 |
Correct |
555 ms |
57564 KB |
Output is correct |
10 |
Correct |
902 ms |
37412 KB |
Output is correct |
11 |
Correct |
499 ms |
42068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
392 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
483 ms |
43156 KB |
Output is correct |
21 |
Correct |
486 ms |
44240 KB |
Output is correct |
22 |
Correct |
475 ms |
44264 KB |
Output is correct |
23 |
Correct |
495 ms |
43916 KB |
Output is correct |
24 |
Correct |
478 ms |
44224 KB |
Output is correct |
25 |
Correct |
495 ms |
43944 KB |
Output is correct |
26 |
Correct |
480 ms |
43084 KB |
Output is correct |
27 |
Correct |
503 ms |
33864 KB |
Output is correct |
28 |
Correct |
461 ms |
44196 KB |
Output is correct |
29 |
Correct |
474 ms |
43932 KB |
Output is correct |
30 |
Correct |
486 ms |
40756 KB |
Output is correct |
31 |
Correct |
472 ms |
40888 KB |
Output is correct |
32 |
Correct |
470 ms |
34068 KB |
Output is correct |
33 |
Correct |
482 ms |
33352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
392 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
660 ms |
8008 KB |
Output is correct |
21 |
Correct |
337 ms |
8004 KB |
Output is correct |
22 |
Correct |
446 ms |
8036 KB |
Output is correct |
23 |
Correct |
480 ms |
8080 KB |
Output is correct |
24 |
Correct |
520 ms |
8084 KB |
Output is correct |
25 |
Correct |
592 ms |
8064 KB |
Output is correct |
26 |
Correct |
604 ms |
8072 KB |
Output is correct |
27 |
Correct |
606 ms |
8060 KB |
Output is correct |
28 |
Correct |
598 ms |
8020 KB |
Output is correct |
29 |
Correct |
368 ms |
6672 KB |
Output is correct |
30 |
Correct |
606 ms |
7784 KB |
Output is correct |
31 |
Correct |
604 ms |
7880 KB |
Output is correct |
32 |
Correct |
601 ms |
8100 KB |
Output is correct |
33 |
Correct |
592 ms |
7284 KB |
Output is correct |
34 |
Correct |
244 ms |
6496 KB |
Output is correct |
35 |
Correct |
612 ms |
7380 KB |
Output is correct |
36 |
Correct |
627 ms |
8916 KB |
Output is correct |
37 |
Correct |
295 ms |
8836 KB |
Output is correct |
38 |
Correct |
458 ms |
8840 KB |
Output is correct |
39 |
Correct |
492 ms |
8768 KB |
Output is correct |
40 |
Correct |
549 ms |
8840 KB |
Output is correct |
41 |
Correct |
663 ms |
8760 KB |
Output is correct |
42 |
Correct |
640 ms |
8700 KB |
Output is correct |
43 |
Correct |
634 ms |
8708 KB |
Output is correct |
44 |
Correct |
603 ms |
8900 KB |
Output is correct |
45 |
Correct |
379 ms |
7104 KB |
Output is correct |
46 |
Correct |
634 ms |
8796 KB |
Output is correct |
47 |
Correct |
625 ms |
8932 KB |
Output is correct |
48 |
Correct |
635 ms |
8844 KB |
Output is correct |
49 |
Correct |
615 ms |
8064 KB |
Output is correct |
50 |
Correct |
268 ms |
7040 KB |
Output is correct |
51 |
Correct |
601 ms |
7760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
392 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
660 ms |
8008 KB |
Output is correct |
21 |
Correct |
337 ms |
8004 KB |
Output is correct |
22 |
Correct |
446 ms |
8036 KB |
Output is correct |
23 |
Correct |
480 ms |
8080 KB |
Output is correct |
24 |
Correct |
520 ms |
8084 KB |
Output is correct |
25 |
Correct |
592 ms |
8064 KB |
Output is correct |
26 |
Correct |
604 ms |
8072 KB |
Output is correct |
27 |
Correct |
606 ms |
8060 KB |
Output is correct |
28 |
Correct |
598 ms |
8020 KB |
Output is correct |
29 |
Correct |
368 ms |
6672 KB |
Output is correct |
30 |
Correct |
606 ms |
7784 KB |
Output is correct |
31 |
Correct |
604 ms |
7880 KB |
Output is correct |
32 |
Correct |
601 ms |
8100 KB |
Output is correct |
33 |
Correct |
592 ms |
7284 KB |
Output is correct |
34 |
Correct |
244 ms |
6496 KB |
Output is correct |
35 |
Correct |
612 ms |
7380 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
0 ms |
468 KB |
Output is correct |
39 |
Correct |
1065 ms |
52892 KB |
Output is correct |
40 |
Correct |
1030 ms |
53560 KB |
Output is correct |
41 |
Correct |
1037 ms |
53456 KB |
Output is correct |
42 |
Correct |
617 ms |
56960 KB |
Output is correct |
43 |
Correct |
569 ms |
57592 KB |
Output is correct |
44 |
Correct |
555 ms |
57564 KB |
Output is correct |
45 |
Correct |
902 ms |
37412 KB |
Output is correct |
46 |
Correct |
499 ms |
42068 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
483 ms |
43156 KB |
Output is correct |
49 |
Correct |
486 ms |
44240 KB |
Output is correct |
50 |
Correct |
475 ms |
44264 KB |
Output is correct |
51 |
Correct |
495 ms |
43916 KB |
Output is correct |
52 |
Correct |
478 ms |
44224 KB |
Output is correct |
53 |
Correct |
495 ms |
43944 KB |
Output is correct |
54 |
Correct |
480 ms |
43084 KB |
Output is correct |
55 |
Correct |
503 ms |
33864 KB |
Output is correct |
56 |
Correct |
461 ms |
44196 KB |
Output is correct |
57 |
Correct |
474 ms |
43932 KB |
Output is correct |
58 |
Correct |
486 ms |
40756 KB |
Output is correct |
59 |
Correct |
472 ms |
40888 KB |
Output is correct |
60 |
Correct |
470 ms |
34068 KB |
Output is correct |
61 |
Correct |
482 ms |
33352 KB |
Output is correct |
62 |
Correct |
627 ms |
8916 KB |
Output is correct |
63 |
Correct |
295 ms |
8836 KB |
Output is correct |
64 |
Correct |
458 ms |
8840 KB |
Output is correct |
65 |
Correct |
492 ms |
8768 KB |
Output is correct |
66 |
Correct |
549 ms |
8840 KB |
Output is correct |
67 |
Correct |
663 ms |
8760 KB |
Output is correct |
68 |
Correct |
640 ms |
8700 KB |
Output is correct |
69 |
Correct |
634 ms |
8708 KB |
Output is correct |
70 |
Correct |
603 ms |
8900 KB |
Output is correct |
71 |
Correct |
379 ms |
7104 KB |
Output is correct |
72 |
Correct |
634 ms |
8796 KB |
Output is correct |
73 |
Correct |
625 ms |
8932 KB |
Output is correct |
74 |
Correct |
635 ms |
8844 KB |
Output is correct |
75 |
Correct |
615 ms |
8064 KB |
Output is correct |
76 |
Correct |
268 ms |
7040 KB |
Output is correct |
77 |
Correct |
601 ms |
7760 KB |
Output is correct |
78 |
Correct |
1106 ms |
52560 KB |
Output is correct |
79 |
Correct |
777 ms |
60184 KB |
Output is correct |
80 |
Correct |
928 ms |
57028 KB |
Output is correct |
81 |
Correct |
937 ms |
57296 KB |
Output is correct |
82 |
Correct |
995 ms |
56164 KB |
Output is correct |
83 |
Correct |
1102 ms |
56164 KB |
Output is correct |
84 |
Correct |
1086 ms |
54420 KB |
Output is correct |
85 |
Correct |
1094 ms |
56244 KB |
Output is correct |
86 |
Correct |
1046 ms |
49020 KB |
Output is correct |
87 |
Correct |
779 ms |
38504 KB |
Output is correct |
88 |
Correct |
1080 ms |
56140 KB |
Output is correct |
89 |
Correct |
1106 ms |
56092 KB |
Output is correct |
90 |
Correct |
1095 ms |
56184 KB |
Output is correct |
91 |
Correct |
1040 ms |
52852 KB |
Output is correct |
92 |
Correct |
606 ms |
37420 KB |
Output is correct |
93 |
Correct |
972 ms |
37556 KB |
Output is correct |