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
| #include <cstdio>
#include <iostream>
using namespace std;
const int N=100005;
int getint() {
int r = 0; char c = getchar();
for (; '0' > c || c > '9'; c = getchar());
for(;'0'<=c&&c<='9';c=getchar())r=r*10-'0'+c;
return r;
}
char getop() {
char c = getchar();
for(;c<42||c>47;c=getchar());
return c;
}
typedef unsigned LL;
const LL mod = 51061;
int c[N][2],fa[N],rev[N],st[N];
LL mul[N],plu[N],tree[N],sum[N],siz[N];
bool isroot(int x) {
return !(c[fa[x]][0]==x||c[fa[x]][1]==x);
}
void pd(int x) {
int l=c[x][0], r=c[x][1];
if (rev[x]){
rev[l]^=1;
rev[r]^=1;
rev[x]=0;
swap(c[x][0],c[x][1]);
}
if (mul[x]!=1||plu[x]!=0){
sum[l]=(sum[l]*mul[x]+siz[l]*plu[x])%mod;
mul[l]=(mul[l]*mul[x])%mod;
plu[l]=(plu[l]*mul[x]+plu[x])%mod;
sum[r]=(sum[r]*mul[x]+siz[r]*plu[x])%mod;
mul[r]=(mul[r]*mul[x])%mod;
plu[r]=(plu[r]*mul[x]+plu[x])%mod;
tree[x]=(tree[x]*mul[x]+plu[x])%mod;
mul[x]=1;plu[x]=0;
}
}
void pu(int x) {
int l=c[x][0],r=c[x][1];
siz[x]=siz[l]+siz[r]+1;
sum[x]=(sum[l]+sum[r]+tree[x])%mod;
}
void rotate(int x){
int y=fa[x], z=fa[y], a=(c[fa[x]][1]==x), b=!a;
if(!isroot(y)) c[z][c[z][1]==y]=x;
fa[x]=z;fa[y]=x;fa[c[x][b]]=y;
c[y][a]=c[x][b];c[x][b]=y;
pu(y);pu(x);
}
void splay(int x) {
int t=1;st[1]=x;
for(int i=x;!isroot(i);i=fa[i]) st[++t]=fa[i];
while(t) pd(st[t--]);
while(!isroot(x)){
int y=fa[x],z=fa[y];
if(!isroot(y)){
if(c[y][0]==x^c[z][0]==y)rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x){
int y = 0;
while(x){
splay(x);
c[x][1]=y;
pu(x);
y=x;
x=fa[x];
}
}
void mkrt(int x){ access(x);splay(x);rev[x]^=1; }
void cut(int x, int y) {
mkrt(x);
access(y);
splay(y);
c[y][0]=0;
fa[x]=0;
}
void link(int x, int y) {
mkrt(x);
fa[x]=y;
splay(x);
}
int n, q;
void chanp(int x, int y, LL pluval) {
mkrt(x);
access(y);
splay(y);
sum[y]=(sum[y]+siz[y]*pluval)%mod;
mul[y]=(mul[y])%mod;
plu[y]=(plu[y]+pluval)%mod;
}
void chanm(int x, int y, LL mulval) {
mkrt(x);
access(y);
splay(y);
sum[y]=(sum[y]*mulval)%mod;
mul[y]=(mul[y]*mulval)%mod;
plu[y]=(plu[y]*mulval)%mod;
}
LL query(int x, int y) {
mkrt(x);
access(y);
splay(y);
return sum[y]%mod;
}
int main() {
n=getint();q=getint();
for (int i = 1; i <= n; ++i) tree[i]=siz[i]=sum[i]=mul[i]=1,plu[i]=0;
int x,y,z,zz;
char o;
for(int i=1;i<n;++i){
x=getint();
y=getint();
link(x,y);
}
while (q--){
o=getop();
if(o=='+'){
x=getint();y=getint();z=getint();
chanp(x,y,z);
}
if(o=='-'){
x=getint();y=getint();z=getint();zz=getint();
cut(x,y);link(z,zz);
}
if(o=='*'){
x=getint();y=getint();z=getint();
chanm(x,y,z);
}
if(o=='/'){
x=getint();y=getint();
printf("%u\n", query(x,y));
}
}
}
|