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/CF/CF923-D.test.cpp

Depends on

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_A"

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

#include <iostream>
using namespace zawa;

using M1 = ChmaxMonoid<int, int>;
using M2 = ChminMonoid<int, int>;

void solve() {
    int t; std::cin >> t;
    while (t--) {
        int n; std::cin >> n;
        std::vector<M1::Element> a(n);
        std::vector<M2::Element> b(n);
        for (int i{} ; i < n ; i++) {
            int v; std::cin >> v;
            a[i] = { v, i + 1 };
            b[i] = { v, i + 1 };
        }
        SparseTable<M1> spt1(a);
        SparseTable<M2> spt2(b);
        int q; std::cin >> q;
        for (int _{} ; _ < q ; _++) {
            int l, r; std::cin >> l >> r;
            l--;
            auto prod1{spt1.product(l, r)};
            auto prod2{spt2.product(l, r)};
            if (prod1.priority() == prod2.priority()) {
                std::cout << -1 << ' ' << -1 << '\n';
            }
            else {
                std::cout << prod1.value() << ' ' << prod2.value() << '\n';
            }
        }
    }
}

int main() {
#ifdef ONLINE_JUDGE
    SetFastIO();
    solve();
#else
    std::cout << "Hello World" << '\n';
#endif
}
#line 1 "Test/CF/CF923-D.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_A"

#line 2 "Src/Template/IOSetting.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/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 2 "Src/Algebra/Monoid/ChminMonoid.hpp"

#line 4 "Src/Algebra/Monoid/ChminMonoid.hpp"

#include <algorithm>
#include <optional>

namespace zawa {

template <class T, class U>
class ChminMonoidData {
private:
    std::optional<T> priority_{};
    U value_{};
public:
    ChminMonoidData() = default;
    ChminMonoidData(const U& value)
        : priority_{std::nullopt}, value_{value} {}
    ChminMonoidData(const T& priority, const U& value)
        : priority_{priority}, value_{value} {}

    constexpr bool infty() const noexcept {
        return !priority_.has_value();
    }
    constexpr const T& priority() const noexcept {
        return priority_.value();
    }
    constexpr const U& value() const noexcept {
        return value_;
    }
    friend constexpr bool operator<(const ChminMonoidData& l, const ChminMonoidData& r) {
        if (l.infty()) return false;
        else if (r.infty()) return true;
        else return l.priority() < r.priority();
    }
};

template <class T, class U>
struct ChminMonoid {
    using Element = ChminMonoidData<T, U>;
    static Element identity() noexcept {
        return Element{};
    }
    // タイブレークはl側を優先するようになっている。
    static Element operation(const Element& l, const Element& r) noexcept {
        return (r < l ? r : l);
    }
};

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

#line 4 "Src/Algebra/Monoid/ChmaxMonoid.hpp"

#line 7 "Src/Algebra/Monoid/ChmaxMonoid.hpp"

namespace zawa {

template <class T, class U>
class ChmaxMonoidData {
private:
    std::optional<T> priority_{};
    U value_{};
public:
    ChmaxMonoidData() = default;
    ChmaxMonoidData(const U& value)
        : priority_{std::nullopt}, value_{value} {}
    ChmaxMonoidData(const T& priority, const U& value)
        : priority_{priority}, value_{value} {}

    constexpr bool infty() const noexcept {
        return !priority_.has_value();
    }
    constexpr const T& priority() const noexcept {
        return priority_.value();
    }
    constexpr const U& value() const noexcept {
        return value_;
    }
    friend constexpr bool operator>(const ChmaxMonoidData& l, const ChmaxMonoidData& r) {
        if (l.infty()) return false;
        else if (r.infty()) return true;
        else return l.priority() > r.priority();
    }
};

template <class T, class U>
struct ChmaxMonoid {
    using Element = ChmaxMonoidData<T, U>;
    static Element identity() noexcept {
        return Element{};
    }
    // タイブレークはl側を優先するようになっている。
    static Element operation(const Element& l, const Element& r) noexcept {
        return (r > l ? r : l);
    }
};

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

#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 7 "Test/CF/CF923-D.test.cpp"

#line 9 "Test/CF/CF923-D.test.cpp"
using namespace zawa;

using M1 = ChmaxMonoid<int, int>;
using M2 = ChminMonoid<int, int>;

void solve() {
    int t; std::cin >> t;
    while (t--) {
        int n; std::cin >> n;
        std::vector<M1::Element> a(n);
        std::vector<M2::Element> b(n);
        for (int i{} ; i < n ; i++) {
            int v; std::cin >> v;
            a[i] = { v, i + 1 };
            b[i] = { v, i + 1 };
        }
        SparseTable<M1> spt1(a);
        SparseTable<M2> spt2(b);
        int q; std::cin >> q;
        for (int _{} ; _ < q ; _++) {
            int l, r; std::cin >> l >> r;
            l--;
            auto prod1{spt1.product(l, r)};
            auto prod2{spt2.product(l, r)};
            if (prod1.priority() == prod2.priority()) {
                std::cout << -1 << ' ' << -1 << '\n';
            }
            else {
                std::cout << prod1.value() << ' ' << prod2.value() << '\n';
            }
        }
    }
}

int main() {
#ifdef ONLINE_JUDGE
    SetFastIO();
    solve();
#else
    std::cout << "Hello World" << '\n';
#endif
}
Back to top page