結果

問題 No.875 Range Mindex Query
ユーザー ninoinuininoinui
提出日時 2020-09-24 07:53:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 302 ms / 2,000 ms
コード長 2,957 bytes
コンパイル時間 2,151 ms
コンパイル使用メモリ 208,656 KB
実行使用メモリ 9,200 KB
最終ジャッジ日時 2023-09-10 13:21:08
合計ジャッジ時間 5,392 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 262 ms
8,024 KB
testcase_12 AC 203 ms
6,388 KB
testcase_13 AC 202 ms
8,764 KB
testcase_14 AC 197 ms
8,508 KB
testcase_15 AC 267 ms
8,772 KB
testcase_16 AC 280 ms
8,984 KB
testcase_17 AC 302 ms
9,076 KB
testcase_18 AC 294 ms
9,200 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;
  }
};

int op(int a, int b) { return min(a, b); };
int el() { return INT_MAX; };

signed main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> A(N);
  map<int, int> MA;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    MA[A.at(i)] = i;
  }

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