#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
struct edge { int u,v,w; };
edge edges[100000];
int X[1000000];
int l[100000],r[100000];
vpii adjList[500];
int parent[500],pi[500],qq[500];
vpii events;
multiset<int> S1,S2;
int main() {
int i;
int N,M,Q,A,B,W;
scanf("%d %d",&N,&M);
for (i = 0; i < M; i++) {
scanf("%d %d %d",&A,&B,&W);
edges[i] = (edge){A-1,B-1,W};
}
scanf("%d",&Q);
for (i = 0; i < Q; i++) scanf("%d",&X[i]);
sort(edges,edges+M,[](edge a,edge b) { return a.w < b.w; });
for (i = 0; i < M; i++) {
r[i] = 1e9;
memset(parent,-1,sizeof(parent));
int ll = 0,rr = 1;
qq[0] = edges[i].u;
while (ll < rr) {
int u = qq[ll++];
if (u == edges[i].v) break;
for (auto [v,x]: adjList[u]) {
if ((v != edges[i].u) && (parent[v] == -1)) {
parent[v] = u,pi[v] = x;
qq[rr++] = v;
}
}
}
if (parent[edges[i].v] == -1) l[i] = 0;
else {
int u = edges[i].v;
int m = pi[u];
while (u != edges[i].u) m = min(m,pi[u]),u = parent[u];
l[i] = (edges[m].w+edges[i].w)/2+1,r[m] = (edges[m].w+edges[i].w)/2;
adjList[edges[m].u].erase(find(adjList[edges[m].u].begin(),adjList[edges[m].u].end(),mp(edges[m].v,m)));
adjList[edges[m].v].erase(find(adjList[edges[m].v].begin(),adjList[edges[m].v].end(),mp(edges[m].u,m)));
}
adjList[edges[i].u].pb(mp(edges[i].v,i));
adjList[edges[i].v].pb(mp(edges[i].u,i));
}
int j = 0;
LLI sum1 = 0,sum2 = 0;
for (i = 0; i < M; i++) {
if (l[i] <= r[i]) events.pb(mp(l[i],i+1)),events.pb(mp(r[i]+1,-(i+1)));
}
sort(events.begin(),events.end());
for (i = 0; i < Q; i++) {
while ((j < events.size()) && (events[j].first <= X[i])) {
if (events[j].second > 0) S1.insert(edges[events[j].second-1].w),sum1 += edges[events[j].second-1].w;
else {
if (S1.find(edges[-events[j].second-1].w) != S1.end())
S1.erase(S1.find(edges[-events[j].second-1].w)),sum1 -= edges[-events[j].second-1].w;
else S2.erase(S2.find(edges[-events[j].second-1].w)),sum2 -= edges[-events[j].second-1].w;
}
j++;
}
while (!S1.empty() && (*S1.rbegin() > X[i])) {
S2.insert(*S1.rbegin()),sum2 += *S1.rbegin();
sum1 -= *S1.rbegin(),S1.erase(--S1.end());
}
while (!S2.empty() && (*S2.begin() <= X[i])) {
S1.insert(*S2.begin()),sum1 += *S2.begin();
sum2 -= *S2.begin(),S2.erase(S2.begin());
}
printf("%lld\n",(LLI) S1.size()*X[i]-sum1+sum2-(LLI) S2.size()*X[i]);
}
return 0;
}
Compilation message
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:114:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | while ((j < events.size()) && (events[j].first <= X[i])) {
| ~~^~~~~~~~~~~~~~~
reconstruction.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%d %d",&N,&M);
| ~~~~~^~~~~~~~~~~~~~~
reconstruction.cpp:71:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | scanf("%d %d %d",&A,&B,&W);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:74:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | scanf("%d",&Q);
| ~~~~~^~~~~~~~~
reconstruction.cpp:75:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | for (i = 0; i < Q; i++) scanf("%d",&X[i]);
| ~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 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 |
1 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 |
1 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 |
1 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 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 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 |
1 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 |
1 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 |
324 KB |
Output is correct |
20 |
Correct |
547 ms |
6056 KB |
Output is correct |
21 |
Correct |
91 ms |
4452 KB |
Output is correct |
22 |
Correct |
97 ms |
4400 KB |
Output is correct |
23 |
Correct |
117 ms |
4416 KB |
Output is correct |
24 |
Correct |
247 ms |
4416 KB |
Output is correct |
25 |
Correct |
514 ms |
4544 KB |
Output is correct |
26 |
Correct |
540 ms |
6220 KB |
Output is correct |
27 |
Correct |
541 ms |
6048 KB |
Output is correct |
28 |
Correct |
524 ms |
5976 KB |
Output is correct |
29 |
Correct |
504 ms |
4412 KB |
Output is correct |
30 |
Correct |
551 ms |
6328 KB |
Output is correct |
31 |
Correct |
541 ms |
6016 KB |
Output is correct |
32 |
Correct |
534 ms |
6032 KB |
Output is correct |
33 |
Correct |
524 ms |
4472 KB |
Output is correct |
34 |
Correct |
503 ms |
3864 KB |
Output is correct |
35 |
Correct |
530 ms |
6160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
Correct |
313 ms |
21528 KB |
Output is correct |
5 |
Correct |
309 ms |
21688 KB |
Output is correct |
6 |
Correct |
304 ms |
21680 KB |
Output is correct |
7 |
Correct |
308 ms |
23492 KB |
Output is correct |
8 |
Correct |
302 ms |
23640 KB |
Output is correct |
9 |
Correct |
288 ms |
23548 KB |
Output is correct |
10 |
Correct |
279 ms |
21832 KB |
Output is correct |
11 |
Correct |
291 ms |
23736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 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 |
1 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 |
1 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 |
219 ms |
19180 KB |
Output is correct |
21 |
Correct |
200 ms |
18424 KB |
Output is correct |
22 |
Correct |
213 ms |
19284 KB |
Output is correct |
23 |
Correct |
221 ms |
19276 KB |
Output is correct |
24 |
Correct |
209 ms |
19240 KB |
Output is correct |
25 |
Correct |
209 ms |
19336 KB |
Output is correct |
26 |
Correct |
210 ms |
19272 KB |
Output is correct |
27 |
Correct |
211 ms |
19180 KB |
Output is correct |
28 |
Correct |
211 ms |
19384 KB |
Output is correct |
29 |
Correct |
210 ms |
19216 KB |
Output is correct |
30 |
Correct |
246 ms |
19268 KB |
Output is correct |
31 |
Correct |
208 ms |
19228 KB |
Output is correct |
32 |
Correct |
222 ms |
19792 KB |
Output is correct |
33 |
Correct |
202 ms |
18684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 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 |
1 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 |
1 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 |
324 KB |
Output is correct |
20 |
Correct |
547 ms |
6056 KB |
Output is correct |
21 |
Correct |
91 ms |
4452 KB |
Output is correct |
22 |
Correct |
97 ms |
4400 KB |
Output is correct |
23 |
Correct |
117 ms |
4416 KB |
Output is correct |
24 |
Correct |
247 ms |
4416 KB |
Output is correct |
25 |
Correct |
514 ms |
4544 KB |
Output is correct |
26 |
Correct |
540 ms |
6220 KB |
Output is correct |
27 |
Correct |
541 ms |
6048 KB |
Output is correct |
28 |
Correct |
524 ms |
5976 KB |
Output is correct |
29 |
Correct |
504 ms |
4412 KB |
Output is correct |
30 |
Correct |
551 ms |
6328 KB |
Output is correct |
31 |
Correct |
541 ms |
6016 KB |
Output is correct |
32 |
Correct |
534 ms |
6032 KB |
Output is correct |
33 |
Correct |
524 ms |
4472 KB |
Output is correct |
34 |
Correct |
503 ms |
3864 KB |
Output is correct |
35 |
Correct |
530 ms |
6160 KB |
Output is correct |
36 |
Correct |
562 ms |
6468 KB |
Output is correct |
37 |
Correct |
116 ms |
6348 KB |
Output is correct |
38 |
Correct |
116 ms |
4600 KB |
Output is correct |
39 |
Correct |
139 ms |
4508 KB |
Output is correct |
40 |
Correct |
267 ms |
4484 KB |
Output is correct |
41 |
Correct |
532 ms |
4636 KB |
Output is correct |
42 |
Correct |
561 ms |
5564 KB |
Output is correct |
43 |
Correct |
557 ms |
5700 KB |
Output is correct |
44 |
Correct |
551 ms |
4560 KB |
Output is correct |
45 |
Correct |
505 ms |
2980 KB |
Output is correct |
46 |
Correct |
577 ms |
4612 KB |
Output is correct |
47 |
Correct |
561 ms |
4564 KB |
Output is correct |
48 |
Correct |
551 ms |
4664 KB |
Output is correct |
49 |
Correct |
537 ms |
4528 KB |
Output is correct |
50 |
Correct |
499 ms |
2592 KB |
Output is correct |
51 |
Correct |
535 ms |
4740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 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 |
1 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 |
1 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 |
324 KB |
Output is correct |
20 |
Correct |
547 ms |
6056 KB |
Output is correct |
21 |
Correct |
91 ms |
4452 KB |
Output is correct |
22 |
Correct |
97 ms |
4400 KB |
Output is correct |
23 |
Correct |
117 ms |
4416 KB |
Output is correct |
24 |
Correct |
247 ms |
4416 KB |
Output is correct |
25 |
Correct |
514 ms |
4544 KB |
Output is correct |
26 |
Correct |
540 ms |
6220 KB |
Output is correct |
27 |
Correct |
541 ms |
6048 KB |
Output is correct |
28 |
Correct |
524 ms |
5976 KB |
Output is correct |
29 |
Correct |
504 ms |
4412 KB |
Output is correct |
30 |
Correct |
551 ms |
6328 KB |
Output is correct |
31 |
Correct |
541 ms |
6016 KB |
Output is correct |
32 |
Correct |
534 ms |
6032 KB |
Output is correct |
33 |
Correct |
524 ms |
4472 KB |
Output is correct |
34 |
Correct |
503 ms |
3864 KB |
Output is correct |
35 |
Correct |
530 ms |
6160 KB |
Output is correct |
36 |
Correct |
1 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 |
Correct |
313 ms |
21528 KB |
Output is correct |
40 |
Correct |
309 ms |
21688 KB |
Output is correct |
41 |
Correct |
304 ms |
21680 KB |
Output is correct |
42 |
Correct |
308 ms |
23492 KB |
Output is correct |
43 |
Correct |
302 ms |
23640 KB |
Output is correct |
44 |
Correct |
288 ms |
23548 KB |
Output is correct |
45 |
Correct |
279 ms |
21832 KB |
Output is correct |
46 |
Correct |
291 ms |
23736 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
219 ms |
19180 KB |
Output is correct |
49 |
Correct |
200 ms |
18424 KB |
Output is correct |
50 |
Correct |
213 ms |
19284 KB |
Output is correct |
51 |
Correct |
221 ms |
19276 KB |
Output is correct |
52 |
Correct |
209 ms |
19240 KB |
Output is correct |
53 |
Correct |
209 ms |
19336 KB |
Output is correct |
54 |
Correct |
210 ms |
19272 KB |
Output is correct |
55 |
Correct |
211 ms |
19180 KB |
Output is correct |
56 |
Correct |
211 ms |
19384 KB |
Output is correct |
57 |
Correct |
210 ms |
19216 KB |
Output is correct |
58 |
Correct |
246 ms |
19268 KB |
Output is correct |
59 |
Correct |
208 ms |
19228 KB |
Output is correct |
60 |
Correct |
222 ms |
19792 KB |
Output is correct |
61 |
Correct |
202 ms |
18684 KB |
Output is correct |
62 |
Correct |
562 ms |
6468 KB |
Output is correct |
63 |
Correct |
116 ms |
6348 KB |
Output is correct |
64 |
Correct |
116 ms |
4600 KB |
Output is correct |
65 |
Correct |
139 ms |
4508 KB |
Output is correct |
66 |
Correct |
267 ms |
4484 KB |
Output is correct |
67 |
Correct |
532 ms |
4636 KB |
Output is correct |
68 |
Correct |
561 ms |
5564 KB |
Output is correct |
69 |
Correct |
557 ms |
5700 KB |
Output is correct |
70 |
Correct |
551 ms |
4560 KB |
Output is correct |
71 |
Correct |
505 ms |
2980 KB |
Output is correct |
72 |
Correct |
577 ms |
4612 KB |
Output is correct |
73 |
Correct |
561 ms |
4564 KB |
Output is correct |
74 |
Correct |
551 ms |
4664 KB |
Output is correct |
75 |
Correct |
537 ms |
4528 KB |
Output is correct |
76 |
Correct |
499 ms |
2592 KB |
Output is correct |
77 |
Correct |
535 ms |
4740 KB |
Output is correct |
78 |
Correct |
751 ms |
17948 KB |
Output is correct |
79 |
Correct |
311 ms |
19952 KB |
Output is correct |
80 |
Correct |
318 ms |
18892 KB |
Output is correct |
81 |
Correct |
346 ms |
19052 KB |
Output is correct |
82 |
Correct |
457 ms |
18152 KB |
Output is correct |
83 |
Correct |
734 ms |
17924 KB |
Output is correct |
84 |
Correct |
750 ms |
17960 KB |
Output is correct |
85 |
Correct |
764 ms |
17984 KB |
Output is correct |
86 |
Correct |
754 ms |
17892 KB |
Output is correct |
87 |
Correct |
688 ms |
18120 KB |
Output is correct |
88 |
Correct |
743 ms |
18000 KB |
Output is correct |
89 |
Correct |
752 ms |
18016 KB |
Output is correct |
90 |
Correct |
729 ms |
17976 KB |
Output is correct |
91 |
Correct |
720 ms |
17956 KB |
Output is correct |
92 |
Correct |
689 ms |
19012 KB |
Output is correct |
93 |
Correct |
716 ms |
19056 KB |
Output is correct |