競技プログラミング用の C++ ライブラリ
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_3_B"
#include <iostream>
#include <vector>
#include "../../src/data_structures/segtree.hpp"
// 最小値と最大値の組 {min, max} を 1 本のセグメント木で持つ
int main() {
int n;
std::cin >> n;
using M = monoid::MinMax<long long>;
std::vector<M::S> a(n);
for (auto& [mn, mx] : a) {
std::cin >> mn;
mx = mn;
}
SegTree<M> seg(a);
int q;
std::cin >> q;
while (q--) {
int com, b, e;
std::cin >> com >> b >> e;
auto [mn, mx] = seg.prod(b, e);
std::cout << (com == 0 ? mn : mx) << '\n';
}
}#line 1 "test/aoj/itp2-3-b.2.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_3_B"
#include <iostream>
#include <vector>
#line 2 "src/data_structures/segtree.hpp"
/**
* @file segtree.hpp
* @brief セグメント木
*/
#include <cassert>
#include <functional>
#include <ostream>
#include <utility>
#line 2 "src/algebra/monoid.hpp"
/**
* @file monoid.hpp
* @brief モノイドの定義
*
* モノイドは次のメンバを持つ型として表す
* - using S: 要素の型
* - S op(const S& a, const S& b): 積 (結合的)
* - S e(): 単位元
* op, e は static でも非 static でもよい (データ構造はモノイドのオブジェクトを保持して呼び出す)
*/
#include <algorithm>
#include <istream>
#include <limits>
#include <numeric>
#line 19 "src/algebra/monoid.hpp"
#include <type_traits>
#line 21 "src/algebra/monoid.hpp"
/**
* @brief 1次関数 f(x) = a * x + b
*
* monoid::Composite の要素や acted_monoid::AffineSum の作用素に使う
* 例: Affine<mint> f{2, 3}; f(x) は 2x + 3。省略すると恒等写像 {1, 0}
*/
template <typename T>
struct Affine {
T a = T(1); //!< 傾き
T b = T(0); //!< 切片
constexpr T operator()(const T& x) const { return a * x + b; }
friend constexpr bool operator==(const Affine& f, const Affine& g) { return f.a == g.a && f.b == g.b; }
friend constexpr bool operator!=(const Affine& f, const Affine& g) { return !(f == g); }
template <class CharT, class Traits>
friend std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is, Affine& f) {
return is >> f.a >> f.b;
}
template <class CharT, class Traits>
friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const Affine& f) {
return os << f.a << CharT(' ') << f.b;
}
};
namespace monoid {
/**
* @brief 和
*/
template <typename T>
struct Sum {
using S = T;
static constexpr S op(const S& a, const S& b) { return a + b; }
static constexpr S e() { return S(0); }
};
/**
* @brief 積
*/
template <typename T>
struct Prod {
using S = T;
static constexpr S op(const S& a, const S& b) { return a * b; }
static constexpr S e() { return S(1); }
};
/**
* @brief 最大値 単位元は型の最小値
*/
template <typename T>
struct Max {
using S = T;
static constexpr S op(const S& a, const S& b) { return std::max(a, b); }
static constexpr S e() { return std::numeric_limits<S>::lowest(); }
};
/**
* @brief 最小値 単位元は型の最大値
*/
template <typename T>
struct Min {
using S = T;
static constexpr S op(const S& a, const S& b) { return std::min(a, b); }
static constexpr S e() { return std::numeric_limits<S>::max(); }
};
/**
* @brief 最小値と最大値の組 {min, max}
*/
template <typename T>
struct MinMax {
using S = std::pair<T, T>;
static constexpr S op(const S& a, const S& b) { return {std::min(a.first, b.first), std::max(a.second, b.second)}; }
static constexpr S e() { return {std::numeric_limits<T>::max(), std::numeric_limits<T>::lowest()}; }
};
/**
* @brief 排他的論理和
*/
template <typename T>
struct Xor {
using S = T;
static constexpr S op(const S& a, const S& b) { return a ^ b; }
static constexpr S e() { return S(0); }
};
/**
* @brief 論理積
*/
template <typename T>
struct And {
using S = T;
static constexpr S op(const S& a, const S& b) { return a & b; }
static constexpr S e() { return ~S(0); }
};
/**
* @brief 論理和
*/
template <typename T>
struct Or {
using S = T;
static constexpr S op(const S& a, const S& b) { return a | b; }
static constexpr S e() { return S(0); }
};
/**
* @brief 最大公約数 単位元は0
*/
template <typename T>
struct Gcd {
static_assert(std::is_integral_v<T>);
using S = T;
static constexpr S op(const S& a, const S& b) { return std::gcd(a, b); }
static constexpr S e() { return S(0); }
};
/**
* @brief 最小公倍数 単位元は1
*/
template <typename T>
struct Lcm {
static_assert(std::is_integral_v<T>);
using S = T;
static constexpr S op(const S& a, const S& b) { return std::lcm(a, b); }
static constexpr S e() { return S(1); }
};
/**
* @brief 1次関数 (Affine) の合成
* op(f, g) = g∘f (左から順に適用する)
* 区間 [l, r) の積は f_{r-1}(...f_l(x)) を表す
*/
template <typename T>
struct Composite {
using S = Affine<T>;
static constexpr S op(const S& f, const S& g) { return {g.a * f.a, g.a * f.b + g.b}; }
static constexpr S e() { return {T(1), T(0)}; }
};
/**
* @brief 括弧列 対応の取れていない ')' と '(' の個数
*/
struct Parenthesis {
struct S {
long long close; // 対応の取れていない ')' の個数
long long open; // 対応の取れていない '(' の個数
constexpr S(char c = 0) : close(c == ')'), open(c == '(') {}
constexpr S(long long close, long long open) : close(close), open(open) {}
/**
* @brief 正しい括弧列か
*/
constexpr bool is_valid() const { return close == 0 && open == 0; }
};
static constexpr S op(const S& a, const S& b) {
if(a.open >= b.close) return S(a.close, a.open - b.close + b.open);
return S(a.close + b.close - a.open, b.open);
}
static constexpr S e() { return S(); }
};
/**
* @brief 最小値とその位置 同じ値なら左側を優先
*
* @tparam T 値の型
* @tparam U 位置などの付随する値の型
*/
template <typename T, typename U = int>
struct MinWith {
using S = std::pair<T, U>;
static constexpr S op(const S& a, const S& b) { return b.first < a.first ? b : a; }
static constexpr S e() { return {std::numeric_limits<T>::max(), U{}}; }
};
/**
* @brief 最大値とその位置 同じ値なら左側を優先
*
* @tparam T 値の型
* @tparam U 位置などの付随する値の型
*/
template <typename T, typename U = int>
struct MaxWith {
using S = std::pair<T, U>;
static constexpr S op(const S& a, const S& b) { return a.first < b.first ? b : a; }
static constexpr S e() { return {std::numeric_limits<T>::lowest(), U{}}; }
};
/**
* @brief 最小値とその個数
*
* @tparam T 値の型
* @tparam U 個数の型
*/
template <typename T, typename U = long long>
struct MinCount {
using S = std::pair<T, U>;
static constexpr S op(const S& a, const S& b) {
if(a.first < b.first) return a;
if(b.first < a.first) return b;
return {a.first, a.second + b.second};
}
static constexpr S e() { return {std::numeric_limits<T>::max(), U(0)}; }
};
/**
* @brief 最大値とその個数
*
* @tparam T 値の型
* @tparam U 個数の型
*/
template <typename T, typename U = long long>
struct MaxCount {
using S = std::pair<T, U>;
static constexpr S op(const S& a, const S& b) {
if(a.first < b.first) return b;
if(b.first < a.first) return a;
return {a.first, a.second + b.second};
}
static constexpr S e() { return {std::numeric_limits<T>::lowest(), U(0)}; }
};
} // namespace monoid
/**
* @brief 関数オブジェクトと単位元から実行時に作るモノイド
* ラムダ式を使える
*
* 例: SegTree seg(v, Monoid([](int a, int b) { return std::max(a, b); }, -1));
*
* @tparam T 要素の型
* @tparam Op 積の関数オブジェクトの型
*/
template <typename T, typename Op>
struct Monoid {
using S = T;
Op op_object;
S identity;
Monoid() = default;
constexpr Monoid(Op op, const S& e) : op_object(std::move(op)), identity(e) {}
constexpr S op(const S& a, const S& b) const { return op_object(a, b); }
constexpr S e() const { return identity; }
};
template <typename Op, typename T>
Monoid(Op, T) -> Monoid<T, Op>;
#line 14 "src/data_structures/segtree.hpp"
/**
* @brief セグメント木
*
* 例:
* SegTree<monoid::Max<int>> seg(n);
* SegTree seg(v, Monoid([](int a, int b) { return a + b; }, 0));
*
* @tparam M モノイド (algebra/monoid.hpp)
*/
template <typename M>
class SegTree {
public:
using S = typename M::S;
private:
M m;
S op(const S& a, const S& b) const { return m.op(a, b); }
S e() const { return m.e(); }
int n, sz, height;
std::vector<S> data;
void update(int k) { data[k] = op(data[2 * k], data[2 * k + 1]); }
class SegTreeReference {
SegTree& segtree;
int k;
public:
SegTreeReference(SegTree& segtree, int k) : segtree(segtree), k(k) {}
SegTreeReference& operator=(const S& x) {
segtree.set(k, x);
return *this;
}
operator S() const { return segtree.get(k); }
};
void construct_data() {
sz = 1;
height = 0;
while (sz < n) {
sz <<= 1;
height++;
}
data.assign(sz * 2, e());
}
public:
/**
* @brief コンストラクタ
*
* @param n 要素数
* @param m モノイド
*/
explicit SegTree(int n = 0, M m = M()) : m(std::move(m)), n(n) {
construct_data();
}
/**
* @brief コンストラクタ
*
* @param v 初期の要素
* @param m モノイド
*/
explicit SegTree(const std::vector<S>& v, M m = M()) : SegTree(v.size(), std::move(m)) {
for (int i = 0; i < n; i++) data[sz + i] = v[i];
for (int i = sz - 1; i > 0; i--) update(i);
}
/**
* @brief 要素数を返す
*/
int size() const { return n; }
/**
* @brief 指定された要素の値を返す
*
* @param k インデックス
* @return S 値
*/
S get(int k) const {
assert(0 <= k && k < n);
return data[sz + k];
}
/**
* @brief 指定された要素の値を返す
*
* @param k インデックス
* @return S 値
*/
S operator[] (int k) const { return get(k); }
/**
* @brief 指定された要素への参照を返す
*
* @param k
* @return SegTreeReference 要素への参照 代入されるとset()が呼ばれる
*/
SegTreeReference operator[] (int k) { return SegTreeReference(*this, k); }
/**
* @brief 内容を出力する
*
* @tparam CharT 出力ストリームの文字型
* @tparam Traits 出力ストリームの文字型特性
* @param os 出力ストリーム
* @param rhs セグメント木
* @return std::basic_ostream<CharT, Traits>& 出力ストリーム
*/
template <class CharT, class Traits>
friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const SegTree& rhs) {
for(int i = 0; i < rhs.n; i++) {
if(i != 0) os << CharT(' ');
os << rhs[i];
}
return os;
}
/**
* @brief 指定された要素の値をxに更新する
*
* @param k インデックス
* @param x 新しい値
*/
void set(int k, const S& x) {
assert(0 <= k && k < n);
k += sz;
data[k] = x;
for (int i = 1; i <= height; i++) update(k >> i);
}
/**
* @brief 指定された要素の値にxを作用させる
*
* @param k インデックス
* @param x 作用素
*/
void apply(int k, const S& x) { set(k, op(get(k), x)); }
/**
* @brief [l, r)の区間の総積を返す
*
* @param l 半開区間の開始
* @param r 半開区間の終端 0<=l<=r<=n
* @return S 総積
*/
S prod(int l, int r) const {
assert(0 <= l && l <= r && r <= n);
S left_prod = e(), right_prod = e();
l += sz;
r += sz;
while (l < r) {
if (l & 1) left_prod = op(left_prod, data[l++]);
if (r & 1) right_prod = op(data[--r], right_prod);
l >>= 1;
r >>= 1;
}
return op(left_prod, right_prod);
}
/**
* @brief すべての要素の総積を返す
*
* @return S 総積
*/
S all_prod() const { return data[1]; }
/**
* @brief (r = l or f(prod([l, r))) = true) and (r = n or f(prod([l, r+1))) = false)となるrを返す
* fが単調なら、f(prod([l, r))) = trueとなる最大のr
*
* @tparam F
* @param l 半開区間の開始 0<=l<=n
* @param f 判定関数 f(e) = true
* @return int
*/
template <typename F>
int max_right(int l, F f) const {
assert(f(e()));
assert(0 <= l && l <= n);
if (l == n) return n;
l += sz;
while (l % 2 == 0) l >>= 1;
S sum = e();
while(f(op(sum, data[l]))) {
sum = op(sum, data[l]);
l++;
while (l % 2 == 0) l >>= 1;
if (l == 1) return n;
}
while (l < sz) {
if (!f(op(sum, data[l * 2]))) l *= 2;
else {
sum = op(sum, data[l * 2]);
l = l * 2 + 1;
}
}
return l - sz;
}
/**
* @brief (l = 0 or f(prod([l, r))) = true) and (l = r or f(prod([l-1, r))) = false)となるlを返す
* fが単調なら、f(prod([l, r))) = trueとなる最小のl
*
* @tparam F
* @param r 半開区間の終端 0<=r<=n
* @param f 判定関数 f(e) = true
* @return int
*/
template <typename F>
int min_left(int r, F f) const {
assert(f(e()));
assert(0 <= r && r <= n);
if (r == 0) return 0;
r += sz;
S sum = e();
do {
r--;
while (r > 1 && r % 2 == 1) r >>= 1;
if (!f(op(data[r], sum))) {
while (r < sz) {
r = r * 2 + 1;
if (f(op(data[r], sum))) {
sum = op(data[r], sum);
r--;
}
}
return r + 1 - sz;
}
sum = op(data[r], sum);
} while ((r & -r) != r);
return 0;
}
};
template <typename M>
SegTree(int, M) -> SegTree<M>;
template <typename M>
SegTree(std::vector<typename M::S>, M) -> SegTree<M>;
/**
* @brief RangeMaxQuery
*
* @tparam S 型
*/
template <typename S>
using RMaxQ = SegTree<monoid::Max<S>>;
/**
* @brief RangeMinQuery
*
* @tparam S 型
*/
template <typename S>
using RMinQ = SegTree<monoid::Min<S>>;
/**
* @brief RangeSumQuery
*
* @tparam S 型
*/
template <typename S>
using RSumQ = SegTree<monoid::Sum<S>>;
/**
* @brief RangeProdQuery
*
* @tparam S 型
*/
template <typename S>
using RProdQ = SegTree<monoid::Prod<S>>;
/**
* @brief RangeXorQuery
*
* @tparam S 型
*/
template <typename S>
using RXorQ = SegTree<monoid::Xor<S>>;
/**
* @brief RangeGcdQuery
*
* @tparam S 型
*/
template <typename S>
using RGcdQ = SegTree<monoid::Gcd<S>>;
#line 7 "test/aoj/itp2-3-b.2.test.cpp"
// 最小値と最大値の組 {min, max} を 1 本のセグメント木で持つ
int main() {
int n;
std::cin >> n;
using M = monoid::MinMax<long long>;
std::vector<M::S> a(n);
for (auto& [mn, mx] : a) {
std::cin >> mn;
mx = mn;
}
SegTree<M> seg(a);
int q;
std::cin >> q;
while (q--) {
int com, b, e;
std::cin >> com >> b >> e;
auto [mn, mx] = seg.prod(b, e);
std::cout << (com == 0 ? mn : mx) << '\n';
}
}