This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub hitonanode/cplib-cpp
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A" // DUMMY #include "../link_cut_tree.hpp" #include "../../modint.hpp" #include "../../random/xorshift.hpp" #include <algorithm> #include <cassert> #include <cstdio> #include <unordered_set> #include <utility> using namespace std; constexpr int md = 998244353; const int NTRY = 10000; const int VMAX = 100; const int QPERTRY = 500; using mint = ModInt<md>; using S = pair<int, mint>; using F = pair<bool, mint>; S op(S l, S r) { return {l.first + r.first, l.second + r.second}; } S mapping(F f, S x) { return f.first ? S{x.first, x.second + x.first * f.second} : x; } S reversal(S x) { return x; } F composition(F fnew, F gold) { return fnew.first ? F{true, fnew.second + gold.second} : gold; } F id() { return F{false, 0}; } using LCT = lazy_linkcuttree<S, F, op, reversal, mapping, composition, id>; vector<int> connected_vertices(int N, int r, const vector<unordered_set<int>> &to) { vector<int> visited(N); vector<int> ret, tmp{r}; while (tmp.size()) { int now = tmp.back(); tmp.pop_back(); ret.push_back(now); visited[now] = 1; for (auto nxt : to[now]) { if (!visited[nxt]) tmp.push_back(nxt); } } return ret; } vector<int> get_rev_path(int s, int t, int prv, const vector<unordered_set<int>> &to) { if (s == t) return {s}; for (auto nxt : to[s]) { if (nxt == prv) continue; auto v = get_rev_path(nxt, t, s, to); if (v.size()) { v.push_back(s); return v; } } return {}; } mint gen_random_mint() { return mint(rand_int() % mint::mod()); } int main() { for (int ntry = 0; ntry < NTRY; ntry++) { const int N = 2 + rand_int() % (VMAX - 1); const int W = rand_int() % N + 1; vector<mint> A(N); LCT tree; vector<LCT::Node *> nodes; for (int i = 0; i < N; i++) { A[i] = gen_random_mint(); nodes.push_back(tree.make_node({1, A[i]})); } vector<pair<int, int>> edges; vector<unordered_set<int>> to(N); for (int i = 1; i < N; i++) { int j = max<int>(0, i - 1 - rand_int() % W); edges.emplace_back(i, j); to[i].insert(j); to[j].insert(i); tree.link(nodes[i], nodes[j]); } for (int q = 0; q < QPERTRY; q++) { const int tp = rand_int() % 5; if (tp == 0) { // cut() & link() int e = rand_int() % edges.size(); int a = edges[e].first, b = edges[e].second; to[a].erase(b), to[b].erase(a); tree.cut(nodes[a], nodes[b]); vector<int> va = connected_vertices(N, a, to), vb = connected_vertices(N, b, to); assert(int(va.size() + vb.size()) == N); a = va[rand_int() % va.size()], b = vb[rand_int() % vb.size()]; to[a].insert(b), to[b].insert(a); edges[e] = {a, b}; tree.link(nodes[a], nodes[b]); } else if (tp == 1) { // apply() const int u = rand_int() % N, v = rand_int() % N; const auto a = gen_random_mint(); tree.apply(nodes[u], nodes[v], {true, a}); for (auto i : get_rev_path(u, v, -1, to)) A[i] += a; } else if (tp == 2) { // prod() const int u = rand_int() % N, v = rand_int() % N; mint ret1 = tree.prod(nodes[u], nodes[v]).second, ret2 = 0; for (auto i : get_rev_path(v, u, -1, to)) ret2 += A[i]; assert(ret1 == ret2); } else if (tp == 3) { // set() const int u = rand_int() % N; const auto a = gen_random_mint(); tree.set(nodes[u], {1, a}); A[u] = a; } else if (tp == 4) { // get() const int u = rand_int() % N; const mint a = tree.get(nodes[u]).second; assert(a == A[u]); } else { exit(8); } } } puts("Hello World"); }
#line 1 "data_structure/test/link_cut_tree.pathadd.stress.test.cpp" #define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A" // DUMMY #line 2 "data_structure/link_cut_tree.hpp" // CUT begin // Link-Cut Tree // Reference: // - https://www.slideshare.net/iwiwi/2-12188845 // - https://ei1333.github.io/library/structure/lct/link-cut-tree-lazy-path.cpp template <class S, class F, S (*op)(S, S), S (*reversal)(S), S (*mapping)(F, S), F (*composition)(F, F), F (*id)()> class lazy_linkcuttree { public: struct Node { Node *l, *r, *p; S d, sum; F lz; bool is_reversed; int sz; Node(S val) : l(nullptr), r(nullptr), p(nullptr), d(val), sum(val), lz(id()), is_reversed(false), sz(1) {} bool is_root() const { return !p || (p->l != this and p->r != this); } template <class OStream> friend OStream &operator<<(OStream &os, const Node &n) { os << '['; if (n.l) os << *(n.l) << ','; os << n.d << ','; if (n.r) os << *(n.r); return os << ']'; } }; protected: void update(Node *t) { if (t == nullptr) return; t->sz = 1; t->sum = t->d; if (t->l) { t->sz += t->l->sz; t->sum = op(t->l->sum, t->sum); } if (t->r) { t->sz += t->r->sz; t->sum = op(t->sum, t->r->sum); } } void all_apply(Node *a, F b) { a->d = mapping(b, a->d); a->sum = mapping(b, a->sum); a->lz = composition(b, a->lz); } void _toggle(Node *t) { auto tmp = t->l; t->l = t->r, t->r = tmp; t->sum = reversal(t->sum); t->is_reversed ^= true; } void push(Node *&t) { if (t->lz != id()) { if (t->l) all_apply(t->l, t->lz); if (t->r) all_apply(t->r, t->lz); t->lz = id(); } if (t->is_reversed) { if (t->l) _toggle(t->l); if (t->r) _toggle(t->r); t->is_reversed = false; } } void _rot_r(Node *t) { Node *x = t->p, *y = x->p; if ((x->l = t->r)) t->r->p = x; t->r = x, x->p = t; update(x), update(t); if ((t->p = y)) { if (y->l == x) y->l = t; if (y->r == x) y->r = t; update(y); } } void _rot_l(Node *t) { Node *x = t->p, *y = x->p; if ((x->r = t->l)) t->l->p = x; t->l = x, x->p = t; update(x), update(t); if ((t->p = y)) { if (y->l == x) y->l = t; if (y->r == x) y->r = t; update(y); } } void _splay(Node *t) { push(t); while (!t->is_root()) { Node *q = t->p; if (q->is_root()) { push(q), push(t); if (q->l == t) _rot_r(t); else _rot_l(t); } else { Node *r = q->p; push(r), push(q), push(t); if (r->l == q) { if (q->l == t) _rot_r(q), _rot_r(t); else _rot_l(t), _rot_r(t); } else { if (q->r == t) _rot_l(q), _rot_l(t); else _rot_r(t), _rot_l(t); } } } } public: [[nodiscard]] Node *make_node(S val) { return new Node(val); } void evert(Node *t) { expose(t), _toggle(t), push(t); } Node *expose(Node *t) { Node *rp = nullptr; for (Node *cur = t; cur; cur = cur->p) { _splay(cur); cur->r = rp; update(cur); rp = cur; } _splay(t); return rp; } void link(Node *chi, Node *par) { evert(chi); expose(par); chi->p = par; par->r = chi; update(par); } void cut(Node *chi) { expose(chi); Node *par = chi->l; chi->l = nullptr; update(chi); par->p = nullptr; } void cut(Node *u, Node *v) { evert(u), cut(v); } Node *lca(Node *u, Node *v) { return expose(u), expose(v); } void set(Node *t, S x) { expose(t), t->d = x, update(t); } S get(Node *t) { return expose(t), t->d; } void apply(Node *u, Node *v, const F &x) { evert(u); expose(v); all_apply(v, x); push(v); } S prod(Node *u, Node *v) { evert(u); expose(v); return v->sum; } Node *kth_parent(Node *t, int k) { expose(t); while (t) { push(t); if (t->r and t->r->sz > k) { t = t->r; } else { if (t->r) k -= t->r->sz; if (k == 0) return t; k--; t = t->l; } } return nullptr; } bool is_connected(Node *u, Node *v) { expose(u), expose(v); return u == v or u->p; } }; /* example usage: struct S { int sz, sum, lhi, rhi, inhi; S(int x) : sz(1), sum(x), lhi(x), rhi(x), inhi(x) {} S(int sz_, int sum_, int lhi_, int rhi_, int inhi_) : sz(sz_), sum(sum_), lhi(lhi_), rhi(rhi_), inhi(inhi_) {} }; using F = pair<bool, int>; S op(S l, S r) { return S(l.sz + r.sz, l.sum + r.sum, max(l.sum + r.lhi, l.lhi), max(l.rhi + r.sum, r.rhi), max<int>({l.inhi, r.inhi, l.rhi + r.lhi})); } S reversal(S x) { return S(x.sz, x.sum, x.rhi, x.lhi, x.inhi); } S mapping(F f, S x) { if (f.first) { auto v = f.second; auto sum = x.sz * v; return S{x.sz, sum, max(v, sum), max(v, sum), max(v, sum)}; } else { return x; } } F composition(F fnew, F gold) { return fnew.first ? fnew : gold; } F id() { return {false, 0}; } using LCT = lazy_linkcuttree<S, F, op, reversal, mapping, composition, id>; vector<LCT::Node*> vs; */ #line 2 "modint.hpp" #include <cassert> #include <iostream> #include <set> #include <vector> template <int md> struct ModInt { using lint = long long; constexpr static int mod() { return md; } static int get_primitive_root() { static int primitive_root = 0; if (!primitive_root) { primitive_root = [&]() { std::set<int> fac; int v = md - 1; for (lint i = 2; i * i <= v; i++) while (v % i == 0) fac.insert(i), v /= i; if (v > 1) fac.insert(v); for (int g = 1; g < md; g++) { bool ok = true; for (auto i : fac) if (ModInt(g).pow((md - 1) / i) == 1) { ok = false; break; } if (ok) return g; } return -1; }(); } return primitive_root; } int val_; int val() const noexcept { return val_; } constexpr ModInt() : val_(0) {} constexpr ModInt &_setval(lint v) { return val_ = (v >= md ? v - md : v), *this; } constexpr ModInt(lint v) { _setval(v % md + md); } constexpr explicit operator bool() const { return val_ != 0; } constexpr ModInt operator+(const ModInt &x) const { return ModInt()._setval((lint)val_ + x.val_); } constexpr ModInt operator-(const ModInt &x) const { return ModInt()._setval((lint)val_ - x.val_ + md); } constexpr ModInt operator*(const ModInt &x) const { return ModInt()._setval((lint)val_ * x.val_ % md); } constexpr ModInt operator/(const ModInt &x) const { return ModInt()._setval((lint)val_ * x.inv().val() % md); } constexpr ModInt operator-() const { return ModInt()._setval(md - val_); } constexpr ModInt &operator+=(const ModInt &x) { return *this = *this + x; } constexpr ModInt &operator-=(const ModInt &x) { return *this = *this - x; } constexpr ModInt &operator*=(const ModInt &x) { return *this = *this * x; } constexpr ModInt &operator/=(const ModInt &x) { return *this = *this / x; } friend constexpr ModInt operator+(lint a, const ModInt &x) { return ModInt(a) + x; } friend constexpr ModInt operator-(lint a, const ModInt &x) { return ModInt(a) - x; } friend constexpr ModInt operator*(lint a, const ModInt &x) { return ModInt(a) * x; } friend constexpr ModInt operator/(lint a, const ModInt &x) { return ModInt(a) / x; } constexpr bool operator==(const ModInt &x) const { return val_ == x.val_; } constexpr bool operator!=(const ModInt &x) const { return val_ != x.val_; } constexpr bool operator<(const ModInt &x) const { return val_ < x.val_; } // To use std::map<ModInt, T> friend std::istream &operator>>(std::istream &is, ModInt &x) { lint t; return is >> t, x = ModInt(t), is; } constexpr friend std::ostream &operator<<(std::ostream &os, const ModInt &x) { return os << x.val_; } constexpr ModInt pow(lint n) const { ModInt ans = 1, tmp = *this; while (n) { if (n & 1) ans *= tmp; tmp *= tmp, n >>= 1; } return ans; } static constexpr int cache_limit = std::min(md, 1 << 21); static std::vector<ModInt> facs, facinvs, invs; constexpr static void _precalculation(int N) { const int l0 = facs.size(); if (N > md) N = md; if (N <= l0) return; facs.resize(N), facinvs.resize(N), invs.resize(N); for (int i = l0; i < N; i++) facs[i] = facs[i - 1] * i; facinvs[N - 1] = facs.back().pow(md - 2); for (int i = N - 2; i >= l0; i--) facinvs[i] = facinvs[i + 1] * (i + 1); for (int i = N - 1; i >= l0; i--) invs[i] = facinvs[i] * facs[i - 1]; } constexpr ModInt inv() const { if (this->val_ < cache_limit) { if (facs.empty()) facs = {1}, facinvs = {1}, invs = {0}; while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2); return invs[this->val_]; } else { return this->pow(md - 2); } } constexpr ModInt fac() const { while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2); return facs[this->val_]; } constexpr ModInt facinv() const { while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2); return facinvs[this->val_]; } constexpr ModInt doublefac() const { lint k = (this->val_ + 1) / 2; return (this->val_ & 1) ? ModInt(k * 2).fac() / (ModInt(2).pow(k) * ModInt(k).fac()) : ModInt(k).fac() * ModInt(2).pow(k); } constexpr ModInt nCr(int r) const { if (r < 0 or this->val_ < r) return ModInt(0); return this->fac() * (*this - r).facinv() * ModInt(r).facinv(); } constexpr ModInt nPr(int r) const { if (r < 0 or this->val_ < r) return ModInt(0); return this->fac() * (*this - r).facinv(); } static ModInt binom(int n, int r) { static long long bruteforce_times = 0; if (r < 0 or n < r) return ModInt(0); if (n <= bruteforce_times or n < (int)facs.size()) return ModInt(n).nCr(r); r = std::min(r, n - r); ModInt ret = ModInt(r).facinv(); for (int i = 0; i < r; ++i) ret *= n - i; bruteforce_times += r; return ret; } // Multinomial coefficient, (k_1 + k_2 + ... + k_m)! / (k_1! k_2! ... k_m!) // Complexity: O(sum(ks)) template <class Vec> static ModInt multinomial(const Vec &ks) { ModInt ret{1}; int sum = 0; for (int k : ks) { assert(k >= 0); ret *= ModInt(k).facinv(), sum += k; } return ret * ModInt(sum).fac(); } // Catalan number, C_n = binom(2n, n) / (n + 1) // C_0 = 1, C_1 = 1, C_2 = 2, C_3 = 5, C_4 = 14, ... // https://oeis.org/A000108 // Complexity: O(n) static ModInt catalan(int n) { if (n < 0) return ModInt(0); return ModInt(n * 2).fac() * ModInt(n + 1).facinv() * ModInt(n).facinv(); } ModInt sqrt() const { if (val_ == 0) return 0; if (md == 2) return val_; if (pow((md - 1) / 2) != 1) return 0; ModInt b = 1; while (b.pow((md - 1) / 2) == 1) b += 1; int e = 0, m = md - 1; while (m % 2 == 0) m >>= 1, e++; ModInt x = pow((m - 1) / 2), y = (*this) * x * x; x *= (*this); ModInt z = b.pow(m); while (y != 1) { int j = 0; ModInt t = y; while (t != 1) j++, t *= t; z = z.pow(1LL << (e - j - 1)); x *= z, z *= z, y *= z; e = j; } return ModInt(std::min(x.val_, md - x.val_)); } }; template <int md> std::vector<ModInt<md>> ModInt<md>::facs = {1}; template <int md> std::vector<ModInt<md>> ModInt<md>::facinvs = {1}; template <int md> std::vector<ModInt<md>> ModInt<md>::invs = {0}; using ModInt998244353 = ModInt<998244353>; // using mint = ModInt<998244353>; // using mint = ModInt<1000000007>; #line 2 "random/xorshift.hpp" #include <cstdint> // CUT begin uint32_t rand_int() // XorShift random integer generator { static uint32_t x = 123456789, y = 362436069, z = 521288629, w = 88675123; uint32_t t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } double rand_double() { return (double)rand_int() / UINT32_MAX; } #line 5 "data_structure/test/link_cut_tree.pathadd.stress.test.cpp" #include <algorithm> #line 8 "data_structure/test/link_cut_tree.pathadd.stress.test.cpp" #include <cstdio> #include <unordered_set> #include <utility> using namespace std; constexpr int md = 998244353; const int NTRY = 10000; const int VMAX = 100; const int QPERTRY = 500; using mint = ModInt<md>; using S = pair<int, mint>; using F = pair<bool, mint>; S op(S l, S r) { return {l.first + r.first, l.second + r.second}; } S mapping(F f, S x) { return f.first ? S{x.first, x.second + x.first * f.second} : x; } S reversal(S x) { return x; } F composition(F fnew, F gold) { return fnew.first ? F{true, fnew.second + gold.second} : gold; } F id() { return F{false, 0}; } using LCT = lazy_linkcuttree<S, F, op, reversal, mapping, composition, id>; vector<int> connected_vertices(int N, int r, const vector<unordered_set<int>> &to) { vector<int> visited(N); vector<int> ret, tmp{r}; while (tmp.size()) { int now = tmp.back(); tmp.pop_back(); ret.push_back(now); visited[now] = 1; for (auto nxt : to[now]) { if (!visited[nxt]) tmp.push_back(nxt); } } return ret; } vector<int> get_rev_path(int s, int t, int prv, const vector<unordered_set<int>> &to) { if (s == t) return {s}; for (auto nxt : to[s]) { if (nxt == prv) continue; auto v = get_rev_path(nxt, t, s, to); if (v.size()) { v.push_back(s); return v; } } return {}; } mint gen_random_mint() { return mint(rand_int() % mint::mod()); } int main() { for (int ntry = 0; ntry < NTRY; ntry++) { const int N = 2 + rand_int() % (VMAX - 1); const int W = rand_int() % N + 1; vector<mint> A(N); LCT tree; vector<LCT::Node *> nodes; for (int i = 0; i < N; i++) { A[i] = gen_random_mint(); nodes.push_back(tree.make_node({1, A[i]})); } vector<pair<int, int>> edges; vector<unordered_set<int>> to(N); for (int i = 1; i < N; i++) { int j = max<int>(0, i - 1 - rand_int() % W); edges.emplace_back(i, j); to[i].insert(j); to[j].insert(i); tree.link(nodes[i], nodes[j]); } for (int q = 0; q < QPERTRY; q++) { const int tp = rand_int() % 5; if (tp == 0) { // cut() & link() int e = rand_int() % edges.size(); int a = edges[e].first, b = edges[e].second; to[a].erase(b), to[b].erase(a); tree.cut(nodes[a], nodes[b]); vector<int> va = connected_vertices(N, a, to), vb = connected_vertices(N, b, to); assert(int(va.size() + vb.size()) == N); a = va[rand_int() % va.size()], b = vb[rand_int() % vb.size()]; to[a].insert(b), to[b].insert(a); edges[e] = {a, b}; tree.link(nodes[a], nodes[b]); } else if (tp == 1) { // apply() const int u = rand_int() % N, v = rand_int() % N; const auto a = gen_random_mint(); tree.apply(nodes[u], nodes[v], {true, a}); for (auto i : get_rev_path(u, v, -1, to)) A[i] += a; } else if (tp == 2) { // prod() const int u = rand_int() % N, v = rand_int() % N; mint ret1 = tree.prod(nodes[u], nodes[v]).second, ret2 = 0; for (auto i : get_rev_path(v, u, -1, to)) ret2 += A[i]; assert(ret1 == ret2); } else if (tp == 3) { // set() const int u = rand_int() % N; const auto a = gen_random_mint(); tree.set(nodes[u], {1, a}); A[u] = a; } else if (tp == 4) { // get() const int u = rand_int() % N; const mint a = tree.get(nodes[u]).second; assert(a == A[u]); } else { exit(8); } } } puts("Hello World"); }