#include <bits/stdc++.h>
#define all(v) v.begin(),v.end()
#define F first
#define S second
using namespace std;
typedef int ll;
typedef pair<ll,ll>pll;
const ll oo=1e5+9;
const ll inf=1e9+9;
struct node
{
ll a,b,c;
} a[oo];
bool cmp(const node&A,const node&B)
{
return A.c < B.c;
}
ll n,m,p[509],q;
bool vis[oo];
vector<ll>lst;
vector<pll>v;
ll gp(ll z)
{
if(p[z]<0)
return z;
return p[z]=gp(p[z]);
}
bool mrg(ll x,ll y)
{
x=gp(x),y=gp(y);
if(x==y)
return 0;
if(p[x]>p[y])
swap(x,y);
p[x]+=p[y];
p[y]=x;
return 1;
}
void solve()
{
cin>>n>>m;
for(ll i=0; i<m; i++)
cin>>a[i].a>>a[i].b>>a[i].c;
sort(a,a+m,cmp);
for(ll i=0; i<m; i++)
{
for(ll j=1; j<=n; j++)
p[j]=-1;
lst.push_back(i);
ll id=-1;
for(ll j=(ll)lst.size()-1; j>=0; j--)
{
ll x=lst[j];
if(!mrg(a[x].a,a[x].b))
{
id=j;
break;
}
}
if(id!=-1)
{
ll x=lst[id];
vis[x]=1;
v.push_back({(a[x].c+a[i].c)/2+1,x});
v.push_back({(a[x].c+a[i].c)/2+1,i});
lst.erase(lst.begin()+id);
}
else
v.push_back({0,i});
}
for(auto&z:lst)
v.push_back({inf,z});
sort(all(v));
memset(vis,0,sizeof vis);
cin>>q;
ll le=0,cl=0,cr=0;
long long suml=0,sumr=0;
multiset<ll>s;
while(q--)
{
ll x;
cin>>x;
while(le<v.size()&&v[le].F<=x)
{
ll o=v[le].S;
if(vis[o])
{
if(s.find(a[o].c)!=s.end())
{
cl--,suml-=a[o].c;
s.erase(s.find(a[o].c));
}
else
{
cr--,sumr-=a[o].c;
}
}
else
{
vis[o]=1;
cl++,suml+=a[o].c;
s.insert(a[o].c);
}
le++;
}
while(!s.empty()&&*s.begin()<x)
{
cl--,suml-=*s.begin();
cr++,sumr+=*s.begin();
s.erase(s.begin());
}
cout<<suml-sumr-1ll*cl*x+1ll*cr*x<<"\n";
}
}
int main()
{
ios_base::sync_with_stdio(0),cin.tie(0);
ll tt=1;
// cin>>tt;
while(tt--)
solve();
return 0;
}
Compilation message
reconstruction.cpp: In function 'void solve()':
reconstruction.cpp:83:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | while(le<v.size()&&v[le].F<=x)
| ~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
267 ms |
5488 KB |
Output is correct |
21 |
Correct |
173 ms |
5584 KB |
Output is correct |
22 |
Correct |
163 ms |
5580 KB |
Output is correct |
23 |
Correct |
177 ms |
5580 KB |
Output is correct |
24 |
Correct |
210 ms |
5580 KB |
Output is correct |
25 |
Correct |
261 ms |
5580 KB |
Output is correct |
26 |
Correct |
265 ms |
5452 KB |
Output is correct |
27 |
Correct |
271 ms |
5432 KB |
Output is correct |
28 |
Correct |
271 ms |
5580 KB |
Output is correct |
29 |
Correct |
253 ms |
5584 KB |
Output is correct |
30 |
Correct |
270 ms |
5428 KB |
Output is correct |
31 |
Correct |
273 ms |
5576 KB |
Output is correct |
32 |
Correct |
306 ms |
5484 KB |
Output is correct |
33 |
Correct |
271 ms |
5584 KB |
Output is correct |
34 |
Correct |
256 ms |
5580 KB |
Output is correct |
35 |
Correct |
266 ms |
5496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
295 ms |
25656 KB |
Output is correct |
5 |
Correct |
318 ms |
25752 KB |
Output is correct |
6 |
Correct |
305 ms |
25800 KB |
Output is correct |
7 |
Correct |
212 ms |
27528 KB |
Output is correct |
8 |
Correct |
186 ms |
27848 KB |
Output is correct |
9 |
Correct |
176 ms |
27852 KB |
Output is correct |
10 |
Correct |
293 ms |
25812 KB |
Output is correct |
11 |
Correct |
185 ms |
27848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
130 ms |
22468 KB |
Output is correct |
21 |
Correct |
127 ms |
22500 KB |
Output is correct |
22 |
Correct |
134 ms |
22584 KB |
Output is correct |
23 |
Correct |
127 ms |
22352 KB |
Output is correct |
24 |
Correct |
133 ms |
22456 KB |
Output is correct |
25 |
Correct |
127 ms |
22356 KB |
Output is correct |
26 |
Correct |
130 ms |
22372 KB |
Output is correct |
27 |
Correct |
128 ms |
22356 KB |
Output is correct |
28 |
Correct |
134 ms |
22356 KB |
Output is correct |
29 |
Correct |
126 ms |
22352 KB |
Output is correct |
30 |
Correct |
137 ms |
22612 KB |
Output is correct |
31 |
Correct |
127 ms |
22352 KB |
Output is correct |
32 |
Correct |
127 ms |
23008 KB |
Output is correct |
33 |
Correct |
136 ms |
22356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
267 ms |
5488 KB |
Output is correct |
21 |
Correct |
173 ms |
5584 KB |
Output is correct |
22 |
Correct |
163 ms |
5580 KB |
Output is correct |
23 |
Correct |
177 ms |
5580 KB |
Output is correct |
24 |
Correct |
210 ms |
5580 KB |
Output is correct |
25 |
Correct |
261 ms |
5580 KB |
Output is correct |
26 |
Correct |
265 ms |
5452 KB |
Output is correct |
27 |
Correct |
271 ms |
5432 KB |
Output is correct |
28 |
Correct |
271 ms |
5580 KB |
Output is correct |
29 |
Correct |
253 ms |
5584 KB |
Output is correct |
30 |
Correct |
270 ms |
5428 KB |
Output is correct |
31 |
Correct |
273 ms |
5576 KB |
Output is correct |
32 |
Correct |
306 ms |
5484 KB |
Output is correct |
33 |
Correct |
271 ms |
5584 KB |
Output is correct |
34 |
Correct |
256 ms |
5580 KB |
Output is correct |
35 |
Correct |
266 ms |
5496 KB |
Output is correct |
36 |
Correct |
263 ms |
5584 KB |
Output is correct |
37 |
Correct |
148 ms |
5576 KB |
Output is correct |
38 |
Correct |
156 ms |
5576 KB |
Output is correct |
39 |
Correct |
169 ms |
5580 KB |
Output is correct |
40 |
Correct |
209 ms |
5580 KB |
Output is correct |
41 |
Correct |
252 ms |
5584 KB |
Output is correct |
42 |
Correct |
267 ms |
5636 KB |
Output is correct |
43 |
Correct |
269 ms |
5696 KB |
Output is correct |
44 |
Correct |
262 ms |
5636 KB |
Output is correct |
45 |
Correct |
255 ms |
5580 KB |
Output is correct |
46 |
Correct |
264 ms |
5580 KB |
Output is correct |
47 |
Correct |
271 ms |
5548 KB |
Output is correct |
48 |
Correct |
266 ms |
5580 KB |
Output is correct |
49 |
Correct |
259 ms |
5696 KB |
Output is correct |
50 |
Correct |
255 ms |
5864 KB |
Output is correct |
51 |
Correct |
262 ms |
5616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
267 ms |
5488 KB |
Output is correct |
21 |
Correct |
173 ms |
5584 KB |
Output is correct |
22 |
Correct |
163 ms |
5580 KB |
Output is correct |
23 |
Correct |
177 ms |
5580 KB |
Output is correct |
24 |
Correct |
210 ms |
5580 KB |
Output is correct |
25 |
Correct |
261 ms |
5580 KB |
Output is correct |
26 |
Correct |
265 ms |
5452 KB |
Output is correct |
27 |
Correct |
271 ms |
5432 KB |
Output is correct |
28 |
Correct |
271 ms |
5580 KB |
Output is correct |
29 |
Correct |
253 ms |
5584 KB |
Output is correct |
30 |
Correct |
270 ms |
5428 KB |
Output is correct |
31 |
Correct |
273 ms |
5576 KB |
Output is correct |
32 |
Correct |
306 ms |
5484 KB |
Output is correct |
33 |
Correct |
271 ms |
5584 KB |
Output is correct |
34 |
Correct |
256 ms |
5580 KB |
Output is correct |
35 |
Correct |
266 ms |
5496 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
344 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
295 ms |
25656 KB |
Output is correct |
40 |
Correct |
318 ms |
25752 KB |
Output is correct |
41 |
Correct |
305 ms |
25800 KB |
Output is correct |
42 |
Correct |
212 ms |
27528 KB |
Output is correct |
43 |
Correct |
186 ms |
27848 KB |
Output is correct |
44 |
Correct |
176 ms |
27852 KB |
Output is correct |
45 |
Correct |
293 ms |
25812 KB |
Output is correct |
46 |
Correct |
185 ms |
27848 KB |
Output is correct |
47 |
Correct |
1 ms |
344 KB |
Output is correct |
48 |
Correct |
130 ms |
22468 KB |
Output is correct |
49 |
Correct |
127 ms |
22500 KB |
Output is correct |
50 |
Correct |
134 ms |
22584 KB |
Output is correct |
51 |
Correct |
127 ms |
22352 KB |
Output is correct |
52 |
Correct |
133 ms |
22456 KB |
Output is correct |
53 |
Correct |
127 ms |
22356 KB |
Output is correct |
54 |
Correct |
130 ms |
22372 KB |
Output is correct |
55 |
Correct |
128 ms |
22356 KB |
Output is correct |
56 |
Correct |
134 ms |
22356 KB |
Output is correct |
57 |
Correct |
126 ms |
22352 KB |
Output is correct |
58 |
Correct |
137 ms |
22612 KB |
Output is correct |
59 |
Correct |
127 ms |
22352 KB |
Output is correct |
60 |
Correct |
127 ms |
23008 KB |
Output is correct |
61 |
Correct |
136 ms |
22356 KB |
Output is correct |
62 |
Correct |
263 ms |
5584 KB |
Output is correct |
63 |
Correct |
148 ms |
5576 KB |
Output is correct |
64 |
Correct |
156 ms |
5576 KB |
Output is correct |
65 |
Correct |
169 ms |
5580 KB |
Output is correct |
66 |
Correct |
209 ms |
5580 KB |
Output is correct |
67 |
Correct |
252 ms |
5584 KB |
Output is correct |
68 |
Correct |
267 ms |
5636 KB |
Output is correct |
69 |
Correct |
269 ms |
5696 KB |
Output is correct |
70 |
Correct |
262 ms |
5636 KB |
Output is correct |
71 |
Correct |
255 ms |
5580 KB |
Output is correct |
72 |
Correct |
264 ms |
5580 KB |
Output is correct |
73 |
Correct |
271 ms |
5548 KB |
Output is correct |
74 |
Correct |
266 ms |
5580 KB |
Output is correct |
75 |
Correct |
259 ms |
5696 KB |
Output is correct |
76 |
Correct |
255 ms |
5864 KB |
Output is correct |
77 |
Correct |
262 ms |
5616 KB |
Output is correct |
78 |
Correct |
406 ms |
24668 KB |
Output is correct |
79 |
Correct |
283 ms |
26820 KB |
Output is correct |
80 |
Correct |
290 ms |
25668 KB |
Output is correct |
81 |
Correct |
314 ms |
25760 KB |
Output is correct |
82 |
Correct |
367 ms |
24748 KB |
Output is correct |
83 |
Correct |
393 ms |
24644 KB |
Output is correct |
84 |
Correct |
400 ms |
24764 KB |
Output is correct |
85 |
Correct |
408 ms |
24792 KB |
Output is correct |
86 |
Correct |
405 ms |
24784 KB |
Output is correct |
87 |
Correct |
381 ms |
26312 KB |
Output is correct |
88 |
Correct |
388 ms |
24776 KB |
Output is correct |
89 |
Correct |
397 ms |
24608 KB |
Output is correct |
90 |
Correct |
390 ms |
24772 KB |
Output is correct |
91 |
Correct |
380 ms |
24516 KB |
Output is correct |
92 |
Correct |
381 ms |
27532 KB |
Output is correct |
93 |
Correct |
384 ms |
25800 KB |
Output is correct |