[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 分析 看上去又是一道强行把序列上问题搬运到树上的裸题,然而分析之后发现并不然... 首先我们考虑如何在 ...
随机推荐
- Filter讲解4
想要 浏览更多Fiddler内容:请点击进入Fiddler官方文档 阅读目录: 一.使用.NET代码扩展Fiddler 二.实现Fiddler接口 三.创建Fiddler扩展项目 四.在扩展程序选项卡 ...
- Java基础IO类之数据流
DataInputStream: 数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本java数据类型.应用程序可以使用数据输出流 写入稍后由数据输入流读取的数据.DataInputStrea ...
- Z算法板子
给定一个串$s$, $Z$算法可以$O(n)$时间求出一个$z$数组 $z_i$表示$s[i...n]$与$s$的前缀匹配的最长长度, 下标从$0$开始 void init(char *s, int ...
- MySQL 体系结构及存储引擎
MySQL 原理篇 MySQL 索引机制 MySQL 体系结构及存储引擎 MySQL 语句执行过程详解 MySQL 执行计划详解 MySQL InnoDB 缓冲池 MySQL InnoDB 事务 My ...
- 滤波器算法(2)-最小均方(LMS)
2018.09.09 写的版本 ①残差平方和 ②平方损失函数: ③函数的极值点为偏导数为0的点:(将问题变成一个求极值的问题) ④求解得: matlab代码: ① y=ax+b+e方程 functio ...
- VS 2015 .net UI界面报错总结
一.提示錯誤 解決方法: 右击解决方案点击properties Window Ctrl+W ,P 将Mnaged Pipeline Mode 从Integrated更改为Classic 二.提示錯誤 ...
- redis设置密码,解决重启后密码丢失及自启服务配置
一.安装redis redis3.0及redisManage管理工具 链接:https://pan.baidu.com/s/1p5EWeF2Jgsw9xOE1ADMmRg 提取码:thyf 二.red ...
- IDEA 中常用快捷键
1.搜索文件(整个项目) ctrl+shift+n 2.最近打开文件 ctrl+e 3.实现接口中方法 ctrl+i 4.跳到上一行 ctrl+alt+enter 5.删除当前行 ctrl+y 6.重 ...
- RestFramework之序列化组件
一.restframework的序列化组件的使用 1.导入序列化组件与模型类 from rest_framework import serializers from app01.models impo ...
- vsftpd设置虚拟用户
centos6.5环境 软件安装: yum install vsftpd db4-utils 1. 添加虚拟用户口令文件 #添加一个虚拟用户testvim /etc/vsftpd/vftp_vuser ...