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
| #include <cstdio> #include <algorithm> using namespace std; typedef long long LL; const int N = 1e5 + 10, mod = 51061; struct Node { int sz, s[2], p; bool rev; LL mul, add; LL v, sum; } tr[N]; int n, q; void pushup(int x) { tr[x].sum = (tr[tr[x].s[0]].sum + tr[x].v + tr[tr[x].s[1]].sum) % mod; tr[x].sz = tr[tr[x].s[0]].sz + 1 + tr[tr[x].s[1]].sz; } void reverse(int x) { tr[x].rev ^= 1; swap(tr[x].s[0], tr[x].s[1]); } void cal(Node &x, LL mul, LL add) { x.mul = x.mul * mul % mod; x.add = (x.add * mul % mod + add) % mod; x.v = (x.v * mul % mod + add) % mod; x.sum = (x.sum * mul % mod + x.sz * add % mod) % mod; } void pushdown(int x) { if (tr[x].rev) { reverse(tr[x].s[0]); reverse(tr[x].s[1]); tr[x].rev = false; } cal(tr[tr[x].s[0]], tr[x].mul, tr[x].add); cal(tr[tr[x].s[1]], tr[x].mul, tr[x].add); tr[x].mul = 1; tr[x].add = 0; } bool isroot(int x) { return tr[tr[x].p].s[0] != x && tr[tr[x].p].s[1] != x; } void rotate(int x) { int y = tr[x].p, z = tr[y].p; int k = tr[y].s[1] == x; if (!isroot(y)) tr[z].s[tr[z].s[1] == y] = x; tr[x].p = z; tr[y].s[k] = tr[x].s[k ^ 1]; tr[tr[x].s[k ^ 1]].p = y; tr[x].s[k ^ 1] = y; tr[y].p = x; pushup(y); pushup(x); } void update(int x) { if (!isroot(x)) update(tr[x].p); pushdown(x); } void splay(int x) { update(x); while (!isroot(x)) { int y = tr[x].p, z = tr[y].p; if (!isroot(y)) if ((y == tr[z].s[1]) ^ (x == tr[y].s[1])) rotate(x); else rotate(y); rotate(x); } } void access(int x) { int tmp = x; for (int y = 0; x; y = x, x = tr[x].p) { splay(x); tr[x].s[1] = y; pushup(x); } splay(tmp); } void mkroot(int x) { access(x); reverse(x); } int findroot(int x) { access(x); while (tr[x].s[0]) { pushdown(x); x = tr[x].s[0]; } splay(x); return x; } void split(int x, int y) { mkroot(x); access(y); } void link(int x, int y) { mkroot(x); if (findroot(y) != x) tr[x].p = y; } void cut(int x, int y) { mkroot(x); if (findroot(y) == x && tr[y].p == x && !tr[y].s[0]) { tr[x].s[1] = tr[y].p = 0; pushup(x); } } int main() { scanf("%d%d", &n, &q); for (int i = 1; i <= n; ++i) { tr[i].mul = 1; tr[i].v = 1; } for (int i = 1, a, b; i < n; i++) { scanf("%d%d", &a, &b); link(a, b); } for (char op[2]; q; q--) { scanf("%s", op); if (op[0] == '+') { int u, v, c; scanf("%d%d%d", &u, &v, &c); split(u, v); cal(tr[v], 1, c); } else if (op[0] == '-') { int u1, v1, u2, v2; scanf("%d%d%d%d", &u1, &v1, &u2, &v2); cut(u1, v1); link(u2, v2); } else if (op[0] == '*') { int u, v, c; scanf("%d%d%d", &u, &v, &c); split(u, v); cal(tr[v], c, 0); } else if (op[0] == '/') { int u, v; scanf("%d%d", &u, &v); split(u, v); printf("%lld\n", tr[v].sum); } } return 0; }
|