bzoj 2631 tree LCT+标记处理

这个题目名字好大众啊。
从昨天下午开始码,刚调出来。
由于LCT常数很大,一定要丧心病狂的优化常数,非常管用。

#include<iostream>
#include<cstdio>
#define LL unsigned int
#define P 51061
#define maxn 200005
using namespace std;
int n,q;
int fa[maxn],ch[maxn][2],sz[maxn];
LL v[maxn],sum[maxn],add[maxn],mul[maxn];
bool rev[maxn];
bool dir(int x)
{return x==ch[fa[x]][1];}
bool isroot(int x)
{return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
void up(int x)
{   
    sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;
    sum[x]=(sum[ch[x][0]]+sum[ch[x][1]]+v[x])%P;
}
void down(int x)
{
    if(rev[x])
    {
        rev[ch[x][0]]^=1;
        rev[ch[x][1]]^=1;
        rev[x]=0;
        swap(ch[x][0],ch[x][1]);
    }
    if(mul[x]==1&&add[x]==0) return ;
    if(ch[x][0])
    {
        v[ch[x][0]]=(v[ch[x][0]]*mul[x]+add[x])%P;
        sum[ch[x][0]]=(sum[ch[x][0]]*mul[x]+add[x]*sz[ch[x][0]])%P;
        mul[ch[x][0]]=(mul[x]*mul[ch[x][0]])%P;
        add[ch[x][0]]=(add[ch[x][0]]*mul[x]+add[x])%P;
    }
    if(ch[x][1])
    {
        v[ch[x][1]]=(v[ch[x][1]]*mul[x]+add[x])%P;
        sum[ch[x][1]]=(sum[ch[x][1]]*mul[x]+add[x]*sz[ch[x][1]])%P;
        mul[ch[x][1]]=(mul[x]*mul[ch[x][1]])%P;
        add[ch[x][1]]=(add[ch[x][1]]*mul[x]+add[x])%P;
    }
    mul[x]=1;add[x]=0;
}
void rotate(int x)
{
    int y=fa[x],z=fa[y];
    bool b=dir(x);
    int a=ch[x][!b];
    if(!isroot(y)) ch[z][dir(y)]=x;
    fa[x]=z;ch[x][!b]=y;
    fa[y]=x;ch[y][b]=a;
    if(a) fa[a]=y;
    up(y);up(x);
}
int S[maxn],tot;
void splay(int x)
{
    S[++tot]=x;
    for(int i=x;!isroot(i);i=fa[i]) //这里迷之错误,忘了加'!'  调了一天 
        S[++tot]=fa[i];
    while(tot)down(S[tot--]);
    while(!isroot(x))
    {
        int y=fa[x];int z=fa[y];
        if(isroot(y)) rotate(x);
        else
        {
            bool b=dir(x),c=dir(y);
            if(b^c) {rotate(y);rotate(x);}
            else    {rotate(x);rotate(x);}
        }
    }
}
void access(int x)
{
    for(int t=0;x;t=x,x=fa[x])
        splay(x),ch[x][1]=t,up(x);
}
void make_root(int x)
{access(x);splay(x);rev[x]^=1;}
void link(int x,int y)
{make_root(x);fa[x]=y;}
void cut(int x,int y)
{
    make_root(x);
    access(y);splay(y);
    ch[y][0]=fa[x]=0;
    up(y);
}
void change(int x,int y,LL a,LL m)
{
    make_root(x);
    access(y);splay(y);
    v[y]=(v[y]*m+a)%P;
    sum[y]=(sum[y]*m+sz[y]*a)%P;
    mul[y]*=m;mul[y]%=P;
    add[y]=(add[y]*m+a)%P;
}
int ask(int x,int y)
{
    make_root(x);
    access(y);splay(y);
    return sum[y]%P;
}
void read(int &a)
{
    a=0;char c=getchar();
    while(c<'0'||c>'9')
        c=getchar();
    while(c>='0'&&c<='9')
    {
        a=a*10+c-'0';
        c=getchar();
    }
}
char C[3];
int main()
{
    read(n);read(q);
    for(int i=1;i<=n;i++)
        v[i]=sum[i]=mul[i]=sz[i]=1;
    int u,v,u1,v1,c;
    for(int i=1;i<n;i++)
    {
        read(u);read(v);
        link(v,u);
    }
    for(int i=1;i<=q;i++)
    {
        scanf("%s",C);
        if(C[0]=='+')
        {
            read(u);read(v);read(c);
            change(u,v,c,1);
        }
        if(C[0]=='-')
        {
            read(u);read(v);read(u1);read(v1);
            cut(u,v);link(u1,v1);
        }
        if(C[0]=='*')
        {
            read(u);read(v);read(c);
            change(u,v,0,c);
        }
        if(C[0]=='/')
        {
            read(u);read(v);
            printf("%d\n",ask(u,v));
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值