cp-documentation

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

View the Project on GitHub zawa-tin/cp-documentation

:heavy_check_mark: Test/LC/staticrmq/SparseTable.test.cpp

Depends on

Code

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

#include "../../../Src/DataStructure/SparseTable/SparseTable.hpp"
#include "../../../Src/Algebra/Monoid/MinMonoid.hpp"
#include "../../../Src/Template/IOSetting.hpp"

#include <iostream>
#include <vector>

int main() {
    using namespace zawa;
    SetFastIO();
    using M = MinMonoid<int>;
    using MD = M::Element;
    int n, q; std::cin >> n >> q;
    std::vector<MD> a(n);
    for (auto& x : a) {
        int v; std::cin >> v;
        x = v;
    }
    SparseTable<M> spt(a);
    for (int _{} ; _ < q ; _++) {
        int l, r; std::cin >> l >> r;
        MD ans{spt.product(l, r)};
        std::cout << ans.value() << '\n';
    }
}
#line 1 "Test/LC/staticrmq/SparseTable.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/staticrmq"

#line 2 "Src/DataStructure/SparseTable/SparseTable.hpp"

#line 2 "Src/Template/TypeAlias.hpp"

#include <cstdint>
#include <cstddef>

namespace zawa {

using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using i128 = __int128_t;

using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;

using usize = std::size_t;

} // namespace zawa
#line 4 "Src/DataStructure/SparseTable/SparseTable.hpp"

#include <vector>
#include <cassert>
#include <ostream>

namespace zawa {

template <class Structure>
class SparseTable {
private:
    using Value = typename Structure::Element;
    std::vector<u32> L;
    std::vector<std::vector<Value>> dat;
public:

    SparseTable() : L{}, dat{} {}
    SparseTable(const std::vector<Value>& a) : L(a.size() + 1), dat{} {
        for (u32 i{1} ; i < L.size() ; i++) {
            L[i] = L[i - 1] + (i >> (L[i - 1] + 1));
        }
        dat.resize(L.back() + 1);
        dat[0] = a;
        for (u32 i{1}, len{2} ; i < dat.size() ; i++, len <<= 1) {
            dat[i] = dat[i - 1];
            for (u32 j{} ; j + len - 1 < dat[i].size() ; j++) {
                dat[i][j] = Structure::operation(dat[i - 1][j], dat[i - 1][j + (len >> 1)]);
            }
        }
    }

    Value product(u32 l, u32 r) const {
        assert(l <= r);
        assert(l < dat[0].size());
        assert(r <= dat[0].size());
        u32 now{L[r - l]};
        return Structure::operation(dat[now][l], dat[now][r - (1 << now)]);
    }

    friend std::ostream& operator<<(std::ostream& os, const SparseTable<Structure>& spt) {
        for (u32 i{}, len{1} ; i < spt.dat.size() ; i++, len <<= 1) {
            os << "length = " << len << '\n';
            for (u32 j{} ; j + len - 1 < spt.dat[i].size() ; j++) {
                os << spt.dat[i][j] << (j + len == spt.dat[i].size() ? '\n' : ' ');
            }
        }
        return os;
    }
};

} // namespace zawa
#line 2 "Src/Algebra/Monoid/MinMonoid.hpp"

#include <algorithm>
#include <optional>

namespace zawa {

template <class T>
class MinMonoid {
public:
    using Element = std::optional<T>;
    static constexpr Element identity() noexcept {
        return std::nullopt;
    }
    static constexpr Element operation(const Element& l, const Element& r) noexcept {
        if (l and r) {
            return std::min(l, r);
        }
        else if (l) {
            return l;
        }
        else if (r) {
            return r;
        }
        else {
            return std::nullopt;
        }
    }
};

} // namespace zawa
#line 2 "Src/Template/IOSetting.hpp"

#line 4 "Src/Template/IOSetting.hpp"

#include <iostream>
#include <iomanip>

namespace zawa {

void SetFastIO() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
}

void SetPrecision(u32 dig) {
    std::cout << std::fixed << std::setprecision(dig);
}

} // namespace zawa
#line 6 "Test/LC/staticrmq/SparseTable.test.cpp"

#line 9 "Test/LC/staticrmq/SparseTable.test.cpp"

int main() {
    using namespace zawa;
    SetFastIO();
    using M = MinMonoid<int>;
    using MD = M::Element;
    int n, q; std::cin >> n >> q;
    std::vector<MD> a(n);
    for (auto& x : a) {
        int v; std::cin >> v;
        x = v;
    }
    SparseTable<M> spt(a);
    for (int _{} ; _ < q ; _++) {
        int l, r; std::cin >> l >> r;
        MD ans{spt.product(l, r)};
        std::cout << ans.value() << '\n';
    }
}
Back to top page