結果

問題 No.875 Range Mindex Query
ユーザー ninoinuininoinui
提出日時 2020-09-27 17:34:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 3,037 bytes
コンパイル時間 3,365 ms
コンパイル使用メモリ 204,712 KB
実行使用メモリ 6,000 KB
最終ジャッジ日時 2023-09-13 02:07:26
合計ジャッジ時間 4,123 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 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,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 69 ms
5,712 KB
testcase_12 AC 58 ms
4,608 KB
testcase_13 AC 50 ms
5,956 KB
testcase_14 AC 49 ms
5,620 KB
testcase_15 AC 67 ms
6,000 KB
testcase_16 AC 60 ms
5,880 KB
testcase_17 AC 65 ms
5,984 KB
testcase_18 AC 63 ms
5,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template <typename T, typename U> bool cmin(T &a, U b) { return a > b && (a = b, true); }
template <typename T, typename U> bool cmax(T &a, U b) { return a < b && (a = b, true); }

template <typename T, T (*op)(T, T), T (*ie)()> class SegmentTree {
private:
  int N, size, log;
  vector<T> D;
  void apply(int k) { D.at(k) = op(D.at(2 * k), D.at(2 * k + 1)); }
  int ceil_pow(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
  }

public:
  SegmentTree() : SegmentTree(0) {}
  SegmentTree(int n) : SegmentTree(vector<T>(n, ie())) {}
  SegmentTree(const vector<T> &v) : N((int)v.size()) {
    log = ceil_pow(N);
    size = 1 << log;
    D = vector<T>(2 * size, ie());
    for (int i = 0; i < N; i++) D.at(size + i) = v.at(i);
    for (int i = size - 1; i >= 1; i--) apply(i);
  }
  void set(int p, T x) {
    p += size;
    D.at(p) = x;
    for (int i = 1; i <= log; i++) apply(p >> i);
  }
  T get(int p) { return D.at(p + size); }
  T query(int l, int r) {
    r++;
    T sml = ie(), smr = ie();
    l += size, r += size;
    while (l < r) {
      if (l & 1) sml = op(sml, D.at(l++));
      if (r & 1) smr = op(D.at(--r), smr);
      l >>= 1, r >>= 1;
    }
    return op(sml, smr);
  }
  T query_all() { return D.at(1); }
  template <bool (*f)(T)> int max_right(int l) {
    return max_right(l, [](T x) { return f(x); });
  }
  template <class F> int max_right(int l, F f) {
    if (l == N) return N;
    l += size;
    T sm = ie();
    do {
      while (l % 2 == 0) l >>= 1;
      if (!f(op(sm, D.at(l)))) {
        while (l < size) {
          l = (2 * l);
          if (f(op(sm, D.at(l)))) sm = op(sm, D.at(l++));
        }
        return l - size;
      }
      sm = op(sm, D.at(l++));
    } while ((l & -l) != l);
    return N;
  }
  template <bool (*f)(T)> int min_left(int r) {
    return min_left(r, [](T x) { return f(x); });
  }
  template <class F> int min_left(int r, F f) {
    if (r == 0) return 0;
    r += size;
    T sm = ie();
    do {
      r--;
      while (r > 1 && (r % 2)) r >>= 1;
      if (!f(op(D.at(r), sm))) {
        while (r < size) {
          r = (2 * r + 1);
          if (f(op(D.at(r), sm))) sm = op(D.at(r--), sm);
        }
        return r + 1 - size;
      }
      sm = op(D.at(r), sm);
    } while ((r & -r) != r);
    return 0;
  }
};

// monoid setting

using mono = pair<int, int>;
mono op(mono a, mono b) { return min(a, b); };
mono ie() { return {INT_MAX, -1}; };

// setting end

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  int N, Q;
  cin >> N >> Q;
  vector<mono> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i).first;
    A.at(i).second = i;
  }

  SegmentTree<mono, op, ie> ST(A);
  for (int i = 0; i < Q; i++) {
    int q, l, r;
    cin >> q >> l >> r, l--, r--;
    if (q == 1) {
      auto [a, b] = ST.get(l);
      auto [c, d] = ST.get(r);
      ST.set(l, {c, b});
      ST.set(r, {a, d});
    } else {
      cout << ST.query(l, r).second + 1 << "\n";
    }
  }
}
0