0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-08 14:58:48 +00:00

P3384 【模板】轻重链剖分/树链剖分

https://www.luogu.com.cn/record/92005288
This commit is contained in:
Baoshuo Ren 2022-10-29 11:10:45 +08:00
parent 28b2dc42c4
commit 719bb0ec83
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

View File

@ -1,79 +1,26 @@
#include <iostream> #include <iostream>
#include <algorithm>
#include <vector> #include <vector>
using std::cin; using std::cin;
using std::cout; using std::cout;
using std::endl; const char endl = '\n';
const int N = 100005; const int N = 1e5 + 5;
int p, a[N]; int n, m, r, p, a[N];
std::vector<int> g[N]; std::vector<int> g[N];
// Segment Tree
void build(int, int, int);
// Heavy Path Decomposition
int cnt, id[N], rid[N], fa[N], dep[N], siz[N], son[N], top[N]; int cnt, id[N], rid[N], fa[N], dep[N], siz[N], son[N], top[N];
void dfs1(int, int);
void dfs2(int, int);
void modify_path(int, int, int);
void modify_tree(int, int);
long long query_path(int, int);
long long query_tree(int);
int main() {
int n, m, r;
cin >> n >> m >> r >> p;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs1(r, r);
dfs2(r, r);
build(1, 1, n);
while (m--) {
int op, x, y, z;
cin >> op;
if (op == 1) {
cin >> x >> y >> z;
modify_path(x, y, z);
} else if (op == 2) {
cin >> x >> y;
cout << query_path(x, y) << endl;
} else if (op == 3) {
cin >> x >> z;
modify_tree(x, z);
} else {
cin >> x;
cout << query_tree(x) << endl;
}
}
return 0;
}
// === Segment Tree ===
struct node { struct node {
int l, r; int l, r;
long long s, d; long long s, d;
node() node()
: l(0), : l(0), r(0), s(0), d(0) {}
r(0),
s(0),
d(0) {}
node(int _l, int _r) node(int _l, int _r)
: l(_l), : l(_l), r(_r), s(0), d(0) {}
r(_r),
s(0),
d(0) {}
} tr[N << 2]; } tr[N << 2];
inline void pushup(int u) { inline void pushup(int u) {
@ -82,22 +29,29 @@ inline void pushup(int u) {
inline void pushdown(int u) { inline void pushdown(int u) {
node &root = tr[u], &left = tr[u << 1], &right = tr[u << 1 | 1]; node &root = tr[u], &left = tr[u << 1], &right = tr[u << 1 | 1];
left.s = (left.s + (left.r - left.l + 1) * root.d) % p; left.s = (left.s + (left.r - left.l + 1) * root.d) % p;
left.d = (left.d + root.d) % p; left.d = (left.d + root.d) % p;
right.s = (right.s + (right.r - right.l + 1) * root.d) % p; right.s = (right.s + (right.r - right.l + 1) * root.d) % p;
right.d = (right.d + root.d) % p; right.d = (right.d + root.d) % p;
root.d = 0; root.d = 0;
} }
void build(int u, int l, int r) { void build(int u, int l, int r) {
tr[u] = node(l, r); tr[u] = node(l, r);
if (l == r) { if (l == r) {
tr[u].s = a[rid[l]] % p; tr[u].s = a[rid[l]] % p;
return; return;
} }
int mid = l + r >> 1; int mid = l + r >> 1;
build(u << 1, l, mid); build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r); build(u << 1 | 1, mid + 1, r);
pushup(u); pushup(u);
} }
@ -107,29 +61,35 @@ void modify(int u, int l, int r, int d) {
tr[u].d = (tr[u].d + d) % p; tr[u].d = (tr[u].d + d) % p;
return; return;
} }
pushdown(u); pushdown(u);
int mid = tr[u].l + tr[u].r >> 1; int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) modify(u << 1, l, r, d); if (l <= mid) modify(u << 1, l, r, d);
if (r > mid) modify(u << 1 | 1, l, r, d); if (r > mid) modify(u << 1 | 1, l, r, d);
pushup(u); pushup(u);
} }
long long query(int u, int l, int r) { long long query(int u, int l, int r) {
if (l <= tr[u].l && tr[u].r <= r) return tr[u].s % p; if (l <= tr[u].l && tr[u].r <= r) return tr[u].s % p;
pushdown(u); pushdown(u);
int mid = tr[u].l + tr[u].r >> 1; int mid = tr[u].l + tr[u].r >> 1;
long long res = 0; long long res = 0;
if (l <= mid) res = (res + query(u << 1, l, r)) % p; if (l <= mid) res = (res + query(u << 1, l, r)) % p;
if (r > mid) res = (res + query(u << 1 | 1, l, r)) % p; if (r > mid) res = (res + query(u << 1 | 1, l, r)) % p;
return res; return res;
} }
// == Heavy Path Decomposition ===
void dfs1(int u, int f) { void dfs1(int u, int f) {
fa[u] = f; fa[u] = f;
dep[u] = dep[f] + 1; dep[u] = dep[f] + 1;
siz[u] = 1; siz[u] = 1;
for (int v : g[u]) { for (int v : g[u]) {
if (v == f) continue; if (v == f) continue;
dfs1(v, u); dfs1(v, u);
@ -139,44 +99,106 @@ void dfs1(int u, int f) {
} }
void dfs2(int u, int t) { void dfs2(int u, int t) {
id[u] = ++cnt; rid[id[u] = ++cnt] = u;
rid[id[u]] = u;
top[u] = t; top[u] = t;
if (!son[u]) return; if (!son[u]) return;
dfs2(son[u], t); dfs2(son[u], t);
for (int v : g[u]) { for (int v : g[u]) {
if (v == fa[u]) continue; if (v == fa[u] || v == son[u]) continue;
if (v == son[u]) continue;
dfs2(v, v); dfs2(v, v);
} }
} }
void modify_path(int u, int v, int d) { void modify_path(int u, int v, int x) {
while (top[u] != top[v]) { while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) std::swap(u, v); if (dep[top[u]] < dep[top[v]]) std::swap(u, v);
modify(1, id[top[u]], id[u], d); modify(1, id[top[u]], id[u], x);
u = fa[top[u]]; u = fa[top[u]];
} }
if (dep[u] < dep[v]) std::swap(u, v); if (dep[u] < dep[v]) std::swap(u, v);
modify(1, id[v], id[u], d); modify(1, id[v], id[u], x);
} }
long long query_path(int u, int v) { int query_path(int u, int v) {
long long sum = 0; int res = 0;
while (top[u] != top[v]) { while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) std::swap(u, v); if (dep[top[u]] < dep[top[v]]) std::swap(u, v);
sum = (sum + query(1, id[top[u]], id[u])) % p; res = (res + query(1, id[top[u]], id[u])) % p;
u = fa[top[u]]; u = fa[top[u]];
} }
if (dep[u] < dep[v]) std::swap(u, v); if (dep[u] < dep[v]) std::swap(u, v);
sum = (sum + query(1, id[v], id[u])) % p; res = (res + query(1, id[v], id[u])) % p;
return sum;
return res;
} }
inline void modify_tree(int u, int d) { void modify_tree(int u, int x) {
modify(1, id[u], id[u] + siz[u] - 1, d); modify(1, id[u], id[u] + siz[u] - 1, x);
} }
inline long long query_tree(int u) { int query_tree(int u) {
return query(1, id[u], id[u] + siz[u] - 1); return query(1, id[u], id[u] + siz[u] - 1);
} }
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> r >> p;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1, x, y; i < n; i++) {
cin >> x >> y;
g[x].emplace_back(y);
g[y].emplace_back(x);
}
dfs1(r, r);
dfs2(r, r);
build(1, 1, n);
for (int i = 1; i <= m; i++) {
int op;
cin >> op;
if (op == 1) {
int x, y, z;
cin >> x >> y >> z;
modify_path(x, y, z);
} else if (op == 2) {
int x, y;
cin >> x >> y;
cout << query_path(x, y) << endl;
} else if (op == 3) {
int x, z;
cin >> x >> z;
modify_tree(x, z);
} else { // op == 4
int x;
cin >> x;
cout << query_tree(x) << endl;
}
}
return 0;
}