Bzoj2631 Tree

Link-Cut-Tree模板题。

/*
簡単でしょ?笑えるよね。
*/
#include
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]));
        }
    }
}

你可能感兴趣的:(LCT)