1. 程式人生 > >P3833 [SHOI2012]魔法樹

P3833 [SHOI2012]魔法樹

def getchar() roo oid 過程 所有 題解 ++ 依次

$ \color{#0066ff}{ 題目描述 }$

Harry Potter 新學了一種魔法:可以讓改變樹上的果子個數。滿心歡喜的他找到了一個巨大的果樹,來試驗他的新法術。

這棵果樹共有N個節點,其中節點0是根節點,每個節點u的父親記為fa[u],保證有fa[u] < u。初始時,這棵果樹上的果子都被 Dumbledore 用魔法清除掉了,所以這個果樹的每個節點上都沒有果子(即0個果子)。

不幸的是,Harry 的法術學得不到位,只能對樹上一段路徑的節點上的果子個數統一增加一定的數量。也就是說,Harry 的魔法可以這樣描述:

Add u v d

表示將點u和v之間的路徑上的所有節點的果子個數都加上d。

接下來,為了方便檢驗 Harry 的魔法是否成功,你需要告訴他在釋放魔法的過程中的一些有關果樹的信息:

Query u

表示當前果樹中,以點u為根的子樹中,總共有多少個果子?

\(\color{#0066ff}{輸入格式}\)

第一行一個正整數N (1 ≤ N ≤ 100000),表示果樹的節點總數,節點以0,1,…,N ? 1標號,0一定代表根節點。

接下來N ? 1行,每行兩個整數a,b (0 ≤ a < b < N),表示a是b的父親。

接下來是一個正整數Q(1 ≤ ? ≤ 100000),表示共有Q次操作。

後面跟著Q行,每行是以下兩種中的一種:

A u v d,表示將u到v的路徑上的所有節點的果子數加上d;0 ≤ u,v <N,0 < d < 100000

Q u,表示詢問以u為根的子樹中的總果子數,註意是包括u本身的。

\(\color{#0066ff}{輸出格式}\)

對於所有的Query操作,依次輸出詢問的答案,每行一個。答案可能會超過2^32 ,但不會超過10^15 。

\(\color{#0066ff}{輸入樣例}\)

4
0 1
1 2
2 3
4
A 1 3 1
Q 0
Q 1
Q 2

\(\color{#0066ff}{輸出樣例}\)

3
3
2

\(\color{#0066ff}{數據範圍與提示}\)

none

\(\color{#0066ff}{題解}\)

顯然樹剖裸題

一個是路徑加,跳重鏈\(O(nlog^2n)\)

詢問直接線段樹區間查詢即可

#include<bits/stdc++.h>
#define LL long long
LL in() {
    char ch; LL x = 0, f = 1;
    while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
    for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
    return x * f;
}
const int maxn = 1e5 + 10;
struct Tree  {
protected:
    struct node {
        node *ch[2];
        int l, r;
        LL val, tag;
        node(int l = 0, int r = 0, LL val = 0, LL tag = 0): l(l), r(r), val(val), tag(tag) {
            ch[0] = ch[1] = NULL;
        }
        void add(LL v) { tag += v, val += (r - l + 1) * v; }
        void upd() { val = ch[0]->val + ch[1]->val; }
        int mid() { return (l + r) >> 1; }
        void dwn() {
            if(!tag) return;
            ch[0]->add(tag), ch[1]->add(tag);
            tag = 0;
        }
    }*root;
    void build(node *&o, int l, int r) {
        o = new node(l, r);
        if(l == r) return;
        int mid = (l + r) >> 1;
        build(o->ch[0], l, mid);
        build(o->ch[1], mid + 1, r);
    }
    void lazy(node *o, int l, int r, LL val) {
        if(l <= o->l && o->r <= r) return o->add(val);
        o->dwn();
        if(l <= o->mid()) lazy(o->ch[0], l, r, val);
        if(r > o->mid()) lazy(o->ch[1], l, r, val);
        o->upd();
    }
    LL query(node *o, int l, int r) {
        if(l <= o->l && o->r <= r) return o->val;
        o->dwn();
        LL ans = 0;
        if(l <= o->mid()) ans += query(o->ch[0], l, r);
        if(r > o->mid()) ans += query(o->ch[1], l, r);
        return ans;
    }
public:
    Tree() { root = NULL; }
    void build(int l, int r) { build(root, l, r); }
    void lazy(int l, int r, LL val) { lazy(root, l, r, val); }
    LL query(int l, int r) { return query(root, l, r); }
}s;
int top[maxn], dfn[maxn], siz[maxn], fa[maxn], n, cnt;
int dep[maxn], son[maxn], nfd[maxn];
struct node {
    int to;
    node *nxt;
    node(int to = 0, node *nxt = NULL): to(to), nxt(nxt) {}
};
node *head[maxn];
void add(int from, int to) {
    head[from] = new node(to, head[from]);
}
void dfs1(int x, int f) {
    dep[x] = dep[fa[x] = f] + (siz[x] = 1);
    for(node *i = head[x]; i; i = i->nxt) {
        if(i->to == f) continue;
        dfs1(i->to, x);
        siz[x] += siz[i->to];
        if(!son[x] || siz[i->to] > siz[son[x]]) son[x] = i->to;
    }
}
void dfs2(int x, int t) {
    top[nfd[dfn[x] = ++cnt] = x] = t;
    if(son[x]) dfs2(son[x], t);
    for(node *i = head[x]; i; i = i->nxt) 
        if(!dfn[i->to]) dfs2(i->to, i->to);
}
void addpath(int x, int y, LL val) {
    int fx = top[x], fy = top[y];
    while(fx != fy) {
        if(dep[fx] >= dep[fy]) {
            s.lazy(dfn[fx], dfn[x], val);
            x = fa[fx];
        }
        else {
            s.lazy(dfn[fy], dfn[y], val);
            y = fa[fy];
        }
        fx = top[x];
        fy = top[y];
    }
    if(dep[x] < dep[y]) std::swap(x, y);
    s.lazy(dfn[y], dfn[x], val);
}
char getch() { 
    char ch;
    while(!isalpha(ch = getchar()));
    return ch;
}
int main() {
    int n = in();
    int x, y;
    LL z;
    for(int i = 1; i < n; i++) x = in(), y = in(), add(x, y);
    dfs1(0, n + 1), dfs2(0, 0), s.build(1, n);
    for(int T = in(); T --> 0;) {
        if(getch() == 'A') x = in(), y = in(), z = in(), addpath(x, y, z);
        else x = in(), printf("%lld\n", s.query(dfn[x], dfn[x] + siz[x] - 1));
    }
    return 0;
}

P3833 [SHOI2012]魔法樹