Bzoj2631 Tree

Link-Cut-Tree模板题。

/*
簡単でしょ?笑えるよね。
*/
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
const int mod = 51061;
#define LL long long
#define uint unsigned int
struct node {
    int siz;
    uint sum,key;
    uint add,mul;
    bool rev;
    node *ch[2],*fa;
} tree[maxn];
int n,m;
#define siz(x) ( x == NULL ? 0 : x->siz )
#define sum(x) ( x == NULL ? 0 : x->sum )
void update(node *x,uint a,uint m) {
    if ( x == NULL ) return;
    x->key = ( (LL) x->key * m ) % mod + a % mod;
    x->sum = (( (LL)x->sum * m ) % mod + (LL) a * x->siz ) % mod;
    x->add = ( (LL) x->add * m ) % mod + a % mod;
    x->mul = (LL) x->mul * m % mod;
}
bool isroot(node *x) {
    return x->fa == NULL || ( x->fa->ch[0] != x && x->fa->ch[1] != x);
}
void pushup(node *x) {
    x->sum = ((LL)x->key + sum(x->ch[0]) + sum(x->ch[1]) ) % mod;
    x->siz = siz(x->ch[0]) + siz(x->ch[1]) + 1;
}
void pushdown(node *x) {
    if ( x->rev ) {
        x->rev = 0;
        if ( x->ch[0] ) x->ch[0]->rev ^= 1;
        if ( x->ch[1] ) x->ch[1]->rev ^= 1;
        swap(x->ch[0],x->ch[1]);
    }
    if ( x->add == 0 && x->mul == 1) return;
    if ( x->ch[0] ) update(x->ch[0],x->add,x->mul);
    if ( x->ch[1] ) update(x->ch[1],x->add,x->mul);
    x->add = 0; x->mul = 1;
}
void rotate(node *x,bool d) {
    node *y = x->fa;
    y->ch[!d] = x->ch[d]; if ( x->ch[d] ) x->ch[d]->fa = y;
    x->fa = y->fa;
    if ( y->fa && y == y->fa->ch[0] ) 
        y->fa->ch[0] = x;
    else if ( y->fa && y == y->fa->ch[1] ) 
        y->fa->ch[1] = x;
    x->ch[d] = y; y->fa = x;
    pushup(y); pushup(x);
}
node *sta[maxn]; int top = 0;
void splay(node *x) {
    top = 0;
    sta[++top] = x;
    for (node *y = x; !isroot(y); y = y->fa) sta[++top] = y->fa;
    while(top) pushdown(sta[top--]);
    while (!isroot(x)) {
        node *y = x->fa;
        if ( x == y->ch[0] ) {
            if ( !isroot(y) && y == y->fa->ch[0])
                rotate(y,1);
            rotate(x,1);
        } else {
            if ( !isroot(y) && y == y->fa->ch[1])
                rotate(y,0);
            rotate(x,0);
        }
    }
}
void access(node *x) {
    for (node *y = NULL; x; x = x->fa) {
        splay(x); x->ch[1] = y; pushup(y = x);
    }
}
void makeroot(node *x) {
    access(x); splay(x); x->rev ^= 1;
}
void link(node *x,node *y) {
    makeroot(x); x->fa = y;
}
void cut(node *x,node *y) {
    makeroot(x); access(y); splay(y);
    y->ch[0] = x->fa = NULL;
}
void modify(node *x,node *y,uint a,uint m) {
    makeroot(x); access(y); splay(y);
    update(y,a,m);
}
uint query(node *x,node *y) {
    makeroot(x); access(y); splay(y);
    return y->sum;
}
int main() {
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);

    scanf("%d%d",&n,&m);
    for (int i = 1; i <= n; i++)
        tree[i].key = tree[i].siz = tree[i].sum = tree[i].mul = 1;
    for (int i = 1; i <= n-1; i++) {
        int x,y;
        scanf("%d%d",&x,&y);
        link(&tree[x],&tree[y]);
    }
    char str[3]; int x,y,z,w;
    while (m--) {
        scanf("%s",str);
        if ( str[0] == '+' ) {
            scanf("%d%d%d",&x,&y,&z);
            modify(&tree[x],&tree[y],z,1);
        }
        if ( str[0] == '-' ) {
            scanf("%d%d%d%d",&x,&y,&z,&w);
            cut(&tree[x],&tree[y]);
            link(&tree[z],&tree[w]);
        }
        if ( str[0] == '*' ) {
            scanf("%d%d%d",&x,&y,&z);
            modify(&tree[x],&tree[y],0,z);
        }
        if ( str[0] == '/' ) {
            scanf("%d%d",&x,&y);
            printf("%d\n",query(&tree[x],&tree[y]));
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值