题面
Sol
这不是一道LCT模板题吗?
和线段树一样维护区间加法和乘法标记
记得要更新自己本身的权值
这种题就该一遍AC
# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
# define Sqr(x) ((x) * (x))
# define ls ch[0][x]
# define rs ch[1][x]
using namespace std;
typedef long long ll;
const int _(1e5 + 10), Zsy(51061);
IL ll Read(){
RG ll x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
}
int n, q, val[_], size[_], fa[_], ch[2][_], S[_], top, rev[_], sum[_], adj[_], adc[_];
IL bool Son(RG int x){ return ch[1][fa[x]] == x; }
IL bool Isroot(RG int x){ return ch[0][fa[x]] != x && ch[1][fa[x]] != x; }
IL void Reverse(RG int x){ if(!x) return; swap(ls, rs); rev[x] ^= 1; }
IL void Adjust_j(RG int x, RG int d){
if(!x) return;
adj[x] += d; val[x] = (val[x] + d) % Zsy;
sum[x] = (sum[x] + 1LL * size[x] * d % Zsy) % Zsy;
}
IL void Adjust_c(RG int x, RG int dc, RG int dj){
if(!x) return;
adc[x] = 1LL * adc[x] * dc % Zsy; adj[x] = (1LL * adj[x] * dc % Zsy + dj) % Zsy;
sum[x] = (1LL * sum[x] * dc % Zsy + 1LL * size[x] * dj % Zsy) % Zsy;
val[x] = (1LL * val[x] * dc % Zsy + dj % Zsy) % Zsy;
}
IL void Pushdown(RG int x){
if(rev[x]) Reverse(ls), Reverse(rs), rev[x] ^= 1;
if(adc[x] != 1) Adjust_c(ls, adc[x], adj[x]), Adjust_c(rs, adc[x], adj[x]), adc[x] = 1, adj[x] = 0;
else if(adj[x]) Adjust_j(ls, adj[x]), Adjust_j(rs, adj[x]), adj[x] = 0;
}
IL void Update(RG int x){ size[x] = size[ls] + size[rs] + 1; sum[x] = (val[x] + sum[ls] + sum[rs]) % Zsy; }
IL void Rotate(RG int x){
RG int y = fa[x], z = fa[y], c = Son(x);
if(!Isroot(y)) ch[Son(y)][z] = x; fa[x] = z;
ch[c][y] = ch[!c][x]; fa[ch[c][y]] = y;
ch[!c][x] = y; fa[y] = x; Update(y);
}
IL void Splay(RG int x){
S[top = 1] = x;
for(RG int y = x; !Isroot(y); y = fa[y]) S[++top] = fa[y];
while(top) Pushdown(S[top--]);
for(RG int y = fa[x]; !Isroot(x); Rotate(x), y = fa[x])
if(!Isroot(y)) Son(x) ^ Son(y) ? Rotate(x) : Rotate(y);
Update(x);
}
IL void Access(RG int x){ for(RG int y = 0; x; y = x, x = fa[x]) Splay(x), ch[1][x] = y, Update(x); }
IL int Findroot(RG int x){ Access(x); Splay(x); while(ch[0][x]) x = ch[0][x]; return x; }
IL void Makeroot(RG int x){ Access(x); Splay(x); Reverse(x); }
IL void Split(RG int x, RG int y){ Makeroot(x); Access(y); Splay(y); }
IL void Link(RG int x, RG int y){ Makeroot(x); fa[x] = y; }
IL void Cut(RG int x, RG int y){ Split(x, y); fa[x] = ch[0][y] = 0; Update(y); }
int main(RG int argc, RG char* argv[]){
n = Read(); q = Read();
for(RG int i = 1; i <= n; ++i) val[i] = sum[i] = size[i] = adc[i] = 1;
for(RG int i = 1, x, y; i < n; ++i) x = Read(), y = Read(), Link(x, y);
while(q--){
RG char op; scanf(" %c", &op); RG int x = Read(), y = Read(), a, b;
if(op == '+'){
a = Read(); Split(x, y);
adj[y] = (adj[y] + a) % Zsy; val[y] = (val[y] + a) % Zsy;
sum[y] = (sum[y] + 1LL * size[y] * a % Zsy) % Zsy;
}
else if(op == '-') a = Read(), b = Read(), Cut(x, y), Link(a, b);
else if(op == '*'){
a = Read(); Split(x, y);
adc[y] = 1LL * a * adc[y] % Zsy; adj[y] = 1LL * a * adj[y] % Zsy;
val[y] = 1LL * a * val[y] % Zsy; sum[y] = 1LL * a * sum[y] % Zsy;
}
else Split(x, y), printf("%d\n", sum[y]);
}
return 0;
}