diff --git a/LibreOJ/2125/2125.cpp b/LibreOJ/2125/2125.cpp new file mode 100644 index 00000000..1db47bf3 --- /dev/null +++ b/LibreOJ/2125/2125.cpp @@ -0,0 +1,169 @@ +#include + +using std::cin; +using std::cout; +using std::endl; + +const int N = 100005; + +// Basic +int n, m, u, v, op, a[N]; +std::vector g[N]; + +// Heavy Path Decomposition +int cnt, w[N], id[N], fa[N], dep[N], son[N], siz[N], top[N]; +void dfs1(int, int); +void dfs2(int, int); +void modify_path(int, int, long long); +void modify_tree(int, long long); +long long query_path(int, int); + +// Segment Tree +void pushup(int); +void pushdown(int); +void build(int, int, int); +void modify(int, int, int, long long); +long long query(int, int, int); + +int main() { + cin >> n >> m; + for (int i = 1; i <= n; i++) { + cin >> a[i]; + } + for (int i = 1; i < n; i++) { + cin >> u >> v; + g[u].push_back(v); + g[v].push_back(u); + } + dfs1(1, 0); + dfs2(1, 1); + build(1, 1, n); + while (m--) { + cin >> op >> u; + if (op == 1) { + cin >> v; + modify_path(u, u, v); + } else if (op == 2) { + cin >> v; + modify_tree(u, v); + } else { + cout << query_path(1, u) << endl; + } + } + return 0; +} + +// === Heavy Path Decomposition === + +void dfs1(int u, int f) { + fa[u] = f; + dep[u] = dep[f] + 1; + siz[u] = 1; + for (int v : g[u]) { + if (v == f) continue; + dfs1(v, u); + siz[u] += siz[v]; + if (siz[son[u]] < siz[v]) son[u] = v; + } +} + +void dfs2(int u, int t) { + id[u] = ++cnt; + w[cnt] = a[u]; + top[u] = t; + if (!son[u]) return; + dfs2(son[u], t); + for (int v : g[u]) { + if (v == fa[u]) continue; + if (v == son[u]) continue; + dfs2(v, v); + } +} + +void modify_path(int u, int v, long long d) { + while (top[u] != top[v]) { + if (dep[top[u]] < dep[top[v]]) std::swap(u, v); + modify(1, id[top[u]], id[u], d); + u = fa[top[u]]; + } + if (dep[u] < dep[v]) std::swap(u, v); + modify(1, id[v], id[u], d); +} + +inline void modify_tree(int u, long long d) { + modify(1, id[u], id[u] + siz[u] - 1, d); +} + +long long query_path(int u, int v) { + long long sum = 0; + while (top[u] != top[v]) { + if (dep[top[u]] < dep[top[v]]) std::swap(u, v); + sum += query(1, id[top[u]], id[u]); + u = fa[top[u]]; + } + if (dep[u] < dep[v]) std::swap(u, v); + sum += query(1, id[v], id[u]); + return sum; +} + +// === Segment Tree === + +struct node { + int l, r; + long long s, d; + + node() + : l(0), r(0), s(0), d(0) {} + node(int _l, int _r) + : l(_l), r(_r), s(0), d(0) {} +} tr[N << 2]; + +inline void pushup(int u) { + tr[u].s = tr[u << 1].s + tr[u << 1 | 1].s; +} + +inline void pushdown(int u) { + auto &root = tr[u], &left = tr[u << 1], &right = tr[u << 1 | 1]; + left.s += (left.r - left.l + 1) * root.d; + left.d += root.d; + right.s += (right.r - right.l + 1) * root.d; + right.d += root.d; + root.d = 0; +} + +void build(int u, int l, int r) { + tr[u] = node(l, r); + if (l == r) { + tr[u].s = w[l]; + return; + } + int mid = l + r >> 1; + build(u << 1, l, mid); + build(u << 1 | 1, mid + 1, r); + pushup(u); +} + +void modify(int u, int l, int r, long long d) { + if (l <= tr[u].l && tr[u].r <= r) { + tr[u].d += d; + tr[u].s += (tr[u].r - tr[u].l + 1) * d; + return; + } + int mid = tr[u].l + tr[u].r >> 1; + pushdown(u); + if (l <= mid) modify(u << 1, l, r, d); + if (r > mid) modify(u << 1 | 1, l, r, d); + pushup(u); +} + +long long query(int u, int l, int r) { + if (l <= tr[u].l && tr[u].r <= r) { + return tr[u].s; + } + int mid = tr[u].l + tr[u].r >> 1; + long long sum = 0; + pushdown(u); + if (l <= mid) sum += query(u << 1, l, r); + if (r > mid) sum += query(u << 1 | 1, l, r); + return sum; +} diff --git a/LibreOJ/2125/data/data1.in b/LibreOJ/2125/data/data1.in new file mode 100644 index 00000000..2215e55e --- /dev/null +++ b/LibreOJ/2125/data/data1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b1f8d334e5025a91c79065887ec461c65189ff058b92ca0f7e128caebbf6a4d4 +size 28160 diff --git a/LibreOJ/2125/data/data1.out b/LibreOJ/2125/data/data1.out new file mode 100644 index 00000000..897faee3 --- /dev/null +++ b/LibreOJ/2125/data/data1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7d62e728efd4080d3e83d261cdd2203a98f5232cc6629709268b2dce8b558b8c +size 1967 diff --git a/LibreOJ/2125/data/data10.in b/LibreOJ/2125/data/data10.in new file mode 100644 index 00000000..56f103aa --- /dev/null +++ b/LibreOJ/2125/data/data10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:00d640b63aa4ab769b220f96275ccf0c04e08cc1d777a12bee558acaa774b01b +size 3247756 diff --git a/LibreOJ/2125/data/data10.out b/LibreOJ/2125/data/data10.out new file mode 100644 index 00000000..420bdec7 --- /dev/null +++ b/LibreOJ/2125/data/data10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:01c569d94e69f3b3fe941cb739039ee1f1836a05e9ab782164e266e59527a37d +size 340197 diff --git a/LibreOJ/2125/data/data2.in b/LibreOJ/2125/data/data2.in new file mode 100644 index 00000000..52449ef8 --- /dev/null +++ b/LibreOJ/2125/data/data2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4303e6b8346264655fd9325e14b45685c466f65d73d865f8dc15f843e26dc2a2 +size 28046 diff --git a/LibreOJ/2125/data/data2.out b/LibreOJ/2125/data/data2.out new file mode 100644 index 00000000..3f983143 --- /dev/null +++ b/LibreOJ/2125/data/data2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c191c22ea4849ca5893f9241788f67fe30d91c7956173a7de65c3c853eadd9fd +size 2083 diff --git a/LibreOJ/2125/data/data3.in b/LibreOJ/2125/data/data3.in new file mode 100644 index 00000000..77c1fdba --- /dev/null +++ b/LibreOJ/2125/data/data3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d2cc1c1732b37891a44a1758ba95ce8b4a13691434aae27b2e5663155f7bdd79 +size 28096 diff --git a/LibreOJ/2125/data/data3.out b/LibreOJ/2125/data/data3.out new file mode 100644 index 00000000..348b16ba --- /dev/null +++ b/LibreOJ/2125/data/data3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bac854429ac9397f0deddaaea9196c4cdf5d8f3c2e7b8dc6e1e738dc5eab00e0 +size 1980 diff --git a/LibreOJ/2125/data/data4.in b/LibreOJ/2125/data/data4.in new file mode 100644 index 00000000..96402aa4 --- /dev/null +++ b/LibreOJ/2125/data/data4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4fc0d9a3add4fe374b2435d237a7e46914cdaf26bb0a58ba0f3a74339a9e3183 +size 3408894 diff --git a/LibreOJ/2125/data/data4.out b/LibreOJ/2125/data/data4.out new file mode 100644 index 00000000..c96c4ce8 --- /dev/null +++ b/LibreOJ/2125/data/data4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:baf18353566e9235be3f8d910016f2e7ccd414a299df5d5900b4dc6edf5a5f87 +size 204927 diff --git a/LibreOJ/2125/data/data5.in b/LibreOJ/2125/data/data5.in new file mode 100644 index 00000000..1cd73c54 --- /dev/null +++ b/LibreOJ/2125/data/data5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:85288fcb128d2c14baa9e0cb5d601b183feb9b52b9c9d6ccb36cdfd9b94dee1a +size 3406236 diff --git a/LibreOJ/2125/data/data5.out b/LibreOJ/2125/data/data5.out new file mode 100644 index 00000000..3f751f1a --- /dev/null +++ b/LibreOJ/2125/data/data5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:745baf2dc5aa0e0392fbbf29e6294e467d4cb613c109f1119fb28f743869f7d8 +size 212342 diff --git a/LibreOJ/2125/data/data6.in b/LibreOJ/2125/data/data6.in new file mode 100644 index 00000000..27982600 --- /dev/null +++ b/LibreOJ/2125/data/data6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:928b4a6233236af75ff18d141b1f77e380e8c03a781a287f9c2c9dabd0292cdd +size 3291370 diff --git a/LibreOJ/2125/data/data6.out b/LibreOJ/2125/data/data6.out new file mode 100644 index 00000000..932e90ce --- /dev/null +++ b/LibreOJ/2125/data/data6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fd9b278b36bb4cf16a025a6a79a5152d88c5aa586642eef8dda462c86589fe50 +size 332864 diff --git a/LibreOJ/2125/data/data7.in b/LibreOJ/2125/data/data7.in new file mode 100644 index 00000000..ae06b055 --- /dev/null +++ b/LibreOJ/2125/data/data7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8df394de69a2715163bc14d72451f67963b45d0cd59ebe15391712b705aa3282 +size 3292662 diff --git a/LibreOJ/2125/data/data7.out b/LibreOJ/2125/data/data7.out new file mode 100644 index 00000000..e17be408 --- /dev/null +++ b/LibreOJ/2125/data/data7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fe77304fbbc217b8ea12737e96a697b020b327ebb2acd11f42b636ac281811f8 +size 330700 diff --git a/LibreOJ/2125/data/data8.in b/LibreOJ/2125/data/data8.in new file mode 100644 index 00000000..7114c6fd --- /dev/null +++ b/LibreOJ/2125/data/data8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c92c5f8ef90b9a77d8e873cbbd4032fb447f16da0cd3f71d5192610dcd22acc0 +size 3247404 diff --git a/LibreOJ/2125/data/data8.out b/LibreOJ/2125/data/data8.out new file mode 100644 index 00000000..3fb556bc --- /dev/null +++ b/LibreOJ/2125/data/data8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7ebb4fb1abda0fc6c88f99d4a62515d68095509135836949d7729505eed44c8f +size 340241 diff --git a/LibreOJ/2125/data/data9.in b/LibreOJ/2125/data/data9.in new file mode 100644 index 00000000..b199062d --- /dev/null +++ b/LibreOJ/2125/data/data9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b6872556b31645826da1bcb0b5b4f40d6f6341b24f5f9be7438d37865ff59791 +size 3247853 diff --git a/LibreOJ/2125/data/data9.out b/LibreOJ/2125/data/data9.out new file mode 100644 index 00000000..b5a5da64 --- /dev/null +++ b/LibreOJ/2125/data/data9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9976c2093bb04d9bb433819541618008e6557dfdfe6e970ea73915f43799d4c7 +size 339056