Note

1.DFS1

mark all the depth

mark fathers

mark the heavy/light children

mark the size of each subtree

void dfs1(long long pos, long long f, long long depth){
dep[pos] = depth;
fat[pos] = f;
sz[pos] = 1;
long long maxi = -1;
for (long long v : adj[pos]){
if (v == f) continue;
dfs1(v,pos,depth+1);
sz[pos]+=sz[v];
if (maxi<sz[v]) {maxi = sz[v];son[pos] = v;}
}
}

2.DFS2

mark the of the members in the base array

record the top of each node

traverse heavy son first then light son

mark the heavy son along the path

give the base array a value

record the id of each node

void dfs2(long long pos, long long top_pos){
id[pos] = ++cnt;
wt[cnt] = num[pos];
top[pos] = top_pos;
if (!son[pos]) return;
dfs2(son[pos],top_pos);
for (long long v : adj[pos]){
if (v==fat[pos] || v==son[pos]) continue;
dfs2(v,v);
}
}

3.make_tree

make segment tree base on the base array

void make_tree(int way, int l, int r){
if (l==r) {seg[way] = wt[l]%p;return;}
int mid = (l+r)/2;
make_tree(way*2,l,mid);
make_tree(way*2+1,mid+1,r);
seg[way] = (seg[way*2]+seg[way*2+1])%p;
}
//push function
void push(int way,int lenn){
lazy[way*2]+=lazy[way];
lazy[way*2+1]+=lazy[way];
seg[way*2]+=lazy[way]*(lenn-(lenn>>1));
seg[way*2+1]+=lazy[way]*(lenn>>1);
seg[way*2]%=p;
seg[way*2+1]%=p;
lazy[way]=0;
}

4.query_up

query base on the segment tree

int query_up(int way, int l, int r, int qlow, int qhigh){
push(way,l,r);
if (qlow<= l && r<=qhigh) return seg[way]%p;
if (l>qhigh || r<qlow) return 0; int mid = (l+r)/2;
return (query_up(way*2,l,mid,qlow,qhigh) + query_up(way*2+1,mid+1,r,qlow,qhigh))%p;
}

5.query

1) check if they are on the same chain. if Not, add the distance from a node to the top of the chain, and move up

2) repeat step 1 until they are on the same chain

3) query the distance between the nodes based on the base array

int query(int x, int y){
int ans = 0;
while(top[x]!=top[y]){
if (dep[top[x]]<dep[top[y]]) swap(x,y);
ans = (ans+query_up(1,1,n,id[top[x]],id[x]))%p;
x = fat[top[x]];
}
if (dep[x]>dep[y]) swap(x,y);
ans = (ans + query_up(1,1,n,id[x],id[y]))%p;
return ans;
}

6.query_son

query the id[x] plus its size -1 the subtree of a node is consecutive in the base array

int qSon(int x){
return query_up(1,1,n,id[x],id[x]+sz[x]-1);
}

7.update

update normal segment tree (use id as substitution)

void update(int way, int l, int r, int qlow, int qhigh, int val){
push(way,l,r);
if (qlow<=l && r<=qhigh) {
lazy[way] += val;
push(way,l,r);
return;
}
if (l>qhigh || r<qlow) return;
int mid = (l+r)/2;
update(way*2,l,mid,qlow,qhigh,val);
update(way*2+1,mid+1,r,qlow,qhigh,val);
seg[way] = (seg[way*2]+seg[way*2+1])%p;
}

8.update chain

1) check if they are on the same chain. If not, update the chain, and go to the father of the chain head

2) repeat until they are on the same chain

3) update the id of the nodes on the segment tree

void upPath(int x, int y, int val){
while(top[x]!=top[y]){
if (dep[top[x]]<dep[top[y]]) swap(x,y);
update(1,1,n,id[top[x]],id[x],val);
x = fat[top[x]];
}
if (dep[x]>dep[y]) swap(x,y);
update(1,1,n,id[x],id[y],val);
}

9.update_son

update id[x] plus its size -1

void upSon(int x, int val){
update(1,1,n,id[x],id[x]+sz[x]-1,val);
}

optional: LCA

1) check if the two nodes are on the same chain

2) if not, find the one with a deeper head, move it up

3) repeat until they are in the same chain

3) return the one with higher depth

int LCA (int x , int y ) {
int fx = top[x] , fy = top[y] ;
while(fx!=fy) {
if(level[fx]< level[fy]) swap(x,y) , swap(fx ,fy) ;
x = fat[fx] ; fx = top[x];
}
if( level[x] > level[y] ) swap(x,y) ;
return x ;
}

Note to myself

Segment tree is able to support different data structure

Heavy Light Decomposition的更多相关文章

  1. 树链剖分I 原理

    树链剖分(Heavy Light Decomposition, HLD)是一种将对[树上两点间的路径]上[边或点]的[修改与查询]转化到[序列]上来处理的方法. 目的:将树的边或点转化到一个线性结构( ...

  2. 神奇的树上启发式合并 (dsu on tree)

    参考资料 https://www.cnblogs.com/zhoushuyu/p/9069164.html https://www.cnblogs.com/candy99/p/dsuontree.ht ...

  3. HDU 5111 Alexandra and Two Trees 树链剖分 + 主席树

    题意: 给出两棵树,每棵树的节点都有一个权值. 同一棵树上的节点的权值互不相同,不同树上节点的权值可以相同. 要求回答如下询问: \(u_1 \, v_1 \, u_2 \, v_2\):询问第一棵树 ...

  4. [HNOI2018]毒瘤

    Description 从前有一名毒瘤. 毒瘤最近发现了量产毒瘤题的奥秘.考虑如下类型的数据结构题:给出一个数组,要求支持若干种奇奇怪怪的修改操作(比如区间加一个数,或者区间开平方),并支持询问区间和 ...

  5. Wolfycz的娱乐赛题解

    现在不会放题解的!比赛完了我会把题解放上来的 祝大家玩的愉快~ 等会,cnblogs不会显示更新时间?我禁赛我自己 UPD:2018.12.15 欢迎大家爆踩标程- painting 我们考虑转化题意 ...

  6. [ZJOI2011]道馆之战

    Description 口袋妖怪(又名神奇宝贝或宠物小精灵)红/蓝/绿宝石中的水系道馆需要经过三个冰地才能到达馆主的面前,冰地中的每一个冰块都只能经过一次.当一个冰地上的所有冰块都被经过之后,到下一个 ...

  7. codechef Heavy-light Decompositions

    Heavy-light Decompositions Problem Code: HLDOTSSubmit All submissions for this problem are available ...

  8. ACM/ICPC 之 拓扑排序-反向(POJ3687)

    难点依旧是题意....需要反向构图+去重+看题 POJ3687-Labeling Balls 题意:1-N编号的球,输出满足给定约束的按原编号排列的重量序列,如果有多组答案,则输出编号最小的Ball重 ...

  9. poj1013

    题目大意:假造的银币 Sally Jones有一些游客给的银币,但是只有11枚是真正的银币(有一枚是假的),从颜色和大小是无法区分真比还是假币的,但是它的重量和真币是不同的,Sally Jones它是 ...

随机推荐

  1. jqGrid 添加 合计行 footDate

    jQuery(table_id).jqGrid({ url : url,//组件创建完成之后请求数据的url datatype : "json",//请求数据返回的类型.可选jso ...

  2. select2 智能补全模糊查询select2的下拉选择框使用

    我们在上篇文章中已经在SpringMVC基础框架的基础上应用了BootStrap的后台框架,在此基础上记录select2的使用. 应用bootstrap模板 基础项目源码下载地址为: SpringMV ...

  3. C#路径2

    String apppath = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase; //获取整个文件路径名ap ...

  4. SciKit-Learn 可视化数据:主成分分析(PCA)

    ## 保留版权所有,转帖注明出处 章节 SciKit-Learn 加载数据集 SciKit-Learn 数据集基本信息 SciKit-Learn 使用matplotlib可视化数据 SciKit-Le ...

  5. C# Stream篇(二) -- TextReader 和StreamReader

    TextReader 和StreamReader 目录: 为什么要介绍 TextReader? TextReader的常用属性和方法 TextReader 示例 从StreamReader想到多态 简 ...

  6. 使用BurpSuite和Hydra爆破相关的服务(9.25 第十一天)

    使用BP和Hydra爆破相关的服务 Hydra:九头蛇,开源的功能强大的爆破工具,支持的服务有很多,使用Hydra爆破C/S架构的服务. 使用BurpSuite爆破web服务 DVWA:web应用程序 ...

  7. 实战 迁移学习 VGG19、ResNet50、InceptionV3 实践 猫狗大战 问题

    实战 迁移学习 VGG19.ResNet50.InceptionV3 实践 猫狗大战 问题   参考博客:::https://blog.csdn.net/pengdali/article/detail ...

  8. Ubuntu 安装VirtualBox 虚拟机

    转载 1.终端命令 编辑sources.list ? 1 sudo gedit /etc/apt/sources.list 2.添加 软件源 将下面的地址加入sources.list 的末尾,保存并退 ...

  9. 翻译小工具制作,Python简单破解有道JS加密!

    写这篇文章之前,我记得我以前好像公布一次.百度翻译的接口把版本号修改可以得到老版本,而老版本是没JS加密的,有道的呢也是一样的. ! 不过今天的教程不会这么low,咱们今天就老老实实把有道翻译的JS破 ...

  10. 吴裕雄--天生自然 PHP开发学习:运算符

    <?php $x=10; $y=6; echo ($x + $y); // 输出16 echo '<br>'; // 换行 echo ($x - $y); // 输出4 echo ' ...