// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define pb push_back
#define pf push_front
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
template<class T> using V = vector<T>;
using pi = pair<int, int>;
using vi = V<int>;
using vpi = V<pi>;
using ll = long long;
using pl = pair<ll, ll>;
using vpl = V<pl>;
using vl = V<ll>;
using db = long double;
template<class T> using pq = priority_queue<T, V<T>, greater<T>>;
const int MOD = 1e9 + 7;
const ll INFL = ll(1e17);
// START OF SEGTREE
struct Seg {
const ll ID = INFL, IDL = 0; ll comb(ll a, ll b) { return min(a, b); }
int n; vl seg, lazy;
void init(int N) {
n = 1; while(n < N) n *= 2;
seg.assign(2*n, ID); lazy.assign(2*n, IDL);
}
void pull(int x) { seg[x] = comb(seg[2*x], seg[2*x+1]); }
void push(int x, int L, int R) {
seg[x] += lazy[x];
if (L != R) for(int i = 0; i < 2; i++) lazy[2*x+i] += lazy[x];
lazy[x] = IDL;
}
void upd(int l, int r, ll v, int x, int L, int R) {
push(x, L, R); if (r < L || R < l) return;
if (l <= L && R <= r) {
lazy[x] = v; push(x, L, R); return;
}
int M = (L + R) / 2;
upd(l, r, v, 2*x, L, M);
upd(l, r, v, 2*x+1, M+1, R);
pull(x);
}
ll query(int l, int r, int x, int L, int R) {
push(x, L, R); if (r < L || R < l) return 2 * ID;
if (l <= L && R <= r) return seg[x];
int M = (L + R) / 2;
return comb(query(l, r, 2*x, L, M), query(l, r, 2*x+1, M+1, R));
}
void upd(int l, int r, ll v) { upd(l, r, v, 1, 0, n - 1); }
ll query(int l, int r) { return query(l, r, 1, 0, n - 1); }
ll query() { return seg[1]; }
};
// END OF SEGTREE
const int nax = 1e5 + 5;
// MAIN
vpi adj[nax]; int W[nax];
// CENTROID
vi chd[nax];
int sub[nax], proc[nax], siz[nax];
// CENTROID SUBTREES
vi eid[nax], st[nax], en[nax], par[nax];
Seg T[nax];
// __[u][t] = a property of the subtree rooted at u's t-th parent in the centroid tree
// START OF CENTROID
void gen(int u, int p = -1) {
sub[u] = 1;
for(auto& e : adj[u]) {
int v, i; tie(v, i) = e;
if (v != p && !proc[v]) {
gen(v, u); sub[u] += sub[v];
}
}
}
int t = 0, C = -1;
void dfs(int u, int p = -1) {
par[u].pb(C); st[u].pb(t++);
for(auto& e : adj[u]) {
int v, i; tie(v, i) = e;
if (v != p && !proc[v]) {
eid[v].pb(i);
dfs(v, u); sub[u] += sub[v];
}
}
en[u].pb(t-1);
}
int find(int u, int n, int p = -1) {
for(auto& e : adj[u]) {
int v, i; tie(v, i) = e;
if (v != p && !proc[v]) {
if (2 * sub[v] > n) return find(v, n, u);
}
}
return u;
}
int init(int u = 0) {
gen(u); int c = find(u, sub[u]);
t = 0, C = c; eid[c].pb(-1); dfs(c);
proc[c] = 1;
siz[c] = sub[u];
assert(t <= 2 * siz[c]);
T[c].init(2 * siz[c]);
for(auto& e : adj[c]) {
int v, i; tie(v, i) = e;
if (!proc[v]) {
int cv = init(v);
chd[c].pb(cv);
}
}
return c;
}
// END OF CENTROID
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, Q; cin >> N >> Q;
unordered_map<ll, int> E;
for(int i = 0; i < N - 1; i++) {
int u, v; cin >> u >> v >> W[i]; --u, --v;
adj[u].pb(mp(v, i));
adj[v].pb(mp(u, i));
E[u * 1LL * N + v] = E[v * 1LL * N + u] = i;
}
int R = init();
for(int u = 0; u < N; u++) {
reverse(begin(par[u]), end(par[u]));
reverse(begin(eid[u]), end(eid[u]));
reverse(begin(st[u]), end(st[u]));
reverse(begin(en[u]), end(en[u]));
}
for(int u = 0; u < N; u++) {
int M = sz(par[u]);
for(int p = 0; p < M; p++) {
int e = eid[u][p], x = par[u][p];
if (e != -1) {
// cout << x << " " << u << endl;
// cout << st[u][p] << " " << en[u][p] << " " << W[e] << endl;
T[x].upd(st[u][p], en[u][p], W[e]);
}
}
}
// cout << endl;
// cout << endl;
for(int q = 0; q < Q; q++) {
int type; cin >> type;
if (type == 1) {
int u; cin >> u; --u;
ll ans = 2 * INFL;
int M = sz(par[u]);
for(int p = 0; p < M; p++) {
int x = par[u][p], pos = st[u][p];
ll dst = T[x].query(pos, pos);
if (dst >= INFL) dst -= INFL;
// cout << x << " TO " << u << ": " << dst << endl;
// cout << x << " TO CLOSEST: " << T[x].query() << endl;
ans = min(ans, dst + T[x].query());
}
cout << (ans >= INFL ? -1 : ans) << nl;
}
if (type == 2) {
int u; cin >> u; --u;
int M = sz(par[u]);
for(int p = 0; p < M; p++) {
int x = par[u][p];
int pos = st[u][p];
if (T[x].query(pos, pos) >= INFL) T[x].upd(pos, pos, -INFL);
else T[x].upd(pos, pos, +INFL);
// cout << u << " TO " << x << ": " << T[x].query(pos, pos) << endl;
}
}
if (type == 3) {
int u, v, w; cin >> u >> v >> w; --u, --v;
int i = E[u * 1LL * N + v];
for(int rep = 0; rep < 2; rep++) {
int M = sz(par[u]);
for(int p = 0; p < M; p++) {
int x = par[u][p], e = eid[u][p];
int l = st[u][p], r = en[u][p];
if (e == i) T[x].upd(l, r, w - W[i]);
}
swap(u, v);
}
W[i] = w;
}
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:157:6: warning: unused variable 'R' [-Wunused-variable]
157 | int R = init();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
23892 KB |
Output is correct |
2 |
Correct |
18 ms |
23892 KB |
Output is correct |
3 |
Correct |
19 ms |
24004 KB |
Output is correct |
4 |
Correct |
18 ms |
24012 KB |
Output is correct |
5 |
Correct |
19 ms |
23892 KB |
Output is correct |
6 |
Correct |
19 ms |
23820 KB |
Output is correct |
7 |
Correct |
12 ms |
22524 KB |
Output is correct |
8 |
Correct |
12 ms |
22484 KB |
Output is correct |
9 |
Correct |
13 ms |
22508 KB |
Output is correct |
10 |
Correct |
19 ms |
24076 KB |
Output is correct |
11 |
Correct |
18 ms |
23892 KB |
Output is correct |
12 |
Correct |
19 ms |
24020 KB |
Output is correct |
13 |
Correct |
12 ms |
22508 KB |
Output is correct |
14 |
Correct |
12 ms |
22484 KB |
Output is correct |
15 |
Correct |
12 ms |
22536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1635 ms |
196736 KB |
Output is correct |
2 |
Correct |
1698 ms |
199872 KB |
Output is correct |
3 |
Correct |
1580 ms |
197964 KB |
Output is correct |
4 |
Correct |
1249 ms |
216116 KB |
Output is correct |
5 |
Correct |
1169 ms |
200604 KB |
Output is correct |
6 |
Correct |
1229 ms |
204868 KB |
Output is correct |
7 |
Correct |
217 ms |
77124 KB |
Output is correct |
8 |
Correct |
216 ms |
76300 KB |
Output is correct |
9 |
Correct |
221 ms |
76484 KB |
Output is correct |
10 |
Correct |
1285 ms |
210812 KB |
Output is correct |
11 |
Correct |
1259 ms |
206200 KB |
Output is correct |
12 |
Correct |
1274 ms |
217180 KB |
Output is correct |
13 |
Correct |
239 ms |
76548 KB |
Output is correct |
14 |
Correct |
221 ms |
76700 KB |
Output is correct |
15 |
Correct |
215 ms |
76940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2292 ms |
1018756 KB |
Output is correct |
2 |
Correct |
2091 ms |
1022764 KB |
Output is correct |
3 |
Correct |
2198 ms |
1025124 KB |
Output is correct |
4 |
Correct |
1999 ms |
978212 KB |
Output is correct |
5 |
Correct |
2169 ms |
981572 KB |
Output is correct |
6 |
Correct |
2131 ms |
975572 KB |
Output is correct |
7 |
Correct |
1992 ms |
973504 KB |
Output is correct |
8 |
Correct |
2085 ms |
1016736 KB |
Output is correct |
9 |
Correct |
2158 ms |
976224 KB |
Output is correct |
10 |
Correct |
2096 ms |
1022896 KB |
Output is correct |
11 |
Correct |
2235 ms |
979128 KB |
Output is correct |
12 |
Correct |
2110 ms |
969624 KB |
Output is correct |
13 |
Correct |
2130 ms |
1016784 KB |
Output is correct |
14 |
Correct |
2067 ms |
969392 KB |
Output is correct |
15 |
Correct |
2205 ms |
979280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1976 ms |
217352 KB |
Output is correct |
2 |
Correct |
1982 ms |
226276 KB |
Output is correct |
3 |
Correct |
1848 ms |
193608 KB |
Output is correct |
4 |
Correct |
1531 ms |
216076 KB |
Output is correct |
5 |
Correct |
1564 ms |
210692 KB |
Output is correct |
6 |
Correct |
1576 ms |
212120 KB |
Output is correct |
7 |
Correct |
248 ms |
76904 KB |
Output is correct |
8 |
Correct |
241 ms |
77720 KB |
Output is correct |
9 |
Correct |
239 ms |
76988 KB |
Output is correct |
10 |
Correct |
1560 ms |
242236 KB |
Output is correct |
11 |
Correct |
1409 ms |
204508 KB |
Output is correct |
12 |
Correct |
1437 ms |
211420 KB |
Output is correct |
13 |
Correct |
243 ms |
76688 KB |
Output is correct |
14 |
Correct |
256 ms |
77824 KB |
Output is correct |
15 |
Correct |
239 ms |
77120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2017 ms |
213540 KB |
Output is correct |
2 |
Correct |
1914 ms |
217468 KB |
Output is correct |
3 |
Correct |
1939 ms |
206020 KB |
Output is correct |
4 |
Correct |
1437 ms |
218316 KB |
Output is correct |
5 |
Correct |
1532 ms |
216764 KB |
Output is correct |
6 |
Correct |
1471 ms |
212332 KB |
Output is correct |
7 |
Correct |
257 ms |
76500 KB |
Output is correct |
8 |
Correct |
240 ms |
77132 KB |
Output is correct |
9 |
Correct |
260 ms |
77212 KB |
Output is correct |
10 |
Correct |
1432 ms |
199160 KB |
Output is correct |
11 |
Correct |
1424 ms |
204500 KB |
Output is correct |
12 |
Correct |
1465 ms |
229996 KB |
Output is correct |
13 |
Correct |
248 ms |
77212 KB |
Output is correct |
14 |
Correct |
277 ms |
76632 KB |
Output is correct |
15 |
Correct |
260 ms |
76616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
23892 KB |
Output is correct |
2 |
Correct |
18 ms |
23892 KB |
Output is correct |
3 |
Correct |
19 ms |
24004 KB |
Output is correct |
4 |
Correct |
18 ms |
24012 KB |
Output is correct |
5 |
Correct |
19 ms |
23892 KB |
Output is correct |
6 |
Correct |
19 ms |
23820 KB |
Output is correct |
7 |
Correct |
12 ms |
22524 KB |
Output is correct |
8 |
Correct |
12 ms |
22484 KB |
Output is correct |
9 |
Correct |
13 ms |
22508 KB |
Output is correct |
10 |
Correct |
19 ms |
24076 KB |
Output is correct |
11 |
Correct |
18 ms |
23892 KB |
Output is correct |
12 |
Correct |
19 ms |
24020 KB |
Output is correct |
13 |
Correct |
12 ms |
22508 KB |
Output is correct |
14 |
Correct |
12 ms |
22484 KB |
Output is correct |
15 |
Correct |
12 ms |
22536 KB |
Output is correct |
16 |
Correct |
1635 ms |
196736 KB |
Output is correct |
17 |
Correct |
1698 ms |
199872 KB |
Output is correct |
18 |
Correct |
1580 ms |
197964 KB |
Output is correct |
19 |
Correct |
1249 ms |
216116 KB |
Output is correct |
20 |
Correct |
1169 ms |
200604 KB |
Output is correct |
21 |
Correct |
1229 ms |
204868 KB |
Output is correct |
22 |
Correct |
217 ms |
77124 KB |
Output is correct |
23 |
Correct |
216 ms |
76300 KB |
Output is correct |
24 |
Correct |
221 ms |
76484 KB |
Output is correct |
25 |
Correct |
1285 ms |
210812 KB |
Output is correct |
26 |
Correct |
1259 ms |
206200 KB |
Output is correct |
27 |
Correct |
1274 ms |
217180 KB |
Output is correct |
28 |
Correct |
239 ms |
76548 KB |
Output is correct |
29 |
Correct |
221 ms |
76700 KB |
Output is correct |
30 |
Correct |
215 ms |
76940 KB |
Output is correct |
31 |
Correct |
2292 ms |
1018756 KB |
Output is correct |
32 |
Correct |
2091 ms |
1022764 KB |
Output is correct |
33 |
Correct |
2198 ms |
1025124 KB |
Output is correct |
34 |
Correct |
1999 ms |
978212 KB |
Output is correct |
35 |
Correct |
2169 ms |
981572 KB |
Output is correct |
36 |
Correct |
2131 ms |
975572 KB |
Output is correct |
37 |
Correct |
1992 ms |
973504 KB |
Output is correct |
38 |
Correct |
2085 ms |
1016736 KB |
Output is correct |
39 |
Correct |
2158 ms |
976224 KB |
Output is correct |
40 |
Correct |
2096 ms |
1022896 KB |
Output is correct |
41 |
Correct |
2235 ms |
979128 KB |
Output is correct |
42 |
Correct |
2110 ms |
969624 KB |
Output is correct |
43 |
Correct |
2130 ms |
1016784 KB |
Output is correct |
44 |
Correct |
2067 ms |
969392 KB |
Output is correct |
45 |
Correct |
2205 ms |
979280 KB |
Output is correct |
46 |
Correct |
1976 ms |
217352 KB |
Output is correct |
47 |
Correct |
1982 ms |
226276 KB |
Output is correct |
48 |
Correct |
1848 ms |
193608 KB |
Output is correct |
49 |
Correct |
1531 ms |
216076 KB |
Output is correct |
50 |
Correct |
1564 ms |
210692 KB |
Output is correct |
51 |
Correct |
1576 ms |
212120 KB |
Output is correct |
52 |
Correct |
248 ms |
76904 KB |
Output is correct |
53 |
Correct |
241 ms |
77720 KB |
Output is correct |
54 |
Correct |
239 ms |
76988 KB |
Output is correct |
55 |
Correct |
1560 ms |
242236 KB |
Output is correct |
56 |
Correct |
1409 ms |
204508 KB |
Output is correct |
57 |
Correct |
1437 ms |
211420 KB |
Output is correct |
58 |
Correct |
243 ms |
76688 KB |
Output is correct |
59 |
Correct |
256 ms |
77824 KB |
Output is correct |
60 |
Correct |
239 ms |
77120 KB |
Output is correct |
61 |
Correct |
2017 ms |
213540 KB |
Output is correct |
62 |
Correct |
1914 ms |
217468 KB |
Output is correct |
63 |
Correct |
1939 ms |
206020 KB |
Output is correct |
64 |
Correct |
1437 ms |
218316 KB |
Output is correct |
65 |
Correct |
1532 ms |
216764 KB |
Output is correct |
66 |
Correct |
1471 ms |
212332 KB |
Output is correct |
67 |
Correct |
257 ms |
76500 KB |
Output is correct |
68 |
Correct |
240 ms |
77132 KB |
Output is correct |
69 |
Correct |
260 ms |
77212 KB |
Output is correct |
70 |
Correct |
1432 ms |
199160 KB |
Output is correct |
71 |
Correct |
1424 ms |
204500 KB |
Output is correct |
72 |
Correct |
1465 ms |
229996 KB |
Output is correct |
73 |
Correct |
248 ms |
77212 KB |
Output is correct |
74 |
Correct |
277 ms |
76632 KB |
Output is correct |
75 |
Correct |
260 ms |
76616 KB |
Output is correct |
76 |
Correct |
12 ms |
21472 KB |
Output is correct |
77 |
Correct |
11 ms |
21388 KB |
Output is correct |
78 |
Correct |
11 ms |
21480 KB |
Output is correct |
79 |
Correct |
1803 ms |
196304 KB |
Output is correct |
80 |
Correct |
1963 ms |
225076 KB |
Output is correct |
81 |
Correct |
2095 ms |
202996 KB |
Output is correct |
82 |
Correct |
1474 ms |
204920 KB |
Output is correct |
83 |
Correct |
1458 ms |
208528 KB |
Output is correct |
84 |
Correct |
1547 ms |
228084 KB |
Output is correct |
85 |
Correct |
248 ms |
77908 KB |
Output is correct |
86 |
Correct |
268 ms |
77424 KB |
Output is correct |
87 |
Correct |
251 ms |
76820 KB |
Output is correct |
88 |
Correct |
1473 ms |
236620 KB |
Output is correct |
89 |
Runtime error |
473 ms |
1048576 KB |
Execution killed with signal 9 |
90 |
Halted |
0 ms |
0 KB |
- |