求和

求和

http://www.nowcoder.com/questionTerminal/0e52b2d6514746c7a2ab273d5b6bd32a

显然这就是一个树链剖分板子题

code

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

#define N 1000010
#define M 1010
#define lson rt << 1
#define rson rt << 1 | 1
#define int long long

using namespace std;
int n, m, cnt, k;
int dep[N], dfn[N], fa[N], son[N], pre[N], siz[N], top[N], w[N];

int read() {
    int s = 0, f = 0; char ch = getchar();
    while (!isdigit(ch)) f |= (ch == '-'), ch = getchar();
    while (isdigit(ch)) s = s * 10 + (ch ^ 48), ch = getchar();
    return f ? -s : s;
}

namespace Seg {
    struct Tree {
        int sum, len, lazy;
    }tree[N << 1];
    void push_up(int rt) {
        tree[rt].sum = tree[lson].sum + tree[rson].sum;
    }
    void build(int rt, int l, int r) {
        tree[rt].len = r - l + 1;
        if (l == r) {
            tree[rt].sum = w[pre[l]];
            return;
        }
        int mid = (l + r) >> 1;
        build(lson, l, mid);
        build(rson, mid + 1, r);
        push_up(rt);
    }
    void push_down(int rt) {
        if (!tree[rt].sum) return;
        tree[lson].sum += tree[rt].lazy * tree[lson].len;
        tree[rson].sum += tree[rt].lazy * tree[rson].len;
        tree[lson].lazy += tree[rt].lazy;
        tree[rson].lazy += tree[rt].lazy;
        tree[rt].lazy = 0;
    }
    void updata(int rt, int c, int l, int r, int L, int R) {
        if (L <= l && r <= R) {
            tree[rt].sum += tree[rt].len * c;
            tree[rt].lazy += c;
            return;
        }
        push_down(rt);
        int mid = (l + r) >> 1;
        if (L <= mid) updata(lson, c, l, mid, L, R);
        if (R > mid) updata(rson, c, mid + 1, r, L, R);
        push_up(rt);
    }
    int query(int rt, int l, int r, int L, int R) {
        if (L <= l && r <= R) return tree[rt].sum;
        push_down(rt);
        int mid = (l + r) >> 1, ans = 0;
        if (L <= mid) ans += query(lson, l, mid, L, R);
        if (R > mid) ans += query(rson, mid + 1, r, L, R);
        return ans;
    }
}

namespace Cut {
    int head[N << 1], add_edge;
    struct node {
        int next, to;
    }edge[N << 1];
    void add(int from, int to) {
        edge[++add_edge].next = head[from];
        edge[add_edge].to = to;
        head[from] = add_edge;
    }
    void dfs(int x, int fath) {
        dep[x] = dep[fath] + 1, fa[x] = fath, siz[x] = 1;
        for (int i = head[x]; i; i = edge[i].next) {
            int to = edge[i].to;
            if (to == fath) continue;
            dfs(to, x);
            siz[x] += siz[to];
            if (siz[to] > siz[son[x]]) son[x] = to;
        }
    }
    void dfs2(int x, int tp) {
        dfn[x] = ++cnt, pre[cnt] = x, top[x] = tp;
        if (son[x]) dfs2(son[x], tp);
        for (int i = head[x]; i; i = edge[i].next) {
            int to = edge[i].to;
            if (to == son[x] || to == fa[x]) continue;
            dfs2(to, to); 
        }
    }
    void change(int x, int y, int c) {
        while (top[x] != top[y]) {
            if (dep[top[x]] < dep[top[y]]) swap(x, y);
            Seg::updata(1, c, 1, n, dfn[top[x]], dfn[x]);
            x = fa[top[x]];
        }
        if (dep[x] > dep[y]) swap(x, y);
        Seg::updata(1, c, 1, n, dfn[x], dfn[y]);
    }
    int asksum(int x, int y) {
        int ans = 0;
        while (top[x] != top[y]) {
            if (dep[top[x]] < dep[top[y]]) swap(x, y);
            ans += Seg::query(1, 1, n, dfn[top[x]], dfn[x]);
            x = fa[top[x]];
        }
        if (dep[x] > dep[y]) swap(x, y);
        ans += Seg::query(1, 1, n, dfn[x], dfn[y]);
        return ans;
    }
}

signed main() {
    n = read(), m = read(), k = read();
    for (int i = 1; i <= n; i++) w[i] = read();
    for (int i = 1, x, y; i <= n - 1; i++) 
        x = read(), y = read(), Cut::add(x, y), Cut::add(y, x);
    Cut::dfs(k, 0), Cut::dfs2(k, 1), Seg::build(1, 1, n);
    for (int i = 1, opt, x, y; i <= m; i++) {
        opt = read();
        if (opt == 1) x = read(), y = read(), Cut::change(x, x, y);
        if (opt == 2) {
            x = read();
            printf("%d\n", Seg::query(1, 1, n, dfn[x], dfn[x] + siz[x] - 1));
        }
    }
}
全部评论

相关推荐

搜索部&nbsp;首先说下timeline8.18,投递8.19,约一面8.21,晚上一面call约二面8.22,上午二面下午oc周末等待(8.23,8.24)8.25,offer一年前,我还是懵懵懂懂,高考完的暑假,只会提前学学高数,未来的画像是什么?我或许无法预测。开学后,自学Python,接单,无数个客户的ddl,偷偷摸摸一个人找自习的地方,这一步步竟然为后来的我,搭建工程能力的基础。大一上,我也要感谢我的第一位老板,让我接触到了实习,师兄带着我一步步入门,看他们写的飞书文档。大一下,导师带我参与企业项目,这让我渐渐发现,应该去实践,增长见识,而非局限当下,盯着自己的小新pro。不久后,第一波投递开始,结果当然是约面极少。盯着简历上的文字和ssob,我开始思考,确实很多可以去提升。带着些许不甘心,继续沉淀,慢慢的约面也越来越多,有的时候两天7场,准备完就接着下一个日程。这一次,也许是刚好到位吧,比较match,面试答的流利,关关难关关过,成为度孝子展望未来,依然是重重挑战,果然只有收到offer的那一刻是开心的。愿在百度星海拆解的每一段代码,都能成为丈量宇宙的诗行;此志终赴星河,而今迈步重铸天阶。屏幕前的你们,在无数个向星海奔赴的日夜,一定一定,会在未来化作群星回响的征程——请永远相信此刻埋首耕耘的自己!!!
一天三顿半:???百度提前批发 offer了?不是统一和正式批排序完再发吗我靠
百度求职进展汇总
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务