This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/staticrmq"
#include "my_template.hpp"
#include "other/io.hpp"
#include "ds/segtree/range_min.hpp"
void solve() {
INT(N, Q);
Range_Min<int> seg(N, [&](int i) -> int {
INT(x);
return x;
});
FOR(Q) {
INT(l, r);
print(seg.prod(l, r));
}
}
signed main() {
int T = 1;
// INT(T);
FOR(T) solve();
return 0;
}#line 1 "test/2_library_checker/data_structure/staticrmq_simd.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/staticrmq"
#line 1 "my_template.hpp"
// Source: https://github.com/maspypy/library/blob/main/my_template.hpp
#if defined(USE_PCH)
#include <my_template_compiled.hpp>
#if defined(__GNUC__)
#pragma GCC optimize("Ofast,unroll-loops")
#if !defined(MASPY_NON_AVX2)
#pragma GCC target("avx2,popcnt")
#endif
#endif
#else // MASPY_TEMPLATE_BODY
#include <bits/stdc++.h>
#include <cassert>
#if defined(__GNUC__)
#pragma GCC optimize("Ofast,unroll-loops")
#endif
using namespace std;
using ll = long long;
using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;
template <class>
constexpr bool dependent_false = false;
template <class T>
constexpr T infty = [] {
static_assert(dependent_false<T>, "infty<T> is not defined");
return T{};
}();
template <>
constexpr int infty<int> = 1'010'000'000;
template <>
constexpr ll infty<ll> = 4'040'000'000'000'000'000;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * 4'000'000'000'000'000'000;
template <>
constexpr double infty<double> = infty<i128>;
template <>
constexpr long double infty<long double> = infty<i128>;
using pi = pair<ll, ll>;
using vi = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using pq_max = priority_queue<T>;
template <class T>
using pq_min = priority_queue<T, vector<T>, greater<T>>;
#define vv(type, name, h, ...) \
vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...) \
vector<vector<vector<type>>> name( \
h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...) \
vector<vector<vector<vector<type>>>> name( \
a, vector<vector<vector<type>>>( \
b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))
// https://trap.jp/post/1224/
#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)
#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)
#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)
#define FOR1_R(a) for (ll i = ll(a) - 1; i >= ll(0); --i)
#define FOR2_R(i, a) for (ll i = ll(a) - 1; i >= ll(0); --i)
#define FOR3_R(i, a, b) for (ll i = ll(b) - 1; i >= ll(a); --i)
#define overload3(a, b, c, d, ...) d
#define FOR(...) overload3(__VA_ARGS__, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define len(x) ll(x.size())
#define elif else if
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define stoi stoll
// require y > 0
template <typename T>
T floor(T x, T y) {
return x / y - (x % y < 0);
}
// require y > 0
template <typename T>
T ceil(T x, T y) {
return (x / y) + (x % y > 0);
}
// require y > 0
template <typename T>
T bmod(T x, T y) {
T r = x % y;
return (r < 0 ? r + y : r);
}
// require y > 0
template <typename T>
pair<T, T> divmod(T x, T y) {
T q = x / y, r = x % y;
if (r < 0) --q, r += y;
return {q, r};
}
constexpr auto TEN = [] {
array<u64, 20> A{};
A[0] = 1;
for (int i = 1; i < 20; ++i) A[i] = 10 * A[i - 1];
return A;
}();
template <typename T, typename U>
T SUM(const U &A) {
return std::accumulate(A.begin(), A.end(), T{});
}
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
template <class C, class T>
inline long long LB(const C &c, const T &x) {
return lower_bound(c.begin(), c.end(), x) - c.begin();
}
template <class C, class T>
inline long long UB(const C &c, const T &x) {
return upper_bound(c.begin(), c.end(), x) - c.begin();
}
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())
template <typename T>
T POP(deque<T> &que) {
T a = que.front();
que.pop_front();
return a;
}
template <class T, class Container, class Compare>
T POP(priority_queue<T, Container, Compare> &que) {
T a = que.top();
que.pop();
return a;
}
template <typename T>
T POP(vc<T> &que) {
T a = que.back();
que.pop_back();
return a;
}
template <typename F>
i128 binary_search(F check, i128 ok, i128 ng, bool check_ok = true) {
if (check_ok) assert(check(ok));
while (1) {
i128 x = (ok + ng) / 2;
if (x == ok || x == ng) break;
(check(x) ? ok : ng) = x;
}
return ok;
}
template <typename F>
double binary_search_real(F check, double ok, double ng, int iter = 100) {
FOR(iter) {
double x = (ok + ng) / 2;
(check(x) ? ok : ng) = x;
}
return (ok + ng) / 2;
}
template <class T, class S>
inline bool chmax(T &a, const S &b) {
T c = max<T>(a, b);
bool changed = (c != a);
a = c;
return changed;
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
T c = min<T>(a, b);
bool changed = (c != a);
a = c;
return changed;
}
// bit operations
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
int popcnt_sgn(int x) { return (__builtin_parity(unsigned(x)) & 1 ? -1 : 1); }
int popcnt_sgn(u32 x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
int popcnt_sgn(ll x) { return (__builtin_parityll(x) & 1 ? -1 : 1); }
int popcnt_sgn(u64 x) { return (__builtin_parityll(x) & 1 ? -1 : 1); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
template <typename T>
T kth_bit(int k) {
assert(0 <= k && k < int(8 * sizeof(T)));
return T(1) << k;
}
template <typename T>
bool has_kth_bit(T x, int k) {
assert(0 <= k && k < int(8 * sizeof(T)));
return x >> k & 1;
}
template <typename T>
struct all_bit {
static_assert(is_integral<T>::value);
T s;
all_bit(T s) : s(s) { assert(s >= 0); }
struct iter {
T s;
int operator*() const { return lowbit(s); }
void operator++() { s &= s - 1; }
bool operator!=(nullptr_t) const { return s; }
};
iter begin() const { return {s}; }
nullptr_t end() const { return nullptr; }
};
template <typename T>
struct all_subset {
static_assert(is_integral<T>::value);
T s;
all_subset(T s) : s(s) { assert(s >= 0); }
struct iter {
T s, t;
bool done = false;
T operator*() const { return t; }
void operator++() {
done = (t == 0);
t = (t - 1) & s;
}
bool operator!=(nullptr_t) const { return !done; }
};
iter begin() const { return {s, s}; }
nullptr_t end() const { return nullptr; }
};
constexpr u64 full_mask(int n) {
assert(0 <= n && n <= 64);
return n == 64 ? -1ULL : (1ULL << n) - 1;
}
u64 bit_reverse(u64 x) {
x = ((x & 0x5555555555555555ULL) << 1) | ((x >> 1) & 0x5555555555555555ULL);
x = ((x & 0x3333333333333333ULL) << 2) | ((x >> 2) & 0x3333333333333333ULL);
x = ((x & 0x0f0f0f0f0f0f0f0fULL) << 4) | ((x >> 4) & 0x0f0f0f0f0f0f0f0fULL);
x = ((x & 0x00ff00ff00ff00ffULL) << 8) | ((x >> 8) & 0x00ff00ff00ff00ffULL);
x = ((x & 0x0000ffff0000ffffULL) << 16) | ((x >> 16) & 0x0000ffff0000ffffULL);
x = (x << 32) | (x >> 32);
return x;
}
// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
vc<int> A(S.size());
FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
return A;
}
template <typename T, typename U>
vc<T> cumsum(const vc<U> &A, int off = 1) {
int N = A.size();
vc<T> B(N + 1);
FOR(i, N) { B[i + 1] = B[i] + A[i]; }
if (off == 0) B.erase(B.begin());
return B;
}
// stable sort
template <typename T>
vc<int> argsort(const vc<T> &A) {
vc<int> ids(len(A));
iota(all(ids), 0);
sort(all(ids),
[&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
return ids;
}
// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
vc<T> B(len(I));
FOR(i, len(I)) B[i] = A[I[i]];
return B;
}
template <typename T, typename... Vectors>
void concat(vc<T> &first, const Vectors &...others) {
size_t n = first.size() + (others.size() + ... + 0);
if (n > first.capacity()) {
first.reserve(max(n, 2 * first.capacity()));
}
(first.insert(first.end(), others.begin(), others.end()), ...);
}
// i128
template <class T, enable_if_t<is_same_v<T, i128>, int> = 0>
constexpr i128 abs(T x) {
return x < 0 ? -x : x;
}
constexpr i128 gcd(i128 a, i128 b) {
while (b != 0) {
i128 c = a % b;
a = b, b = c;
}
return abs(a);
}
#endif // MASPY_TEMPLATE_BODY
#line 1 "other/io.hpp"
// Source: https://github.com/maspypy/library/blob/main/other/io.hpp
#define FASTIO
// https://judge.yosupo.jp/submission/21623
namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf
uint32_t pil = 0, pir = 0, por = 0;
bool input_eof = false;
template <class T>
constexpr bool is_signed_integer_v = is_signed_v<T> || is_same_v<T, i128>;
template <class T>
struct unsigned_integer {
using type = make_unsigned_t<T>;
};
template <>
struct unsigned_integer<i128> {
using type = u128;
};
template <>
struct unsigned_integer<u128> {
using type = u128;
};
template <class T>
using unsigned_integer_t = typename unsigned_integer<T>::type;
[[noreturn]] inline void input_error(const char *message) {
fputs(message, stderr);
fputc('\n', stderr);
exit(EXIT_FAILURE);
}
struct Pre {
char num[10000][4];
constexpr Pre() : num() {
for (int i = 0; i < 10000; i++) {
int n = i;
for (int j = 3; j >= 0; j--) {
num[i][j] = n % 10 | '0';
n /= 10;
}
}
}
} constexpr pre;
inline void load() {
uint32_t n = pir - pil;
memmove(ibuf, ibuf + pil, n);
pil = 0;
pir = n;
if (input_eof) return;
pir += fread(ibuf + pir, 1, SZ - pir, stdin);
if (ferror(stdin)) input_error("fastio: input error");
if (feof(stdin)) {
input_eof = true;
// Allows the last token to end exactly at EOF without a trailing
// whitespace.
if (pir < SZ) ibuf[pir++] = '\n';
}
}
inline char get_char() {
if (pil == pir) {
load();
if (pil == pir) input_error("fastio: unexpected EOF");
}
return ibuf[pil++];
}
inline void flush() {
fwrite(obuf, 1, por, stdout);
por = 0;
}
void rd(char &c) {
do c = get_char();
while (isspace(static_cast<unsigned char>(c)));
}
void rd(string &x) {
x.clear();
char c;
do c = get_char();
while (isspace(static_cast<unsigned char>(c)));
do {
x += c;
c = get_char();
} while (!isspace(static_cast<unsigned char>(c)));
}
template <typename T>
void rd_real(T &x) {
string s;
rd(s);
x = stod(s);
}
template <typename T>
void rd_integer_slow(T &x) {
char c;
do c = get_char();
while (c < '-');
bool minus = 0;
if constexpr (is_signed_integer_v<T>) {
if (c == '-') {
minus = 1, c = get_char();
}
}
x = 0;
assert('0' <= c && c <= '9');
while ('0' <= c && c <= '9') {
x = x * 10 + (c & 15), c = get_char();
}
assert(isspace(static_cast<unsigned char>(c)));
if constexpr (is_signed_integer_v<T>) {
if (minus) x = -x;
}
}
template <typename T>
void rd_integer(T &x) {
if (pil + 100 > pir) {
load();
if (pil + 100 > pir) {
rd_integer_slow(x);
return;
}
}
char c;
do c = ibuf[pil++];
while (c < '-');
bool minus = 0;
if constexpr (is_signed_integer_v<T>) {
if (c == '-') {
minus = 1, c = ibuf[pil++];
}
}
x = 0;
assert('0' <= c && c <= '9');
while ('0' <= c && c <= '9') {
x = x * 10 + (c & 15), c = ibuf[pil++];
}
assert(isspace(static_cast<unsigned char>(c)));
if constexpr (is_signed_integer_v<T>) {
if (minus) x = -x;
}
}
template <class T>
enable_if_t<is_integral_v<T> || is_same_v<T, i128> || is_same_v<T, u128>> rd(
T &x) {
rd_integer(x);
}
template <class T>
enable_if_t<is_floating_point_v<T> || is_same_v<T, f128>> rd(T &x) {
rd_real(x);
}
template <class T, class U>
void rd(pair<T, U> &p) {
rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T>
void rd_tuple(T &t) {
if constexpr (N < tuple_size<T>::value) {
auto &x = get<N>(t);
rd(x);
rd_tuple<N + 1>(t);
}
}
template <class... T>
void rd(tuple<T...> &tpl) {
rd_tuple(tpl);
}
template <class T, size_t N>
void rd(array<T, N> &x) {
for (auto &d : x) rd(d);
}
template <class T>
void rd(vc<T> &x) {
for (auto &d : x) rd(d);
}
template <class... T>
void read(T &...x) {
(rd(x), ...);
}
inline void wt_range(const char *s, size_t n) {
size_t i = 0;
while (i < n) {
if (por == SZ) flush();
size_t chunk = min(n - i, (size_t)(SZ - por));
memcpy(obuf + por, s + i, chunk);
por += chunk;
i += chunk;
}
}
void wt(const char c) {
if (por == SZ) flush();
obuf[por++] = c;
}
void wt(const char *s) { wt_range(s, strlen(s)); }
void wt(const string &s) { wt_range(s.data(), s.size()); }
template <typename T>
void wt_integer(T x) {
if (por > SZ - 100) flush();
using U = unsigned_integer_t<T>;
U y = static_cast<U>(x);
if constexpr (is_signed_integer_v<T>) {
if (x < 0) {
obuf[por++] = '-';
y = U(0) - y;
}
}
int outi;
for (outi = 96; y >= 10000; outi -= 4) {
memcpy(out + outi, pre.num[y % 10000], 4);
y /= 10000;
}
if (y >= 1000) {
memcpy(obuf + por, pre.num[y], 4);
por += 4;
} else if (y >= 100) {
memcpy(obuf + por, pre.num[y] + 1, 3);
por += 3;
} else if (y >= 10) {
int q = (y * 103) >> 10;
obuf[por] = q | '0';
obuf[por + 1] = (y - q * 10) | '0';
por += 2;
} else
obuf[por++] = y | '0';
memcpy(obuf + por, out + outi + 4, 96 - outi);
por += 96 - outi;
}
template <typename T>
inline void wt_real(T x) {
static char buf[1000];
int n = std::snprintf(buf, sizeof(buf), "%.15f", (double)x);
wt_range(buf, (size_t)n);
}
template <class T>
enable_if_t<is_integral_v<T> || is_same_v<T, i128> || is_same_v<T, u128>> wt(
T x) {
wt_integer(x);
}
template <class T>
enable_if_t<is_floating_point_v<T> || is_same_v<T, f128>> wt(T x) {
wt_real(x);
}
inline void wt(bool b) { wt(static_cast<char>('0' + (b ? 1 : 0))); }
template <class T, class U>
void wt(const pair<T, U> &val) {
wt(val.first);
wt(' ');
wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T &t) {
if constexpr (N < tuple_size<T>::value) {
if constexpr (N > 0) wt(' ');
wt(get<N>(t));
wt_tuple<N + 1>(t);
}
}
template <class... T>
void wt(const tuple<T...> &tpl) {
wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> &val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) wt(' ');
wt(val[i]);
}
}
template <class T>
void wt(const vector<T> &val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) wt(' ');
wt(val[i]);
}
}
void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&...tail) {
wt(forward<Head>(head));
((wt(' '), wt(forward<Tail>(tail))), ...);
wt('\n');
}
// gcc expansion. called automaticall after main.
void __attribute__((destructor)) _d() { flush(); }
} // namespace fastio
using fastio::flush;
using fastio::print;
using fastio::read;
#if defined(LOCAL)
#define HDR "[DEBUG:", __func__, __LINE__, "]"
#define SHOW(...) \
SHOW_IMPL(__VA_ARGS__, SHOW8, SHOW7, SHOW6, SHOW5, SHOW4, SHOW3, SHOW2, \
SHOW1) \
(__VA_ARGS__)
#define SHOW_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, NAME, ...) NAME
#define SHOW1(x) print(HDR, #x, "=", (x)), flush()
#define SHOW2(x, y) print(HDR, #x, "=", (x), #y, "=", (y)), flush()
#define SHOW3(x, y, z) \
print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z)), flush()
#define SHOW4(x, y, z, w) \
print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w)), flush()
#define SHOW5(x, y, z, w, v) \
print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", \
(v)), \
flush()
#define SHOW6(x, y, z, w, v, u) \
print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", \
(v), #u, "=", (u)), \
flush()
#define SHOW7(x, y, z, w, v, u, t) \
print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", \
(v), #u, "=", (u), #t, "=", (t)), \
flush()
#define SHOW8(x, y, z, w, v, u, t, s) \
print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", \
(v), #u, "=", (u), #t, "=", (t), #s, "=", (s)), \
flush()
#else
#define SHOW(...)
#endif
#define INT(...) \
int __VA_ARGS__; \
read(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
read(__VA_ARGS__)
#define U32(...) \
u32 __VA_ARGS__; \
read(__VA_ARGS__)
#define U64(...) \
u64 __VA_ARGS__; \
read(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
read(__VA_ARGS__)
#define CHAR(...) \
char __VA_ARGS__; \
read(__VA_ARGS__)
#define DBL(...) \
double __VA_ARGS__; \
read(__VA_ARGS__)
#define VEC(type, name, size) \
vector<type> name(size); \
read(name)
#define VV(type, name, h, w) \
vector<vector<type>> name(h, vector<type>(w)); \
read(name)
void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
void YA(bool t = 1) { print(t ? "YA" : "TIDAK"); }
void TIDAK(bool t = 1) { YA(!t); }
void Alice(bool t = 1) { print(t ? "Alice" : "Bob"); }
void Bob(bool t = 1) { Alice(!t); }
#line 5 "test/2_library_checker/data_structure/staticrmq_simd.test.cpp"
#line 1 "ds/segtree/range_min.hpp"
// Source: https://github.com/maspypy/library/blob/main/ds/segtree/range_min.hpp
#line 1 "alg/monoid/min.hpp"
// Source: https://github.com/maspypy/library/blob/main/alg/monoid/min.hpp
// require: all values x satisfy x < infty<E>
template <typename E>
struct Monoid_Min {
using X = E;
using value_type = X;
static constexpr X op(const X &x, const X &y) noexcept { return min(x, y); }
static constexpr X id() { return infty<E>; }
static constexpr bool commute = true;
};
#line 4 "ds/segtree/range_min.hpp"
// 2026/11/04 AI generated.
#if defined(__x86_64__) && (defined(__GNUC__) || defined(__clang__))
#include <immintrin.h>
#define RANGE_MIN_AVX2 1
#else
#define RANGE_MIN_AVX2 0
#endif
template <typename T>
struct Range_Min {
static_assert(is_same_v<T, int> || is_same_v<T, ll>);
using value_type = T;
using MX = Monoid_Min<T>;
static constexpr T INF = infty<T>;
static constexpr int B = 64 / sizeof(T);
struct alignas(64) Block {
T a[B];
};
static_assert(sizeof(Block) == 64);
int n = 0;
private:
vc<vc<Block>> dat;
bool use_avx2 = false;
static T reduce_scalar(const T* p) {
T x = INF;
for (int j = 0; j < B; ++j) x = min(x, p[j]);
return x;
}
static T range_scalar(const T* p, int l, int r) {
T x = INF;
for (int j = l; j < r; ++j) x = min(x, p[j]);
return x;
}
template <bool tie_is_left>
static int find_equal_scalar(const T* p, int l, int r, T x) {
if constexpr (tie_is_left) {
for (int j = l; j < r; ++j) {
if (p[j] == x) return j;
}
} else {
for (int j = r - 1; j >= l; --j) {
if (p[j] == x) return j;
}
}
assert(false);
return -1;
}
#if RANGE_MIN_AVX2
__attribute__((target("avx2"))) static __m256i min64(__m256i a, __m256i b) {
return _mm256_blendv_epi8(a, b, _mm256_cmpgt_epi64(a, b));
}
__attribute__((target("avx2"))) static T reduce_avx2(const T* p) {
if constexpr (is_same_v<T, int>) {
__m256i a = _mm256_loadu_si256((const __m256i*)p);
__m256i b = _mm256_loadu_si256((const __m256i*)(p + 8));
a = _mm256_min_epi32(a, b);
a = _mm256_min_epi32(a, _mm256_permute2x128_si256(a, a, 1));
a = _mm256_min_epi32(a, _mm256_shuffle_epi32(a, 0x4e));
a = _mm256_min_epi32(a, _mm256_shuffle_epi32(a, 0xb1));
return _mm256_extract_epi32(a, 0);
} else {
__m256i a = _mm256_loadu_si256((const __m256i*)p);
__m256i b = _mm256_loadu_si256((const __m256i*)(p + 4));
a = min64(a, b);
a = min64(a, _mm256_permute4x64_epi64(a, 0x4e));
a = min64(a, _mm256_permute4x64_epi64(a, 0xb1));
return (T)_mm256_extract_epi64(a, 0);
}
}
__attribute__((target("avx2"))) static T range_avx2(
const T* p, int l, int r) {
if constexpr (is_same_v<T, int>) {
__m256i lo = _mm256_set1_epi32(l - 1);
__m256i hi = _mm256_set1_epi32(r);
__m256i x = _mm256_setr_epi32(0, 1, 2, 3, 4, 5, 6, 7);
__m256i y = _mm256_setr_epi32(8, 9, 10, 11, 12, 13, 14, 15);
__m256i mx = _mm256_and_si256(
_mm256_cmpgt_epi32(x, lo), _mm256_cmpgt_epi32(hi, x));
__m256i my = _mm256_and_si256(
_mm256_cmpgt_epi32(y, lo), _mm256_cmpgt_epi32(hi, y));
__m256i inf = _mm256_set1_epi32(INF);
__m256i a =
_mm256_blendv_epi8(inf, _mm256_loadu_si256((const __m256i*)p), mx);
__m256i b = _mm256_blendv_epi8(
inf, _mm256_loadu_si256((const __m256i*)(p + 8)), my);
a = _mm256_min_epi32(a, b);
a = _mm256_min_epi32(a, _mm256_permute2x128_si256(a, a, 1));
a = _mm256_min_epi32(a, _mm256_shuffle_epi32(a, 0x4e));
a = _mm256_min_epi32(a, _mm256_shuffle_epi32(a, 0xb1));
return _mm256_extract_epi32(a, 0);
} else {
__m256i lo = _mm256_set1_epi64x(l - 1);
__m256i hi = _mm256_set1_epi64x(r);
__m256i x = _mm256_setr_epi64x(0, 1, 2, 3);
__m256i y = _mm256_setr_epi64x(4, 5, 6, 7);
__m256i mx = _mm256_and_si256(
_mm256_cmpgt_epi64(x, lo), _mm256_cmpgt_epi64(hi, x));
__m256i my = _mm256_and_si256(
_mm256_cmpgt_epi64(y, lo), _mm256_cmpgt_epi64(hi, y));
__m256i inf = _mm256_set1_epi64x(INF);
__m256i a =
_mm256_blendv_epi8(inf, _mm256_loadu_si256((const __m256i*)p), mx);
__m256i b = _mm256_blendv_epi8(
inf, _mm256_loadu_si256((const __m256i*)(p + 4)), my);
a = min64(a, b);
a = min64(a, _mm256_permute4x64_epi64(a, 0x4e));
a = min64(a, _mm256_permute4x64_epi64(a, 0xb1));
return (T)_mm256_extract_epi64(a, 0);
}
}
template <bool tie_is_left>
__attribute__((target("avx2"))) static int find_equal_avx2(
const T* p, int l, int r, T x) {
if (r - l == 1) return l;
__m256i a = _mm256_loadu_si256((const __m256i*)p);
__m256i b = _mm256_loadu_si256((const __m256i*)(p + B / 2));
unsigned mask;
if constexpr (is_same_v<T, int>) {
__m256i v = _mm256_set1_epi32(x);
mask = _mm256_movemask_ps(_mm256_castsi256_ps(_mm256_cmpeq_epi32(a, v)));
mask |= unsigned(_mm256_movemask_ps(
_mm256_castsi256_ps(_mm256_cmpeq_epi32(b, v))))
<< 8;
} else {
__m256i v = _mm256_set1_epi64x(x);
mask = _mm256_movemask_pd(_mm256_castsi256_pd(_mm256_cmpeq_epi64(a, v)));
mask |= unsigned(_mm256_movemask_pd(
_mm256_castsi256_pd(_mm256_cmpeq_epi64(b, v))))
<< 4;
}
mask &= (1u << r) - (1u << l);
assert(mask);
if constexpr (tie_is_left) return __builtin_ctz(mask);
return 31 - __builtin_clz(mask);
}
#endif
// min 固有の更新最適化
template <typename F>
void set_by(int i, T x, F reduce) {
T& a = dat[0][i / B].a[i % B];
T old = a;
if (old == x) return;
a = x;
for (int k = 0; k + 1 < (int)dat.size(); ++k) {
int b = i / B;
T& p = dat[k + 1][b / B].a[b % B];
T old_p = p;
T new_p;
if (x < old) {
new_p = min(old_p, x);
} else {
if (old > old_p) break;
new_p = reduce(dat[k][b].a);
}
if (new_p == old_p) break;
p = new_p;
old = old_p;
x = new_p;
i = b;
}
}
template <typename F>
T prod_by(int l, int r, F reduce) const {
T ans = INF;
for (const auto& level : dat) {
if (l >= r) break;
int a = l / B, b = (r - 1) / B;
if (a == b) {
return min(ans, reduce(level[a].a, l % B, (r - 1) % B + 1));
}
if (l % B) {
ans = min(ans, reduce(level[a].a, l % B, B));
l = (a + 1) * B;
}
if (r % B) {
ans = min(ans, reduce(level[b].a, 0, r % B));
r = b * B;
}
l /= B;
r /= B;
}
return ans;
}
template <bool tie_is_left, typename F, typename G>
pair<T, int> min_idx_by(int l, int r, F reduce, G find_equal) const {
if (l == r) return {INF, -1};
if (r - l == 1) return {get(l), l};
struct Candidate {
T x;
int k, b, l, r;
};
Candidate a{INF, -1, 0, 0, 0}, b = a;
// Left chunks arrive left-to-right, right chunks right-to-left.
auto add = [&](Candidate& c, int k, int b, int l, int r, bool replace_tie) {
T x = reduce(dat[k][b].a, l, r);
if (c.k == -1 || x < c.x || (replace_tie && x == c.x)) {
c = {x, k, b, l, r};
}
};
for (int k = 0; k < (int)dat.size() && l < r; ++k) {
int p = l / B, q = (r - 1) / B;
if (p == q) {
add(a, k, p, l % B, (r - 1) % B + 1, !tie_is_left);
break;
}
if (l % B) {
add(a, k, p, l % B, B, !tie_is_left);
l = (p + 1) * B;
}
if (r % B) {
add(b, k, q, 0, r % B, tie_is_left);
r = q * B;
}
l /= B;
r /= B;
}
// Every left candidate precedes every right candidate.
const Candidate& c =
(b.k == -1 || (a.k != -1 && (a.x < b.x || (tie_is_left && a.x == b.x))))
? a
: b;
assert(c.k >= 0);
int i = B * c.b + find_equal(dat[c.k][c.b].a, c.l, c.r, c.x);
for (int k = c.k - 1; k >= 0; --k) {
i = B * i + find_equal(dat[k][i].a, 0, B, c.x);
}
return {c.x, i};
}
#if RANGE_MIN_AVX2
__attribute__((target("avx2"))) void set_avx2(int i, T x) {
set_by(i, x, reduce_avx2);
}
__attribute__((target("avx2"))) T prod_avx2(int l, int r) const {
return prod_by(l, r, range_avx2);
}
template <bool tie_is_left>
__attribute__((target("avx2"))) pair<T, int> min_idx_avx2(
int l, int r) const {
return min_idx_by<tie_is_left>(
l, r, range_avx2, find_equal_avx2<tie_is_left>);
}
#endif
// k 段目の失敗した要素から葉へ降りる
template <typename F>
int descend_right(int k, int i, T sm, F& check) const {
for (--k; k >= 0; --k) {
const auto& v = dat[k][i].a;
for (int j = 0; j < B; ++j) {
T y = min(sm, v[j]);
if (!check(y)) {
i = B * i + j;
break;
}
sm = y;
}
}
return min(i, n);
}
template <typename F>
int descend_left(int k, int i, T sm, F& check) const {
for (--k; k >= 0; --k) {
const auto& v = dat[k][i].a;
for (int j = B - 1; j >= 0; --j) {
T y = min(v[j], sm);
if (!check(y)) {
i = B * i + j;
break;
}
sm = y;
}
}
return min(i + 1, n);
}
public:
Range_Min() { build(0); }
Range_Min(int m) { build(m); }
Range_Min(const vc<T>& a) { build(a); }
template <typename F>
Range_Min(int m, F f) {
build(m, f);
}
void build(int m) {
build(m, [](int) -> T { return INF; });
}
void build(const vc<T>& a) {
build((int)a.size(), [&](int i) -> T { return a[i]; });
}
template <typename F>
void build(int m, F f) {
assert(m >= 0);
n = m;
vc<T> cur(max(m, 1));
for (int i = 0; i < m; ++i) cur[i] = f(i);
if (!m) cur[0] = INF;
dat.clear();
while (true) {
int sz = ((int)cur.size() + B - 1) / B;
dat.emplace_back(sz);
vc<T> nxt(sz);
for (int b = 0; b < sz; ++b) {
T* p = dat.back()[b].a;
fill(p, p + B, INF);
for (int j = 0; j < B && b * B + j < (int)cur.size(); ++j) {
p[j] = cur[b * B + j];
}
nxt[b] = reduce_scalar(p);
}
if (cur.size() == 1) break;
cur.swap(nxt);
}
use_avx2 = false;
#if RANGE_MIN_AVX2
__builtin_cpu_init();
use_avx2 = __builtin_cpu_supports("avx2");
#endif
}
T get(int i) const {
assert(0 <= i && i < n);
return dat[0][i / B].a[i % B];
}
vc<T> get_all() const {
vc<T> a(n);
for (int i = 0; i < n; ++i) {
a[i] = dat[0][i / B].a[i % B];
}
return a;
}
void set(int i, const T& x) {
assert(0 <= i && i < n);
#if RANGE_MIN_AVX2
if (use_avx2) return set_avx2(i, x);
#endif
set_by(i, x, reduce_scalar);
}
void multiply(int i, const T& x) { set(i, min(get(i), x)); }
T prod(int l, int r) const {
assert(0 <= l && l <= r && r <= n);
#if RANGE_MIN_AVX2
if (use_avx2) return prod_avx2(l, r);
#endif
return prod_by(l, r, range_scalar);
}
// {minimum, index}; empty interval returns {INF, -1}.
// tie_is_left = false selects the rightmost minimum.
template <bool tie_is_left = true>
pair<T, int> min_idx(int l, int r) const {
assert(0 <= l && l <= r && r <= n);
#if RANGE_MIN_AVX2
if (use_avx2) return min_idx_avx2<tie_is_left>(l, r);
#endif
return min_idx_by<tie_is_left>(
l, r, range_scalar, find_equal_scalar<tie_is_left>);
}
T prod_all() const { return dat.back()[0].a[0]; }
template <typename F>
int max_right(F check, int L) const {
assert(0 <= L && L <= n && check(INF));
if (L == n) return n;
T sm = INF;
int i = L;
for (int k = 0; k < (int)dat.size(); ++k) {
if (i % B || k + 1 == (int)dat.size()) {
int b = i / B;
for (int j = i % B; j < B; ++j) {
T y = min(sm, dat[k][b].a[j]);
if (!check(y)) {
return descend_right(k, b * B + j, sm, check);
}
sm = y;
}
i = b + 1;
} else {
i /= B;
}
}
return n;
}
template <typename F>
int min_left(F check, int R) const {
assert(0 <= R && R <= n && check(INF));
if (R == 0) return 0;
T sm = INF;
int i = R;
for (int k = 0; k < (int)dat.size(); ++k) {
if (i % B || k + 1 == (int)dat.size()) {
if (i == 0) return 0;
int b = (i - 1) / B;
for (int j = (i - 1) % B; j >= 0; --j) {
T y = min(dat[k][b].a[j], sm);
if (!check(y)) {
return descend_left(k, b * B + j, sm, check);
}
sm = y;
}
i = b;
} else {
i /= B;
}
}
return 0;
}
};
#undef RANGE_MIN_AVX2
#line 7 "test/2_library_checker/data_structure/staticrmq_simd.test.cpp"
void solve() {
INT(N, Q);
Range_Min<int> seg(N, [&](int i) -> int {
INT(x);
return x;
});
FOR(Q) {
INT(l, r);
print(seg.prod(l, r));
}
}
signed main() {
int T = 1;
// INT(T);
FOR(T) solve();
return 0;
}