#include <bits/stdc++.h>
using namespace std;
template<typename A, typename B>
string to_string(pair<A, B>);
string to_string(string S) {
return '"' + S + '"';
}
string to_string(const char* c) {
return to_string(string(c));
}
string to_string(bool b) {
return (b ? "true" : "false");
}
template<size_t T>
string to_string(bitset<T> bs) {
return bs.to_string();
}
string to_string(vector<bool> v) {
string res = "{";
for (int i = 0; i < int(v.size()); ++i) {
if (int(res.size()) > 1) {
res += ", ";
}
res += to_string(v[i]);
}
return res + "}";
}
template<typename T>
string to_string(T v) {
string res = "{";
for (auto e : v) {
if (int(res.size()) > 1) {
res += ", ";
}
res += to_string(e);
}
return res + "}";
}
template<typename A, typename B>
string to_string(pair<A, B> p) {
return '(' + to_string(p.first) + ", " + to_string(p.second) + ')';
}
void debug_out() {
cerr << endl;
}
template<typename H, typename... T>
void debug_out(H head, T... tail) {
cerr << " " << to_string(head);
debug_out(tail...);
}
#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) void(37)
#endif
const int BLOCK = 16; // divide smaller seg trees onto blocks if it gets TLE
const long long inf = 1LL << 55;
#define def int mid = (l + r) >> 1, rv = v + ((mid - l + 1) << 1)
#define pull(v) tree[v].mn = min((l < mid) || act[l] ? tree[v + 1].mn : inf, (mid + 1 < r) || act[r] ? tree[rv].mn : inf) //change to if statements if it gets TLE
#define push(v) \
if (tree[v].tag != 0) { \
tree[v + 1].modify(tree[v].tag); \
tree[rv].modify(tree[v].tag); \
tree[v].tag = 0; \
}
struct node {
long long mn = 0;
long long tag = 0;
void modify(long long x) {
mn += x;
tag += x;
}
};
struct SegTree {
vector<node> tree;
vector<bool> act;
int n;
void modify(int v, int l, int r, int ll, int rr, int x) {
if (l >= ll && rr >= r) {
tree[v].modify(x);
return;
}
def;
push(v);
if (ll <= mid) {
modify(v + 1, l, mid, ll, rr, x);
}
if (mid < rr) {
modify(rv, mid + 1, r, ll, rr, x);
}
pull(v);
}
int path(int v, int l, int r, int p) {
if (l == r) {
return v;
}
def;
push(v);
int res = -1;
if (p <= mid) {
res = path(v + 1, l, mid, p);
} else {
res = path(rv, mid + 1, r, p);
}
pull(v);
//debug(v, l, r, tree[v].mn);
return res;
}
void build(int v, int l, int r, vector<int>& a) {
if (l == r) {
tree[v].mn = a[l];
return;
}
def;
build(v + 1, l, mid, a);
build(rv, mid + 1, r, a);
}
SegTree(int _n, vector<int>& a) : n(_n) {
tree.resize(2 * n);
act.resize(n);
build(0, 0, n - 1, a); //same as 137
}
SegTree(int _n) : n(_n) {
tree.resize(2 * n);
if (n > 1) tree[0].mn = inf;
act.resize(n);
}
SegTree() { }
long long mn() {
return (n > 1 || act[0] ? tree[0].mn : inf);
}
long long dist(int i) {
return tree[path(0, 0, n - 1, i)].mn;
}
void change(int i) {
act[i] = !act[i];
path(0, 0, n - 1, i);
}
void modify(int ll, int rr, int x) {
modify(0, 0, n - 1, ll, rr, x); //change variables to globals if it get's tle
}
};
#undef def
#undef pull
#undef push
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N, QQ;
cin >> N >> QQ;
vector<vector<int>> g(N);
vector<int> V(N - 1), U(N - 1), W(N - 1);
for (int i = 0; i < N - 1; ++i) {
cin >> V[i] >> U[i] >> W[i];
--V[i], --U[i];
g[V[i]].push_back(U[i]);
g[U[i]].push_back(V[i]);
}
vector<int> tin(N, -1), tout(N), d(N);
int tt = 0;
function<void(int)> Dfs = [&](int v) {
tin[v] = tt++;
for (auto u : g[v]) {
if (tin[u] == -1) {
d[u] = d[v] + 1;
Dfs(u);
}
}
tout[v] = tt - 1;
};
Dfs(0);
debug(tin, tout, d);
vector<int> size(N);
for (int i = 0; i < N; ++i) {
size[i] = tout[i] - tin[i] + 1;
}
vector<int> par(N, -1);
vector<int> cent_d(N);
function<void(int, int)> Centroid = [&](int v, int from) {
int to = -1;
for (auto u : g[v]) {
if (size[u] * 2 > size[v]) {
to = u;
break;
}
}
if (to == -1) {
par[v] = from;
size[v] = 0;
cent_d[v] = (from == -1 ? 0 : cent_d[from] + 1);
for (auto u : g[v]) {
if (size[u] > 0) {
Centroid(u, v);
}
}
} else {
size[v] -= size[to];
size[to] += size[v];
Centroid(to, from);
}
};
Centroid(0, -1);
debug(par, cent_d);
vector<int> inv_in(N);
for (int i = 0; i < N; ++i) {
inv_in[tin[i]] = i;
}
vector<vector<int>> inv_out(N);
for (int i = 0; i < N; ++i) {
inv_out[tout[i]].push_back(i);
}
vector<vector<int>> in(N), out(N); //change sizes to const if it gets tle
for (int v = 0; v < N; ++v) {
in[v].resize(cent_d[v] + 1);
out[v].resize(cent_d[v] + 1);
}
vector<vector<int>> st_inds(N);
for (auto v : inv_in) {
int x = v;
while (v != -1) {
in[x][cent_d[v]] = int(st_inds[v].size());
st_inds[v].push_back(tin[x]);
v = par[v];
}
}
vector<int> pt(N);
for (int foo = 0; foo < N; ++foo) for (auto v : inv_out[foo]) {
int p = v;
while (p != -1) {
int s = int(st_inds[p].size());
while (pt[p] + 1 < s && st_inds[p][pt[p] + 1] <= tout[v]) {
++pt[p];
}
out[v][cent_d[p]] = pt[p];
p = par[p];
}
}
debug(in, out);
vector<SegTree> st(N);
for (int i = 0; i < N; ++i) {
debug(i, st_inds[i]);
st[i] = SegTree(int(st_inds[i].size()));
}
debug(inv_in);
auto Lca = [&](int a, int b) {
if (cent_d[a] > cent_d[b]) {
swap(a, b);
}
while (cent_d[b] > cent_d[a]) {
b = par[b];
}
while (a != b) {
a = par[a], b = par[b];
}
return a;
};
auto Par = [&](int a, int b) {
return tin[a] <= tin[b] && tout[a] >= tin[b];
};
auto Upd = [&](int a, int b, int w) {
debug(a, b, w);
int v = Lca(a, b);
if (d[a] > d[b]) {
swap(a, b);
}
int sub = b;
while (v != -1) {
int L = in[sub][cent_d[v]];
int R = out[sub][cent_d[v]];
bool inc = !Par(sub, v);
debug(v, st_inds[v], L, R, w, inc);
//reduce two updates to one with constants if it get's tle
if (inc) {
st[v].modify(L, R, w);
} else {
if (L > 0) {
st[v].modify(0, L - 1, w);
}
if (R + 1 < st[v].n) {
st[v].modify(R + 1, st[v].n - 1, w);
}
}
v = par[v];
}
};
vector<int> new_W(N);
for (int i = 0; i < N - 1; ++i) {
if (d[V[i]] > d[U[i]]) {
swap(V[i], U[i]);
}
new_W[U[i]] = W[i];
Upd(V[i], U[i], W[i]);
}
/*
for (int v = 0; v < N; ++v) {
int p = v;
while (p != -1) {
debug(p, v, st[p].dist(in[v][cent_d[p]]));
p = par[p];
}
}
*/
swap(new_W, W);
debug(W);
while (QQ--) {
int T;
cin >> T;
if (T == 1) {
int Q;
cin >> Q;
--Q;
int v = Q;
debug(Q);
long long ans = inf;
while (v != -1) {
debug(v, st[v].dist(in[Q][cent_d[v]]), st[v].mn());
ans = min(ans, st[v].dist(in[Q][cent_d[v]]) + st[v].mn());
v = par[v];
}
cout << (ans == inf ? -1LL : ans) << '\n';
} else if (T == 2) {
int U_i;
cin >> U_i;
--U_i;
int v = U_i;
while (v != -1) {
debug(v, in[U_i][cent_d[v]]);
st[v].change(in[U_i][cent_d[v]]);
v = par[v];
}
} else {
int A, B, W_i;
cin >> A >> B >> W_i;
--A, --B;
if (d[A] > d[B]) {
swap(A, B);
}
debug(A, B, W_i - W[B]);
Upd(A, B, W_i - W[B]);
W[B] = W_i;
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1880 KB |
Output is correct |
2 |
Correct |
5 ms |
1884 KB |
Output is correct |
3 |
Correct |
5 ms |
1908 KB |
Output is correct |
4 |
Correct |
5 ms |
1880 KB |
Output is correct |
5 |
Correct |
5 ms |
1884 KB |
Output is correct |
6 |
Correct |
5 ms |
1884 KB |
Output is correct |
7 |
Correct |
3 ms |
1372 KB |
Output is correct |
8 |
Correct |
2 ms |
1372 KB |
Output is correct |
9 |
Correct |
3 ms |
1372 KB |
Output is correct |
10 |
Correct |
5 ms |
2120 KB |
Output is correct |
11 |
Correct |
5 ms |
2004 KB |
Output is correct |
12 |
Correct |
5 ms |
1884 KB |
Output is correct |
13 |
Correct |
3 ms |
1624 KB |
Output is correct |
14 |
Correct |
2 ms |
1372 KB |
Output is correct |
15 |
Correct |
3 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
766 ms |
90304 KB |
Output is correct |
2 |
Correct |
740 ms |
88492 KB |
Output is correct |
3 |
Correct |
711 ms |
88308 KB |
Output is correct |
4 |
Correct |
619 ms |
107392 KB |
Output is correct |
5 |
Correct |
577 ms |
100164 KB |
Output is correct |
6 |
Correct |
722 ms |
102720 KB |
Output is correct |
7 |
Correct |
119 ms |
50820 KB |
Output is correct |
8 |
Correct |
109 ms |
49608 KB |
Output is correct |
9 |
Correct |
116 ms |
49440 KB |
Output is correct |
10 |
Correct |
599 ms |
105608 KB |
Output is correct |
11 |
Correct |
619 ms |
104264 KB |
Output is correct |
12 |
Correct |
580 ms |
106520 KB |
Output is correct |
13 |
Correct |
110 ms |
49680 KB |
Output is correct |
14 |
Correct |
116 ms |
49864 KB |
Output is correct |
15 |
Correct |
114 ms |
50320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
677 ms |
101580 KB |
Output is correct |
2 |
Correct |
711 ms |
105608 KB |
Output is correct |
3 |
Correct |
712 ms |
106196 KB |
Output is correct |
4 |
Correct |
669 ms |
103228 KB |
Output is correct |
5 |
Correct |
696 ms |
103884 KB |
Output is correct |
6 |
Correct |
688 ms |
102728 KB |
Output is correct |
7 |
Correct |
682 ms |
102344 KB |
Output is correct |
8 |
Correct |
716 ms |
104552 KB |
Output is correct |
9 |
Correct |
698 ms |
102660 KB |
Output is correct |
10 |
Correct |
744 ms |
105780 KB |
Output is correct |
11 |
Correct |
707 ms |
103236 KB |
Output is correct |
12 |
Correct |
707 ms |
101612 KB |
Output is correct |
13 |
Correct |
720 ms |
104400 KB |
Output is correct |
14 |
Correct |
676 ms |
101524 KB |
Output is correct |
15 |
Correct |
721 ms |
103244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
739 ms |
89276 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
854 ms |
91720 KB |
Output is correct |
2 |
Correct |
877 ms |
94352 KB |
Output is correct |
3 |
Correct |
819 ms |
91776 KB |
Output is correct |
4 |
Correct |
727 ms |
103740 KB |
Output is correct |
5 |
Correct |
746 ms |
106308 KB |
Output is correct |
6 |
Correct |
743 ms |
107068 KB |
Output is correct |
7 |
Correct |
130 ms |
52452 KB |
Output is correct |
8 |
Correct |
122 ms |
53388 KB |
Output is correct |
9 |
Correct |
124 ms |
53548 KB |
Output is correct |
10 |
Correct |
680 ms |
102528 KB |
Output is correct |
11 |
Correct |
739 ms |
106868 KB |
Output is correct |
12 |
Correct |
770 ms |
106580 KB |
Output is correct |
13 |
Correct |
121 ms |
53452 KB |
Output is correct |
14 |
Correct |
123 ms |
52640 KB |
Output is correct |
15 |
Correct |
122 ms |
52684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1880 KB |
Output is correct |
2 |
Correct |
5 ms |
1884 KB |
Output is correct |
3 |
Correct |
5 ms |
1908 KB |
Output is correct |
4 |
Correct |
5 ms |
1880 KB |
Output is correct |
5 |
Correct |
5 ms |
1884 KB |
Output is correct |
6 |
Correct |
5 ms |
1884 KB |
Output is correct |
7 |
Correct |
3 ms |
1372 KB |
Output is correct |
8 |
Correct |
2 ms |
1372 KB |
Output is correct |
9 |
Correct |
3 ms |
1372 KB |
Output is correct |
10 |
Correct |
5 ms |
2120 KB |
Output is correct |
11 |
Correct |
5 ms |
2004 KB |
Output is correct |
12 |
Correct |
5 ms |
1884 KB |
Output is correct |
13 |
Correct |
3 ms |
1624 KB |
Output is correct |
14 |
Correct |
2 ms |
1372 KB |
Output is correct |
15 |
Correct |
3 ms |
1372 KB |
Output is correct |
16 |
Correct |
766 ms |
90304 KB |
Output is correct |
17 |
Correct |
740 ms |
88492 KB |
Output is correct |
18 |
Correct |
711 ms |
88308 KB |
Output is correct |
19 |
Correct |
619 ms |
107392 KB |
Output is correct |
20 |
Correct |
577 ms |
100164 KB |
Output is correct |
21 |
Correct |
722 ms |
102720 KB |
Output is correct |
22 |
Correct |
119 ms |
50820 KB |
Output is correct |
23 |
Correct |
109 ms |
49608 KB |
Output is correct |
24 |
Correct |
116 ms |
49440 KB |
Output is correct |
25 |
Correct |
599 ms |
105608 KB |
Output is correct |
26 |
Correct |
619 ms |
104264 KB |
Output is correct |
27 |
Correct |
580 ms |
106520 KB |
Output is correct |
28 |
Correct |
110 ms |
49680 KB |
Output is correct |
29 |
Correct |
116 ms |
49864 KB |
Output is correct |
30 |
Correct |
114 ms |
50320 KB |
Output is correct |
31 |
Correct |
677 ms |
101580 KB |
Output is correct |
32 |
Correct |
711 ms |
105608 KB |
Output is correct |
33 |
Correct |
712 ms |
106196 KB |
Output is correct |
34 |
Correct |
669 ms |
103228 KB |
Output is correct |
35 |
Correct |
696 ms |
103884 KB |
Output is correct |
36 |
Correct |
688 ms |
102728 KB |
Output is correct |
37 |
Correct |
682 ms |
102344 KB |
Output is correct |
38 |
Correct |
716 ms |
104552 KB |
Output is correct |
39 |
Correct |
698 ms |
102660 KB |
Output is correct |
40 |
Correct |
744 ms |
105780 KB |
Output is correct |
41 |
Correct |
707 ms |
103236 KB |
Output is correct |
42 |
Correct |
707 ms |
101612 KB |
Output is correct |
43 |
Correct |
720 ms |
104400 KB |
Output is correct |
44 |
Correct |
676 ms |
101524 KB |
Output is correct |
45 |
Correct |
721 ms |
103244 KB |
Output is correct |
46 |
Incorrect |
739 ms |
89276 KB |
Output isn't correct |
47 |
Halted |
0 ms |
0 KB |
- |