码迷,mamicode.com
首页 > 其他好文 > 详细

【JLOI2014】松鼠的新家(树上差分)

时间:2020-02-25 19:42:16      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:ios   val   读者   main   using   int   sdi   关于   diff   

题目链接:https://loj.ac/problem/2236

做法

其实没有什么好说的,这个题就属于那种看起来没什么细节,实际也没有什么细节的题,但是却把我埋了,关于这道题的细节请读者自悟

然后就是树上差分,适用于多次操作一次查询的题

把路径$(u, v)$的边权加$val$的操作:

  • $diff[u] += val$, $diff[v] += val$
  • $diff[lca(u, v)] -= val$, $diff[fa[lca(u, v)]] -= val$

#include <iostream>
#include <cstdio>
#define Re register
using namespace std;
inline int read() {
    int x = 0;
    char ch = getchar();
    while (!isdigit(ch)) ch = getchar();
    while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
    return x;
}
const int maxN = 300005;
int N, diff[maxN], ord[maxN], num[maxN];
struct Edge {
    int nxt, to;
} e[maxN << 1];
int cnte = 1, head[maxN];
inline void add_Edge(int i, int j) { e[++cnte].nxt = head[i], e[cnte].to = j, head[i] = cnte; }
int dep[maxN], anc[maxN][21];
void dfslca(int u, int fa) {
    dep[u] = dep[fa] + 1;
    anc[u][0] = fa;
    for (int i = head[u]; i; i = e[i].nxt) {
        if (e[i].to == fa)
            continue;
        dfslca(e[i].to, u);
    }
}
inline int LCA(int x, int y) {
    if (dep[x] < dep[y])
        x ^= y ^= x ^= y;
    for (int i = 18; i >= 0; --i)
        if (dep[anc[x][i]] >= dep[y])
            x = anc[x][i];
    if (x == y)
        return x;
    for (int i = 18; i >= 0; --i)
        if (anc[x][i] != anc[y][i])
            x = anc[x][i], y = anc[y][i];
    return anc[x][0];
}
void dfs(int u, int fa) {
    for (Re int v, i = head[u]; i; i = e[i].nxt) {
        if ((v = e[i].to) == fa)
            continue;
        dfs(v, u);
        diff[u] += diff[v];
    }
}
int main() {
    N = read();
    for (Re int i = 1; i <= N; ++i) ord[i] = read(), num[ord[i]] = i;
    for (Re int i = 1, u, v; i < N; ++i) {
        u = read(), v = read();
        add_Edge(u, v), add_Edge(v, u);
    }
    dfslca(1, 0);
    for (int i = 1; i <= 18; ++i)
        for (int u = 1; u <= N; ++u) anc[u][i] = anc[anc[u][i - 1]][i - 1];
    for (Re int i = 1; i < N; ++i) {
        Re int x = ord[i], y = ord[i + 1];
        Re int lca = LCA(x, y);
        diff[anc[lca][0]] -= 1, diff[lca] -= 1;
        diff[x] += 1, diff[y] += 1;
    }
    dfs(1, 0);
    for (int i = 1; i <= N; ++i) printf("%d\n", (num[i] == 1) ? diff[i] : (diff[i] - 1));
    return 0;
}

 

【JLOI2014】松鼠的新家(树上差分)

标签:ios   val   读者   main   using   int   sdi   关于   diff   

原文地址:https://www.cnblogs.com/blog-fgy/p/12363101.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!