#include <bits/stdc++.h>
#define LL long long
#define PII pair<int, int>
#define all_of(v) (v).begin(), (v).end()
#define fi first
#define se second
const int MAXM = 100005;
const int MAXN = 505;
const LL INF = (LL) 1e9 + 8763;
using namespace std;
struct Edge {
int u, v, w;
};
struct DSU {
int pa[MAXN], sz[MAXN];
void init(int n) {
for (int i = 0; i <= n; i++) {
pa[i] = i;
sz[i] = 1;
}
}
int find(int x) {
return x == pa[x] ? x : pa[x] = find(pa[x]);
}
int merge(int x, int y) {
x = find(x); y = find(y);
if (x == y) return 0;
if (sz[x] < sz[y]) swap(x, y);
sz[x] += sz[y];
pa[y] = x;
return 1;
}
} D;
int n, m;
Edge E[MAXM];
int qn;
vector<int> X;
void compute(vector<int> &L, vector<int> &R) {
L.resize(m);
R.resize(m);
for (int i = 0; i < m; i++) {
L[i] = i;
R[i] = m - i - 1;
}
vector<int> prev_tree;
for (int i = 0; i < m; i++) {
vector<int> cur_tree(1, i);
D.init(n);
D.merge(E[i].u, E[i].v);
for (int id : prev_tree) {
if (D.find(E[id].u) == D.find(E[id].v)) {
L[i] = R[id] = i - id - 1;
continue;
}
D.merge(E[id].u, E[id].v);
cur_tree.push_back(id);
}
prev_tree = move(cur_tree);
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
E[i] = (Edge){u, v, w};
}
sort(E, E + m, [&] (Edge e1, Edge e2) {
return e1.w < e2.w;
});
cin >> qn;
X.resize(qn);
for (int i = 0; i < qn; i++) {
cin >> X[i];
}
// compute
vector<int> L, R;
compute(L, R);
// make answer
vector<PII> ev;
for (int i = 0; i < m; i++) {
int l = i - L[i] - 1, r = i + R[i] + 1, wl, wr;
if (l < 0) {
wl = -INF;
}
else {
wl = (E[i].w + E[l].w) / 2 + 1;
}
if (r >= m) {
wr = INF;
}
else {
wr = (E[i].w + E[r].w) / 2;
}
if (wl <= wr) {
int pl = lower_bound(all_of(X), wl) - X.begin();
int pm = lower_bound(all_of(X), max(E[i].w, wl)) - X.begin();
int pr = upper_bound(all_of(X), wr) - X.begin();
ev.push_back(PII(pl, i));
ev.push_back(PII(pm, i));
ev.push_back(PII(pr, i));
}
}
// sweep
int ptr = 0, neg = 0, pos = 0;
LL cur = 0;
vector<int> state(m);
vector<LL> ans(qn, 0);
sort(all_of(ev));
for (int i = 0; i < qn; i++) {
while (ev[ptr].fi <= i) {
int id = ev[ptr].se;
if (state[id] == 0) {
// insert
state[id] = 1;
neg++;
cur += E[id].w;
}
else if (state[id] == 1) {
// change
state[id] = 2;
neg--;
pos++;
cur -= 2 * E[id].w;
}
else {
// remove
pos--;
state[id] = 3;
cur += E[id].w;
}
ptr++;
}
// calc
assert(pos + neg == n - 1);
ans[i] = cur + (LL)pos * X[i] - (LL)neg * X[i];
}
for (int i = 0; i < qn; i++) {
cout << ans[i] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
332 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 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 |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
332 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 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 |
1082 ms |
8204 KB |
Output is correct |
21 |
Correct |
966 ms |
8204 KB |
Output is correct |
22 |
Correct |
1022 ms |
8204 KB |
Output is correct |
23 |
Correct |
1030 ms |
8172 KB |
Output is correct |
24 |
Correct |
1048 ms |
8208 KB |
Output is correct |
25 |
Correct |
1094 ms |
8200 KB |
Output is correct |
26 |
Correct |
1108 ms |
8204 KB |
Output is correct |
27 |
Correct |
1104 ms |
8208 KB |
Output is correct |
28 |
Correct |
1091 ms |
8256 KB |
Output is correct |
29 |
Correct |
1080 ms |
4636 KB |
Output is correct |
30 |
Correct |
1082 ms |
8208 KB |
Output is correct |
31 |
Correct |
1089 ms |
8204 KB |
Output is correct |
32 |
Correct |
1099 ms |
8200 KB |
Output is correct |
33 |
Correct |
1097 ms |
8320 KB |
Output is correct |
34 |
Correct |
1077 ms |
4484 KB |
Output is correct |
35 |
Correct |
1098 ms |
8204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1209 ms |
39092 KB |
Output is correct |
5 |
Correct |
1244 ms |
39176 KB |
Output is correct |
6 |
Correct |
1203 ms |
39256 KB |
Output is correct |
7 |
Correct |
838 ms |
41084 KB |
Output is correct |
8 |
Correct |
729 ms |
41156 KB |
Output is correct |
9 |
Correct |
761 ms |
41136 KB |
Output is correct |
10 |
Correct |
1198 ms |
39292 KB |
Output is correct |
11 |
Correct |
738 ms |
41380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
332 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 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 |
183 ms |
34044 KB |
Output is correct |
21 |
Correct |
203 ms |
34164 KB |
Output is correct |
22 |
Correct |
183 ms |
34108 KB |
Output is correct |
23 |
Correct |
180 ms |
34136 KB |
Output is correct |
24 |
Correct |
183 ms |
34072 KB |
Output is correct |
25 |
Correct |
183 ms |
34056 KB |
Output is correct |
26 |
Correct |
184 ms |
34056 KB |
Output is correct |
27 |
Correct |
195 ms |
34096 KB |
Output is correct |
28 |
Correct |
186 ms |
34112 KB |
Output is correct |
29 |
Correct |
188 ms |
34056 KB |
Output is correct |
30 |
Correct |
187 ms |
34136 KB |
Output is correct |
31 |
Correct |
221 ms |
34088 KB |
Output is correct |
32 |
Correct |
179 ms |
34636 KB |
Output is correct |
33 |
Correct |
193 ms |
33916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
332 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 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 |
1082 ms |
8204 KB |
Output is correct |
21 |
Correct |
966 ms |
8204 KB |
Output is correct |
22 |
Correct |
1022 ms |
8204 KB |
Output is correct |
23 |
Correct |
1030 ms |
8172 KB |
Output is correct |
24 |
Correct |
1048 ms |
8208 KB |
Output is correct |
25 |
Correct |
1094 ms |
8200 KB |
Output is correct |
26 |
Correct |
1108 ms |
8204 KB |
Output is correct |
27 |
Correct |
1104 ms |
8208 KB |
Output is correct |
28 |
Correct |
1091 ms |
8256 KB |
Output is correct |
29 |
Correct |
1080 ms |
4636 KB |
Output is correct |
30 |
Correct |
1082 ms |
8208 KB |
Output is correct |
31 |
Correct |
1089 ms |
8204 KB |
Output is correct |
32 |
Correct |
1099 ms |
8200 KB |
Output is correct |
33 |
Correct |
1097 ms |
8320 KB |
Output is correct |
34 |
Correct |
1077 ms |
4484 KB |
Output is correct |
35 |
Correct |
1098 ms |
8204 KB |
Output is correct |
36 |
Correct |
1129 ms |
8476 KB |
Output is correct |
37 |
Correct |
990 ms |
8468 KB |
Output is correct |
38 |
Correct |
1026 ms |
8408 KB |
Output is correct |
39 |
Correct |
1044 ms |
8508 KB |
Output is correct |
40 |
Correct |
1060 ms |
8472 KB |
Output is correct |
41 |
Correct |
1112 ms |
8472 KB |
Output is correct |
42 |
Correct |
1104 ms |
8476 KB |
Output is correct |
43 |
Correct |
1105 ms |
8572 KB |
Output is correct |
44 |
Correct |
1118 ms |
8484 KB |
Output is correct |
45 |
Correct |
1074 ms |
5296 KB |
Output is correct |
46 |
Correct |
1122 ms |
8472 KB |
Output is correct |
47 |
Correct |
1121 ms |
8508 KB |
Output is correct |
48 |
Correct |
1113 ms |
8476 KB |
Output is correct |
49 |
Correct |
1113 ms |
8468 KB |
Output is correct |
50 |
Correct |
1086 ms |
5148 KB |
Output is correct |
51 |
Correct |
1115 ms |
8480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
332 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 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 |
1082 ms |
8204 KB |
Output is correct |
21 |
Correct |
966 ms |
8204 KB |
Output is correct |
22 |
Correct |
1022 ms |
8204 KB |
Output is correct |
23 |
Correct |
1030 ms |
8172 KB |
Output is correct |
24 |
Correct |
1048 ms |
8208 KB |
Output is correct |
25 |
Correct |
1094 ms |
8200 KB |
Output is correct |
26 |
Correct |
1108 ms |
8204 KB |
Output is correct |
27 |
Correct |
1104 ms |
8208 KB |
Output is correct |
28 |
Correct |
1091 ms |
8256 KB |
Output is correct |
29 |
Correct |
1080 ms |
4636 KB |
Output is correct |
30 |
Correct |
1082 ms |
8208 KB |
Output is correct |
31 |
Correct |
1089 ms |
8204 KB |
Output is correct |
32 |
Correct |
1099 ms |
8200 KB |
Output is correct |
33 |
Correct |
1097 ms |
8320 KB |
Output is correct |
34 |
Correct |
1077 ms |
4484 KB |
Output is correct |
35 |
Correct |
1098 ms |
8204 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
328 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1209 ms |
39092 KB |
Output is correct |
40 |
Correct |
1244 ms |
39176 KB |
Output is correct |
41 |
Correct |
1203 ms |
39256 KB |
Output is correct |
42 |
Correct |
838 ms |
41084 KB |
Output is correct |
43 |
Correct |
729 ms |
41156 KB |
Output is correct |
44 |
Correct |
761 ms |
41136 KB |
Output is correct |
45 |
Correct |
1198 ms |
39292 KB |
Output is correct |
46 |
Correct |
738 ms |
41380 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
183 ms |
34044 KB |
Output is correct |
49 |
Correct |
203 ms |
34164 KB |
Output is correct |
50 |
Correct |
183 ms |
34108 KB |
Output is correct |
51 |
Correct |
180 ms |
34136 KB |
Output is correct |
52 |
Correct |
183 ms |
34072 KB |
Output is correct |
53 |
Correct |
183 ms |
34056 KB |
Output is correct |
54 |
Correct |
184 ms |
34056 KB |
Output is correct |
55 |
Correct |
195 ms |
34096 KB |
Output is correct |
56 |
Correct |
186 ms |
34112 KB |
Output is correct |
57 |
Correct |
188 ms |
34056 KB |
Output is correct |
58 |
Correct |
187 ms |
34136 KB |
Output is correct |
59 |
Correct |
221 ms |
34088 KB |
Output is correct |
60 |
Correct |
179 ms |
34636 KB |
Output is correct |
61 |
Correct |
193 ms |
33916 KB |
Output is correct |
62 |
Correct |
1129 ms |
8476 KB |
Output is correct |
63 |
Correct |
990 ms |
8468 KB |
Output is correct |
64 |
Correct |
1026 ms |
8408 KB |
Output is correct |
65 |
Correct |
1044 ms |
8508 KB |
Output is correct |
66 |
Correct |
1060 ms |
8472 KB |
Output is correct |
67 |
Correct |
1112 ms |
8472 KB |
Output is correct |
68 |
Correct |
1104 ms |
8476 KB |
Output is correct |
69 |
Correct |
1105 ms |
8572 KB |
Output is correct |
70 |
Correct |
1118 ms |
8484 KB |
Output is correct |
71 |
Correct |
1074 ms |
5296 KB |
Output is correct |
72 |
Correct |
1122 ms |
8472 KB |
Output is correct |
73 |
Correct |
1121 ms |
8508 KB |
Output is correct |
74 |
Correct |
1113 ms |
8476 KB |
Output is correct |
75 |
Correct |
1113 ms |
8468 KB |
Output is correct |
76 |
Correct |
1086 ms |
5148 KB |
Output is correct |
77 |
Correct |
1115 ms |
8480 KB |
Output is correct |
78 |
Correct |
1354 ms |
38156 KB |
Output is correct |
79 |
Correct |
1121 ms |
40208 KB |
Output is correct |
80 |
Correct |
1213 ms |
39296 KB |
Output is correct |
81 |
Correct |
1199 ms |
39180 KB |
Output is correct |
82 |
Correct |
1232 ms |
38340 KB |
Output is correct |
83 |
Correct |
1296 ms |
38136 KB |
Output is correct |
84 |
Correct |
1280 ms |
38124 KB |
Output is correct |
85 |
Correct |
1306 ms |
38264 KB |
Output is correct |
86 |
Correct |
1321 ms |
38148 KB |
Output is correct |
87 |
Correct |
1254 ms |
37704 KB |
Output is correct |
88 |
Correct |
1308 ms |
38188 KB |
Output is correct |
89 |
Correct |
1319 ms |
38172 KB |
Output is correct |
90 |
Correct |
1288 ms |
38256 KB |
Output is correct |
91 |
Correct |
1273 ms |
38044 KB |
Output is correct |
92 |
Correct |
1219 ms |
38664 KB |
Output is correct |
93 |
Correct |
1260 ms |
39288 KB |
Output is correct |