# 对着题解改了很久，还是错了，求助

@JK_LOVER 2020-01-15 16:35 回复
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = (int)1e5+10;
bool r[N];
#define mod 51061
#define lc c[x][0]
#define rc c[x][1]
void add(int &x,int y){x = (x+y)%mod;}
void mul(int &x,int y){x = (x*y)%mod;}
bool isroot(int x){return c[f[x]][0] == x||c[f[x]][1] == x;}
void pushup(int x){
s[x] = (v[x]+s[lc]+s[rc])%mod;
sz[x] = sz[lc]+sz[rc]+1;
}
void pushr(int x){swap(lc,rc);r[x]^=1;}
void pushdown(int x)
{
if(mu[x]!=1)pushm(lc,mu[x]),pushm(rc,mu[x]),mu[x]=1;
if(r[x])
{
if(lc)pushr(lc);
if(rc)pushr(rc);
r[x] = 0;
}
}
void rotate(int x)
{
int y = f[x],z = f[y],k = c[y][1]==x,w = c[x][!k];
if(isroot(y))c[z][c[z][1]==1] = x;
c[x][!k] = y;
c[y][k] = w;
f[w] = y;
f[y] = x;
f[x] = z;
pushup(y);
}
void splay(int x)
{
int y = x,z = 0;
st[++z] = y;
while(isroot(y))st[++z] = y = f[y];
while(z)pushdown(st[z--]);
while(isroot(x))
{
y = f[x];z = f[y];
if(isroot(y))
{
rotate(((c[y][1]==x)^(c[z][1]==y))?x:y);
}
rotate(x);
}
pushup(x);
}
void access(int x){
for(int y = 0;x;x = f[y=x])
{
splay(x);
rc = y;
pushup(x);
}
}
void makeroot(int x)
{
access(x);

splay(x);
pushr(x);
}
void split(int x,int y)
{
makeroot(x);
access(y);
splay(y);
}
{
makeroot(x);
f[x] = y;
}
void cut(int x,int y)
{
split(x,y);
f[x] = c[y][0] = 0;
}
signed main()
{
int n,T;
cin>>n>>T;
for(int i = 1;i <= n;i++)
v[i] = sz[i] = mu[i] = 1;
for(int i = 1;i < n;i++)
{
int a,b;
cin>>a>>b;
}
for(int i = 1;i <= T;i++)
{
char ch;
cin>>ch;
if(ch == '+')
{
int a,b,k;
cin>>a>>b>>k;
split(a,b);pusha(b,k);
}
if(ch == '-')
{
int a,b,A,B;
cin>>a>>b>>A>>B;
}
if(ch == '*')
{
int a,b,k;
cin>>a>>b>>k;
split(a,b);pushm(b,k);
}
if(ch == '/')
{
int a,b;
cin>>a>>b;
split(a,b);
printf("%d\n",s[b]);
}
}
return 0;
}
@panyf  2020-01-15 18:48 回复 举报
if(isroot(y))c[z][c[z][1]==1] = x;

if(isroot(y))c[z][c[z][1]==y] = x;