动态树(LCT)
建议先做 模板题
本题就是个板子,唯一需要注意的地方就是懒标记,同时要维护乘法与加法的懒标记,如果不会维护双标记的话请先移步 线段树2
还有就是数据范围的问题, $ 51061^2 > 2^{31} $ , 所以需要开 $ unsigned $ $ int $ 或 $ long $ $ long $
直接放代码吧
#include <bits/stdc++.h>
#define int long long
#define N (int)2e5
#define MOD 51061
using namespace std;
struct lct{
int fa, son[2], val, size;
int add, mul = 1, sum, r;
}a[N];
int n, q;
int st[N], top;
bool isroot(int x){
return !(a[a[x].fa].son[0] == x || a[a[x].fa].son[1] == x);
}
void update(int x){
a[x].sum = (a[a[x].son[0]].sum + a[a[x].son[1]].sum + a[x].val) % MOD;
a[x].size = (a[a[x].son[0]].size + a[a[x].son[1]].size + 1) % MOD;
}
void pushr(int x){
swap(a[x].son[0], a[x].son[1]);
a[x].r ^= 1;
}
void pushadd(int x, int val){
a[x].sum += a[x].size * val, a[x].add += val, a[x].val += val;
a[x].sum %= MOD, a[x].add %= MOD, a[x].val %= MOD;
}
void pushmul(int x, int val){
a[x].sum *= val, a[x].mul *= val, a[x].val *= val, a[x].add *= val;
a[x].sum %= MOD, a[x].mul %= MOD, a[x].val %= MOD, a[x].add %= MOD;
}
void pushdown(int x){
int lc = a[x].son[0], rc = a[x].son[1];
if(a[x].mul != 1) pushmul(lc, a[x].mul), pushmul(rc, a[x].mul), a[x].mul = 1;
if(a[x].add) pushadd(lc, a[x].add), pushadd(rc, a[x].add), a[x].add = 0;
if(a[x].r) {
if(lc) pushr(lc);
if(rc) pushr(rc);
a[x].r = 0;
}
}
void rotate(int x){
int y = a[x].fa, z = a[y].fa, k = a[y].son[1] == x;
if(!isroot(y)) a[z].son[a[z].son[1] == y] = x;
a[x].fa = z;
a[y].son[k] = a[x].son[k ^ 1], a[a[x].son[k ^ 1]].fa = y;
a[x].son[k ^ 1] = y, a[y].fa = x;
update(y), update(x);
}
void splay(int x){
int t = x;
st[++top] = t;
while(!isroot(t)) st[++top] = a[t].fa, t = a[t].fa;
while(top) pushdown(st[top--]);
while(!isroot(x)){
int y = a[x].fa, z = a[y].fa;
if(!isroot(y)) rotate((a[z].son[1] == y) ^ (a[y].son[1] == x) ? x : y);
rotate(x);
}
update(x);
}
void access(int x){
for(int son = 0; x; x = a[son = x].fa){
splay(x), a[x].son[1] = son;
update(x);
}
}
void makeroot(int x){
access(x), splay(x), pushr(x);
}
int findroot(int x){
access(x), splay(x);
while(a[x].son[0]) pushdown(x), x = a[x].son[0];
splay(x);
return x;
}
void split(int x, int y){
makeroot(x);
access(y);
splay(y);
}
void link(int x, int y){
makeroot(x);
if(findroot(y) != x) a[x].fa = y;
}
void cut(int x, int y){
makeroot(x);
if(findroot(y) == x && a[y].fa == x && !a[y].son[0]){
a[y].fa = 0, a[x].son[1] = 0;
update(x);
}
}
void add(int x, int y, int val){
split(x, y);
pushadd(y, val % MOD);
}
void mul(int x, int y, int val){
split(x, y);
pushmul(y, val % MOD);
}
signed main(){
scanf("%lld%lld", &n, &q);
for(int i = 1; i <= n; ++i) a[i].val = 1, a[i].mul = 1, a[i].size = 1;
for(int i = 1; i < n; ++i){
int x, y;
scanf("%lld%lld", &x, &y);
link(x, y);
}
while(q--){
char ch[2];
int x, y, val, z, w;
scanf("%s%lld%lld", ch, &x, &y);
char c = ch[0];
if(c == '+') {
scanf("%lld", &val);
add(x, y, val);
}
if(c == '-'){
scanf("%lld%lld", &z, &w);
cut(x, y), link(z, w);
}
if(c == '*'){
scanf("%lld", &val);
mul(x, y, val);
}
if(c == '/'){
split(x, y);
printf("%lld\n", a[y].sum % MOD);
}
}
return 0;
}