模板题,对于对为某个点为根的子树进行处理时,只需每个节点记录两个值

分别为搜索以该节点为根的子树时的最初搜索序和最末搜索序,将这两

个数作为线段树区间操作的端点进行操作

#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] 树链剖分的更多相关文章

  1. Luogu P4643 【模板】动态dp(矩阵乘法,线段树,树链剖分)

    题面 给定一棵 \(n\) 个点的树,点带点权. 有 \(m\) 次操作,每次操作给定 \(x,y\) ,表示修改点 \(x\) 的权值为 \(y\) . 你需要在每次操作之后求出这棵树的最大权独立集 ...

  2. Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分)

    Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分) Description L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之 ...

  3. Luogu 2590 [ZJOI2008]树的统计 / HYSBZ 1036 [ZJOI2008]树的统计Count (树链剖分,LCA,线段树)

    Luogu 2590 [ZJOI2008]树的统计 / HYSBZ 1036 [ZJOI2008]树的统计Count (树链剖分,LCA,线段树) Description 一棵树上有n个节点,编号分别 ...

  4. [luogu P3384] [模板]树链剖分

    [luogu P3384] [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点 ...

  5. 【Luogu】P3313旅行(树链剖分)

    题目链接 动态开点的树链剖分qwq. 跟小奇的花园一模一样,不做过多讲解. #include<cstdio> #include<cstring> #include<cct ...

  6. luogu题解 P3950部落冲突--树链剖分

    题目链接 https://www.luogu.org/problemnew/show/P3950 分析 大佬都用LCT,我太弱只会树链剖分 一个很裸的维护边权树链剖分题.按照套路,对于一条边\(< ...

  7. luogu题解P1967货车运输--树链剖分

    题目链接 https://www.luogu.org/problemnew/show/P1967 分析 NOIp的一道裸题,直接在最大生成树上剖分取最小值一下就完事了,非常好写,常数也比较小,然而题解 ...

  8. luogu题解 P4092 【[HEOI2016/TJOI2016]树】树链剖分

    题目链接: https://www.luogu.org/problemnew/show/P4092 瞎扯--\(O(Q \log^3 N)\)解法 这道先yy出了一个\(O(Q \log^3 N)\) ...

  9. luogu题解P2486[SDOI2011]染色--树链剖分+trick

    题目链接 https://www.luogu.org/problemnew/show/P2486 分析 看上去又是一道强行把序列上问题搬运到树上的裸题,然而分析之后发现并不然... 首先我们考虑如何在 ...

随机推荐

  1. GitHub的Fork是什么意思

    GitHub的Fork 是什么意思[转] GitHub Help Simple guide to forks in GitHub and Git GitHub的Fork 是什么意思-N神3-博客园 G ...

  2. java.lang.AbstractMethodError: null

    在使用springcloud的时候运行报这个错,原因是版本冲突导致的,在idea中创建springcloud项目的时候,这里默认是${spring-cloud.version},但是如果你使用的是高版 ...

  3. Java安装和环境配置

    Java安装和环境配置 从事Java开发第一关就是安装JAVA环境. 我们要安装JDK, 全称Java开发全套. 其中包含了JRE(运行时环境), 如果你打游戏的时候可能会提示你缺少JRE. 我们要做 ...

  4. java -jar 参数前后位置说明

    springboot项目启动的时候可以直接使用java -jar xxx.jar这样.下面说说参数的一些讲究 1.-DpropName=propValue的形式携带,要放在-jar参数前面 eg:ja ...

  5. JavaDoc工具和Ideade javadoc工具

    命令参考: javadoc -locale zh_CN -protected -notree -nonavbar -noindex -use -author -version -encoding UT ...

  6. Java构建器(多个构造器参数)

    今天看netty权威指南,第一次听说构建器,百度了几个博客,但是并没有通俗易懂一点儿的,综合别人的博客,总结如下: 1. 构建器是什么? 当创建对象需要传入多个参数的时候我们通常会根据参数的数量写不同 ...

  7. 发现一个对列排版挺好用的命令:column

    help [root@hdpool1 tmp]# column -h Usage: column [options] [file ...] Options: -c, --columns <wid ...

  8. 【postman】postman使用教程

    postman基础功能 一.变量设置 编写的API往往需要在多个环境下执行,而Postman 提供了两种类型的变量:环境变量和全局变量,从而很好的解决了这个问题.同时变量还常用于关联接口间的参数传递. ...

  9. 什么是软件工具开发包(SDK)

    开发一个软件,需要经过编辑.编译.调试.运行几个过程. 编辑:使用编程语言编写程序代码的过程. 编译:将编写的程序进行翻译. 调试:程序不可能一次性编写成功,编写过程中难免会出现语法.语义上的错误,调 ...

  10. python之csv操作

    在使用python爬虫时或者其他情况,都会用到csv存储与读取的相关操作,我们在这里就浅谈一下: CSV(Comma-Separated Values)逗号分隔符,也就是每条记录中的值与值之间是用分号 ...