题目: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. Spring AOP(2)

  2. MySQL主备复制原理、实现及异常处理

    复制概述 MySQL支持三种复制方式:基于行(Row)的复制.基于语句(Statement)的复制和混合类型(Mixed)的复制. 基于语句的复制早在3.23版本中就存在,而基于行的复制方式在5.1版 ...

  3. DCU IP Prefether

    DCU IP Prefether 数据高速缓存单元预取I P 设置.如果设置为E n a b l e d,会预取I P 地址以改善网络连接和系统性能,所以建议选择E n a b l ed.选项:E n ...

  4. The revocation function was unable to check revocation for the certificate

    https://stackoverflow.com/questions/45556189/git-the-revocation-function-was-unable-to-check-revocat ...

  5. Java条件语句之 if

    生活中,我们经常需要先做判断,然后才决定是否要做某件事情.例如,如果考试成绩大于 90 分,则奖励一个 IPHONE 5S .对于这种“需要先判断条件,条件满足后才执行的情况”,就可以使用if 条件语 ...

  6. dypedef 和 define

    typedef char *String_t; 和 #define String_dchar * 这两句在使用上有什么区别? 答:typedef char *String_t 定义了一个新的类型别名, ...

  7. 布置weblogic10 64位系统

    1.下载64位JDK并安装 2.java -D64 -jar wls1036_generic.jar(注意下载下来的wls1036_generic.jar文件不要解压,用此命令解压) 3.点击下一步, ...

  8. 八 web爬虫讲解2—urllib库爬虫—ip代理—用户代理和ip代理结合应用

    使用IP代理 ProxyHandler()格式化IP,第一个参数,请求目标可能是http或者https,对应设置build_opener()初始化IPinstall_opener()将代理IP设置成全 ...

  9. [转载]java调用PageOffice生成word

    一.在开发OA办公或与文档相关的Web系统中,难免会遇到动态生成word文档的需求,为了解决工作中遇到导出word文档的需求,前一段时间上网找了一些资料,在word导出这方面有很多工具可以使用,jac ...

  10. 【MAF】MAF插件框架简介

    引言    Managed Add-In Framework是一个插件式框架.它有两大作用,一个是解耦,插件和宿主由7个管道组成,两边都有适配器Adapter管道,能最大程度地降低插件和宿主的耦合度: ...