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

回复帖子

@JK_LOVER 2020-01-15 16:35 回复
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = (int)1e5+10;
int c[N][2],f[N],v[N],s[N],ad[N],mu[N],st[N],sz[N];
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 pushm(int x,int d){mul(s[x],d);mul(v[x],d);mul(mu[x],d);mul(ad[x],d);}
void pusha(int x,int d){add(s[x],d*sz[x]);add(v[x],d);add(ad[x],d);}
void pushdown(int x)
{
    if(mu[x]!=1)pushm(lc,mu[x]),pushm(rc,mu[x]),mu[x]=1;
    if(ad[x]!=0)pusha(lc,ad[x]),pusha(rc,ad[x]),ad[x]=0;
    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);
}
void link(int x,int 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;
        link(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;
            cut(a,b);link(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;
}
反馈
如果你认为某个帖子有问题,欢迎向洛谷反馈,以帮助更多的同学。



请具体说明理由,以增加反馈的可信度。