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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
| #include <bits/stdc++.h>
using i64 = long long;
#define debug(a) std::cout << #a << '=' << (a) << ' '
template <class T> inline void chmin(T &x, const T &y) { if (x > y) { x = y; } } template <class T> inline void chmax(T &x, const T &y) { if (x < y) { x = y; } }
const int N = 50010, MaxM = 100100; const int sup = 1e9;
const int B = 2000; const int Blen = B + 10;
int n, m; int lstn;
std::vector<std::vector<int>> G;
struct Info { i64 c, s; Info() {} Info(i64 _c, i64 _s) : c(_c), s(_s) {} friend Info operator + (const Info &lhs, const Info &rhs) { return Info(lhs.c + rhs.c, lhs.s + rhs.s); } friend Info operator - (const Info &lhs, const Info &rhs) { return Info(lhs.c - rhs.c, lhs.s - rhs.s); } Info &operator += (const Info &rhs) { return (*this) = (*this) + rhs; } };
int root[N]; namespace SGT { const int pond = 10001000;
int nodeCount; struct node { int lc, rc; Info info; } t[pond];
void init() { nodeCount = 0; }
void insert(int &p, int q, int l, int r, int x, Info y) { p = ++ nodeCount, t[p] = t[q], t[p].info += y; if (l == r) return; int mid = (l + r) >> 1; if (x <= mid) { insert(t[p].lc, t[q].lc, l, mid, x, y); } else { insert(t[p].rc, t[q].rc, mid + 1, r, x, y); } }
Info ask(int p, int l, int r, int x) { if (l == r) return t[p].info; int mid = (l + r) >> 1; if (x <= mid) { return ask(t[p].lc, l, mid, x); } else { return t[t[p].lc].info + ask(t[p].rc, mid + 1, r, x); } } }
int dep[N]; int anc[18][N];
void dfs_start(int u, int fu) { dep[u] = dep[fu] + 1; anc[0][u] = fu; for (int i = 1; i <= 17; i ++) { anc[i][u] = anc[i - 1][anc[i - 1][u]]; }
for (int v : G[u]) { if (v == fu) { continue; } dfs_start(v, u); } }
int lca(int x, int y) { if (dep[x] > dep[y]) std::swap(x, y); for (int i = 17; i >= 0; i --) if (dep[x] <= dep[y] - (1 << i)) y = anc[i][y]; if (x == y) return x; for (int i = 17; i >= 0; i --) if (anc[i][x] ^ anc[i][y]) x = anc[i][x], y = anc[i][y]; return anc[0][x]; }
int dfsClock, dfn[N], idx[N]; int sze[N];
void dfs_init(int u, int fu) { dfsClock ++; dfn[u] = dfsClock, idx[dfsClock] = u;
sze[u] = 1; for (int v : G[u]) { if (v == fu) { continue; } dfs_init(v, u); sze[u] += sze[v]; } }
int fa[N]; int inc[Blen][Blen];
int contain(int x, int y) { if (x <= lstn) { y = fa[y]; return dfn[x] <= dfn[y] && dfn[y] <= dfn[x] + sze[x] - 1; } else { if (y <= lstn) { return 0; } else { return inc[y - lstn][x - lstn]; } } }
std::vector<std::array<int, 3>> lstop[N];
std::vector<std::array<int, 5>> curop;
void rebuild(int del = 1) { if (del) { for (int i = lstn + 1; i <= n; i ++) { int x = i; while (x > lstn) { inc[i - lstn][x - lstn] = 0; x = anc[0][x]; } } }
for (auto [x, y, z, c, k] : curop) { lstop[x].push_back({c, k, +1}); lstop[y].push_back({c, k, +1}); lstop[z].push_back({c, k, -1}); if (anc[0][z]) lstop[anc[0][z]].push_back({c, k, -1}); } std::vector<std::array<int, 5>>().swap(curop);
lstn = n; for (int i = 1; i <= n; i ++) { fa[i] = i; }
dfsClock = 0; dfs_init(1, 0);
SGT::init(); for (int i = 1; i <= n; i ++) { root[i] = root[i - 1];
int u = idx[i]; for (auto [c, k, opt] : lstop[u]) { Info info(opt * k, 1ll * opt * k * dep[u]); SGT::insert(root[i], root[i], 1, sup, c, info); } } }
i64 ask(int u, int c) { i64 ans = 0; if (u <= lstn) { int l = dfn[u], r = dfn[u] + sze[u] - 1; Info info = SGT::ask(root[r], 1, sup, c) - SGT::ask(root[l - 1], 1, sup, c); ans += info.s - info.c * (dep[u] - 1); }
for (auto [x, y, z, qc, k] : curop) { if (qc > c) { continue; } if (contain(u, z)) { ans += 1ll * (dep[x] + dep[y] - dep[z] * 2 + 1) * k; } else { if (contain(u, x)) { ans += 1ll * (dep[x] - dep[u] + 1) * k; } if (contain(u, y)) { ans += 1ll * (dep[y] - dep[u] + 1) * k; } } } return ans; }
const i64 mod = 1LL << 31;
int main() { std::ios::sync_with_stdio(0); std::cin.tie(0);
std::cin >> n >> m;
G.assign(n + m + 1, {}); for (int i = 1; i < n; i ++) { int x, y; std::cin >> x >> y;
G[x].push_back(y), G[y].push_back(x); }
dfs_start(1, 0);
rebuild(0);
int lastans = 0; for (int qid = 1; qid <= m; qid ++) { int opt, u, v, c, k; std::cin >> opt >> u; u ^= lastans;
if (opt == 1) { int p = ++ n; G[u].push_back(p);
fa[p] = fa[u]; dep[p] = dep[u] + 1; anc[0][p] = u; for (int i = 1; i <= 17; i ++) { anc[i][p] = anc[i - 1][anc[i - 1][p]]; }
int x = p; while (x > lstn) { inc[p - lstn][x - lstn] = 1; x = anc[0][x]; } } else if (opt == 2) { std::cin >> v >> c >> k; v ^= lastans, c ^= lastans, k ^= lastans;
curop.push_back({u, v, lca(u, v), c, k}); } else { std::cin >> c; c ^= lastans;
i64 ans = ask(u, c); std::cout << ans << '\n';
lastans = ans % mod; }
if (qid % B == 0) { rebuild(); } }
return 0; }
|