結果

問題 No.875 Range Mindex Query
ユーザー ninoinuininoinui
提出日時 2020-09-24 08:04:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 71 ms / 2,000 ms
コード長 2,952 bytes
コンパイル時間 2,100 ms
コンパイル使用メモリ 204,228 KB
実行使用メモリ 6,176 KB
最終ジャッジ日時 2023-09-10 13:21:24
合計ジャッジ時間 3,907 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 71 ms
5,596 KB
testcase_12 AC 58 ms
4,596 KB
testcase_13 AC 51 ms
6,176 KB
testcase_14 AC 50 ms
5,688 KB
testcase_15 AC 69 ms
5,884 KB
testcase_16 AC 60 ms
6,028 KB
testcase_17 AC 66 ms
6,140 KB
testcase_18 AC 63 ms
6,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<typename T, typename U> void cmax(T &a, U b) { if (a < b) a = b; }
template<typename T, typename U> void cmin(T &a, U b) { if (a > b) a = b; }

template <class S, S (*op)(S, S), S (*el)()> class SegmentTree {
private:
  int N, size, log;
  vector<S> 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<S>(n, el())) {}
  SegmentTree(const vector<S>& v) : N(int(v.size())) {
    log = ceil_pow(N);
    size = 1 << log;
    D = vector<S>(2 * size, el());
    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, S x) {
    p += size;
    D.at(p) = x;
    for (int i = 1; i <= log; i++) apply(p >> i);
  }
  S get(int p) { return D.at(p + size); }
  S query(int l, int r) {
    r++;
    S sml = el(), smr = el();
    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);
  }
  S query_all() { return D.at(1); }
  template <bool (*f)(S)> int max_right(int l) {
    return max_right(l, [](S x) { return f(x); });
  }
  template <class F> int max_right(int l, F f) {
    if (l == N) return N;
    l += size;
    S sm = el();
    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)(S)> int min_left(int r) {
    return min_left(r, [](S x) { return f(x); });
  }
  template <class F> int min_left(int r, F f) {
    if (r == 0) return 0;
    r += size;
    S sm = el();
    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;
  }
};

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

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

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

  SegmentTree<S, op, el> ST(A);
  while (Q--) {
    int a, b, c;
    cin >> a >> b >> c, b--, c--;
    if (a == 1) {
      auto [x, y] = ST.get(b);
      auto [x2, y2] = ST.get(c);
      ST.set(b, {x2, y});
      ST.set(c, {x, y2});
    } else {
      cout << ST.query(b, c).second + 1 << "\n";
    }
  }
}
0