[Luogu] 树链剖分
模板题,对于对为某个点为根的子树进行处理时,只需每个节点记录两个值
分别为搜索以该节点为根的子树时的最初搜索序和最末搜索序,将这两
个数作为线段树区间操作的端点进行操作
#include <bits/stdc++.h> using namespace std;
const int N = ; #define gc getchar()
#define lson jd << 1
#define rson jd << 1 | 1 struct Node_1{
int u, v, nxt;
}G[N << ];
struct Node_2{
int fa, son, size, topp, deep, l, r;
}P[N];
struct Node_3{
int l, r, w, f;
}T[N << ];
int n, Ti, root, mod, now = , tim, opt, x_, y_, z_;
int head[N], tree[N], bef[N], data[N];
long long ret, ans; inline int read(){
int x = , f = ; char c = gc;
while(c < '' || c > '') {if(c == '-') f = -; c = gc;}
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x * f;
} inline void add(int u, int v){
G[now].u = u; G[now].v = v; G[now].nxt = head[u]; head[u] = now ++;
} inline void init(){
n = read(); Ti = read(); root = read(); mod = read();
for(int i = ; i <= n; i ++) head[i] = -, data[i] = read();
for(int i = ; i < n; i ++){
int u = read(), v = read();
add(u, v); add(v, u);
}
} void dfs_find_son(int u, int fa, int dep){
P[u].fa = fa;
P[u].deep = dep;
P[u].size = ;
for(int i = head[u]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(v != fa){
dfs_find_son(v, u, dep + );
P[u].size += P[v].size;
if(! P[u].son || P[v].size > P[P[u].son].size) P[u].son = v;
}
}
} void dfs_to_un(int u, int tp){
P[u].topp = tp;
tree[u] = ++ tim;
bef[tim] = u;
P[u].l = tim;
if(!P[u].son){P[u].r = tim; return ;}
dfs_to_un(P[u].son, tp);
for(int i = head[u]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(v != P[u].fa && v != P[u].son) dfs_to_un(v, v);
}
P[u].r = tim;
} void build_tree(int l, int r, int jd){
T[jd].l = l; T[jd].r = r;
if(l == r){
T[jd].w = data[bef[l]] % mod;
return ;
}
int mid = (l + r) >> ;
build_tree(l, mid, lson);
build_tree(mid + , r, rson);
T[jd].w = T[lson].w + T[rson].w;
} void down(int jd){
int F = T[jd].f;
T[lson].w += ((T[lson].r - T[lson].l + ) * F); T[lson].w %= mod;
T[rson].w += ((T[rson].r - T[rson].l + ) * F); T[rson].w %= mod;
T[lson].f += F;
T[rson].f += F;
T[jd].f = ;
} void Sec_G(int l, int r, int jd, int x, int y, int yj){
if(x <= l && r <= y){
T[jd].w += ((r - l + ) * yj);
T[jd].w %= mod;
T[jd].f += yj;
return ;
}
if(T[jd].f) down(jd);
int mid = (l + r) >> ;
if(x <= mid) Sec_G(l, mid, lson, x, y, yj);
if(y > mid) Sec_G(mid + , r, rson, x, y, yj);
T[jd].w = T[lson].w + T[rson].w;
} void Sec_A(int l, int r, int jd, int x, int y){
if(x <= l && r <= y){
ans += T[jd].w; ans %= mod;
return ;
}
if(T[jd].f) down(jd);
int mid = (l + r) >> ;
if(x <= mid) Sec_A(l, mid, lson, x, y);
if(y > mid) Sec_A(mid + , r, rson, x, y);
} void Sec_G_imp(int x, int y, int z){
int tp1 = P[x].topp, tp2 = P[y].topp;
while(tp1 != tp2){
if(P[tp1].deep < P[tp2].deep) swap(tp1, tp2), swap(x, y);
Sec_G(, n, , tree[tp1], tree[x], z);
x = P[tp1].fa;
tp1 = P[x].topp;
}
if(P[x].deep > P[y].deep) Sec_G(, n, , tree[y], tree[x], z);
else Sec_G(, n, , tree[x], tree[y], z);
} int Sec_A_imp(int x, int y){
int tp1 = P[x].topp, tp2 = P[y].topp;
ret = ;
while(tp1 != tp2){
if(P[tp1].deep < P[tp2].deep) swap(tp1, tp2), swap(x, y);
ans = ;
Sec_A(, n, , tree[tp1], tree[x]);
ret += ans;
x = P[tp1].fa;
tp1 = P[x].topp;
}
ans = ;
if(P[x].deep > P[y].deep) Sec_A(, n, , tree[y], tree[x]);
else Sec_A(, n, , tree[x], tree[y]);
ret += ans;
return ret % mod;
} void good_look(){
ans = ;
Sec_A(, n, , P[x_].l, P[x_].r);
cout << ans << endl;
} void get_all(){
opt = read(); x_ = read();
if(opt == ) y_ = read(), z_ = read();
else if(opt == ) y_ = read();
else if(opt == ) z_ = read();
} void debug(){
cout << "top: "; for(int i = ; i <= n; i ++) cout << P[i].topp << " "; cout << endl;
cout << "fa: "; for(int i = ; i <= n; i ++) cout << P[i].fa << " "; cout << endl;
cout << "deep: "; for(int i = ; i <= n; i ++) cout << P[i].deep << " "; cout << endl;
cout << "size: "; for(int i = ; i <= n; i ++) cout << P[i].size << " "; cout << endl;
cout << "son: "; for(int i = ; i <= n; i ++) cout << P[i].son << " "; cout << endl;
cout << "L: "; for(int i = ; i <= n; i ++) cout << P[i].l << " "; cout << endl;
cout << "R: "; for(int i = ; i <= n; i ++) cout << P[i].r << " "; cout << endl;
exit();
} int main()
{
init();
dfs_find_son(root, , );
dfs_to_un(root, root);
build_tree(, n, );
//debug();
while(Ti --){
get_all();
if(opt == ) Sec_G_imp(x_, y_, z_);
else if(opt == ) cout << Sec_A_imp(x_, y_) << endl;
else if(opt == ) Sec_G(, n, , P[x_].l, P[x_].r, z_);
else good_look();
} return ;
}
/*
操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节点的值都加上z
操作2: 格式: 2 x y 表示求树从x到y结点最短路径上所有节点的值之和
操作3: 格式: 3 x z 表示将以x为根节点的子树内所有节点值都加上z
操作4: 格式: 4 x 表示求以x为根节点的子树内所有节点值之和
5 5 2 24
7 3 7 8 0
1 2
1 5
3 1
4 1
3 4 2
3 2 2
4 5
1 5 1 3
2 1 3
*/
[Luogu] 树链剖分的更多相关文章
- Luogu P4643 【模板】动态dp(矩阵乘法,线段树,树链剖分)
题面 给定一棵 \(n\) 个点的树,点带点权. 有 \(m\) 次操作,每次操作给定 \(x,y\) ,表示修改点 \(x\) 的权值为 \(y\) . 你需要在每次操作之后求出这棵树的最大权独立集 ...
- Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分)
Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分) Description L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之 ...
- Luogu 2590 [ZJOI2008]树的统计 / HYSBZ 1036 [ZJOI2008]树的统计Count (树链剖分,LCA,线段树)
Luogu 2590 [ZJOI2008]树的统计 / HYSBZ 1036 [ZJOI2008]树的统计Count (树链剖分,LCA,线段树) Description 一棵树上有n个节点,编号分别 ...
- [luogu P3384] [模板]树链剖分
[luogu P3384] [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点 ...
- 【Luogu】P3313旅行(树链剖分)
题目链接 动态开点的树链剖分qwq. 跟小奇的花园一模一样,不做过多讲解. #include<cstdio> #include<cstring> #include<cct ...
- luogu题解 P3950部落冲突--树链剖分
题目链接 https://www.luogu.org/problemnew/show/P3950 分析 大佬都用LCT,我太弱只会树链剖分 一个很裸的维护边权树链剖分题.按照套路,对于一条边\(< ...
- luogu题解P1967货车运输--树链剖分
题目链接 https://www.luogu.org/problemnew/show/P1967 分析 NOIp的一道裸题,直接在最大生成树上剖分取最小值一下就完事了,非常好写,常数也比较小,然而题解 ...
- luogu题解 P4092 【[HEOI2016/TJOI2016]树】树链剖分
题目链接: https://www.luogu.org/problemnew/show/P4092 瞎扯--\(O(Q \log^3 N)\)解法 这道先yy出了一个\(O(Q \log^3 N)\) ...
- luogu题解P2486[SDOI2011]染色--树链剖分+trick
题目链接 https://www.luogu.org/problemnew/show/P2486 分析 看上去又是一道强行把序列上问题搬运到树上的裸题,然而分析之后发现并不然... 首先我们考虑如何在 ...
随机推荐
- VMware虚拟机(Ubuntu)通过主机代理实现——浏览器+终端访问外网
环境说明:主机win10 + 虚拟机ubunut16.04 + 主机s-h-a-d-o-w-socks win10 主机相关操作配置1: 按下 Win + R 快捷键,输入 cmd ,然后在命令行中输 ...
- Istio最佳实践:在K8s上通过Istio服务网格进行灰度发布
Istio是什么? Istio是Google继Kubernetes之后的又一开源力作,主要参与的公司包括Google,IBM,Lyft等公司.它提供了完整的非侵入式的微服务治理解决方案,包含微服务的管 ...
- iOS - starckView 类似Android线性布局
同iOS以往每个迭代一样,iOS 9带来了很多新特性.UIKit框架每个版本都在改变,而在iOS 9比较特别的是UIStackView,它将从根本上改变开发者在iOS上创建用户界面的方式.本文将带你学 ...
- jQuery标签操作
样式操作 样式类操作 //添加指定的css类名 $('元素选择器')addClass('类名'); //移除指定的css类名 removeClass(); //判断样式存不存在 hasClass(); ...
- Java反射机制、注解及JPA实现
1.java反射概述 JAVA反射机制是在运行状态中,对于任意一个实体类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象方法的功能称 ...
- MACOS 安装mysqlclient 的 Library not loaded错误
报错场景 >>> import MySQLdb Traceback (most recent call last): File "<stdin>", ...
- iOS自动布局学习(UIView+AutoLayout)
自动布局虽然在iOS6的时候已经推出,不过由于各个原因并没有被开发组广泛使用.一方面是大家的app支持版本都是低于iOS6的,另一方面来说是Xcode支持木有现在这么好.以前由于iPhone设备相对固 ...
- 理解JVM之java内存模型
java虚拟机规范中试图定义一种java内存模型(JMM)来屏蔽掉各种硬件和操作系统内存访问差异,以实现让java程序在各种平台都能打到一致的内存访问效果.所以java内存模型的主要目标是定义程序中各 ...
- http接口测试工具-Advanced-REST-client
非常好用的http接口测试工具 相信作为一个java开发人员,大家或多或少的要写或者接触一些http接口.而当我们需要本地调试接口常常会因为没有一款好用的工具而烦恼.今天要给大家介绍一款非常好用.实用 ...
- Python基础笔记一
1. 分片的步长,默认为值1,表示为 xx[s:t:v] ----从索引s到索引t,每隔v,取对应索引位置的值 xx = 'hello,world' #从索引0-10,共11个字符 xx[2:] #从 ...