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
|
#include <bits/stdc++.h> using namespace std; #define int long long #define resetIO(x) \ freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout) #define debug(fmt, ...) \ fprintf(stderr, "[%s:%d] " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__) template <class _Tp> inline _Tp& read(_Tp& x) { bool sign = false; char ch = getchar(); long double tmp = 1; for (; !isdigit(ch); ch = getchar()) sign |= (ch == '-'); for (x = 0; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ 48); if (ch == '.') for (ch = getchar(); isdigit(ch); ch = getchar()) tmp /= 10.0, x += tmp * (ch ^ 48); return sign ? (x = -x) : x; } template <class _Tp> inline void write(_Tp x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar((x % 10) ^ 48); } const int MAXN = 1e5 + 10; const int INF = 0x3f3f3f3f3f3f3f3f; const int MOD = 51061; struct LCT { int fa[MAXN], ch[MAXN][2]; int val[MAXN], sum[MAXN], siz[MAXN]; int tag[MAXN], add[MAXN], mul[MAXN]; void connect(int x, int f, int w) { fa[x] = f, (~w) && (ch[f][w] = x); } void clear(int x) { val[x] = sum[x] = siz[x] = mul[x] = 1; tag[x] = add[x] = 0; } void pushup(int x) { siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + 1; sum[x] = (sum[ch[x][0]] + sum[ch[x][1]] + val[x]) % MOD; } int get(int x) { if (ch[fa[x]][0] == x) return 0; if (ch[fa[x]][1] == x) return 1; return -1; } void getdown(int x, int vm, int va) { mul[x] = mul[x] * vm % MOD; add[x] = (add[x] * vm + va) % MOD; val[x] = (val[x] * vm + va) % MOD; sum[x] = (sum[x] * vm + va * siz[x]) % MOD; } void pushdown(int x) { if (mul[x] != 1 || add[x] != 0 || tag[x]) { if (tag[x]) swap(ch[x][0], ch[x][1]); if (ch[x][0]) getdown(ch[x][0], mul[x], add[x]), tag[ch[x][0]] ^= tag[x]; if (ch[x][1]) getdown(ch[x][1], mul[x], add[x]), tag[ch[x][1]] ^= tag[x]; mul[x] = 1, add[x] = tag[x] = 0; } } void pushall(int x) { if (~get(x)) pushall(fa[x]); pushdown(x); } void rotate(int x) { int y = fa[x], z = fa[y], w = get(x); if (~w) connect(ch[x][w ^ 1], y, w); connect(x, z, get(y)); connect(y, x, w ^ 1); pushup(y); pushup(x); } void splay(int x) { pushall(x); for (int f = fa[x]; f = fa[x], ~get(x); rotate(x)) if (~get(f)) rotate(get(f) == get(x) ? f : x); } void access(int x) { int pre = 0; while (x) { splay(x); ch[x][1] = pre; pushup(x); pre = x; x = fa[x]; } } void makeroot(int x) { access(x); splay(x); tag[x] ^= 1; } int findroot(int x) { access(x); splay(x); while (ch[x][0]) pushdown(x), x = ch[x][0]; splay(x); return x; } void split(int x, int y) { makeroot(x); access(y); splay(y); } bool link(int x, int y) { makeroot(x); if (findroot(y) == x) return false; fa[x] = y; return true; } bool cut(int x, int y) { if (findroot(x) != findroot(y)) return false; split(x, y); if (fa[x] != y || ch[x][1]) return false; fa[x] = ch[y][0] = 0; return true; } }; int n, q; LCT lct; signed main() { read(n), read(q); for (int i = 1; i <= n; ++i) lct.clear(i); for (int i = 1; i < n; ++i) { int u, v; read(u), read(v); lct.link(u, v); } while (q--) { char op = getchar(); if (op == '+') { int u, v, c; read(u), read(v), read(c); lct.split(u, v); lct.getdown(v, 1, c); } else if (op == '-') { int u1, v1, u2, v2; read(u1), read(v1), read(u2), read(v2); lct.cut(u1, v1); lct.link(u2, v2); } else if (op == '*') { int u, v, c; read(u), read(v), read(c); lct.split(u, v); lct.getdown(v, c, 0); } else { int u, v; read(u), read(v); lct.split(u, v); write(lct.sum[v]), putchar('\n'); } } return 0; }
|