[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 分析 看上去又是一道强行把序列上问题搬运到树上的裸题,然而分析之后发现并不然... 首先我们考虑如何在 ...
随机推荐
- C++标识符的作用域与可见性
一.标识符的作用域与可见性 作用域讨论的是标识符的有效范围,可见性讨论的是标识符是否可以被引用. 二.作用域 作用域是一个标识符在程序正文中有效的区域.C++中标识符的作用域有函数原型作用域.局部作用 ...
- golang的for循环基本语法
- Vue使用指南(一)
Vue Vue:前台框架 渐进式JavaScript框架 渐进式:vue可以控制页面的一个局部,vue也可以控制整个页面,vue也能控制整个前端项目 -- 根据项目需求,来决定vue控制项目的 ...
- Delphi XE10.1 引用计数(Delphi XE10.1 Berlin终于增加了对接口的Weak, UnSafe的支持)
以往的Delphi版本,不支持接口的Weak,和UnSafe的引用,支持对象的Weak, UnSafe,而且仅在Android和Ios平台上支持. 现在Delphi XE10.1 Berlin终于增加 ...
- Go part 6 接口,接口排序,接口嵌套组合,接口与类型转换,接口断言
接口 接口是一种协议,比如一个汽车的协议,就应该有 “行驶”,“按喇叭”,“开远光” 等功能(方法),这就是实现汽车的协议规范,完成了汽车的协议规范,就实现了汽车的接口,然后使用接口 接口的定义:本身 ...
- $.ajaxSetup()与$.ajax()区别
ajaxSetup()其使用方法与ajax()并无区别,只是在其之后的ajax()不用再重复添加相同参数,节省了代码量. 附:API中关于jQuery.ajaxSetup([options])的描述和 ...
- node.js 接口调用示例
测试用例git地址(node.js部分):https://github.com/wuyongxian20/node-api.git 项目架构如下: controllers: 文件夹下为接口文件 log ...
- java--分析简单java类与反射的联系
分析简单java类与反射的联系 web对反射的操作支持 在JSP之中有一种技术--javaBean.而且在jsp里面也配套有相应的操作方式,javaBean的核心在于简单java类,于是下面演示此操作 ...
- 1.Git & GitHup
1.常见的版本控制(管理代码的版本迭代)工具: @ svn:集中式版本控制系统: SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以首先要从中央服务器哪里 ...
- Vue项目打包发布,配置Nginx
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...