树链剖分+线段树
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
#include <iostream> #include <cstring> #include <algorithm> #define ls(x) (x << 1) #define rs(x) (x << 1 | 1) using namespace std; typedef long long LL; const int N = 5e5 + 10, M = N << 1; int n, m, root, p; int h[N], e[M], ne[M], idx; int dfn[N], rnk[N], tot; int fa[N], son[N], siz[N], top[N], dep[N]; int w[N]; struct node { int l, r; LL v, add; } tr[N << 2]; void pushup(int u) { tr[u].v = (tr[ls(u)].v + tr[rs(u)].v) % p; } void pushdown(int u) { auto &rt = tr[u], &l = tr[ls(u)], &r = tr[rs(u)]; if (rt.add) { l.v = (l.v + rt.add * (l.r - l.l + 1) % p) % p; l.add = (l.add + rt.add) % p; r.v = (r.v + rt.add * (r.r - r.l + 1) % p) % p; r.add = (r.add + rt.add) % p; rt.add = 0; } } void build(int u, int l, int r) { if (l == r) tr[u] = {l, r, w[rnk[r]], 0}; else { tr[u] = {l, r}; int mid = l + r >> 1; build(ls(u), l, mid), build(rs(u), mid + 1, r); pushup(u); } } void modify(int u, int l, int r, int d) { if (tr[u].l >= l && tr[u].r <= r) { tr[u].v = (tr[u].v + (tr[u].r - tr[u].l + 1) * d % p) % p; tr[u].add = (tr[u].add + d) % p; } else { pushdown(u); int mid = tr[u].l + tr[u].r >> 1; if (l <= mid) modify(ls(u), l, r, d); if (r > mid) modify(rs(u), l, r, d); pushup(u); } } int query(int u, int l, int r) { if (tr[u].l >= l && tr[u].r <= r) return tr[u].v; pushdown(u); int mid = tr[u].l + tr[u].r >> 1; int res = 0; if (l <= mid) res = (res + query(ls(u), l, r)) % p; if (r > mid) res = (res + query(rs(u), l, r)) % p; return res; } void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx ++; } void dfs1(int p, int depth, int father) { fa[p] = father; dep[p] = depth; siz[p] = 1; for (int i = h[p]; ~i; i = ne[i]) { int j = e[i]; if (j == father) continue; dfs1(j, depth + 1, p); if (siz[j] > siz[son[p]]) son[p] = j; siz[p] += siz[j]; } } void dfs2(int p, int tp) { top[p] = tp; dfn[p] = ++ tot; rnk[tot] = p; if (!son[p]) return; dfs2(son[p], tp); for (int i = h[p]; ~i; i = ne[i]) { int j = e[i]; if (j != fa[p] && j != son[p]) dfs2(j, j); } } int ask(int x, int y) { int res = 0; while (top[x] != top[y]) { if (dep[top[x]] < dep[top[y]]) swap(x, y); res = (res + query(1, dfn[top[x]], dfn[x])) % p; x = fa[top[x]]; } if (dep[x] > dep[y]) swap(x, y); res = (res + query(1, dfn[x], dfn[y])) % p; return res; } void change(int x, int y, int z) { while (top[x] != top[y]) { if (dep[top[x]] < dep[top[y]]) swap(x, y); modify(1, dfn[top[x]], dfn[x], z); x = fa[top[x]]; } if (dep[x] > dep[y]) swap(x, y); modify(1, dfn[x], dfn[y], z); } int main() { memset(h, -1, sizeof h); scanf("%d%d%d%d", &n, &m, &root, &p); for (int i = 1; i <= n; ++ i) scanf("%d", w + i); for (int i = 1; i < n; ++ i) { int a, b; scanf("%d%d", &a, &b); add(a, b), add(b, a); } dfs1(root, 1, root); dfs2(root, root); build(1, 1, n); while (m --) { int op, x, y, z; scanf("%d", &op); if (op == 1) { scanf("%d%d%d", &x, &y, &z); change(x, y, z); } else if (op == 2) { scanf("%d%d", &x, &y); printf("%d\n", ask(x, y)); } else if (op == 3) { scanf("%d%d", &x, &z); modify(1, dfn[x], dfn[x] + siz[x] - 1, z); } else { scanf("%d", &x); printf("%d\n", query(1, dfn[x], dfn[x] + siz[x] - 1)); } } } |
发表回复