[BZOJ2631]tree

tree

Description
 一棵n个点的树,每个点的初始权值为1。对于这棵树有q个操作,每个操作为以下四种操作之一:
+ u v c:将u到v的路径上的点的权值都加上自然数c;
- u1 v1 u2 v2:将树中原有的边(u1,v1)删除,加入一条新边(u2,v2),保证操作完之后仍然是一棵树;
* u v c:将u到v的路径上的点的权值都乘上自然数c;
/ u v:询问u到v的路径上的点的权值和,求出答案对于51061的余数。
Input
第一行两个整数n,q
接下来n-1行每行两个正整数u,v,描述这棵树
接下来q行,每行描述一个操作
Output
  对于每个/对应的答案输出一行
Sample Input
3 2
1 2
2 3
* 1 3 4
/ 1 1
Sample Output
4
HINT
数据规模和约定
10%的数据保证,1<=n,q<=2000
另外15%的数据保证,1<=n,q<=5*10^4,没有-操作,并且初始树为一条链
另外35%的数据保证,1<=n,q<=5*10^4,没有-操作
100%的数据保证,1<=n,q<=10^5,0<=c<=10^4

Solution
注意处理好tag

Code

#include <bits/stdc++.h>
using namespace std;
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
typedef unsigned int ui;
template<typename T> inline void read(T &x){
    x = 0; T f = 1; char ch = getchar();
    while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); }
    while (isdigit(ch))  { x = x * 10 + ch - '0'; ch = getchar(); }
    x *= f;
}

const int MOD = 51061;
const int N = 100100;
struct Node{
    ui sz, sum, add_tag, mul_tag, rev_tag, val;
    Node *c[2], *fa;
    inline int d(){
        if (fa->c[0] != this && fa->c[1] != this) return -1;
        return fa->c[1] == this;
    }
    inline void sc(Node *p, int d) { c[d] = p; p->fa = this; }
    inline void push_up(){
        sz = c[0]->sz + c[1]->sz + 1;
        sum = (c[0]->sum + c[1]->sum + val) % MOD;
    }
    inline void rev(){ swap(c[0], c[1]); rev_tag ^= 1; }
    inline void mul(ui dt){
        (val *= dt) %= MOD; (sum *= dt) %= MOD;
        (mul_tag *= dt) %= MOD; (add_tag *= dt) %= MOD;
    }
    inline void add(ui dt){
        (val += dt) %= MOD; (sum += sz % MOD * dt) %= MOD;
        (add_tag += dt) %= MOD;
    }
    void push_down();
}pool[N<<1], *null=pool, *tail=pool+1, *loc[N];
void Node:: push_down(){
    if (this->d() != -1) fa->push_down();
    if (rev_tag){
        rep(i, 0, 1) if (c[i] != null) c[i]->rev();
        rev_tag ^= 1;
    }
    if (mul_tag != 1){
        rep(i, 0, 1) if (c[i] != null) c[i]->mul(mul_tag);
        mul_tag = 1;
    }
    if (add_tag){
        rep(i, 0, 1) if (c[i] != null) c[i]->add(add_tag);
        add_tag = 0;
    }
}
int n, q;

inline void setnull(){
    null->c[0] = null->c[1] = null->fa = null;
    null->mul_tag = 1;
    null->rev_tag = null->add_tag = null->sum = null->sz = null->val = 0;
}
inline Node *newNode(ui w){
    tail->c[0] = tail->c[1] = tail->fa = null;
    tail->mul_tag = w;
    tail->rev_tag = tail->add_tag = tail->sum = w; tail->sz = tail->val = 1;
    return tail++;
}
void rot(Node *x){
    Node *y = x->fa; int d = x->d();
    if (y->d() == -1) x->fa = y->fa; else y->fa->sc(x, y->d());
    y->sc(x->c[!d], d); y->push_up();
    x->sc(y, !d);
}
void splay(Node *x){
    for (x->push_down(); x->d() != -1; ){
        if (x->fa->d() == -1) rot(x);
        else x->d() == x->fa->d() ? 
            (rot(x->fa), rot(x)) : (rot(x), rot(x));
    } 
    x->push_up();
}
void access(Node *x){
    for (Node *t = null; x != null; t = x, x = x->fa)
        splay(x), x->c[1] = t, x->push_up();
}
void mkroot(Node *x){access(x); splay(x); x->rev();}
void link(Node *x, Node *y){mkroot(x); x->fa = y;}
void cut(Node *x, Node *y){mkroot(x); access(y); splay(y); y->c[0]=x->fa=null; y->push_up();}
void split(Node *x, Node *y){mkroot(x); access(y); splay(y);}
int main(){
    setnull();

    read(n); read(q);
    for (int i = 1; i <= n; i++) loc[i] = newNode(1);
    for (int i = 1; i < n; i++){
        int u, v; read(u); read(v); link(loc[u], loc[v]);
    }   
    while (q--){
        char opt; scanf(" %c", &opt);
        int u, v, x, y; ui c; read(u); read(v);
        if (opt == '+'){ read(c); split(loc[u], loc[v]); loc[v]->add(c); }
        else if (opt == '-'){ read(x); read(y); cut(loc[u], loc[v]); link(loc[x], loc[y]); }
        else if (opt == '*'){ read(c); split(loc[u], loc[v]); loc[v]->mul(c); }
        else if (opt == '/'){ split(loc[u], loc[v]); printf("%u\n", loc[v]->sum);}
    }

    return 0;
}

你可能感兴趣的:(LCT)