[Luogu] 树链剖分

模板题,对于对为某个点为根的子树进行处理时,只需每个节点记录两个值

分别为搜索以该节点为根的子树时的最初搜索序和最末搜索序,将这两

个数作为线段树区间操作的端点进行操作

#include <bits/stdc++.h>

using namespace std;
const int N = 100005;

#define gc getchar()
#define lson jd << 1
#define rson jd << 1 | 1

struct Node_1{
    int u, v, nxt;
}G[N << 2];
struct Node_2{
    int fa, son, size, topp, deep, l, r;
}P[N];
struct Node_3{
    int l, r, w, f;
}T[N << 2];
int n, Ti, root, mod, now = 1, tim, opt, x_, y_, z_;
int head[N], tree[N], bef[N], data[N];
long long ret, ans;

inline int read(){
    int x = 0, f = 1; char c = gc;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = gc;}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc;
    return x * f;
}

inline void add(int u, int v){
    G[now].u = u; G[now].v = v; G[now].nxt = head[u]; head[u] = now ++;
}

inline void init(){
    n = read(); Ti = read(); root = read(); mod = read();
    for(int i = 1; i <= n; i ++) head[i] = -1, data[i] = read();
    for(int i = 1; i < n; i ++){ 
        int u = read(), v = read(); 
        add(u, v); add(v, u);
    }
}

void dfs_find_son(int u, int fa, int dep){
    P[u].fa = fa;
    P[u].deep = dep;
    P[u].size = 1;
    for(int i = head[u]; ~ i; i = G[i].nxt){
        int v = G[i].v;
        if(v != fa){
            dfs_find_son(v, u, dep + 1);
            P[u].size += P[v].size;
            if(! P[u].son || P[v].size > P[P[u].son].size) P[u].son = v;
        }
    }
}

void dfs_to_un(int u, int tp){
    P[u].topp = tp;
    tree[u] = ++ tim;
    bef[tim] = u;
    P[u].l = tim;
    if(!P[u].son){P[u].r = tim; return ;}
    dfs_to_un(P[u].son, tp);
    for(int i = head[u]; ~ i; i = G[i].nxt){
        int v = G[i].v;
        if(v != P[u].fa && v != P[u].son) dfs_to_un(v, v);
    }
    P[u].r = tim;
}

void build_tree(int l, int r, int jd){
    T[jd].l = l; T[jd].r = r;
    if(l == r){
        T[jd].w = data[bef[l]] % mod;
        return ;
    }
    int mid = (l + r) >> 1;
    build_tree(l, mid, lson);
    build_tree(mid + 1, r, rson);
    T[jd].w = T[lson].w + T[rson].w;
}

void down(int jd){
    int F = T[jd].f;
    T[lson].w += ((T[lson].r - T[lson].l + 1) * F); T[lson].w %= mod;
    T[rson].w += ((T[rson].r - T[rson].l + 1) * F); T[rson].w %= mod;
    T[lson].f += F;
    T[rson].f += F;
    T[jd].f = 0;
}

void Sec_G(int l, int r, int jd, int x, int y, int yj){
    if(x <= l && r <= y){
        T[jd].w += ((r - l + 1) * yj); 
        T[jd].w %= mod;
        T[jd].f += yj;
        return ; 
    }
    if(T[jd].f) down(jd);
    int mid = (l + r) >> 1;
    if(x <= mid) Sec_G(l, mid, lson, x, y, yj);
    if(y > mid) Sec_G(mid + 1, r, rson, x, y, yj);
    T[jd].w = T[lson].w + T[rson].w;
}

void Sec_A(int l, int r, int jd, int x, int y){
    if(x <= l && r <= y){
        ans += T[jd].w; ans %= mod;
        return ;
    }
    if(T[jd].f) down(jd);
    int mid = (l + r) >> 1;
    if(x <= mid) Sec_A(l, mid, lson, x, y);
    if(y > mid) Sec_A(mid + 1, r, rson, x, y);
}

void Sec_G_imp(int x, int y, int z){
    int tp1 = P[x].topp, tp2 = P[y].topp;
    while(tp1 != tp2){
        if(P[tp1].deep < P[tp2].deep) swap(tp1, tp2), swap(x, y);
        Sec_G(1, n, 1, tree[tp1], tree[x], z);
        x = P[tp1].fa;
        tp1 = P[x].topp;
    }
    if(P[x].deep > P[y].deep) Sec_G(1, n, 1, tree[y], tree[x], z);
    else Sec_G(1, n, 1, tree[x], tree[y], z);
}

int Sec_A_imp(int x, int y){
    int tp1 = P[x].topp, tp2 = P[y].topp;
    ret = 0;
    while(tp1 != tp2){
        if(P[tp1].deep < P[tp2].deep) swap(tp1, tp2), swap(x, y);
        ans = 0;
        Sec_A(1, n, 1, tree[tp1], tree[x]);
        ret += ans;
        x = P[tp1].fa;
        tp1 = P[x].topp;
    }
    ans = 0;
    if(P[x].deep > P[y].deep) Sec_A(1, n, 1, tree[y], tree[x]);
    else Sec_A(1, n, 1, tree[x], tree[y]);
    ret += ans;
    return ret % mod;
}

void good_look(){
    ans = 0;
    Sec_A(1, n, 1, P[x_].l, P[x_].r);
    cout << ans << endl;
}

void get_all(){
    opt = read(); x_ = read();
    if(opt == 1) y_ = read(), z_ = read();
    else if(opt == 2) y_ = read();
    else if(opt == 3) z_ = read();
}

void debug(){
    cout << "top: ";  for(int i = 1; i <= n; i ++) cout << P[i].topp << " "; cout << endl;
    cout << "fa: ";   for(int i = 1; i <= n; i ++) cout << P[i].fa << " ";   cout << endl;
    cout << "deep: "; for(int i = 1; i <= n; i ++) cout << P[i].deep << " "; cout << endl;
    cout << "size: "; for(int i = 1; i <= n; i ++) cout << P[i].size << " "; cout << endl;
    cout << "son: ";  for(int i = 1; i <= n; i ++) cout << P[i].son << " ";  cout << endl;
    cout << "L: ";    for(int i = 1; i <= n; i ++) cout << P[i].l << " ";    cout << endl;
    cout << "R: ";    for(int i = 1; i <= n; i ++) cout << P[i].r << " ";    cout << endl;
    exit(0);
}

int main()
{
    init();
    dfs_find_son(root, 0, 1);
    dfs_to_un(root, root);
    build_tree(1, n, 1);
    //debug();
    while(Ti --){
        get_all();
        if(opt == 1) Sec_G_imp(x_, y_, z_);
        else if(opt == 2) cout << Sec_A_imp(x_, y_) << endl;
        else if(opt == 3) Sec_G(1, n, 1, P[x_].l, P[x_].r, z_);
        else good_look();
    }
    
    return 0;
}
/*
操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节点的值都加上z
操作2: 格式: 2 x y 表示求树从x到y结点最短路径上所有节点的值之和
操作3: 格式: 3 x z 表示将以x为根节点的子树内所有节点值都加上z
操作4: 格式: 4 x 表示求以x为根节点的子树内所有节点值之和
5 5 2 24
7 3 7 8 0
1 2
1 5
3 1
4 1
3 4 2
3 2 2
4 5
1 5 1 3
2 1 3
*/

 

posted @ 2017-11-28 21:44  xayata  阅读(173)  评论(0编辑  收藏  举报