library

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


Project maintained by tko919 Hosted on GitHub Pages — Theme by mattgraham

:x: Verify/LC_staticrmq-2.test.cpp

Depends on

Code

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

#include "Template/template.hpp"

#include "DataStructure/disjointsparsetable.hpp"


int f(int a,int b){return min(a,b);}

int main(){
    int n,q;
    cin>>n>>q;
    vector<int> a(n);
    rep(i,0,n)cin>>a[i];

    DisjointSparseTable<int,f> st(a);
    while(q--){
        int L,R;
        cin>>L>>R;
        cout<<st.query(L,R)<<'\n';
    }
    return 0;
}
#line 1 "Verify/LC_staticrmq-2.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/staticrmq"

#line 1 "Template/template.hpp"
#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T, typename S = T> S SUM(const vector<T> &a) {
    return accumulate(ALL(a), S(0));
}
template <typename S, typename T = S> S POW(S a, T b) {
    S ret = 1, base = a;
    for (;;) {
        if (b & 1)
            ret *= base;
        b >>= 1;
        if (b == 0)
            break;
        base *= base;
    }
    return ret;
}
template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define debug 1
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define debug 0
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}
#line 2 "DataStructure/disjointsparsetable.hpp"

template <typename T, T (*f)(T, T)> struct DisjointSparseTable {
    vector<vector<T>> buf;
    vector<int> height;
    DisjointSparseTable() {}
    DisjointSparseTable(const vector<T> &a) {
        int n = a.size(), LG = 0;
        while ((1 << LG) <= n)
            LG++;
        buf.assign(LG, vector<T>(n));
        height.assign(1 << LG, 0);
        rep(i, 2, 1 << LG) height[i] = height[i >> 1] + 1;
        rep(i, 0, n) buf[0][i] = a[i];
        rep(lg, 1, LG) {
            int add = 1 << lg;
            for (int j = 0; j < n; j += (add << 1)) {
                int pos = min(j + add, n);
                buf[lg][pos - 1] = a[pos - 1];
                for (int k = pos - 2; k >= j; k--)
                    buf[lg][k] = f(a[k], buf[lg][k + 1]);
                if (n <= pos)
                    break;
                buf[lg][pos] = a[pos];
                for (int k = pos + 1; k < min(pos + add, n); k++)
                    buf[lg][k] = f(buf[lg][k - 1], a[k]);
            }
        }
    }
    T query(int L, int R) {
        if (L >= --R)
            return buf[0][L];
        return f(buf[height[L ^ R]][L], buf[height[L ^ R]][R]);
    }
};

/**
 * @brief Disjoint Sparse Table
 */
#line 5 "Verify/LC_staticrmq-2.test.cpp"

int f(int a,int b){return min(a,b);}

int main(){
    int n,q;
    cin>>n>>q;
    vector<int> a(n);
    rep(i,0,n)cin>>a[i];

    DisjointSparseTable<int,f> st(a);
    while(q--){
        int L,R;
        cin>>L>>R;
        cout<<st.query(L,R)<<'\n';
    }
    return 0;
}
Back to top page