Fu_L's Library

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

View the Project on GitHub Fu-L/cp-library

:heavy_check_mark: verify/library_checker/data_structure/static_rmq_2.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/staticrmq"
#include "../../../src/template/template.hpp"
#include "../../../src/data_structure/disjoint_sparse_table.hpp"
int op(int x, int y) {
    return min(x, y);
}
int e() {
    return 1e9;
}
int main(void) {
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    rep(i, 0, n) {
        cin >> a[i];
    }
    DisjointSparseTable<int, op, e> dst(a);
    while(q--) {
        int l, r;
        cin >> l >> r;
        cout << dst.prod(l, r) << '\n';
    }
}
#line 1 "verify/library_checker/data_structure/static_rmq_2.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/staticrmq"
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
#line 3 "src/data_structure/disjoint_sparse_table.hpp"
template <typename S, auto op, auto e>
struct DisjointSparseTable {
    DisjointSparseTable(const vector<S>& v)
        : n((int)v.size() + 2) {
        const int b = 32 - __builtin_clz(n - 1);
        table.assign(b, vector<S>(n, e()));
        for(int k = 1; k < b; ++k) {
            const int w = (1 << k);
            for(int i = w; i < n; i += w * 2) {
                for(int j = i - 1; j > i - w; --j) {
                    table[k][j - 1] = op(table[k][j], v[j - 1]);
                }
                const int m = min(i + w - 1, n - 1);
                for(int j = i; j < m; ++j) {
                    table[k][j + 1] = op(table[k][j], v[j - 1]);
                }
            }
        }
    }
    S prod(const int l, int r) const {
        assert(0 <= l and l <= r and r <= n);
        ++r;
        const auto& s = table[31 - __builtin_clz(l xor r)];
        return op(s[l], s[r]);
    }

   private:
    int n;
    vector<vector<S>> table;
};
#line 4 "verify/library_checker/data_structure/static_rmq_2.test.cpp"
int op(int x, int y) {
    return min(x, y);
}
int e() {
    return 1e9;
}
int main(void) {
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    rep(i, 0, n) {
        cin >> a[i];
    }
    DisjointSparseTable<int, op, e> dst(a);
    while(q--) {
        int l, r;
        cin >> l >> r;
        cout << dst.prod(l, r) << '\n';
    }
}
Back to top page