rcpl

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ruthen71/rcpl

:heavy_check_mark: segment_tree/test/segment_tree_rolling_hash.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"

#include <iostream>

#include "../../algebra/monoid/monoid_rolling_hash.hpp"
#include "../../segment_tree/segment_tree.hpp"
#include "../../math/modint261.hpp"

using mint = mint261;
using mrh = MonoidRollingHash<mint>;
template <> mint mrh::base = 0;

void test1_basic() {
    assert(mrh::commutative == false);
    assert(mrh::identity().hash == mint(0));
    assert(mrh::identity().power == mint(1));
}

void test2_sequence() {
    mint B = mrh::base;

    std::vector<Hash<mint>> a = {
        mrh::make_element(mint(3)),
        mrh::make_element(mint(1)),
        mrh::make_element(mint(4)),
    };
    SegmentTree<mrh> seg(a);

    assert(seg.prod(0, 0).hash == mint(0));
    assert(seg.prod(0, 0).power == mint(1));

    assert(seg.prod(0, 1).hash == mint(3));
    assert(seg.prod(0, 1).power == B);

    assert(seg.prod(0, 2).hash == mint(3) * B + mint(1));
    assert(seg.prod(0, 2).power == B * B);

    assert(seg.prod(0, 3).hash == mint(3) * B * B + mint(1) * B + mint(4));
    assert(seg.prod(0, 3).power == B * B * B);

    assert(seg.prod(1, 3).hash == mint(1) * B + mint(4));
    assert(seg.prod(1, 3).power == B * B);
}

int main() {
    mrh::set_base(mint(100));
    test1_basic();
    test2_sequence();
    int a, b;
    std::cin >> a >> b;
    std::cout << a + b << '\n';
    return 0;
}
#line 1 "segment_tree/test/segment_tree_rolling_hash.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"

#include <iostream>

#line 2 "algebra/monoid/monoid_rolling_hash.hpp"

#include <chrono>
#include <cstdint>
#include <random>
#include <utility>

template <class Mint> struct Hash {
    // {hash(s), base ^ len(s)}
    Mint hash;
    Mint power;
    friend std::ostream& operator<<(std::ostream& os, const Hash& v) {
        return os << v.hash << ", " << v.power;
    }
};

template <class Mint> struct MonoidRollingHash {
    using value_type = Hash<Mint>;

    static Mint base;

    static void set_base(Mint b = Mint(0)) {
        if (b == Mint(0)) {
            std::mt19937_64 mt(
                std::chrono::steady_clock::now().time_since_epoch().count());
            std::uniform_int_distribution<uint64_t> rand(1, Mint::mod() - 1);
            base = Mint(rand(mt));
        } else {
            base = b;
        }
    }

    static constexpr value_type make_element(Mint x) { return {x, base}; }

    static constexpr value_type operation(const value_type& a,
                                          const value_type& b) {
        return {a.hash * b.power + b.hash, a.power * b.power};
    }
    static constexpr value_type identity() {
        // {hash(""), base ^ len("")}
        return {Mint(0), Mint(1)};
    }

    static constexpr bool commutative = false;
};
#line 2 "segment_tree/segment_tree.hpp"

#line 2 "misc/bit_ceil.hpp"

#include <cassert>

#if __cplusplus >= 202002L
#include <bit>
#endif

// bit_ceil
// (0, 1, 2, 3, 4) -> (1, 1, 2, 4, 4)
#if __cplusplus >= 202002L
using std::bit_ceil;
#else
unsigned int bit_ceil(unsigned int x) {
    unsigned int p = 1;
    while (p < x) p *= 2;
    return p;
}
unsigned long long int bit_ceil(unsigned long long int x) {
    unsigned long long int p = 1;
    while (p < x) p *= 2;
    return p;
}
#endif
int bit_ceil(int x) {
    assert(x >= 0);
    return bit_ceil((unsigned int)(x));
}
long long int bit_ceil(long long int x) {
    assert(x >= 0);
    return bit_ceil((unsigned long long int)(x));
}
#line 2 "misc/countr_zero.hpp"

#if __cplusplus >= 202002L
#include <bit>
#endif

// countr_zero
// (000, 001, 010, 011, 100) -> (32, 0, 1, 0, 2)
#if __cplusplus >= 202002L
using std::countr_zero;
#else
int countr_zero(unsigned int x) {
    return x == 0 ? 32 : __builtin_ctz(x);
}
int countr_zero(unsigned long long int x) {
    return x == 0 ? 64 : __builtin_ctzll(x);
}
#endif
int countr_zero(int x) { return countr_zero((unsigned int)(x)); }
int countr_zero(long long int x) {
    return countr_zero((unsigned long long int)(x));
}
#line 5 "segment_tree/segment_tree.hpp"

#line 7 "segment_tree/segment_tree.hpp"
#include <vector>

// Segment Tree
template <class MS> struct SegmentTree {
  public:
    using S = typename MS::value_type;

    SegmentTree() = default;

    explicit SegmentTree(int n)
        : SegmentTree(std::vector<S>(n, MS::identity())) {}

    explicit SegmentTree(const std::vector<S>& v) : n((int)(v.size())) {
        size = bit_ceil(n);
        log = countr_zero(size);
        d = std::vector<S>(size << 1, MS::identity());
        for (int i = 0; i < n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, const S& x) {
        assert(0 <= p and p < n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    void add(int p, const S& x) {
        assert(0 <= p and p < n);
        p += size;
        d[p] = MS::operation(d[p], x);
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S operator[](int p) const {
        assert(0 <= p and p < n);
        return d[p + size];
    }

    S get(int p) const {
        assert(0 <= p && p < n);
        return d[p + size];
    }

    S prod(int l, int r) const {
        assert(0 <= l and l <= r and r <= n);
        S sml = MS::identity(), smr = MS::identity();
        l += size;
        r += size;

        while (l < r) {
            if (l & 1) sml = MS::operation(sml, d[l++]);
            if (r & 1) smr = MS::operation(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return MS::operation(sml, smr);
    }

    S all_prod() const { return d[1]; }

    template <class G> int max_right(int l, G& g) const {
        assert(0 <= l and l <= n);
        assert(g(MS::identity()));
        if (l == n) return n;
        l += size;
        S sm = MS::identity();
        do {
            while ((l & 1) == 0) l >>= 1;
            if (!g(MS::operation(sm, d[l]))) {
                while (l < size) {
                    l <<= 1;
                    if (g(MS::operation(sm, d[l]))) {
                        sm = MS::operation(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = MS::operation(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return n;
    }

    template <class G> int min_left(int r, G& g) const {
        assert(0 <= r and r <= n);
        assert(g(MS::identity()));
        if (r == 0) return 0;
        r += size;
        S sm = MS::identity();
        do {
            r--;
            while (r > 1 and (r & 1)) r >>= 1;
            if (!g(MS::operation(d[r], sm))) {
                while (r < size) {
                    r = (r << 1) | 1;
                    if (g(MS::operation(d[r], sm))) {
                        sm = MS::operation(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = MS::operation(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

    std::vector<S> make_vector() const {
        std::vector<S> vec(n);
        for (int i = 0; i < n; i++) vec[i] = get(i);
        return vec;
    }

  private:
    int n, log, size;
    std::vector<S> d;

    inline void update(int k) {
        d[k] = MS::operation(d[k << 1], d[(k << 1) | 1]);
    }
};
#line 2 "math/modint261.hpp"

#line 4 "math/modint261.hpp"

struct Modint261 {
    static constexpr unsigned long long m = (1ULL << 61) - 1;
    using mint = Modint261;
    unsigned long long _v;

    static constexpr long long mod() { return m; }
    static constexpr unsigned long long umod() { return m; }

    Modint261() : _v(0ULL) {}

    template <class T> Modint261(T v) {
        long long x = (long long)(v % (long long)(umod()));
        if (x < 0) x += umod();
        _v = (unsigned long long)(x);
    }

    unsigned long long val() const { return _v; }

    mint &operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint &operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint &operator+=(const mint &rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint &operator-=(const mint &rhs) {
        _v -= rhs._v;
        if (_v >= umod()) _v += umod();
        return *this;
    }
    mint &operator*=(const mint &rhs) {
        __uint128_t z = _v;
        z *= rhs._v;
        z = (z >> 61) + (z & umod());
        if (z >= umod()) z -= umod();
        _v = (unsigned long long)z;
        return *this;
    }
    mint &operator/=(const mint &rhs) { return (*this *= rhs.inv()); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(n >= 0);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }

    mint inv() const { return pow(umod() - 2); }

    friend mint operator+(const mint &lhs, const mint &rhs) { return mint(lhs) += rhs; }
    friend mint operator-(const mint &lhs, const mint &rhs) { return mint(lhs) -= rhs; }
    friend mint operator*(const mint &lhs, const mint &rhs) { return mint(lhs) *= rhs; }
    friend mint operator/(const mint &lhs, const mint &rhs) { return mint(lhs) /= rhs; }
    friend bool operator==(const mint &lhs, const mint &rhs) { return lhs._v == rhs._v; }
    friend bool operator!=(const mint &lhs, const mint &rhs) { return lhs._v != rhs._v; }
    friend std::ostream &operator<<(std::ostream &os, const mint &v) { return os << v.val(); }
};
using mint261 = Modint261;
#line 8 "segment_tree/test/segment_tree_rolling_hash.test.cpp"

using mint = mint261;
using mrh = MonoidRollingHash<mint>;
template <> mint mrh::base = 0;

void test1_basic() {
    assert(mrh::commutative == false);
    assert(mrh::identity().hash == mint(0));
    assert(mrh::identity().power == mint(1));
}

void test2_sequence() {
    mint B = mrh::base;

    std::vector<Hash<mint>> a = {
        mrh::make_element(mint(3)),
        mrh::make_element(mint(1)),
        mrh::make_element(mint(4)),
    };
    SegmentTree<mrh> seg(a);

    assert(seg.prod(0, 0).hash == mint(0));
    assert(seg.prod(0, 0).power == mint(1));

    assert(seg.prod(0, 1).hash == mint(3));
    assert(seg.prod(0, 1).power == B);

    assert(seg.prod(0, 2).hash == mint(3) * B + mint(1));
    assert(seg.prod(0, 2).power == B * B);

    assert(seg.prod(0, 3).hash == mint(3) * B * B + mint(1) * B + mint(4));
    assert(seg.prod(0, 3).power == B * B * B);

    assert(seg.prod(1, 3).hash == mint(1) * B + mint(4));
    assert(seg.prod(1, 3).power == B * B);
}

int main() {
    mrh::set_base(mint(100));
    test1_basic();
    test2_sequence();
    int a, b;
    std::cin >> a >> b;
    std::cout << a + b << '\n';
    return 0;
}
Back to top page