树链剖分https://www.luogu.org/problemnew/show/P3384

概念

树链剖分,是一种将树剖分成多条不相交的链的算法,并通过其他的数据结构来维护这些链上的信息。

最简单的例子就是\(LCA\),假设现在有一棵退化成链的树。如果要求任意两点的\(LCA\),因为他们在同一条链上的缘故,只需要判断一下两者的深度就行了。由此可见,在链上是比在树上更好操作的。

那么该怎么将一棵树剖分开来捏?

先搬出一堆概念:

重儿子
在以X节点为根的子树中,节点数最多的子树的根节点,即是X节点的重儿子。
重边
连接X节点与X节点的重儿子的边,我们叫他重边。
重链
一堆重边连起来的链。
轻链
一堆非重边连起来的链。

对于每个节点,找出其重儿子,就可以剖分成一条条重链与轻链。

实现

数组定义

\(val[N]\)每个节点的初值

\(size[N]\)每个节点子树的大小

\(son[N]\)每个节点的重儿子

\(fa[N]\)每个节点的父亲

\(dfn[N]\)每个节点在线段树上的编号

\(rk[N]\)线段树上节点在树中的编号

\(dep[N]\)节点深度

\(top[N]\)每个点所在链的链顶

\(dfs\)

第一遍\(dfs\)处理出\(size[],fa[],son[],dep[]\)

第二遍\(dfs\)处理出\(top[],dfn[],rk[]\)

线段树

用线段树维护树链,并实现链上的操作,常见操作如下:

将树从\(X\)到\(Y\)结点最短路径上所有节点的值都加上\(Z\)

求树从\(X\)到\(Y\)结点最短路径上所有节点的值之和

将以\(X\)为根节点的子树内所有节点值都加上\(Z\)

求以\(X\)为根节点的子树内所有节点值之和

对于子树操作:我们知道一颗子树内的编号一定是连续的,那么以\(X\)节点为根的子树的区间就是\((dfn[x],dfn[x]+size[x]-1)\)

#define RG register
#include<cstdio>
#include<iostream>
using namespace std;
const int N=1e5+5;
inline int read()
{
RG int x=0,w=1;RG char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*w;
}
int n,m,r,p,cnt,ct;
int lazy[N<<2],sum[N<<2];
int val[N],size[N],son[N],fa[N],dfn[N],rk[N],last[N],dep[N],top[N];
struct edge{
int to,next,w;
}e[N<<1];
void insert(int u,int v)
{
e[++cnt]=(edge){v,last[u]};last[u]=cnt;
e[++cnt]=(edge){u,last[v]};last[v]=cnt;
}
inline void dfs1(int now)
{
size[now]=1;
for(int i=last[now];i;i=e[i].next)
{
int v=e[i].to;
if(v==fa[now])continue;
fa[v]=now;
dep[v]=dep[now]+1;
dfs1(v);
size[now]+=size[v];
if(size[v]>size[son[now]])son[now]=v;
}
}
inline void dfs2(int now,int Top)
{
top[now]=Top;dfn[now]=++ct;rk[ct]=now;
if(son[now])dfs2(son[now],Top);//重儿子
for(int i=last[now];i;i=e[i].next)
{
int v=e[i].to;
if(v==son[now]||v==fa[now])continue;
dfs2(v,v);//轻儿子的top就是本身
}
}
inline void Pushup(int root){sum[root]=(sum[root<<1]+sum[root<<1|1])%p;}
void Build(int root,int l,int r)//建树
{
if(l==r){sum[root]=val[rk[l]];return;}
int mid=(l+r)>>1;
Build(root<<1,l,mid);
Build(root<<1|1,mid+1,r);
Pushup(root);
}
inline void Pushdown(int root,int len)//下放懒标记,len为区间长度
{
if(!lazy[root])return;
lazy[root<<1]=(lazy[root<<1]+lazy[root])%p;
lazy[root<<1|1]=(lazy[root<<1|1]+lazy[root])%p;
sum[root<<1]=(sum[root<<1]+lazy[root]*(len-(len>>1))%p)%p;
sum[root<<1|1]=(sum[root<<1|1]+lazy[root]*(len>>1)%p)%p;
lazy[root]=0;
}
void Modify(int root,int l,int r,int ll,int rr,int k)//区间修改
{
Pushdown(root,r-l+1);
if(ll<=l&&r<=rr)
{
lazy[root]=(lazy[root]+k)%p;
sum[root]=(sum[root]+k*(r-l+1)%p)%p;
return;
}
int mid=(l+r)>>1;
if(ll<=mid)Modify(root<<1,l,mid,ll,rr,k);
if(rr>mid)Modify(root<<1|1,mid+1,r,ll,rr,k);
Pushup(root);
}
int Query(int root,int l,int r,int ll,int rr)//区间查询
{
Pushdown(root,r-l+1);
if(ll<=l&&r<=rr)return sum[root];
int mid=(l+r)>>1,Sum=0;
if(ll<=mid)Sum=(Sum+Query(root<<1,l,mid,ll,rr))%p;
if(mid<rr)Sum=(Sum+Query(root<<1|1,mid+1,r,ll,rr))%p;
Pushup(root);
return Sum;
}
inline void Modify_Tree(int x,int y,int k)//修改节点x到节点y路径上点的点权
{
while(top[x]!=top[y])//不在同一条链上
{
if(dep[top[x]]<dep[top[y]])swap(x,y);//注意是比较链顶的深度
Modify(1,1,n,dfn[top[x]],dfn[x],k);//链顶更深的节点跳链
x=fa[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
Modify(1,1,n,dfn[x],dfn[y],k);//同一条链上,直接区间查询
}
inline int Query_Tree(int x,int y)//查询节点x到节点y路径上点权和
{
int Sum=0;
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]])swap(x,y);
Sum=(Sum+Query(1,1,n,dfn[top[x]],dfn[x]))%p;
x=fa[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
return (Sum+Query(1,1,n,dfn[x],dfn[y]))%p;
}
inline void Modify_Son(int x,int k)//子树修改
{
Modify(1,1,n,dfn[x],dfn[x]+size[x]-1,k);
}
inline int Query_Son(int x)
{
return Query(1,1,n,dfn[x],dfn[x]+size[x]-1);//子树查询
}
int main()
{
n=read(),m=read(),r=read(),p=read();
for(int i=1;i<=n;i++)val[i]=read()%p;
for(int i=1;i<n;i++)insert(read(),read());
dep[r]=1;
dfs1(r);
dfs2(r,r);
Build(1,1,n);
while(m--)
{
int f=read();
if(f==1){int x=read(),y=read(),z=read()%p;Modify_Tree(x,y,z);}
if(f==2)printf("%d\n",Query_Tree(read(),read()));
if(f==3){int x=read(),y=read()%p;Modify_Son(x,y);}
if(f==4)printf("%d\n",Query_Son(read()));
}
return 0;
}

[note]树链剖分的更多相关文章

  1. CF 191C Fools and Roads lca 或者 树链剖分

    They say that Berland has exactly two problems, fools and roads. Besides, Berland has n cities, popu ...

  2. CodeForces 916E Jamie and Tree(树链剖分+LCA)

    To your surprise, Jamie is the final boss! Ehehehe. Jamie has given you a tree with n vertices, numb ...

  3. Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组

    Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...

  4. Housewife Wind(边权树链剖分)

    Housewife Wind http://poj.org/problem?id=2763 Time Limit: 4000MS   Memory Limit: 65536K Total Submis ...

  5. HDU3710 Battle over Cities(最小生成树+树链剖分+倍增+线段树)

    Battle over Cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  6. D. Happy Tree Party CodeForces 593D【树链剖分,树边权转点权】

    Codeforces Round #329 (Div. 2) D. Happy Tree Party time limit per test 3 seconds memory limit per te ...

  7. BZOJ3531 SDOI2014 旅行 - 树链剖分,主席树

    题意:给定一棵树,树上每个点有权值和类型.支持:修改某个点的类型:修改某个点的权值:询问某条链上某个类型的点的和/最大值.点数/类型数/询问数<=100000. 分析: 树链剖分,对每个类型的点 ...

  8. BZOJ 3626: [LNOI2014]LCA [树链剖分 离线|主席树]

    3626: [LNOI2014]LCA Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2050  Solved: 817[Submit][Status ...

  9. BZOJ 1984: 月下“毛景树” [树链剖分 边权]

    1984: 月下“毛景树” Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 1728  Solved: 531[Submit][Status][Discu ...

随机推荐

  1. hdu 5012 bfs --- 慎用STL 比方MAP判重

    http://acm.hdu.edu.cn/showproblem.php?pid=5012 发现一个问题 假设Sting s = '1'+'2'+'3'; s!="123"!!! ...

  2. 使用Fiddler作为简单的mockserver

    转载:  http://blog.csdn.net/xt0916020331/article/details/66544526 开发中经常遇到调试过程中对接系统接口无法联调或者后台未开发完成等情况.这 ...

  3. 11. 配置ContextPath【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/51637036 spring boot默认是/ ,这样直接通过http://ip:port/ ...

  4. 关于angularjs dom渲染结束再执行的问题

    情景 当我点击了button, div才能显示.并且我想知道这个div的高度. 问题 当我点击这个button以后这个.chrome就然告诉我这个div高度是0.这不是睁着眼睛说瞎话吗?怎么能如此欺骗 ...

  5. 系统封装 EasyBoot如何将WIN7安装版提取到光盘

    1 将WIN7光盘中的文件提取到Easyboot根目录,注意不要autorun.inf和setup.exe这两个文件.我们这里的Easyboot已经有了一些其他东西(XP的安装版文件,PE的文件等等, ...

  6. Visual Studio 外请版本号管理插件 - AnkhSVN

    Visual Studio 外请版本号管理插件 - AnkhSVN 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致 ...

  7. 使用Maven整合SSH总结

    本人自己进行的SSH整合,中间遇到不少问题,特此做些总结,仅供参考. 项目环境: struts-2.3.31 + spring-4.3.7 + hibernate-4.2.21 + maven-3.3 ...

  8. zabbix监控sockets连接数

    配置zabbix客户端配置文件 vim /etc/zabbix/zabbix_agentd.conf 添加  Include=/etc/zabbix/zabbix_agentd.d/ 添加脚本对soc ...

  9. 会话管理之session技术

    上一节我们总结了cookie技术,这节主要总结一下session技术. 1. session对象 在web开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占 ...

  10. Python---copy()、deepcopy()与赋值的区别

    copy()与deepcopy()之间的主要区别是python对数据的存储方式. 首先直接上结论: —–深复制,即将被复制对象完全再复制一遍作为独立的新个体单独存在.所以改变原有被复制对象不会对已经复 ...