[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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章