【NOIP2016】天天爱跑步(树上差分)
题意:
思路:From http://blog.csdn.net/doyouseeman/article/details/53385565
我们思考一下从x到y的路径,
这个可以拆成从x到lca的路径和从lca到y的路径,这个很明显。
如果一个点i在从x到lca 的路径可以检测到的话,
那么就有deep[i]+w[i]=deep[x]。
如果一个点i在从lca到y的路径上可以检测到的话,
那么就有deep[i]-w[i]=deep[y]-t(t表示x到y的路径长度)。
那么用树链剖分的方法很容易,但是很慢。有一个用桶的方法,跑得很快。
维护两个桶,一个向上的桶a和一个向下的桶b。
从x到y的一个路径,在x中a[deep[x]]加一个,当dfs把lca退栈的时候,x的影响就没有了,那么把a[deep[x]]减掉。
在lca那里需要把一个b[deep[y]]加进来,在y出栈后,就把b[deep[y]]减掉。
每次ans[x]的答案就是子树a[deep[x]+w[x]]+b[deep[x]-w[x]]的数量。
但是如果是一条链的情况,那么这样会算重,所以还要减去重复的数量。
var shang,xia:array[-..]of longint;
head,head1,head2,head3:array[..]of longint;
vet,vet1,vet2,vet3,
next,next1,next2,next3,ans,dep,a,s:array[..]of longint;
f:array[..,..]of longint;
n,m,i,x,y,q,b,tot,tot1,tot2,tot3,t:longint; procedure add(a,b:longint);
begin
inc(tot);
next[tot]:=head[a];
vet[tot]:=b;
head[a]:=tot;
end; procedure add1(a,b:longint);
begin
inc(tot1);
next1[tot1]:=head1[a];
vet1[tot1]:=b;
head1[a]:=tot1;
end; procedure add2(a,b:longint);
begin
inc(tot2);
next2[tot2]:=head2[a];
vet2[tot2]:=b;
head2[a]:=tot2;
end; procedure add3(a,b:longint);
begin
inc(tot3);
next3[tot3]:=head3[a];
vet3[tot3]:=b;
head3[a]:=tot3;
end; procedure dfs(u,fa:longint);
var e,v,i:longint;
begin
for i:= to do
begin
if dep[u]<(<<i) then break;
f[u,i]:=f[f[u,i-],i-];
end;
e:=head[u];
while e<> do
begin
v:=vet[e];
if v<>fa then
begin
f[v,]:=u;
dep[v]:=dep[u]+;
dfs(v,u);
end;
e:=next[e];
end;
end; procedure swap(var x,y:longint);
var t:longint;
begin
t:=x; x:=y; y:=t;
end; function lca(x,y:longint):longint;
var i,d:longint;
begin
if dep[x]<dep[y] then swap(x,y);
d:=dep[x]-dep[y];
for i:= to do
if d and (<<i)> then x:=f[x,i];
for i:= downto do
if f[x,i]<>f[y,i] then
begin
x:=f[x,i]; y:=f[y,i];
end;
if x=y then exit(x);
exit(f[x,]);
end; procedure dfs1(u,fa:longint);
var e,v,x,y:longint;
begin
x:=xia[dep[u]+a[u]];
y:=shang[dep[u]-a[u]];
xia[dep[u]]:=xia[dep[u]]+s[u];
e:=head1[u];
while e<> do
begin
v:=vet1[e];
inc(shang[v]);
e:=next1[e];
end;
e:=head[u];
while e<> do
begin
v:=vet[e];
if v<>fa then dfs1(v,u);
e:=next[e];
end;
ans[u]:=xia[dep[u]+a[u]]+shang[dep[u]-a[u]]-x-y;
e:=head2[u];
while e<> do
begin
v:=vet2[e];
dec(xia[v]);
if v=dep[u]+a[u] then dec(ans[u]);
e:=next2[e];
end;
e:=head3[u];
while e<> do
begin
v:=vet3[e];
dec(shang[v]);
e:=next3[e];
end;
end; begin
assign(input,'bzoj4719.in'); reset(input);
assign(output,'bzoj4719.out'); rewrite(output);
readln(n,m);
for i:= to n- do
begin
readln(x,y);
add(x,y); add(y,x);
end;
dfs(,);
for i:= to n do read(a[i]);
for i:= to m do
begin
readln(x,y);
q:=lca(x,y);
t:=dep[x]+dep[y]-*dep[q];
inc(s[x]); b:=dep[y]-t;
add1(y,b);
add2(q,dep[x]);
add3(q,b);
end;
dfs1(,);
for i:= to n- do write(ans[i],' ');
write(ans[n]);
close(input);
close(output);
end.
【NOIP2016】天天爱跑步(树上差分)的更多相关文章
- 洛谷 1600 (NOIp2016) 天天爱跑步——树上差分
题目:https://www.luogu.org/problemnew/show/P1600 看TJ:https://blog.csdn.net/clove_unique/article/detail ...
- NOIP2016 天天爱跑步 (树上差分+dfs)
题目大意:给你一颗树,树上每个点都有一个观察员,他们仅会在 w[i] 时刻出现,观察正在跑步的玩家 一共有m个玩家,他们分别从节点 s[i] 同时出发,以每秒跑一条边的速度,沿着到 t[i] 的唯一路 ...
- [NOIP2016]天天爱跑步(树上差分+线段树合并)
将每个人跑步的路径拆分成x->lca,lca->y两条路径分别考虑: 对于在点i的观察点,这个人(s->t)能被观察到的充要条件为: 1.直向上的路径:w[i]=dep[s]-dep ...
- NOIP2016 天天爱跑步 - 树上差分
传送门 题目分析: 一年前还是个傻子的时候居然直接放弃了这题. 首先列出两个方程:如果i节点的观察员能够观察到由s->t的那个人,那么: \[dep[s] - dep[i] = w[i], de ...
- NOIP2016 Day1 T2 天天爱跑步(树上差分,LCA)
原文链接 原题链接 题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.<天天爱跑步>是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个游戏 ...
- 洛谷P1600 天天爱跑步——树上差分
题目:https://www.luogu.org/problemnew/show/P1600 看博客:https://blog.csdn.net/clove_unique/article/detail ...
- 洛谷$P1600$ 天天爱跑步 树上差分
正解:树上差分 解题报告: 传送门$QwQ$! 这题还挺妙的,,,我想了半天才会$kk$ 首先对一条链$S-T$,考虑先将它拆成$S-LCA$和$LCA-T$,分别做.因为总体上来说差不多接下来我就只 ...
- BZOJ 4719--天天爱跑步(LCA&差分)
4719: [Noip2016]天天爱跑步 Time Limit: 40 Sec Memory Limit: 512 MBSubmit: 1464 Solved: 490[Submit][Stat ...
- [NOIp2016]天天爱跑步 线段树合并
[NOIp2016]天天爱跑步 LG传送门 作为一道被毒瘤出题人们玩坏了的NOIp经典题,我们先不看毒瘤的"动态爱跑步"和"天天爱仙人掌",回归一下本来的味道. ...
- [Noip2016]天天爱跑步 LCA+DFS
[Noip2016]天天爱跑步 Description 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.?天天爱跑步?是一个养成类游戏,需要玩家每天按时上线,完成打卡任 ...
随机推荐
- 172 Factorial Trailing Zeroes 阶乘后的零
给定一个整数 n,返回 n! 结果尾数中零的数量.注意: 你的解决方案应为对数时间复杂度. 详见:https://leetcode.com/problems/factorial-trailing-ze ...
- T4870 水灾(sliker.cpp/c/pas) 1000MS 64MB
题目描述 大雨应经下了几天雨,却还是没有停的样子.土豪CCY刚从外地赚完1e元回来,知道不久除了自己别墅,其他的地方都将会被洪水淹没. CCY所在的城市可以用一个N*M(N,M<=50)的地图表 ...
- 在Windows7下编译调试C#程序
要在 命令行下编译C#代码,要配置一下 1.在环境变量下新建一个变量 参数名: csc 参数值:C:\Windows\Microsoft.NET\Framework\v4.0.30319 2.在系统变 ...
- 详解Android Activity生命周期
转载注明来自: http://www.cnblogs.com/wujiancheng/ 一.正常情况下Activity的生命周期: Activity的生命周期大概可以归为三部分 整个的生命周期:o ...
- vue里面的Mixins(混合)
Mixins一般有两种用途:1.在你已经写好了构造器后,需要增加方法或者临时的活动时使用的方法,这时用混入会减少源代码的污染.2.很多地方都会用到的公用方法,用混入的方法可以减少代码量,实现代码重用. ...
- AIX6.1平台11.2.0.3RAC 实施手册
1 前言 此文档详细描述了Oracle 11gR2 数据库在AIX6.1上的安装RAC的检查及安装步骤.文档中#表示root用户执行,$表示grid或oracle用户执行. 2 系统环境 操作系统环境 ...
- vue全选和取消全选
代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- 【软件构造】(转)Java中的comparable和comparator
为了方便阅读和复习,转载至此,原地址:温布利往事的博客 阅读目录 一.Comparable简介 二.Comparator简介 三.Comparable和Comparator区别比较 回到顶部 一.Co ...
- 2.10.4 aside元素
aside元素 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> < ...
- python compare with other language
java http://dirtsimple.org/2004/12/python-is-not-java.htmlhttp://twistedmatrix.com/users/glyph/rant/ ...