1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| #include <iostream> #include <cstdio> #define int long long
using namespace std;
const int N = 2e6 + 10;
int head[N], nxt[N], to[N], w[N], e_tot; int sign[N];
struct Node { int dfn, fa, dep, top, siz, son; }tr[N]; int tot;
inline int read() { int res = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -f; c = getchar(); } while ('0' <= c && c <= '9') { res = res * 10 + c - '0'; c = getchar(); } return res * f; }
void add(int x, int y, int z) { nxt[++e_tot] = head[x], head[x] = e_tot, to[e_tot] = y, w[e_tot] = z; }
void dfs1(int u, int _fa) { tr[u].fa = _fa; tr[u].dep = tr[_fa].dep + 1; tr[u].siz = 1; sign[u] = sign[_fa] * -1; for (int i = head[u]; i; i = nxt[i]) { int v = to[i]; dfs1(v, u); tr[u].siz += tr[v].siz; if (tr[v].siz > tr[tr[u].son].siz) tr[u].son = v; } }
void dfs2(int u, int _top) { tr[u].dfn = ++tot; tr[u].top = _top; if (tr[u].son) dfs2(tr[u].son, _top); for (int i = head[u]; i; i = nxt[i]) { int v = to[i]; if (v == tr[u].son) continue; dfs2(v, v); } }
int n, q; int o[N]; int val[N];
int lowbit(int x) { return x & (-x); }
void change(int x, int y, int v) { for (int i = x; i <= n; i += lowbit(i)) { val[i] += v; } for (int i = y + 1; i <= n; i += lowbit(i)) { val[i] -= v; } }
int query(int x) { int res = 0; for (int i = x; i; i -= lowbit(i)) res += val[i]; return res; }
void Change(int x, int v) { change(tr[x].dfn, tr[x].dfn + tr[x].siz - 1, v); }
int Query(int x) { return query(tr[x].dfn); }
int fa[N];
signed main() { n = read(), q = read(); for (int i = 2; i <= n; ++i) { int f, w; f = read(), w = read(); add(f, i, w); fa[i] = f; o[i] = w; } sign[0] = -1; dfs1(1, 0); dfs2(1, 1); for (int i = 2; i <= n; ++i) { Change(i, o[i] * sign[i]); } for (int i = 1; i <= q; ++i) { int op, u, v, s, w; op = read(); if (op == 1) { u = read(), v = read(), s = read(); int w1 = Query(u) * sign[u], w2 = Query(v) * sign[v]; if (sign[v] != sign[u]) { if (w1 + w2 == s) { puts("inf"); continue; } else { puts("none"); continue; } } if (w1 + w2 - s & 1) { puts("none"); continue; } printf("%lld\n", (s - w1 - w2) / (sign[u] + sign[v])); } else { u = read(), w = read(); Change(u, (w - o[u]) * sign[u]); o[u] = w; } } return 0; }
|