zoukankan      html  css  js  c++  java
  • 「NOIP2016」天天爱跑步

    传送门
    Luogu

    解题思路

    树上差分+桶计数。
    我们发现在一条路径上的点 (i) ,它可以观测到玩家的条件是:

    • (i in (u o LCA),dep_u=w_i+dep_i)
    • (i in (LCA o v),dis(u,v)-dep_v=w_i-dep_i)

    所以我们就可以树上差分加桶计数实现,每次的答案就是对应桶的变化量。

    细节注意事项

    • 为了防止数组下标出现负数,可以统一加上一个数

    参考代码

    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <cctype>
    #include <cmath>
    #include <ctime>
    #include <vector>
    #define rg register
    using namespace std;
    template < typename T > inline void read(T& s) {
    	s = 0; int f = 0; char c = getchar();
    	while (!isdigit(c)) f |= (c == '-'), c = getchar();
    	while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
    	s = f ? -s : s;
    }
    
    const int _ = 300002;
    
    int tot, head[_], nxt[_ << 1], ver[_ << 1];
    inline void Add_edge(int u, int v)
    { nxt[++tot] = head[u], head[u] = tot, ver[tot] = v; }
    
    int n, m, w[_], ans[_], tong[2][_ * 3];
    int dep[_], fa[21][_];
    struct node { int f, p, v; } ;
    vector < node > vec[_];
    
    inline void dfs(int u, int f) {
    	dep[u] = dep[f] + 1, fa[0][u] = f;
    	for (rg int i = 1; i <= 19; ++i)
    		fa[i][u] = fa[i - 1][fa[i - 1][u]];
    	for (rg int i = head[u]; i; i = nxt[i]) {
    		int v = ver[i]; if (v == f) continue;
    		dfs(v, u);
    	}
    }
    
    inline int LCA(int x, int y) {
    	if (dep[x] < dep[y]) swap(x, y);
    	for (rg int i = 19; ~i; --i)
    		if (dep[fa[i][x]] >= dep[y]) x = fa[i][x];
    	if (x == y) return x;
    	for (rg int i = 19; ~i; --i)
    		if (fa[i][x] != fa[i][y]) x = fa[i][x], y = fa[i][y];
    	return fa[0][x];
    }
    
    inline void diff(int s, int t) {
    	int lca = LCA(s, t), dis = dep[s] + dep[t] - 2 * dep[lca];
    	vec[s].push_back((node) { 0, dep[s] + _, 1 });
    	vec[lca].push_back((node) { 0, dep[s] + _, -1});
    	vec[t].push_back((node) { 1, dis - dep[t] + _, 1 });
    	vec[fa[0][lca]].push_back((node) { 1, dis - dep[t] + _, -1 });
    }
    
    inline void dfss(int u, int f) {
    	int tmp = tong[0][w[u] + dep[u] + _] + tong[1][w[u] - dep[u] + _];
    	for (rg int i = 0; i < (int) vec[u].size(); ++i)
    		tong[vec[u][i].f][vec[u][i].p] += vec[u][i].v;
    	for (rg int i = head[u]; i; i = nxt[i]) {
    		int v = ver[i]; if (v == f) continue;
    		dfss(v, u);
    	}
    	ans[u] = tong[0][w[u] + dep[u] + _] + tong[1][w[u] - dep[u] + _] - tmp;
    }
    
    int main() {
    #ifndef ONLINE_JUDGE
    	freopen("in.in", "r", stdin);
    	freopen("out.out", "w", stdout);
    #endif
    	read(n), read(m);
    	for (rg int u, v, i = 1; i < n; ++i)
    		read(u), read(v), Add_edge(u, v), Add_edge(v, u);
    	for (rg int i = 1; i <= n; ++i) read(w[i]);
    	dfs(1, 0);
    	for (rg int s, t, i = 1; i <= m; ++i) read(s), read(t), diff(s, t);
    	dfss(1, 0);
    	for (rg int i = 1; i <= n; ++i) printf("%d%c", ans[i], " 
    "[i == n]);
    	return 0;
    }
    

    完结撒花 (qwq)

  • 相关阅读:
    家庭记账本_2
    家庭记账本_1
    安卓学习进度_25
    安卓软件学习进度_24
    对体温上报app的总结
    安卓软件学习进度_23
    安卓软件学习进度_22
    安卓开发
    安卓开发
    安卓开发
  • 原文地址:https://www.cnblogs.com/zsbzsb/p/11796704.html
Copyright © 2011-2022 走看看