結果
問題 | No.875 Range Mindex Query |
ユーザー | tkmst201 |
提出日時 | 2021-02-18 17:56:48 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 76 ms / 2,000 ms |
コード長 | 3,731 bytes |
コンパイル時間 | 2,185 ms |
コンパイル使用メモリ | 208,264 KB |
実行使用メモリ | 6,272 KB |
最終ジャッジ日時 | 2024-09-14 23:29:43 |
合計ジャッジ時間 | 3,885 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge6 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 3 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 3 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 3 ms
5,376 KB |
testcase_11 | AC | 72 ms
6,016 KB |
testcase_12 | AC | 59 ms
5,376 KB |
testcase_13 | AC | 52 ms
6,272 KB |
testcase_14 | AC | 53 ms
6,272 KB |
testcase_15 | AC | 69 ms
6,272 KB |
testcase_16 | AC | 70 ms
6,272 KB |
testcase_17 | AC | 76 ms
6,272 KB |
testcase_18 | AC | 72 ms
6,272 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) begin(v),end(v) template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } using ll = long long; using pii = pair<int, int>; constexpr ll INF = 1ll<<30; constexpr ll longINF = 1ll<<60; constexpr ll MOD = 1000000007; constexpr bool debug = false; //---------------------------------// /** * @brief https://tkmst201.github.io/Library/DataStructure/SegmentTree.hpp */ template<typename T> struct SegmentTree { using value_type = T; using const_reference = const value_type &; using F = std::function<value_type (const_reference, const_reference)>; using size_type = std::size_t; private: size_type n, n_; value_type id_elem; F f; std::vector<value_type> node; public: SegmentTree() = default; SegmentTree(size_type n, const_reference id_elem, const F & f) : n(n), id_elem(id_elem), f(f) { n_ = 1; while (n_ < n) n_ <<= 1; node.resize(2 * n_, id_elem); } SegmentTree(const std::vector<value_type> & v, const_reference id_elem, const F & f) : SegmentTree(v.size(), id_elem, f) { for (size_type i = 0; i < v.size(); ++i) node[i + n_] = v[i]; for (size_type i = n_ - 1; i > 0; --i) node[i] = f(node[i << 1], node[i << 1 | 1]); } size_type size() const noexcept { return n; } void set(size_type i, const_reference x) { assert(i < size()); node[i += n_] = x; while (i > 1) { i >>= 1; node[i] = f(node[i << 1], node[i << 1 | 1]); } } const_reference get(size_type i) const { assert(i < size()); return node[i + n_]; } value_type fold(size_type l, size_type r) const { assert(l <= r); assert(r <= size()); value_type lv = id_elem, rv = id_elem; for (l += n_, r += n_; l < r; l >>= 1, r >>= 1) { if (l & 1) lv = f(lv, node[l++]); if (r & 1) rv = f(node[r - 1], rv); } return f(lv, rv); } const_reference fold_all() const { return node[1]; } size_type max_right(size_type l, std::function<bool (const_reference)> g) const { assert(l <= size()); assert(g(id_elem)); if (l == size()) return size(); l += n_; value_type sum = id_elem; while (true) { while (~l & 1) l >>= 1; const value_type nex_sum = f(sum, node[l]); if (g(nex_sum)) { sum = nex_sum; ++l; } else break; if ((l & -l) == l) return size(); } while (l < n_) { const value_type nex_sum = f(sum, node[l << 1]); l <<= 1; if (g(nex_sum)) { sum = nex_sum; l |= 1; } } return l - n_; } size_type min_left(size_type r, std::function<bool (const_reference)> g) const { assert(r <= size()); assert(g(id_elem)); if (r == 0) return 0; r += n_; value_type sum = id_elem; while (true) { --r; while (r > 1 && (r & 1)) r >>= 1; const value_type nex_sum = f(node[r], sum); if (g(nex_sum)) sum = nex_sum; else break; if ((r & -r) == r) return 0; } while (r < n_) { const value_type nex_sum = f(node[r << 1 | 1], sum); r <<= 1; if (!g(nex_sum)) r |= 1; else sum = nex_sum; } return r + 1 - n_; } }; int main() { int N, Q; cin >> N >> Q; vector<pii> A(N); REP(i, N) { int a; scanf("%d", &a); A[i] = {a, i}; } SegmentTree<pii> seg(A, pii{INF, 0}, [](const pii & a, const pii & b) { return a.first < b.first ? a : b; }); while (Q--) { int q, l, r; scanf("%d %d %d", &q, &l, &r); --l; if (q == 1) { --r; pii p = {seg.get(r).first, l}; seg.set(r, {seg.get(l).first, r}); seg.set(l, p); } else printf("%d\n", seg.fold(l, r).second + 1); } }