#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;
unordered_map<int,long long> fenw[2];
void upd(int pos, int val, int id){
pos++;
while (pos<INF){
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];
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;
}
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});
}
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];
upd(W[t.second],W[t.second],0);
upd(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];
upd(W[t.second],-W[t.second],0);
upd(W[t.second],-1,1);
//printf("del %d\n",t.second);
i2++;
}
//printf("qu %d\n",x.first);
long long sum1 = qu(x.first,0);
long long sumtot = qu(INF-5,0);
long long num1 = qu(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:96: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]
96 | while (i1<add.size() && x.first>add[i1].first){
| ~~^~~~~~~~~~~
reconstruction.cpp:103: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]
103 | while (i2<del.size() && x.first>del[i2].first){
| ~~^~~~~~~~~~~
reconstruction.cpp:50:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
reconstruction.cpp:52:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | scanf("%d",&m);
| ~~~~~^~~~~~~~~
reconstruction.cpp:56:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | scanf("%d%d%d",&a,&b,&c);
| ~~~~~^~~~~~~~~~~~~~~~~~~
reconstruction.cpp:85:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | scanf("%d",&q);
| ~~~~~^~~~~~~~~
reconstruction.cpp:89:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | scanf("%d",&a);
| ~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1578 ms |
77372 KB |
Output is correct |
21 |
Correct |
1301 ms |
77464 KB |
Output is correct |
22 |
Correct |
1388 ms |
77544 KB |
Output is correct |
23 |
Correct |
1524 ms |
77460 KB |
Output is correct |
24 |
Correct |
1546 ms |
77508 KB |
Output is correct |
25 |
Correct |
1590 ms |
77496 KB |
Output is correct |
26 |
Correct |
1744 ms |
77336 KB |
Output is correct |
27 |
Correct |
1548 ms |
59312 KB |
Output is correct |
28 |
Correct |
729 ms |
5596 KB |
Output is correct |
29 |
Correct |
447 ms |
5216 KB |
Output is correct |
30 |
Correct |
1202 ms |
51760 KB |
Output is correct |
31 |
Correct |
1386 ms |
56768 KB |
Output is correct |
32 |
Correct |
1450 ms |
59492 KB |
Output is correct |
33 |
Correct |
942 ms |
30756 KB |
Output is correct |
34 |
Correct |
232 ms |
5200 KB |
Output is correct |
35 |
Correct |
1057 ms |
40424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Execution timed out |
5080 ms |
410732 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Execution timed out |
5095 ms |
468736 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1578 ms |
77372 KB |
Output is correct |
21 |
Correct |
1301 ms |
77464 KB |
Output is correct |
22 |
Correct |
1388 ms |
77544 KB |
Output is correct |
23 |
Correct |
1524 ms |
77460 KB |
Output is correct |
24 |
Correct |
1546 ms |
77508 KB |
Output is correct |
25 |
Correct |
1590 ms |
77496 KB |
Output is correct |
26 |
Correct |
1744 ms |
77336 KB |
Output is correct |
27 |
Correct |
1548 ms |
59312 KB |
Output is correct |
28 |
Correct |
729 ms |
5596 KB |
Output is correct |
29 |
Correct |
447 ms |
5216 KB |
Output is correct |
30 |
Correct |
1202 ms |
51760 KB |
Output is correct |
31 |
Correct |
1386 ms |
56768 KB |
Output is correct |
32 |
Correct |
1450 ms |
59492 KB |
Output is correct |
33 |
Correct |
942 ms |
30756 KB |
Output is correct |
34 |
Correct |
232 ms |
5200 KB |
Output is correct |
35 |
Correct |
1057 ms |
40424 KB |
Output is correct |
36 |
Correct |
1678 ms |
81092 KB |
Output is correct |
37 |
Correct |
1333 ms |
81136 KB |
Output is correct |
38 |
Correct |
1547 ms |
81168 KB |
Output is correct |
39 |
Correct |
1546 ms |
81104 KB |
Output is correct |
40 |
Correct |
1708 ms |
81040 KB |
Output is correct |
41 |
Correct |
1824 ms |
81160 KB |
Output is correct |
42 |
Correct |
1820 ms |
81008 KB |
Output is correct |
43 |
Correct |
1810 ms |
79680 KB |
Output is correct |
44 |
Correct |
930 ms |
17520 KB |
Output is correct |
45 |
Correct |
621 ms |
18788 KB |
Output is correct |
46 |
Correct |
1841 ms |
81384 KB |
Output is correct |
47 |
Correct |
1851 ms |
81496 KB |
Output is correct |
48 |
Correct |
1904 ms |
81468 KB |
Output is correct |
49 |
Correct |
1231 ms |
47444 KB |
Output is correct |
50 |
Correct |
388 ms |
18808 KB |
Output is correct |
51 |
Correct |
1192 ms |
41220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1578 ms |
77372 KB |
Output is correct |
21 |
Correct |
1301 ms |
77464 KB |
Output is correct |
22 |
Correct |
1388 ms |
77544 KB |
Output is correct |
23 |
Correct |
1524 ms |
77460 KB |
Output is correct |
24 |
Correct |
1546 ms |
77508 KB |
Output is correct |
25 |
Correct |
1590 ms |
77496 KB |
Output is correct |
26 |
Correct |
1744 ms |
77336 KB |
Output is correct |
27 |
Correct |
1548 ms |
59312 KB |
Output is correct |
28 |
Correct |
729 ms |
5596 KB |
Output is correct |
29 |
Correct |
447 ms |
5216 KB |
Output is correct |
30 |
Correct |
1202 ms |
51760 KB |
Output is correct |
31 |
Correct |
1386 ms |
56768 KB |
Output is correct |
32 |
Correct |
1450 ms |
59492 KB |
Output is correct |
33 |
Correct |
942 ms |
30756 KB |
Output is correct |
34 |
Correct |
232 ms |
5200 KB |
Output is correct |
35 |
Correct |
1057 ms |
40424 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Execution timed out |
5080 ms |
410732 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |