[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 分析 看上去又是一道强行把序列上问题搬运到树上的裸题,然而分析之后发现并不然... 首先我们考虑如何在 ...
随机推荐
- vm虚拟机啊安装操作
VMware下载与安装一.虚拟机的下载1.进入VMware官网,点击左侧导航栏中的下载,再点击图中标记的Workstation Pro,如下图所示. 2.根据操作系统选择合适的产品,在这里以Windo ...
- 玩转【Mock.js】,前端也能跑的很溜
现在开发已经是前后端分离了,前端和后端可以同时进行开发,互不影响,但是有些时候后端开发的接口慢于前端,导致前端需要等待后端的接口完成才能完成前后端对接,为了解决这个痛点,出现了模拟接口数据的方案,目前 ...
- jquery.fileupload源码解读笔记
基础编程风格 新建 test.html 和 test.js和 main.js和 无论哪种顺序 <body> <script src="/Sandeep/js/jquery ...
- C#笔试题目总结
基础 知识点 try catch finally的执行顺序(有return的情况下): 不管有没有出现异常,finally块中代码都会执行: 当try和catch中有return时,finally仍然 ...
- vue组件上动态添加和删除属性
1.vue组件上动态添加和删除属性 // 添加 this.$set(this.obj, 'propName', val) // 删除 this.$delete(this.obj, 'propName' ...
- SVM-支持向量机总结
一.SVM简介 (一)Support Vector Machine 支持向量机(SVM:Support Vector Machine)是机器学习中常见的一种分类算法. 线性分类器,也可以叫做感知机,其 ...
- REST,以及RESTful的讲解
详见:https://blog.csdn.net/qq_21383435/article/details/80032375 1.传统下的API接口对比规则概念REST 系统的特征演化优点&缺点 ...
- 【SpringMVC】请求乱码处理
一.post请求乱码 二.get请求乱码 一.post请求乱码 在web.xml中加入 <filter> <filter-name>CharacterEncodingFilte ...
- C语言面试题目之指针和数组
说明:所有题目均摘录于网络以及我所见过的面试题目,欢迎补充! 无特殊说明情况下,下面所有题s目都是linux下的32位C程序. 先来几个简单的热热身. 1.计算以下sizeof的值. char str ...
- openwrt双机热备
转自:https://oldwiki.archive.openwrt.org/doc/recipes/high-availability 先记号一下,有空再仔细研究. ---------------- ...