树链剖分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. 编译安装Apache httpd和php搭建KodExplorer网盘

    编译安装Apache httpd和php搭建KodExplorer网盘 环境说明: 系统版本    CentOS 6.9 x86_64 软件版本    httpd-2.2.31        php- ...

  2. fabricjs line

    let line1 = new fabric.Line([lineleft, lineheight, lineleft, 0], {//终止位置,线长,起始位置,top,这里是从项目中截下来的我用了变 ...

  3. 在html页面中直接嵌入图片数据

    一般情况,通常是在html页面中应用图片的链接,如: <img src="http://baidu.com/logo.gif">   但是,这样的前提是我们需要将图片先 ...

  4. Unity3D的三种坐标系

    来自:http://blog.csdn.net/luxiaoyu_sdc/article/details/13168497 1, World Space(世界坐标): 我们在场景中添加物体(如:Cub ...

  5. InnoDB事务和锁

    InnoDB支持事务,MyISAM不支持事务. 一.事务的基本特性 ACID特性 1.原子性(Atomicity):事务是一个原子操作单元,其对数据的修改,要么全都执行,要么全都不执行. 2.一致性( ...

  6. TCP/IP 网络编程(五)

    优于 select 的 epoll (I/O 复用) select 速度慢的原因 调用select后针对全部文件描写叙述符的循环 每次调用函数时都须要向该函数传递监视对象信息 select并非把发生变 ...

  7. URL相对路径和URL绝对路径

    经常在页面中引用图片,html页面等,自己常常弄错相对路径和绝对路径,今天写下此文总结一下.    直接举例说明吧. 在 D:\例子\html下有这么几个文件和文件夹     1.若引用的资源和本身在 ...

  8. Mysql 常用函数汇总

    转自:http://blog.csdn.net/qq_27416209/article/details/52020720 一.数学函数ABS(x)   返回x的绝对值BIN(x)   返回x的二进制( ...

  9. SQL Server中LIKE和PATINDEX的用法

    在SQL Server中,能使用通配符的只有2个:LIKE.PATINDEX. 不过LIKE支持2种通配符转义,无限制最全面:而PATINDEX只支持最简单的通配符转义([]转义),限制较多. LIK ...

  10. 关于浮点数的json解析

    近期在工作中遇到个问题 通过post请求从其他系统(好像是C#写的)获得json字符串 {"geometry":{"rings":[[[40426489.331 ...