結果

問題 No.875 Range Mindex Query
ユーザー kyo1kyo1
提出日時 2020-09-04 13:08:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 115 ms / 2,000 ms
コード長 1,805 bytes
コンパイル時間 2,206 ms
コンパイル使用メモリ 212,504 KB
実行使用メモリ 9,144 KB
最終ジャッジ日時 2023-08-16 18:25:19
合計ジャッジ時間 5,965 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 111 ms
8,000 KB
testcase_12 AC 84 ms
6,492 KB
testcase_13 AC 92 ms
8,872 KB
testcase_14 AC 86 ms
8,528 KB
testcase_15 AC 115 ms
8,780 KB
testcase_16 AC 96 ms
8,860 KB
testcase_17 AC 105 ms
9,144 KB
testcase_18 AC 100 ms
9,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

template <typename M>
class SegmentTree {
 private:
  using Type = typename M::Type;

  size_t sz = 1;
  std::vector<Type> node;

 public:
  SegmentTree(const size_t n) {
    while (sz < n) {
      sz *= 2;
    }
    node.resize(2 * sz, M::e);
  }

  SegmentTree(const std::vector<Type> &vec) {
    while (sz < vec.size()) {
      sz *= 2;
    }
    node.resize(2 * sz, M::e);
    for (size_t i = 0; i < vec.size(); i++) {
      node[i + sz] = vec[i];
    }
    for (size_t i = sz - 1; i < sz; i--) {
      node[i] = M::op(node[2 * i], node[2 * i + 1]);
    }
  }

  void update(size_t i, const Type x) {
    i += sz;
    node[i] = M::ap(node[i], x);
    while (i > 0) {
      i /= 2;
      node[i] = M::op(node[2 * i], node[2 * i + 1]);
    }
  }

  Type query(int l, int r) {
    Type left = M::e, right = M::e;
    for (l += sz, r += sz; l < r; l /= 2, r /= 2) {
      if (l & 1) left = M::op(left, node[l++]);
      if (r & 1) right = M::op(node[--r], right);
    }
    return M::op(left, right);
  }
};

struct Monoid {
  using Type = int;
  constexpr static Type e = 1 << 28;
  static Type op(const Type &a, const Type &b) { return min(a, b); }
  static Type ap(const Type &a, const Type &b) { return b; }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, Q;
  cin >> N >> Q;
  vector<int> A(N);
  map<int, int> mp;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    mp[A[i]] = i;
  }
  SegmentTree<Monoid> sg(A);
  for (int i = 0; i < Q; i++) {
    int q, l, r;
    cin >> q >> l >> r;
    l--, r--;
    if (q == 1) {
      mp[A[r]] = l;
      mp[A[l]] = r;
      sg.update(l, A[r]);
      sg.update(r, A[l]);
      swap(A[l], A[r]);
    } else {
      cout << mp[sg.query(l, r + 1)] + 1 << '\n';
    }
  }
  return 0;
}
0