P3384 [模板] 树链剖分
#include <bits/stdc++.h>
using namespace std;
typedef long long ll; int n, m, rt, mod, cnt, tot;
int val[100005];
int dep[100005];
int id[100005], ed[100005];
int rk[100005];
int sz[100005];
int fa[100005];
int son[100005];
int top[100005];
ll sum[400005];
ll lz[400005]; struct node {
int to, nex;
}E[200005];
int head[100005]; void dfs1(int x, int pre, int d) {
sz[x] = 1;
dep[x] = d;
fa[x] = pre;
for(int i = head[x]; i; i = E[i].nex) {
int v = E[i].to;
if(v == pre) continue; dfs1(v, x, d + 1);
sz[x] += sz[v];
if(sz[son[x]] < sz[v]) son[x] = v;
}
} void dfs2(int x, int t) {
top[x] = t;
id[x] = ++tot;
rk[tot] = x;
if(!son[x]) {
ed[x] = tot;
return;
} dfs2(son[x], t);
for(int i = head[x]; i; i = E[i].nex) {
int v = E[i].to;
if(v == son[x] || v == fa[x]) continue;
dfs2(v, v);
}
ed[x] = tot;
} void pushup(int rt) {
sum[rt] = (sum[rt << 1] + sum[rt << 1 | 1]) % mod;
} void pushdown(int l, int r, int rt) {
if(lz[rt]) {
int m = l + r >> 1;
sum[rt << 1] = (sum[rt << 1] + 1LL * (m - l + 1) * lz[rt] % mod) % mod;
sum[rt << 1 | 1] = (sum[rt << 1 | 1] + 1LL * (r - m) * lz[rt] % mod) % mod;
lz[rt << 1] = (lz[rt << 1] + lz[rt]) % mod;
lz[rt << 1 | 1] = (lz[rt << 1 | 1] + lz[rt]) % mod;
lz[rt] = 0;
}
} void build(int l, int r, int rt) {
if(l == r) {
sum[rt] = 1LL * val[rk[l]] % mod;
return;
} int m = l + r >> 1;
build(l, m, rt << 1);
build(m + 1, r, rt << 1 | 1);
pushup(rt);
} void update(int ql, int qr, ll val, int l, int r, int rt) {
if(ql <= l && qr >= r) {
sum[rt] += 1LL * (r - l + 1) * val % mod;
sum[rt] %= mod;
lz[rt] += val;
lz[rt] %= mod;
return;
} pushdown(l, r, rt);
int m = l + r >> 1;
if(ql <= m) update(ql, qr, val, l, m, rt << 1);
if(qr > m) update(ql, qr, val, m + 1, r, rt << 1 | 1);
pushup(rt);
} ll query(int ql, int qr, int l, int r, int rt) {
if(ql <= l && qr >= r) return sum[rt]; pushdown(l, r, rt);
ll res = 0; int m = l + r >> 1;
if(ql <= m) res += query(ql, qr, l, m, rt << 1);
if(qr > m) res += query(ql, qr, m + 1, r, rt << 1 | 1);
res %= mod;
return res;
} ll cal_sum(int x, int y) {
ll res = 0;
int fx = top[x];
int fy = top[y];
while(fx != fy) {
if(dep[fx] >= dep[fy]) {
res += query(id[fx], id[x], 1, n, 1);
res %= mod;
x = fa[fx]; fx = top[x];
} else {
res += query(id[fy], id[y], 1, n, 1);
res %= mod;
y = fa[fy]; fy = top[y];
}
}
if(id[x] <= id[y]) res += query(id[x], id[y], 1, n, 1);
else res += query(id[y], id[x], 1, n, 1);
res %= mod; return res;
} void update_lian(int x, int y, ll val) {
int fx = top[x];
int fy = top[y];
while(fx != fy) {
if(dep[fx] >= dep[fy]) {
update(id[fx], id[x], val, 1, n, 1);
x = fa[fx]; fx = top[x];
} else {
update(id[fy], id[y], val, 1, n, 1);
y = fa[fy]; fy = top[y];
}
}
if(id[x] <= id[y]) update(id[x], id[y], val, 1, n, 1);
else update(id[y], id[x], val, 1, n, 1);
} int main() {
scanf("%d%d%d%d", &n, &m, &rt, &mod);
cnt = 0;
tot = 0;
for(int i = 1; i <= n; i++) scanf("%d", &val[i]);
for(int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
E[++cnt].to = y; E[cnt].nex = head[x]; head[x] = cnt;
E[++cnt].to = x; E[cnt].nex = head[y]; head[y] = cnt;
}
dfs1(rt, 0, 1);
dfs2(rt, rt);
build(1, n, 1);
while(m--) {
int opt;
scanf("%d", &opt); int a, b, c;
if(opt == 1) {
scanf("%d%d%d", &a, &b, &c);
c %= mod;
update_lian(a, b, 1LL * c);
} else if(opt == 2) {
scanf("%d%d", &a, &b);
printf("%lld\n", cal_sum(a, b));
} else if(opt == 3) {
scanf("%d%d", &a, &b);
b %= mod;
update(id[a], ed[a], 1LL * b, 1, n, 1);
} else {
scanf("%d", &a);
printf("%lld\n", query(id[a], ed[a], 1, n, 1));
}
}
return 0;
}
P3384 [模板] 树链剖分的更多相关文章
- [luogu P3384] [模板]树链剖分
[luogu P3384] [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点 ...
- [洛谷P3384] [模板] 树链剖分
题目传送门 显然是一道模板题. 然而索引出现了错误,狂wa不止. 感谢神犇Dr_J指正.%%%orz. 建线段树的时候,第44行. 把sum[p]=bv[pos[l]]%mod;打成了sum[p]=b ...
- luoguP3384 [模板]树链剖分
luogu P3384 [模板]树链剖分 题目 #include<iostream> #include<cstdlib> #include<cstdio> #inc ...
- 【Luogu P3384】树链剖分模板
树链剖分的基本思想是把一棵树剖分成若干条链,再利用线段树等数据结构维护相关数据,可以非常暴力优雅地解决很多问题. 树链剖分中的几个基本概念: 重儿子:对于当前节点的所有儿子中,子树大小最大的一个儿子就 ...
- 模板 树链剖分BFS版本
//点和线段树都从1开始 //边使用vector vector<int> G[maxn]; ],num[maxn],iii[maxn],b[maxn],a[maxn],top[maxn], ...
- 树链剖分详解(洛谷模板 P3384)
洛谷·[模板]树链剖分 写在前面 首先,在学树链剖分之前最好先把 LCA.树形DP.DFS序 这三个知识点学了 emm还有必备的 链式前向星.线段树 也要先学了. 如果这三个知识点没掌握好的话,树链剖 ...
- 『题解』洛谷P3384 【模板】树链剖分
Problem Portal Portal1: Luogu Description 如题,已知一棵包含\(N\)个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作\(1\): ...
- [note]树链剖分
树链剖分https://www.luogu.org/problemnew/show/P3384 概念 树链剖分,是一种将树剖分成多条不相交的链的算法,并通过其他的数据结构来维护这些链上的信息. 最简单 ...
- P3384 【模板】树链剖分
P3384 [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节 ...
随机推荐
- TCP/IP五层模型-传输层-UDP协议
1.定义:UDP:是非面向连接.不可靠的用户数据包协议. 2.应用场景:适合对数据完整性要求不高,但对延迟很敏感,比如即时通信(语音视频聊天等). 3.UDP报文格式: 4.用UDP传输数据的应用层 ...
- 克隆slave
在日常生活中,我们做的比较多的操作就是在线添加从库,比如线上有一主一丛两个数据库,由于业务的需要一台从库的读取量无法满足现在的需求,这样就需要我们在线添加从库,出于安全考虑,我们通常需要在从库上进行在 ...
- g/test/s/lose/won/g
包含字符串test的任意行商,用lose代替won
- 【EXP】Oracle多表导出问题
有些时候,需要导入某个用户的一些相关表.但是不知道用户的用户名和密码.这样就很尴尬 但是如果手上有dba权限的用户的话,就很方便的能导出了 先要知道多表导出的语句 exp system/123456 ...
- 同步alv的前端显示和输出内表中的数据
在使用CL_GUI_ALV_GRID显示报表的时候,当我们使用了checkbox的时候,或者是有可编辑的字段,当我们 在前段修改了单元格内容的时候,后台的内表并不会自动的更新,此时需要我们调用一个方法 ...
- Podinfo,迷你的 Go 微服务模板
项目介绍 Podinfo 是一个用 Go 制作的小型 web 应用程序,它展示了在 Kubernetes 中运行微服务的最佳实践. 它已实现的技术指标(截选自官方 README.md ): 里面每一 ...
- linux opt, usr文件夹说明
linux下各文件夹介绍: https://www.pathname.com/fhs/pub/fhs-2.3.html /usr:系统级的目录,可以理解为C:/Windows/,/usr/lib理解为 ...
- Frame of Reference and Roaring Bitmaps
https://www.elastic.co/cn/blog/frame-of-reference-and-roaring-bitmaps http://roaringbitmap.org/ 2015 ...
- c 越界 数组越界
int main(int argc, char* argv[]){ int i = 0; int arr[3] = {0}; for(; i<=3; i++){ arr[i] = 0; prin ...
- SQL解析工具
面对复杂的SQL可用这个SQL解析工具,分析出用到了哪些表哪些字段: http://107.170.101.241:8080/getTableColumn/