結果
| 問題 |
No.875 Range Mindex Query
|
| コンテスト | |
| ユーザー |
ngtkana
|
| 提出日時 | 2019-09-06 21:36:49 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 301 ms / 2,000 ms |
| コード長 | 4,893 bytes |
| コンパイル時間 | 2,666 ms |
| コンパイル使用メモリ | 207,948 KB |
| 最終ジャッジ日時 | 2025-01-07 16:43:01 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 18 |
ソースコード
#include <bits/stdc++.h>
#define loop(n) for (int ngtkana_is_genius = 0; ngtkana_is_genius < int(n); ngtkana_is_genius++)
#define rep(i, begin, end) for(int i = int(begin); i < int(end); i++)
#define all(v) v.begin(), v.end()
#define lint long long
auto cmn = [](auto& a, auto b){if (a > b) {a = b; return true;} return false;};
auto cmx = [](auto& a, auto b){if (a < b) {a = b; return true;} return false;};
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail){
std::cerr << " " << head;
debug_impl(tail...);
}
#define debug(...)\
std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
debug_impl(__VA_ARGS__);\
std::cerr << std::noboolalpha;
template<typename Value, typename BinaryOp>
class segment_tree
{
int n, N;
BinaryOp op;
Value id;
std::vector<Value> table;
void cal (int u)
{
table.at(u) = op(table.at(2 * u), table.at(2 * u + 1));
}
public:
segment_tree(int size, BinaryOp op, Value id, const std::vector<Value>& v):
n (std::pow(2, int(std::log2(2 * size - 1)))),
N (n * 2),
op (op),
id (id),
table (N, id)
{
assert(n > 0);
std::move(v.begin(), v.end(), table.begin() + n);
for (int i = n - 1; i != 0; i--) cal(i);
}
segment_tree(int size, BinaryOp op, Value id):
segment_tree(size, std::move(op), id, std::vector<Value>(size, id))
{}
auto at (int i) const -> Value
{
return table.at(n + i);
}
auto collect () const -> std::vector<Value>
{
auto ret = std::vector<Value>(n);
for (auto i = 0; i < n; i++)
{
ret.at(i) = at(i);
}
return ret;
}
auto query (int l, int r) const -> Value
{
struct state {int top, left, right;};
auto ret = id;
std::stack<state> stk; stk.push({1, 0, n});
while (!stk.empty())
{
auto now = stk.top(); stk.pop();
if (l <= now.left && now.right <= r)
{
ret = op(ret, table.at(now.top));
continue;
}
int mid = (now.left + now.right) / 2;
if (l < mid) stk.push({2 * now.top, now.left, mid});
if (mid < r) stk.push({2 * now.top + 1, mid, now.right});
}
return ret;
}
void update (int u, Value val)
{
table.at(u += n) = val;
for (u /= 2; u != 0; u /= 2) cal(u);
}
void add (int u, Value val)
{
update(u, at(u) + val);
}
};
template<typename Value, typename BinaryOp>
auto make_segment_tree(int size, BinaryOp op, Value vid)
{
return segment_tree<Value, BinaryOp>(size, std::move(op), vid);
}
template<typename Value, typename BinaryOp>
auto make_segment_tree(int size, BinaryOp op, Value vid, std::vector<Value> v)
{
return segment_tree<Value, BinaryOp>(size, std::move(op), vid, std::move(v));
}
template <typename T>
std::istream& operator>> (std::istream& is, std::vector<T>& v) {
for (auto & x : v) is >> x;
return is;
}
template <typename T>
std::ostream& operator<< (std::ostream& os, const std::vector<T>& v) {
auto n = v.size();
os << "{";
for (size_t i = 0; i < n; i++)
{os << (i > 0 ? "," : "") << v.at(i);}
return os << "}";
}
template <typename T, typename U>
std::ostream& operator<< (std::ostream& os, const std::pair<T, U>& pair){
return os << "(" << pair.first << "," << pair.second << ")";
}
template <typename T, typename U>
std::istream& operator>> (std::iostream& is, std::pair<T, U>& pair) {
return is >> pair.first >> pair.second;
}
template <typename InputIterator, typename OutputIterator>
constexpr OutputIterator
coenumerate(InputIterator first,
InputIterator last,
OutputIterator result)
{
int cnt = 0;
while (first != last)
{
*result = std::make_pair(*first, cnt);
result++, first++, cnt++;
}
return result;
}
int main() {
std::cin.tie(0); std::cin.sync_with_stdio(false);
constexpr int inf = 1 << 30;
int n, q; std::cin >> n >> q;
std::vector<int> a(n); std::cin >> a;
for (auto & x : a) x--;
std::vector<std::pair<int, int>> pairs(n);
coenumerate(all(a), pairs.begin());
auto min_tree = make_segment_tree<std::pair<int, int>>(
n,
[](auto p, auto q) { return p.first < q.first ? p : q; },
std::pair<int, int>{inf, -1},
pairs
);
// debug(min_tree.collect());
loop(q) {
int c, l, r;
std::cin >> c >> l >> r;
if (c == 1) {
l--, r--;
auto L = min_tree.at(l).first;
auto R = min_tree.at(r).first;
min_tree.update(l, std::pair<int, int>{R, l});
min_tree.update(r, std::pair<int, int>{L, r});
// debug(min_tree.collect());
} else {
l--;
auto ret = min_tree.query(l, r).second + 1;
std::cout << ret << std::endl;
}
}
return 0;
}
ngtkana