• 天天爱跑步【NOIP2016 T4】


    天天爱跑酷

      传送门:天天爱跑步 NOIP 2016 T4

    题目大意

      给你一棵树,有 m m m 个玩家,每个玩家在 0 0 0 时刻从 s i s_i si 跑到 t i t_i ti,速度都是每秒一条边。每个节点上有一个观察员,并且第 i i i 个节点上的观察员会在第 w j w_j wj 秒观察这个节点。现在要求每个节点的观察员会观察到多少个玩家。

      一个玩家被观察到当且仅当他在 w i w_i wi 秒时经过节点 i i i

    算法分析

      我们考虑将一个玩家 s → t s \to t st 的路径拆开,变成 s → l c a ( s , t ) → t s \to lca(s, t) \to t slca(s,t)t。分别考虑这两段路径上会不会被观察到。

    1. 首先时 s → l c a ( s , t ) s \to lca(s, t) slca(s,t) 这一段。我们可以很容易想到,如果要在这一段被观察到,就必须满足这样一个式子: d e p s − d e p i = w i dep_s - dep_i = w_i depsdepi=wi,其中 d e p i dep_i depi 表示节点 i i i 在这棵树中的深度。我们稍微移一下项: d e p s = d e p i + w i dep_s = dep_i + w_i deps=depi+wi。我们会发现右边这一坨对于每一个节点是一个定值,所以我们的任务就变成了,对于每一个节点 i i i 找有多少个 s s s 满足这个式子。
    2. 然后 l c a ( s , t ) → t lca(s, t) \to t lca(s,t)t 这一段也是同理。我们令 d i s ( x , y ) = d e p x + d e p y − 2 d e p l c a ( x , y ) dis(x, y) = dep_x + dep_y - 2dep_{lca(x, y)} dis(x,y)=depx+depy2deplca(x,y),那么这一段里面就是满足: d e p i + d i s ( s , t ) − d e p t = w i dep_i + dis(s, t) - dep_t = w_i depi+dis(s,t)dept=wi,移一下项: d e p t − d i s ( s , t ) = d e p i − w i dep_t - dis(s, t) = dep_i - w_i deptdis(s,t)=depiwi。这需要注意因为不保证 d e p i > w i dep_i > w_i depi>wi,所以我们实际操作的时候应该把等式左右两边都加上 n n n 来统计。

      我们以的 s → l c a ( s , t ) s \to lca(s, t) slca(s,t) 为例,我们要找的就是在 i i i 的子树中,有多少节点满足 d e p s = w i + d e p i dep_s = w_i + dep_i deps=wi+depi。那么问题就能转化成对于一个 s s s,我们在从 s → l c a s \to lca slca 的路径上的所有点上全都放上一个类型为 d e p s dep_s deps 的物品,然后对于点 i i i 我们要查找类型为 d e p i + w i dep_i + w_i depi+wi 的物品的个数。这样我们很容易就能想到树上差分。

      具体来说就是每个节点上放一个桶,然后对于每一个路径在桶里面操作差分就好了。最后统计就直接算子树和。但是这样的空间复杂度都有点爆炸,是 O ( n 2 ) O(n^2) O(n2) 的。所以我们考虑优化这个做法。

      既然不能每个点都开一个桶,我们就考虑全局开桶,具体来说,我们对树上的每一个节点开一个 v e c t o r vector vector,存储差分的时候我们进行的修改。然后我们全局开一个桶(其实是两个,因为有两种路径)。然后我们做一遍 d f s dfs dfs

      在 d f s dfs dfs 到点 x x x 的时候我们记录 c n t cnt cnt 表示当前还没有继续进入子树的时候 d e p x + w x dep_x + w_x depx+wx 类型物品的个数,然后我们再处理这个点对于桶的贡献(就是书上差分求和),最后再继续向下 d f s dfs dfs。再一次从子树回到这个点的时候,我们就能统计这个点的答案就是现在的 d e p x + w x dep_x + w_x depx+wx 的个数减去前面我们记录的没有进入子树时的个数。

      完结撒花!!!

    代码

    #include
    using namespace std;
    #define in read()
    #define MAXN 300300
    #define MAXM MAXN << 2
    
    inline int read(){
    	int x = 0; char c = getchar();
    	while(c < '0' or c > '9') c = getchar();
    	while('0' <= c and c <= '9'){
    		x = x * 10 + c - '0'; c = getchar();
    	}
    	return x;
    }
    
    int w[MAXN] = { 0 };
    int n = 0; int m = 0;
    struct Tnode{
    	int opt, val, c;                     // 路径种类, 值, 差分 
    	Tnode(int o = 0, int v = 0, int C = 0) { opt = o, val = v, c = C; }
    };
    vector<Tnode> num[MAXN];
    int tot = 0;
    int first[MAXN] = { 0 };
    int   nxt[MAXM] = { 0 };
    int    to[MAXM] = { 0 }; 
    int   dep[MAXN] = { 0 };
    inline void add(int x, int y){
    	nxt[++tot] = first[x];
    	first[x] = tot; to[tot] = y;
    }
    
    int f[MAXN][50];
    void prework(int x, int fa){
    	dep[x] = dep[fa] + 1;
    	for(int i = 0; i <= 30; i++) f[x][i + 1] = f[f[x][i]][i];
    	for(int e = first[x]; e; e = nxt[e]){
    		int y = to[e];
    		if(y == fa) continue;
    		f[y][0] = x; prework(y, x);
    	}
    }
    
    int Lca(int x, int y){
    	if(dep[x] < dep[y]) swap(x, y);
    	for(int i = 30; i >= 0; i--){
    		if(dep[f[x][i]] >= dep[y]) x = f[x][i];
    		if(x == y) return x;
    	}
    	for(int i = 30; i >= 0; i--)
    		if(f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
    	return f[x][0];
    }
    
    int up[MAXN] = { 0 };                                 // s -> lca(s, t) 的路径 
    int down[MAXN] = { 0 };                               // lca(s, t) -> t
    int ans[MAXN] = { 0 };
    void dfs(int x, int fa){
    	int cnt = up[dep[x] + w[x]] + down[dep[x] - w[x] + n];
    	for(int i = 0; i < num[x].size(); i++){                 // 差分子树和 
    		Tnode t = num[x][i];
    		if(t.opt == 1) up[t.val] += t.c;
    		if(t.opt == 2) down[t.val] += t.c;
    	}
    	for(int e = first[x]; e; e = nxt[e]){
    		int y = to[e];
    		if(y == fa) continue;
    		dfs(y, x);
    	}
    	ans[x] = up[dep[x] + w[x]] + down[dep[x] - w[x] + n] - cnt;
    }
    
    int main(){
    	n = in; m = in;
    	for(int i = 1; i < n; i++){
    		int x = in, y = in;
    		add(x, y), add(y, x);
    	} prework(1, 0);
    	for(int i = 1; i <= n; i++) w[i] = in;
    	for(int i = 1; i <= m; i++){
    		int s = in, t = in;
    		int lca = Lca(s, t), dis = dep[s] + dep[t] - 2 * dep[lca];
    		num[s].push_back(Tnode(1, dep[s], 1));
    		num[lca].push_back(Tnode(1, dep[s], -1));
    		num[t].push_back(Tnode(2, dep[t] - dis + n, 1));
    		num[f[lca][0]].push_back(Tnode(2, dep[t] - dis + n, -1));
    	} dfs(1, 0);
    	for(int i = 1; i <= n; i++) cout << ans[i] << ' '; puts("");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
  • 相关阅读:
    C专家编程学习记录
    翻页视图ViewPager
    快速上手若依代码生成器(2022)
    肺部阴影识别检测 matlab算法技术
    vue 引入zTree
    Win11未识别的网络无internet怎么办?
    【基于element ui的color选择器】基于element ui的color选择器
    建模竞赛-光传送网建模与价值评估
    ARM体系结构与编程(更)
    python+java+SSM+vue勤工助学管理系统#计算机毕业设计
  • 原文地址:https://blog.csdn.net/ID246783/article/details/125904787