raybbian's CP Algos

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub raybbian/comp-programming

:heavy_check_mark: verify/ds/sparse_table.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/staticrmq"
#include "algo/common.h"

/* #include */
#include "algo/ds/sparse_table.h"

using namespace std;
using namespace algo;
using ds::sparse_table;

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    sparse_table st(a);
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        cout << st.query(l, r - 1) << '\n';
    }
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    // int t;
    // cin >> t;
    // while (t--)
    solve();
}
#line 1 "verify/ds/sparse_table.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/staticrmq"
#line 2 "algo/common.h"
#ifndef PREPROCESS
#include <bits/stdc++.h>
#include <cassert>
#endif

namespace algo {

// Indices and sizes into library containers. Signed, so the usual "walk down to
// -1" loops still terminate; widening the whole library is a change here alone.
using index_t = int;

} // namespace algo
#line 3 "verify/ds/sparse_table.test.cpp"

/* #include */
#line 3 "algo/utils/bits.h"

namespace algo::utils {

// Returns number of set bits in x
constexpr int popcnt(int64_t x) {
    return __builtin_popcountll(x);
}
// Returns floor(log_2(x))
constexpr int lg2(uint64_t x) {
    return std::bit_width(x) - 1;
}

} // namespace algo::utils
#line 4 "algo/ds/sparse_table.h"

namespace algo::ds {

// An op has to be a type here, and std::min and std::max name overload sets
// rather than single functions, so wrap them.
template <typename T>
struct min_op {
    T operator()(T a, T b) const {
        return std::min(a, b);
    }
};

template <typename T>
struct max_op {
    T operator()(T a, T b) const {
        return std::max(a, b);
    }
};

template <typename T, typename Op = min_op<T>>
struct sparse_table {
    // Must be constructed with idempotent function. Call init() after if using
    // this constructor.
    sparse_table(index_t _n, Op op = Op())
        : n(_n), k(utils::lg2(n)), op(op),
          st(std::max<index_t>(k + 1, 1), std::vector<T>(n)) {
    }
    // Must be constructed with idempotent function
    sparse_table(const std::vector<T> &a, Op op = Op())
        : sparse_table((index_t)a.size(), op) {
        init(a);
    }
    void init(const std::vector<T> &a) {
        assert((index_t)a.size() <= n);
        std::copy(a.begin(), a.end(), st[0].begin());
        for (index_t i = 1; i <= k; i++) {
            for (index_t j = 0; j + (index_t(1) << i) <= n; j++) {
                st[i][j] =
                    op(st[i - 1][j], st[i - 1][j + (index_t(1) << (i - 1))]);
            }
        }
    }
    // Queries on [l, r]
    T query(index_t l, index_t r) {
        index_t i = utils::lg2(r - l + 1);
        return op(st[i][l], st[i][r - (index_t(1) << i) + 1]);
    }
    friend std::ostream &operator<<(std::ostream &os, const sparse_table &t) {
        return os << t.st[0];
    }

private:
    // k is the max level index and is -1 when n is 0, so the row count is
    // floored at 1 to keep level 0 present for init() to copy into.
    index_t n, k;
    Op op;
    std::vector<std::vector<T>> st;
};

} // namespace algo::ds
#line 6 "verify/ds/sparse_table.test.cpp"

using namespace std;
using namespace algo;
using ds::sparse_table;

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    sparse_table st(a);
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        cout << st.query(l, r - 1) << '\n';
    }
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    // int t;
    // cin >> t;
    // while (t--)
    solve();
}

Test cases

Env Name Status Elapsed Memory
g++ example_00 :heavy_check_mark: AC 2 ms 4 MB
g++ max_random_00 :heavy_check_mark: AC 75 ms 44 MB
g++ max_random_01 :heavy_check_mark: AC 75 ms 44 MB
g++ max_random_02 :heavy_check_mark: AC 74 ms 44 MB
g++ max_random_03 :heavy_check_mark: AC 74 ms 44 MB
g++ max_random_04 :heavy_check_mark: AC 73 ms 44 MB
g++ random_00 :heavy_check_mark: AC 60 ms 35 MB
g++ random_01 :heavy_check_mark: AC 64 ms 41 MB
g++ random_02 :heavy_check_mark: AC 35 ms 7 MB
g++ random_03 :heavy_check_mark: AC 29 ms 39 MB
g++ random_04 :heavy_check_mark: AC 26 ms 26 MB
g++ small_00 :heavy_check_mark: AC 2 ms 4 MB
g++ small_01 :heavy_check_mark: AC 2 ms 4 MB
g++ small_02 :heavy_check_mark: AC 2 ms 4 MB
g++ small_03 :heavy_check_mark: AC 1 ms 4 MB
g++ small_04 :heavy_check_mark: AC 1 ms 4 MB
g++ small_05 :heavy_check_mark: AC 1 ms 4 MB
g++ small_06 :heavy_check_mark: AC 1 ms 4 MB
g++ small_07 :heavy_check_mark: AC 1 ms 4 MB
g++ small_08 :heavy_check_mark: AC 1 ms 4 MB
g++ small_09 :heavy_check_mark: AC 1 ms 4 MB
g++ small_values_00 :heavy_check_mark: AC 67 ms 44 MB
g++ small_width_query_00 :heavy_check_mark: AC 83 ms 44 MB
g++ small_width_query_01 :heavy_check_mark: AC 85 ms 44 MB
g++ small_width_query_02 :heavy_check_mark: AC 81 ms 44 MB
g++ small_width_query_03 :heavy_check_mark: AC 84 ms 44 MB
g++ small_width_query_04 :heavy_check_mark: AC 83 ms 44 MB
Back to top page