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结点最短路径上所有节 ...
随机推荐
- Spring Boot 应用使用spring session+redis启用分布式session后,如何在配置文件里设置应用的cookiename、session超时时间、redis存储的namespace
现状 项目在使用Spring Cloud搭建微服务框架,其中分布式session采用spring session+redis 模式 需求 希望可以在配置文件(application.yml)里设置应用 ...
- Spark Streaming处理Flume数据练习
把Flume Source(netcat类型),从终端上不断给Flume Source发送消息,Flume把消息汇集到Sink(avro类型),由Sink把消息推送给Spark Streaming并处 ...
- Vulnhub靶场——DC-1
记一次Vulnhub靶场练习记录 靶机DC-1下载地址: 官方地址 https://download.vulnhub.com/dc/DC-1.zip 该靶场共有5个flag,下面我们一个一个寻找 打开 ...
- 微服务网关1-Spring Cloud Gateway简介
一.网关基本概念 1.API网关介绍 API 网关出现的原因是微服务架构的出现,不同的微服务一般会有不同的网络地址,而外部客户端可能需要调用多个服务的接口才能完成一个业务需求,如果让客户端直接与各 ...
- 关于Vue v-model你需要知道的一切
v-model是Vue的一个指令,它提供了input和form数据之间或两个组件之间的双向数据绑定. 这在Vue开发中是一个简单的概念,但是v-model的真正威力需要一些时间才能理解. 到本教程结 ...
- HTTP/1HTTP/2HTTP/3
https://mp.weixin.qq.com/s/fy84edOix5tGgcvdFkJi2w
- (Oracle)取当前日期的最近工作日
描述:现有一需求,日期表中存放了日期和是否节假日(0-工作日,1-节假日),现在需要取日期表中的最近的工作日.如2017/07/23(周日)最近的工作日应该是2017/07/21(周五). ...
- WebRTC 泄漏真实 IP 地址
WebRTC(网页即时通信,Web Real-Time Communication) 它允许浏览器内进行实时语音或视频对话,而无需添加额外的浏览器扩展.包括 Chrome.Firefox.Opera. ...
- ThinkPHP3.2.4 order方法注入
漏洞详情: 漏洞文件:./ThinkPHP\Library\Think\Db\Driver.class.php 中的 parseOrder方法: 这也是继上次order方法注入之后的修复手段. 可以看 ...
- Folly解读(零) Fbstring—— 一个完美替代std::string的库
string 常见的三种实现方式 eager copy COW SSO Fbstring 介绍 Storage strategies Implementation highlights Benchma ...