树链剖分板子

1、求树上  v  ->  u 路径上的和

2、更新树上  v  ->  u 路径上的点权

3、求以x为根节点的子树的点权和

4、更新x为根节点的子树的点权

#include <bits/stdc++.h>
using namespace std;
#define ll long long
template<typename T>
inline void qread(T& x){
    char c = getchar(); x = 0; bool f = 0;
    while(!isdigit(c)) f ^= !(c^45), c = getchar();
    while(isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    if(f)x = -x;
}

struct node {
    int to;
    int nex;
}e[4000005];

struct Tree {
    int lazy;
    ll val, cnt;
}pos[4000005];

int n, m, s;
int dep[100005], siz[100005], fa[100005], son[100005], a[100005];
int top[100005], w[100005], head[10005], cnt, id[10005], tot;

void init(int n) {
    for(int i = 0; i <= n; i++)  {
        head[i] = -1;
        fa[i] = -1;
        siz[i] = 0;
        dep[i] = 0;
        son[i] = 0;
        top[i] = 0;
        w[i] = 0;
        id[i] = 0;
        tot = 0;
    }
    cnt = 0;
}

void addedge(int u, int v) {
    e[++cnt].to = v;
    e[cnt].nex = head[u];
    head[u] = cnt;
}

inline void pushup(int cur) {
    pos[cur].val = pos[cur << 1].val + pos[cur << 1 | 1].val;
    pos[cur].cnt = pos[cur << 1].cnt + pos[cur << 1 | 1].cnt;
}

inline void build(int l, int r, int cur) {
    pos[cur].lazy = 0;
    pos[cur].cnt = 0;
    if(l==r){
        pos[cur].val = w[l];
        pos[cur].cnt = 1;
        return;
    }
    int mid = l + r >> 1;
    build(l, mid, cur << 1);
    build(mid + 1, r, cur << 1 | 1);
    pushup(cur);
}

inline void pushdown(int cur) {
    if(pos[cur].lazy != 0) {
        pos[cur << 1].lazy += pos[cur].lazy;
        pos[cur << 1 | 1].lazy += pos[cur].lazy;
        pos[cur << 1].val += pos[cur].lazy * pos[cur << 1].cnt;
        pos[cur << 1 | 1].val += pos[cur].lazy * pos[cur << 1 | 1].cnt;
        pos[cur].lazy = 0;
    }
}

inline void update(int l, int r, ll val, int cl, int cr, int cur) {
     if(l <= cl && cr <= r)
     {
         pos[cur].val += val * pos[cur].cnt;
         pos[cur].lazy += val;
         return ;
     }
     pushdown(cur);
     int mid = cl + cr >> 1;
     if(l <= mid)update(l, r, val, cl, mid, cur << 1);
     if(mid < r)update(l, r, val, mid + 1, cr, cur << 1 | 1);
     pushup(cur);
}

inline ll getans(int cl, int cr, int l, int r, int cur) {
    if(l <= cl && cr <= r)
        return pos[cur].val;
    pushdown(cur);
    ll ans = 0;
    int mid = cl + cr >> 1;
    if(l <= mid) ans += getans(cl, mid, l, r, cur << 1);
    if(mid < r) ans += getans(mid + 1, cr, l, r, cur << 1 | 1);
    return ans;
}
inline  void updatetree(int s, int t, int c) {// 树上  u到v 的路径所有点+c
    while(top[s] != top[t]) {
        if(dep[top[s]] > dep[top[t]]) swap(s, t);
        update(id[top[t]], id[t], c, 1, n, 1);
        t = fa[top[t]];
    }
    if(dep[s] > dep[t])swap(s, t);
    update(id[top[t]], id[t], c, 1, n, 1);
}

inline int getanstree(int s, int t) {//树上  u到v 的路径之和
    int ans = 0;
    while(top[s] != top[t]) {
        if(dep[top[s]] > dep[top[t]]) swap(s, t);
        ans+=getans(id[top[t]], id[t], 1, n, 1);
        t = fa[top[t]];
    }
    if(dep[s] > dep[t])swap(s, t);
    ans += getans(id[top[t]], id[t], 1, n, 1);
    return ans;
}

inline void updatetreeson(int x, int c){//以x为根的子树每个点+c
    update(id[x], id[x] + siz[x] - 1, c, 1, n, 1);
}
inline int getanstreeson(int x) {
    return getans(id[x], id[x] + siz[x] - 1, 1, n, 1);//以x为根的子树每个点求和
}

inline void dfs1(int x, int fath, int deep) {
    fa[x] = fath;
    dep[x] = deep;
    siz[x] = 1;
    int maxsize = -1;
    for(int i = head[x]; i != -1; i = e[i].nex) {
        int to = e[i].to;
        if(to == fa[x])continue;
        dfs1(to, x, deep + 1);
        siz[x] += siz[to];
        if(siz[to] > maxsize) {
            maxsize = siz[to];
            son[x] = to;
        }
    }
}
inline void dfs2(int x, int ftop) {
    top[x] = ftop;
    id[x] = ++tot;
    w[id[x]] = a[x];
    if(!son[x])return ;
    dfs2(son[x], ftop);
    for(int i = head[x]; i != -1; i = e[i].nex) {
        int to = e[i].to;
        if(to == fa[x] || to == son[x])continue;
        dfs2(to, to);
    }
}
int main(){
    qread(n); qread(m); qread(s);
    init(n);
    for(int i = 0; i < n; i++) qread(a[i]);
    for(int i = 0; i < m; i++) {
        int x, y;
        qread(x); qread(y);
        addedge(x, y);
        addedge(y, x);
    }
    dfs1(s, -1, 1);
    dfs2(s, s);
    build(1, n, 1);
    for(int i = 0; i < m; i++) {
        int op;
        scanf("%d", &op);
        if(op == 0) {

        }
        else{

        }
    }
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值