//Challenge: Accepted
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 505
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
const int inf = 1<<30;
struct edge{
int u, v, w;
edge(){u = v = w = 0;}
edge(int a, int b, int c){u = a, v = b, w = c;}
};
struct DSU{
int par[maxn], siz[maxn];
void init(int n) {
for (int i = 1;i <= n;i++) par[i] = i, siz[i] = 1;
}
int find(int a) {
return a == par[a] ? a : (par[a] = find(par[a]));
}
bool Union(int a, int b) {
a = find(a), b = find(b);
if (a == b) return 0;
if (siz[a] < siz[b]) par[a] = b;
else par[b] = a;
return 1;
}
} dsu;
vector<edge> ed;
vector<int> wei;
int n, m;
vector<int> se(int x) {
int st = lower_bound(wei.begin(), wei.end(), x) - wei.begin();
int l = st-1;
dsu.init(n);
vector<int> ret;
for (;st < m;st++) {
while (l >= 0 && x - wei[l] <= wei[st] - x) {
if (dsu.Union(ed[l].u, ed[l].v)) ret.push_back(l);
l--;
}
if (dsu.Union(ed[st].u, ed[st].v)) ret.push_back(st);
}
while (l >= 0) {
if (dsu.Union(ed[l].u, ed[l].v)) ret.push_back(l);
l--;
}
sort(ret.begin(), ret.end());
/*
debug(x);
pary(ret.begin(), ret.end());
debug();
*/
return ret;
}
ll ans[1000005];
void solve(vector<pii> v) {
if (v.size() == 0) return;
if (v.size() == 1) {
vector<int> e = se(v[0].ff);
for (int i:e) ans[v[0].ss] += abs(wei[i] - v[0].ff);
return;
}
vector<int> e = se(v[0].ff);
if (e == se(v.back().ff)) {
for (pii p:v) {
for (int i:e) ans[p.ss] += abs(wei[i] - p.ff);
}
return;
}
vector<pii> lef(v.begin(), v.begin() + (v.size() / 2));
vector<pii> rig(v.begin() + (v.size()/2), v.end());
solve(lef);
solve(rig);
}
int main() {
io
cin >> n >> m;
for (int i = 0;i < m;i++) {
int u, v, w;
cin >> u >> v >> w;
ed.push_back(edge(u, v, w));
wei.push_back(w);
}
sort(ed.begin(), ed.end(), [&] (edge x, edge y){return x.w < y.w;});
sort(wei.begin(), wei.end());
int q;
cin >> q;
vector<pii> que(q);
for (int i = 0;i < q;i++) {
cin >> que[i].ff;
que[i].ss = i;
}
solve(que);
for (int i = 0;i < q;i++) cout << ans[i] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
65 ms |
2568 KB |
Output is correct |
21 |
Correct |
67 ms |
2592 KB |
Output is correct |
22 |
Correct |
69 ms |
2636 KB |
Output is correct |
23 |
Correct |
65 ms |
2632 KB |
Output is correct |
24 |
Correct |
61 ms |
2564 KB |
Output is correct |
25 |
Correct |
73 ms |
2640 KB |
Output is correct |
26 |
Correct |
68 ms |
2668 KB |
Output is correct |
27 |
Correct |
66 ms |
2620 KB |
Output is correct |
28 |
Correct |
56 ms |
2576 KB |
Output is correct |
29 |
Correct |
53 ms |
2696 KB |
Output is correct |
30 |
Correct |
71 ms |
2564 KB |
Output is correct |
31 |
Correct |
66 ms |
2564 KB |
Output is correct |
32 |
Correct |
71 ms |
2668 KB |
Output is correct |
33 |
Correct |
71 ms |
2568 KB |
Output is correct |
34 |
Correct |
33 ms |
2564 KB |
Output is correct |
35 |
Correct |
45 ms |
2640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Execution timed out |
5067 ms |
41476 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1430 ms |
47448 KB |
Output is correct |
21 |
Correct |
1409 ms |
57132 KB |
Output is correct |
22 |
Correct |
1399 ms |
57016 KB |
Output is correct |
23 |
Correct |
1397 ms |
56948 KB |
Output is correct |
24 |
Correct |
1399 ms |
57136 KB |
Output is correct |
25 |
Correct |
1405 ms |
57084 KB |
Output is correct |
26 |
Correct |
1296 ms |
57036 KB |
Output is correct |
27 |
Correct |
813 ms |
57000 KB |
Output is correct |
28 |
Correct |
1431 ms |
57012 KB |
Output is correct |
29 |
Correct |
1341 ms |
56904 KB |
Output is correct |
30 |
Correct |
1087 ms |
57072 KB |
Output is correct |
31 |
Correct |
1120 ms |
56764 KB |
Output is correct |
32 |
Correct |
716 ms |
38348 KB |
Output is correct |
33 |
Correct |
779 ms |
57056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
65 ms |
2568 KB |
Output is correct |
21 |
Correct |
67 ms |
2592 KB |
Output is correct |
22 |
Correct |
69 ms |
2636 KB |
Output is correct |
23 |
Correct |
65 ms |
2632 KB |
Output is correct |
24 |
Correct |
61 ms |
2564 KB |
Output is correct |
25 |
Correct |
73 ms |
2640 KB |
Output is correct |
26 |
Correct |
68 ms |
2668 KB |
Output is correct |
27 |
Correct |
66 ms |
2620 KB |
Output is correct |
28 |
Correct |
56 ms |
2576 KB |
Output is correct |
29 |
Correct |
53 ms |
2696 KB |
Output is correct |
30 |
Correct |
71 ms |
2564 KB |
Output is correct |
31 |
Correct |
66 ms |
2564 KB |
Output is correct |
32 |
Correct |
71 ms |
2668 KB |
Output is correct |
33 |
Correct |
71 ms |
2568 KB |
Output is correct |
34 |
Correct |
33 ms |
2564 KB |
Output is correct |
35 |
Correct |
45 ms |
2640 KB |
Output is correct |
36 |
Execution timed out |
5048 ms |
3104 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
65 ms |
2568 KB |
Output is correct |
21 |
Correct |
67 ms |
2592 KB |
Output is correct |
22 |
Correct |
69 ms |
2636 KB |
Output is correct |
23 |
Correct |
65 ms |
2632 KB |
Output is correct |
24 |
Correct |
61 ms |
2564 KB |
Output is correct |
25 |
Correct |
73 ms |
2640 KB |
Output is correct |
26 |
Correct |
68 ms |
2668 KB |
Output is correct |
27 |
Correct |
66 ms |
2620 KB |
Output is correct |
28 |
Correct |
56 ms |
2576 KB |
Output is correct |
29 |
Correct |
53 ms |
2696 KB |
Output is correct |
30 |
Correct |
71 ms |
2564 KB |
Output is correct |
31 |
Correct |
66 ms |
2564 KB |
Output is correct |
32 |
Correct |
71 ms |
2668 KB |
Output is correct |
33 |
Correct |
71 ms |
2568 KB |
Output is correct |
34 |
Correct |
33 ms |
2564 KB |
Output is correct |
35 |
Correct |
45 ms |
2640 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Execution timed out |
5067 ms |
41476 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |