diff --git a/LibreOJ/2558/2558.cpp b/LibreOJ/2558/2558.cpp new file mode 100644 index 00000000..20b51560 --- /dev/null +++ b/LibreOJ/2558/2558.cpp @@ -0,0 +1,194 @@ +#include +#include +#include +#include +#include +#include +#include + +const int N = 5e4 + 5; +const int mod = 201314; + +using std::cin; +using std::cout; +const char endl = '\n'; + +int n, q, now, ans[N]; +int cnt, id[N], fa[N], dep[N], siz[N], top[N], son[N]; +std::vector g[N]; +std::vector> qs; + +struct node { + int l, r, s, d; + + node(const int &_l = 0, const int &_r = 0) + : l(_l), r(_r), s(0), d(0) {} +} tr[N << 2]; + +void pushup(int u) { + tr[u].s = tr[u << 1].s + tr[u << 1 | 1].s; +} + +void pushdown(int u) { + if (!tr[u].d) return; + + tr[u << 1].s += (tr[u << 1].r - tr[u << 1].l + 1) * tr[u].d; + tr[u << 1].d += tr[u].d; + + tr[u << 1 | 1].s += (tr[u << 1 | 1].r - tr[u << 1 | 1].l + 1) * tr[u].d; + tr[u << 1 | 1].d += tr[u].d; + + tr[u].d = 0; +} + +void build(int u, int l, int r) { + tr[u] = node(l, r); + + if (l == r) 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, int x) { + if (l <= tr[u].l && tr[u].r <= r) { + tr[u].s += (tr[u].r - tr[u].l + 1) * x; + tr[u].d += x; + + return; + } + + pushdown(u); + + int mid = (tr[u].l + tr[u].r) >> 1; + + if (l <= mid) modify(u << 1, l, r, x); + if (r > mid) modify(u << 1 | 1, l, r, x); + + pushup(u); +} + +int query(int u, int l, int r) { + if (l <= tr[u].l && tr[u].r <= r) { + return tr[u].s; + } + + pushdown(u); + + int mid = (tr[u].l + tr[u].r) >> 1; + int res = 0; + + if (l <= mid) res += query(u << 1, l, r); + if (r > mid) res += query(u << 1 | 1, l, r); + + return res; +} + +void dfs1(int u, int f) { + dep[u] = dep[f] + 1; + fa[u] = f; + 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; + top[u] = t; + + if (son[u]) dfs2(son[u], t); + + for (int v : g[u]) { + if (v == fa[u] || v == son[u]) continue; + + dfs2(v, v); + } +} + +void modify_path(int u, int v, int x) { + while (top[u] != top[v]) { + if (dep[top[u]] < dep[top[v]]) std::swap(u, v); + + modify(1, id[top[u]], id[u], x); + u = fa[top[u]]; + } + + if (dep[u] < dep[v]) std::swap(u, v); + + modify(1, id[v], id[u], x); +} + +int query_path(int u, int v) { + int res = 0; + + while (top[u] != top[v]) { + if (dep[top[u]] < dep[top[v]]) std::swap(u, v); + + res += query(1, id[top[u]], id[u]); + u = fa[top[u]]; + } + + if (dep[u] < dep[v]) std::swap(u, v); + + res += query(1, id[v], id[u]); + + return res; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> q; + + for (int i = 2, x; i <= n; i++) { + cin >> x; + + x++; + g[i].emplace_back(x); + g[x].emplace_back(i); + } + + dfs1(1, 1); + dfs2(1, 1); + build(1, 1, n); + + for (int i = 1, l, r, z; i <= q; i++) { + cin >> l >> r >> z; + + l++, r++, z++; + + qs.emplace_back(i, l - 1, z, -1); + qs.emplace_back(i, r, z, 1); + } + + std::sort(qs.begin(), qs.end(), [&](const decltype(qs)::value_type &lhs, const decltype(qs)::value_type &rhs) -> bool { + return std::get<1>(lhs) < std::get<1>(rhs); + }); + + for (auto o : qs) { + int id, pos, z, k; + + std::tie(id, pos, z, k) = o; + + while (now < pos) modify_path(1, ++now, 1); + + ans[id] = ((ans[id] + k * query_path(1, z)) % mod + mod) % mod; + } + + std::copy_n(ans + 1, q, std::ostream_iterator(cout, "\n")); + + return 0; +} diff --git a/LibreOJ/2558/data/1.in b/LibreOJ/2558/data/1.in new file mode 100644 index 00000000..9ca0d0d2 --- /dev/null +++ b/LibreOJ/2558/data/1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:284ce2cfe72beaa632d133e518858781c7bf2b1b7e92a3945873e774d9277cbc +size 1141528 diff --git a/LibreOJ/2558/data/1.out b/LibreOJ/2558/data/1.out new file mode 100644 index 00000000..d4d64ec6 --- /dev/null +++ b/LibreOJ/2558/data/1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ba3df9ba13d7f4c036d58c6faa59915811253de243e1f9d4af588e265e7058d7 +size 322364 diff --git a/LibreOJ/2558/data/2.in b/LibreOJ/2558/data/2.in new file mode 100644 index 00000000..55c660b4 --- /dev/null +++ b/LibreOJ/2558/data/2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:199629f0ced796a24340e3c7c2904c1d56c0ad086843d4d6ab1eea075e1a22a2 +size 903230 diff --git a/LibreOJ/2558/data/2.out b/LibreOJ/2558/data/2.out new file mode 100644 index 00000000..7d49ff5f --- /dev/null +++ b/LibreOJ/2558/data/2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6415df67bf3da115419315345f99d248f7c9769f4a2a23219d922cc5fb3c7512 +size 257732 diff --git a/LibreOJ/2558/data/3.in b/LibreOJ/2558/data/3.in new file mode 100644 index 00000000..965ab0fa --- /dev/null +++ b/LibreOJ/2558/data/3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a8ada6561be4ed02cdb24a3f157ac5160746da9860552d6023df25ba2a77e135 +size 193751 diff --git a/LibreOJ/2558/data/3.out b/LibreOJ/2558/data/3.out new file mode 100644 index 00000000..d4882868 --- /dev/null +++ b/LibreOJ/2558/data/3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3f21cf7c70325106473f3b2e55b55561e70e63c3ed2a1078851b593e4f0eac26 +size 64521 diff --git a/LibreOJ/2558/data/4.in b/LibreOJ/2558/data/4.in new file mode 100644 index 00000000..6d0fdd71 --- /dev/null +++ b/LibreOJ/2558/data/4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f9b84c49a1cf1387e9dc220483fad64591bf7ac6f970164267321569c757c7f8 +size 666888 diff --git a/LibreOJ/2558/data/4.out b/LibreOJ/2558/data/4.out new file mode 100644 index 00000000..83cf4efb --- /dev/null +++ b/LibreOJ/2558/data/4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1cb1ff760204eaaa597ce2eca1776db4211af60c4794bf3a39314b4113e6b465 +size 193506 diff --git a/LibreOJ/2558/data/5.in b/LibreOJ/2558/data/5.in new file mode 100644 index 00000000..256cb05b --- /dev/null +++ b/LibreOJ/2558/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3d07e8d512e600aeccb1e046c8d0467cbd1a23d6ced5469e8cc1766573bc6746 +size 433499 diff --git a/LibreOJ/2558/data/5.out b/LibreOJ/2558/data/5.out new file mode 100644 index 00000000..30c652f4 --- /dev/null +++ b/LibreOJ/2558/data/5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4abc6f558d0eaef9c10959545b9d60e04a8fa9eeab7b49c5b1c36b59cb903986 +size 129057