<更新提示>

<第一次更新>


<正文>

LCA

Description

给出一个n个节点的有根树(编号为0到n-1,根节点为0)。一个点的深度定义为这个节点到根 的距离+1。

设dep[i]表示点i的深度,LCA(i,j)表示i与j的最近公共祖先。 有q次询问,每次询问给出l r z,求sigma_{l<=i<=r}dep[LCA(i,z)]。 (即,求在[l,r]区间内的每个节点i与z的最近公共祖先的深度之和)

Input Format

第一行2个整数n q。 接下来n-1行,分别表示点1到点n-1的父节点编号。 接下来q行,每行3个整数l r z。

Output Format

输出q行,每行表示一个询问的答案。每个答案对201314取模输出

Sample Input

5 2
0
0
1
1
1 4 3
1 4 2

Sample Output

8
5

解析

容易发现一个询问可以拆成两个询问:

\[\sum_{i=l}^rdep[LCA(i,z)]=\sum_{i=1}^rdep[LCA(i,z)]-\sum_{i=1}^{l-1}dep[LCA(i,z)]
\]

都是前缀和形式的,方便我们处理。

对于一个前缀和询问,我们可以发现它的贡献具有一个实际意义:也就是求每一个\(i\)与\(z\)公共祖先的个数。进一步,我们可以这样理解:把每一个节点\(i\)到根的路径上的点点权都加一,求\(z\)到更路径上的点权和。

我们都知道直接树上加链和查询链可以用树链剖分,但是各个询问之间好像相互有影响。

最简单的解决方法就是把询问离线。我们之前已经把询问转化为前缀和的形式了,那就把所有询问按照\(r\)排序,这样依次处理询问就没有影响了。

\(Code:\)

#include <bits/stdc++.h>
using namespace std;
const int N = 50020 , Mod = 201314;
struct node { int l,r,tag; long long cnt; };
struct SegmentTree
{
node ver[N<<2];
#define l(p) ver[p].l
#define r(p) ver[p].r
#define cnt(p) ver[p].cnt
#define tag(p) ver[p].tag
inline void build(int p,int l,int r)
{
l(p) = l , r(p) = r , cnt(p) = tag(p) = 0;
if ( l == r ) return;
int mid = l + r >> 1;
build( p<<1 , l , mid );
build( p<<1|1 , mid+1 , r );
}
inline void update(int p) { cnt(p) = cnt(p<<1) + cnt(p<<1|1); }
inline void spread(int p)
{
if ( tag(p) )
{
tag(p<<1) = ( tag(p<<1) + tag(p) ) % Mod;
tag(p<<1|1) = ( tag(p<<1|1) + tag(p) ) % Mod;
cnt(p<<1) = ( cnt(p<<1) + 1LL * ( r(p<<1) - l(p<<1) + 1 ) * tag(p) % Mod ) % Mod;
cnt(p<<1|1) = ( cnt(p<<1|1) + 1LL * ( r(p<<1|1) - l(p<<1|1) + 1 ) * tag(p) % Mod ) % Mod;
tag(p) = 0;
}
}
inline void modify(int p,int l,int r,int v)
{
if ( l <= l(p) && r >= r(p) )
{
cnt(p) = ( cnt(p) + ( r(p) - l(p) + 1 ) * v % Mod ) % Mod;
tag(p) = ( tag(p) + v ) % Mod; return;
}
spread( p );
int mid = l(p) + r(p) >> 1;
if ( l <= mid ) modify( p<<1 , l , r , v );
if ( r > mid ) modify( p<<1|1 , l , r , v );
update( p );
}
inline int query(int p,int l,int r)
{
if ( l <= l(p) && r >= r(p) ) return cnt(p);
spread( p );
int mid = l(p) + r(p) >> 1 , res = 0;
if ( l <= mid ) res = ( res + query( p<<1 , l , r ) ) % Mod;
if ( r > mid ) res = ( res + query( p<<1|1 , l , r ) ) % Mod;
return res;
}
};
SegmentTree Tree;
struct edge { int ver,next; } e[N*2];
struct query { int r,z,id,f; } a[N*2];
int n,m,t,Head[N],tot;
int fa[N],dep[N],son[N],size[N],top[N],id[N],cnt;
long long ans[N];
inline void insert(int x,int y)
{
e[++t] = (edge){y,Head[x]} , Head[x] = t;
e[++t] = (edge){x,Head[y]} , Head[y] = t;
}
inline int read(void)
{
int x = 0 , w = 0; char ch = ' ';
while ( !isdigit(ch) ) w |= ch=='-' , ch = getchar();
while ( isdigit(ch) ) x = x*10 + ch-48 , ch = getchar();
return w ? -x : x;
}
inline void input(void)
{
n = read() , m = read();
for ( int i = 2 ; i <= n ; i++ )
insert( i , read() + 1 );
for ( int i = 1 ; i <= m ; i++ )
{
int l = read() + 1 , r = read() + 1 , z = read() + 1;
a[++tot] = (query){ r , z , i , 1 };
a[++tot] = (query){ l-1 , z , i , -1 };
}
}
inline void dfs1(int x,int f,int depth)
{
fa[x] = f , dep[x] = depth , size[x] = 1;
int Max = -1;
for ( int i = Head[x] ; i ; i = e[i].next )
{
int y = e[i].ver;
if ( y == f ) continue;
dfs1( y , x , depth+1 );
size[x] += size[y];
if ( size[y] > Max ) Max = size[y] , son[x] = y;
}
}
inline void dfs2(int x,int Top)
{
id[x] = ++cnt , top[x] = Top;
if ( !son[x] ) return;
else dfs2( son[x] , Top );
for ( int i = Head[x] ; i ; i = e[i].next )
{
int y = e[i].ver;
if ( y == fa[x] || y == son[x] ) continue;
dfs2( y , y );
}
}
inline void modify_chain(int x,int y,int val)
{
while ( top[x] ^ top[y] )
{
if ( dep[top[x]] < dep[top[y]] ) swap( x , y );
Tree.modify( 1 , id[top[x]] , id[x] , val );
x = fa[top[x]];
}
if ( dep[x] > dep[y] ) swap( x , y );
Tree.modify( 1 , id[x] , id[y] , val );
}
inline int query_chain(int x,int y)
{
int res = 0;
while ( top[x] ^ top[y] )
{
if ( dep[top[x]] < dep[top[y]] ) swap( x , y );
res = ( res + Tree.query( 1 , id[top[x]] , id[x] ) ) % Mod;
x = fa[top[x]];
}
if ( dep[x] > dep[y] ) swap( x , y );
return ( res + Tree.query( 1 , id[x] , id[y] ) ) % Mod;
}
inline bool compare(query p1,query p2) { return p1.r < p2.r; }
inline void solve(void)
{
int p = 0;
for ( int i = 1 ; i <= tot ; i++ )
{
while ( p < a[i].r ) modify_chain( 1 , ++p , 1 );
ans[ a[i].id ] += a[i].f * query_chain( 1 , a[i].z );
ans[ a[i].id ] = ( ans[ a[i].id ] % Mod + Mod ) % Mod;
}
}
int main(void)
{
input();
dfs1( 1 , 0 , 1 );
dfs2( 1 , 1 );
Tree.build( 1 , 1 , n );
sort( a+1 , a+tot+1 , compare );
solve();
for (int i=1;i<=m;i++)
printf("%d\n",ans[i]);
return 0;
}

<后记>

『LCA 树链剖分』的更多相关文章

  1. Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式)

    Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式) 题外话,这是我第40篇随笔,纪念一下.<( ̄︶ ̄)↗[GO!] 题意 是说有棵树,每个节点上 ...

  2. [BZOJ3626] [LNOI2014]LCA(树链剖分)

    [BZOJ3626] [LNOI2014]LCA(树链剖分) 题面 给出一棵N个点的树,要求支持Q次询问,每次询问一个点z与编号为区间[l,r]内的点分别求最近公共祖先得到的最近公共祖先深度和.N, ...

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

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

  4. Codeforces Round #329 (Div. 2) D. Happy Tree Party LCA/树链剖分

    D. Happy Tree Party     Bogdan has a birthday today and mom gave him a tree consisting of n vertecie ...

  5. BZOJ 3626: [LNOI2014]LCA( 树链剖分 + 离线 )

    说多了都是泪啊...调了这么久.. 离线可以搞 , 树链剖分就OK了... -------------------------------------------------------------- ...

  6. [CodeVS2370] 小机房的树 (LCA, 树链剖分, LCT)

    Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为0到N-1,有两只虫子名叫飘狗和大吉狗,分居在两个不同的节点上.有一天,他们想爬到一个节点上去搞基,但是作为两只虫子,他们不想花 ...

  7. BZOJ3626[LNOI2014]LCA——树链剖分+线段树

    题目描述 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1.设dep[i]表示点i的深度,LCA(i,j)表示i与j的最近公共祖先.有q次询问,每次询 ...

  8. bzoj 3626 : [LNOI2014]LCA (树链剖分+线段树)

    Description 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1.设dep[i]表示点i的深度,LCA(i,j)表示i与j的最近公共祖先.有q ...

  9. LCA树链剖分

    LCA(Lowest Common Ancestor 最近公共祖先)定义如下:在一棵树中两个节点的LCA为这两个节点所有的公共祖先中深度最大的节点. 比如这棵树 结点5和6的LCA是2,12和7的LC ...

随机推荐

  1. 被严重误会?APS系统没有想象的那么复杂

    APS的出现要从90年代了,但到现在,很多行业内的顾问或用户提到APS都马上想到的是“要求很精确”“难度很大”“脱离实际”“太理想化”“工作量太大”等等,然后把它束之高阁不睬. 在这里,给大家分析一下 ...

  2. loadrunner 基本操作

    1.录制(录制选项) 2.回放(运行时设置) 3.添加事物 4.参数化 5.内容检查 6.添加集合点 1.在脚本中添加集合点函数如下: lr_rendezvous("集合点") / ...

  3. 前端开发者必备的Nginx知识

    摘要: 最常用的Web服务器 -- Nginx 原文:前端开发者必备的Nginx知识 作者:ConardLi Fundebug经授权转载,版权归原作者所有. Nginx在应用程序中的作用 解决跨域 请 ...

  4. Linux从入门到放弃、零基础入门Linux(第二篇):在虚拟机vmware中安装linux(一)超详细手把手教你安装centos分步图解

    一.Vmware vmware介绍:VMware,Inc. (Virtual Machine ware)是一个“虚拟PC”软件公司,提供服务器.桌面虚拟化的解决方案.其虚拟化平台的产品包括播放器:它能 ...

  5. NSQ端口关系以及注意事项

    0.相关参考文章: 官网:https://nsq.io/ <golang实战-nsq集群入门与坑> <nsq系统架构> <NSQ消息队列> 1.启动命令 ①nsql ...

  6. windows下使用ssh(利用paramiko库)

    环境:python3.7.3 win7 or win10 1.首先下载paramiko库 命令:pip install paramiko 2.代码: import paramiko 创建一个 ssh ...

  7. OAuth 2.0 的四种授权模式

    RFC 6749 OAuth 2.0 的标准是 RFC 6749 文件.该文件先解释了 OAuth 是什么. OAuth 引入了一个授权层,用来分离两种不同的角色:客户端和资源所有者.......资源 ...

  8. Java枚举的用法和原理深入

    转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10843644.html 一:枚举的用法 1.定义和组织常量 在JDK1.5之前,我们定义常量都是:publi ...

  9. Apache Flink流式处理

    花了四小时,看完Flink的内容,基本了解了原理. 挖个坑,待总结后填一下. 2019-06-02 01:22:57等欧冠决赛中,填坑. 一.概述 storm最大的特点是快,它的实时性非常好(毫秒级延 ...

  10. Flask的实例化参数及对app的配置

    目录 1.调试模式初测 2.app.config中的其他配置参数详解 3.修改config配置的方式(from_object用法) 3.1直接对app.config进行修改: 3.2使用类的方式导入: ...