题目:https://www.luogu.org/problemnew/show/P1600

看TJ:https://blog.csdn.net/clove_unique/article/details/53427248

树上差分真好。

首先要发现向上和向下的……是定值。然后想到可以差分。

本题的差分略特殊之处在于它的对象是一条连到根的链。把链上的差分值记到非根的端点上。

总之看明白TJ之后感觉真精妙。

而且 从各种中选自己需要的 只需把所有都加进桶中!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=3e5+,M=9e5+;
int n,m,head[N],xnt,w[N],d[N],hd[N],xt,fa[N],tot0,tot1,dfn[N],tim;
int cnt[M],fx=N,ans[N],nw,fatr[N];
struct Edge{
int next,to;Edge(int n=,int t=):next(n),to(t) {}
}edge[N<<];
struct Ed{
int next,to;bool fx;Ed(int n=,int t=,bool f=):next(n),to(t),fx(f) {}
}ed[N<<];
struct Node{
int t,val,s;Node(int t=,int v=,int s=):t(t),val(v),s(s) {}
}a0[N<<],a1[N<<];
void add(int x,int y)
{
edge[++xnt]=Edge(head[x],y);head[x]=xnt;
edge[++xnt]=Edge(head[y],x);head[y]=xnt;
}
void ad(int x,int y)
{
ed[++xt]=Ed(hd[x],y,);hd[x]=xt;
if(x!=y)ed[++xt]=Ed(hd[y],x,);hd[y]=xt;//x!=y!!
}
int find(int a){return fa[a]==a?a:fa[a]=find(fa[a]);}
bool cmp(Node x,Node y){return dfn[x.s]<dfn[y.s];}
void build(int s,int t,int f)
{
a0[++tot0]=Node(,,s);
if(f!=)a0[++tot0]=Node(d[s]-d[f]+,-,fatr[f]);//f!=1 //用fatr
a1[++tot1]=Node(d[s]-d[f]-d[f],-,f);
a1[++tot1]=Node(d[s]-d[f]-d[f],,t);//不要+d[t]-d[f]
}
void dfs(int cr,int f)
{
d[cr]=d[f]+;dfn[cr]=++tim;fatr[cr]=f;
for(int i=hd[cr],v;i;i=ed[i].next)
{
if(dfn[v=ed[i].to])
{
if(ed[i].fx)build(v,cr,find(v));
else build(cr,v,find(v));
}
}
for(int i=head[cr],v;i;i=edge[i].next)
if((v=edge[i].to)!=f)dfs(v,cr),fa[v]=cr;
}
void dfs0(int cr,int f)
{
int pd=d[cr]+w[cr],cpy=cnt[pd];
while(nw<=tot0&&a0[nw].s==cr)cnt[a0[nw].t+d[cr]]+=a0[nw].val,nw++;//+=val
for(int i=head[cr],v;i;i=edge[i].next)
if((v=edge[i].to)!=f)dfs0(v,cr);
ans[cr]+=cnt[pd]-cpy;
}
void dfs1(int cr,int f)
{
int pd=w[cr]-d[cr],cpy=cnt[pd+fx];
while(nw<=tot1&&a1[nw].s==cr)cnt[a1[nw].t+fx]+=a1[nw].val,nw++;
for(int i=head[cr],v;i;i=edge[i].next)
if((v=edge[i].to)!=f)dfs1(v,cr);
ans[cr]+=cnt[pd+fx]-cpy;
}
int main()
{
scanf("%d%d",&n,&m);int x,y;
for(int i=;i<n;i++)
{
scanf("%d%d",&x,&y);add(x,y);fa[i]=i;
}
fa[n]=n;
for(int i=;i<=n;i++)scanf("%d",&w[i]);
for(int i=;i<=m;i++)
{
scanf("%d%d",&x,&y);ad(x,y);
}
d[]=-;dfs(,);sort(a0+,a0+tot0+,cmp);
sort(a1+,a1+tot1+,cmp);
nw=;dfs0(,);memset(cnt,,sizeof cnt);
nw=;dfs1(,);
for(int i=;i<=n;i++)printf("%d ",ans[i]);
return ;
}

然后在洛谷上看到了“文文殿下”的代码。跑得好快!学了一下。

注意到更新cnt值的时候只用和自己节点有关的东西更新。

  所以与其按dfs序排序然后在a[ ]上移动,不如给每个点记一个邻接表,指向a[ ]上的位置。

  感觉人家对邻接表理解深刻。那个nxt是记在 t [ ] 的角标上的,hdhd提供一个指向 t [ ] 某个角标的入口一样的东西。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=3e5+,M=9e5+,Mm=6e5+;
int n,m,head[N],xnt,w[N],d[N],hd[N],xt,fa[N];
int cnt0[Mm],cnt1[M],fx=N,ans[N],fatr[N];
int hdhd0[N][],hdhd1[N][],t[N<<],nxt[N<<],tot;
bool vis[N];
struct Edge{
int next,to;Edge(int n=,int t=):next(n),to(t) {}
}edge[N<<];
struct Ed{
int next,to;bool fx;Ed(int n=,int t=,bool f=):next(n),to(t),fx(f) {}
}ed[N<<];
void add(int x,int y)
{
edge[++xnt]=Edge(head[x],y);head[x]=xnt;
edge[++xnt]=Edge(head[y],x);head[y]=xnt;
}
void ad(int x,int y)
{
ed[++xt]=Ed(hd[x],y,);hd[x]=xt;
if(x!=y)ed[++xt]=Ed(hd[y],x,),hd[y]=xt;//x!=y!!
}
int find(int a){return fa[a]==a?a:fa[a]=find(fa[a]);}
void adad(int &x,int z){t[++tot]=z;nxt[tot]=x;x=tot;}
void build(int s,int t,int f)
{
adad(hdhd0[s][],);if(f!=)adad(hdhd0[fatr[f]][],d[s]-d[f]+);
adad(hdhd1[f][],d[s]-*d[f]);adad(hdhd1[t][],d[s]-*d[f]);
}
void dfs(int cr,int f)
{
d[cr]=d[f]+;vis[cr]=;fatr[cr]=f;
for(int i=hd[cr],v;i;i=ed[i].next)
if(vis[v=ed[i].to])
if(ed[i].fx)build(v,cr,find(v));
else build(cr,v,find(v));
for(int i=head[cr],v;i;i=edge[i].next)
if((v=edge[i].to)!=f)dfs(v,cr),fa[v]=cr;
}
void dfsx(int cr,int f)
{
int pd0=d[cr]+w[cr],pd1=w[cr]-d[cr];
ans[cr]-=cnt0[pd0]+cnt1[pd1+fx];
for(int i=hdhd0[cr][];i;i=nxt[i])cnt0[t[i]+d[cr]]++;
for(int i=hdhd0[cr][];i;i=nxt[i])cnt0[t[i]+d[cr]]--;
for(int i=hdhd1[cr][];i;i=nxt[i])cnt1[t[i]+fx]++;
for(int i=hdhd1[cr][];i;i=nxt[i])cnt1[t[i]+fx]--;
for(int i=head[cr];i;i=edge[i].next)
if(edge[i].to!=f)dfsx(edge[i].to,cr);
ans[cr]+=cnt0[pd0]+cnt1[pd1+fx];
}
int main()
{
scanf("%d%d",&n,&m);int x,y;
for(int i=;i<n;i++)
{
scanf("%d%d",&x,&y);add(x,y);fa[i]=i;
}
fa[n]=n;
for(int i=;i<=n;i++)scanf("%d",&w[i]);
for(int i=;i<=m;i++)
{
scanf("%d%d",&x,&y);ad(x,y);
}
d[]=-;dfs(,);dfsx(,);
for(int i=;i<=n;i++)printf("%d ",ans[i]);
return ;
}

洛谷 1600 (NOIp2016) 天天爱跑步——树上差分的更多相关文章

  1. NOIP2016 天天爱跑步 (树上差分+dfs)

    题目大意:给你一颗树,树上每个点都有一个观察员,他们仅会在 w[i] 时刻出现,观察正在跑步的玩家 一共有m个玩家,他们分别从节点 s[i] 同时出发,以每秒跑一条边的速度,沿着到 t[i] 的唯一路 ...

  2. [NOIP2016]天天爱跑步(树上差分+线段树合并)

    将每个人跑步的路径拆分成x->lca,lca->y两条路径分别考虑: 对于在点i的观察点,这个人(s->t)能被观察到的充要条件为: 1.直向上的路径:w[i]=dep[s]-dep ...

  3. NOIP2016 天天爱跑步 - 树上差分

    传送门 题目分析: 一年前还是个傻子的时候居然直接放弃了这题. 首先列出两个方程:如果i节点的观察员能够观察到由s->t的那个人,那么: \[dep[s] - dep[i] = w[i], de ...

  4. 洛谷$P1600$ 天天爱跑步 树上差分

    正解:树上差分 解题报告: 传送门$QwQ$! 这题还挺妙的,,,我想了半天才会$kk$ 首先对一条链$S-T$,考虑先将它拆成$S-LCA$和$LCA-T$,分别做.因为总体上来说差不多接下来我就只 ...

  5. 洛谷P1600 天天爱跑步——树上差分

    题目:https://www.luogu.org/problemnew/show/P1600 看博客:https://blog.csdn.net/clove_unique/article/detail ...

  6. NOIP2016 Day1 T2 天天爱跑步(树上差分,LCA)

    原文链接 原题链接 题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.<天天爱跑步>是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个游戏 ...

  7. 洛谷P2680 运输计划(倍增LCA + 树上差分 + 二分答案)

    [题目链接] [思路]: 根据题意可以明显看出,当所有任务都完成时的时间是最终的结果,也就是说本题要求,求出最小的最大值. 那这样的话就暗示了将答案二分,进行check. [check方法]: 如果说 ...

  8. 洛谷P2680 运输计划 [LCA,树上差分,二分答案]

    题目传送门 运输计划 Description 公元 2044 年,人类进入了宇宙纪元.L 国有 n 个星球,还有 n?1 条双向航道,每条航道建立在两个星球之间, 这 n?1 条航道连通了 L 国的所 ...

  9. 【LG1600】[NOIP2016]天天爱跑步

    [LG1600][NOIP2016]天天爱跑步 题面 洛谷 题解 考虑一条路径\(S\rightarrow T\)是如何给一个观测点\(x\)造成贡献的, 一种是从\(x\)的子树内出来,另外一种是从 ...

随机推荐

  1. eclipse显示结果窗口字体大小

    设置前的字体大小 设置后的字体大小 步骤

  2. Binder机制-简单用法(一)

    Binder算是android里面比较难懂的部分了,但是非常重要,基本上,当我们深入到进程交互的阶段,Binder都是一个绕不开的槛,所以我也希望帮助大家更浅显地了解到这个知识点.笔者想通过3篇博文简 ...

  3. cmake的使用方法

    4. CMakeLists.txt剖析4.1 cmake_minimum_required命令 cmake_minimum_required (VERSION 2.6) 规定cmake程序的最低版本. ...

  4. java-ConcurrentLinkedQueue 简单使用

    import java.util.concurrent.ConcurrentLinkedQueue; public class CacheTest { /** * * offer(E e) 将指定元素 ...

  5. python学习笔记(unittest)

    刚刚放假回来我想很多人都还没有缓过来吧 这次介绍一个python自带的测试框架 unitest #!/usr/bin/env python # -*- coding: utf_8 -*- import ...

  6. RabbitMQ 资料整理

    前言: 官方教程: https://www.rabbitmq.com/getstarted.html 应用场景(之马云赚钱): http://blog.csdn.net/whoamiyang/arti ...

  7. grunt,gulp,webpack前端打包工具的特性

    1.http://www.cnblogs.com/lovesong/p/6413546.html (gulp与webpack的区别) 2.http://blog.csdn.net/qq_3231263 ...

  8. vim编辑16进制

    你可以在vim中可以把文件转换为16进制来显示: :%!xxd 解释:把所有的行(%)用本地(!)的xxd程序打开. xxd本是linux下一个显示.编辑.转换二进制的命令. 返回正常显示: :%!x ...

  9. LeetCode OJ:Insertion Sort List (插入排序链表)

    Sort a linked list using insertion sort. 用插入排序来排序一个list,额, 我写的好麻烦啊, debug了好久,至少提交了5次...写吐了快,先贴代码,写的也 ...

  10. LeetCode OJ:Perfect Squares(完美平方)

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...