Description
一棵n个点的树,每个点的初始权值为1。对于这棵树有q个操作,每个操作为以下四种操作之一:
+ u v c:将u到v的路径上的点的权值都加上自然数c;
- u1 v1 u2 v2:将树中原有的边(u1,v1)删除,加入一条新边(u2,v2),保证操作完之后仍然是一棵树;
* u v c:将u到v的路径上的点的权值都乘上自然数c;
/ u v:询问u到v的路径上的点的权值和,求出答案对于51061的余数。
Input
第一行两个整数n,q
接下来n-1行每行两个正整数u,v,描述这棵树
接下来q行,每行描述一个操作
Output
对于每个/对应的答案输出一行
Sample Input
3 2
1 2
2 3
* 1 3 4
/ 1 1
Sample Output
4
Hint
10%的数据保证,1<=n,q<=2000
另外15%的数据保证,1<=n,q<=5*10^4,没有-操作,并且初始树为一条链
另外35%的数据保证,1<=n,q<=5*10^4,没有-操作
100%的数据保证,1<=n,q<=10^5,0<=c<=10^4
题解
比较简单,用来练习 $lct$ 上的 $lazy$ 操作。
1 //It is made by Awson on 2018.1.16 2 #include <set> 3 #include <map> 4 #include <cmath> 5 #include <ctime> 6 #include <queue> 7 #include <stack> 8 #include <cstdio> 9 #include <string> 10 #include <vector> 11 #include <cstdlib> 12 #include <cstring> 13 #include <iostream> 14 #include <algorithm> 15 #define LL long long 16 #define Abs(a) ((a) < 0 ? (-(a)) : (a)) 17 #define Max(a, b) ((a) > (b) ? (a) : (b)) 18 #define Min(a, b) ((a) < (b) ? (a) : (b)) 19 #define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b)) 20 using namespace std; 21 const int MOD = 51061; 22 const int N = 1e5; 23 void read(int &x) { 24 char ch; bool flag = 0; 25 for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar()); 26 for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar()); 27 x *= 1-2*flag; 28 } 29 void write(int x) { 30 if (x > 9) write(x/10); 31 putchar(x%10+48); 32 } 33 34 char ch[10]; 35 int n, q, u, v, c; 36 struct Link_Cut_Tree { 37 int ch[N+5][2], pre[N+5], rev[N+5], sum[N+5], prod[N+5], val[N+5], tol[N+5], isrt[N+5], size[N+5]; 38 Link_Cut_Tree() {for (int i = 1; i <= N; i++) val[i] = tol[i] = isrt[i] = prod[i] = size[i] = 1; } 39 void pushup(int o) {tol[o] = (tol[ch[o][0]]+tol[ch[o][1]]+val[o])%MOD, size[o] = (size[ch[o][0]]+size[ch[o][1]]+1)%MOD; } 40 void pushdown(int o) { 41 int ls = ch[o][0], rs = ch[o][1]; 42 if (rev[o]) { 43 Swap(ch[ls][0], ch[ls][1]), Swap(ch[rs][0], ch[rs][1]); 44 rev[ls] ^= 1, rev[rs] ^= 1, rev[o] = 0; 45 } 46 if (prod[o] != 1) { 47 prod[ls] = (LL)prod[ls]*prod[o]%MOD, prod[rs] = (LL)prod[rs]*prod[o]%MOD; 48 sum[ls] = (LL)sum[ls]*prod[o]%MOD, sum[rs] = (LL)sum[rs]*prod[o]%MOD; 49 val[ls] = (LL)val[ls]*prod[o]%MOD, val[rs] = (LL)val[rs]*prod[o]%MOD; 50 tol[ls] = (LL)tol[ls]*prod[o]%MOD, tol[rs] = (LL)tol[rs]*prod[o]%MOD; 51 prod[o] = 1; 52 } 53 if (sum[o]) { 54 sum[ls] = (sum[ls]+sum[o])%MOD, sum[rs] = (sum[rs]+sum[o])%MOD; 55 val[ls] = (val[ls]+sum[o])%MOD, val[rs] = (val[rs]+sum[o])%MOD; 56 tol[ls] = (tol[ls]+(LL)sum[o]*size[ls]%MOD)%MOD, tol[rs] = (tol[rs]+(LL)sum[o]*size[rs]%MOD)%MOD; 57 sum[o] = 0; 58 } 59 } 60 void push(int o) { 61 if (!isrt[o]) push(pre[o]); 62 pushdown(o); 63 } 64 void rotate(int o, int kind) { 65 int p = pre[o]; 66 ch[p][!kind] = ch[o][kind], pre[ch[o][kind]] = p; 67 if (isrt[p]) isrt[o] = 1, isrt[p] = 0; 68 else ch[pre[p]][ch[pre[p]][1] == p] = o; 69 pre[o] = pre[p]; 70 ch[o][kind] = p, pre[p] = o; 71 pushup(p), pushup(o); 72 } 73 void splay(int o) { 74 push(o); 75 while (!isrt[o]) { 76 if (isrt[pre[o]]) rotate(o, ch[pre[o]][0] == o); 77 else { 78 int p = pre[o], kind = ch[pre[p]][0] == p; 79 if (ch[p][kind] == o) rotate(o, !kind), rotate(o, kind); 80 else rotate(p, kind), rotate(o, kind); 81 } 82 } 83 } 84 void access(int o) { 85 int y = 0; 86 while (o) { 87 splay(o); 88 isrt[ch[o][1]] = 1, isrt[ch[o][1] = y] = 0; 89 pushup(o); o = pre[y = o]; 90 } 91 } 92 void makeroot(int o) {access(o), splay(o); rev[o] ^= 1, Swap(ch[o][0], ch[o][1]); } 93 void link(int x, int y) {makeroot(x); pre[x] = y; } 94 void cut(int x, int y) {makeroot(x), access(y), splay(y); ch[y][0] = pre[x] = 0, isrt[x] = 1; pushup(y); } 95 void split(int x, int y) {makeroot(x), access(y), splay(y); } 96 void add(int x, int y, int c) {split(x, y); sum[y] = (sum[y]+c)%MOD, val[y] = (val[y]+c)%MOD, tol[y] = (tol[y]+(LL)c*size[y]%MOD)%MOD; } 97 void plus(int x, int y, int c) {split(x, y); prod[y] = (LL)prod[y]*c%MOD, sum[y] = (LL)sum[y]*c%MOD, val[y] = (LL)val[y]*c%MOD, tol[y] = (LL)tol[y]*c%MOD; } 98 int query(int x, int y) {split(x, y); return tol[y]; } 99 }T; 100 101 void work() { 102 read(n), read(q); 103 for (int i = 1; i < n; i++) { 104 read(u), read(v); T.link(u, v); 105 } 106 while (q--) { 107 scanf("%s", ch); 108 if (ch[0] == '+') {read(u), read(v), read(c); T.add(u, v, c); } 109 else if (ch[0] == '-') { 110 read(u), read(v); T.cut(u, v); 111 read(u), read(v); T.link(u, v); 112 } 113 else if (ch[0] == '*') {read(u), read(v), read(c); T.plus(u, v, c); } 114 else if (ch[0] == '/') {read(u), read(v); write(T.query(u, v)), putchar('\n'); } 115 } 116 } 117 int main() { 118 work(); 119 return 0; 120 }