結果

問題 No.875 Range Mindex Query
ユーザー le_panda_noirle_panda_noir
提出日時 2020-03-27 21:51:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 277 ms / 2,000 ms
コード長 1,786 bytes
コンパイル時間 891 ms
コンパイル使用メモリ 77,324 KB
実行使用メモリ 4,720 KB
最終ジャッジ日時 2023-08-30 16:53:32
合計ジャッジ時間 3,792 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 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,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 189 ms
4,544 KB
testcase_12 AC 155 ms
4,380 KB
testcase_13 AC 136 ms
4,628 KB
testcase_14 AC 133 ms
4,720 KB
testcase_15 AC 177 ms
4,664 KB
testcase_16 AC 254 ms
4,544 KB
testcase_17 AC 277 ms
4,660 KB
testcase_18 AC 263 ms
4,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
const int INF = 1e9+7;

template<class T> struct SegTree {
  private:
    int size;
  public:
    vector<T> v, arr;
    SegTree(int n) {
      size = 1;
      while (size < n) size *= 2;
      v.resize(2 * size - 1, size);
      arr.resize(size+1, INF);
      rep(i, n) v[i + size - 1] = i;
      // 最小インデックスを保持
    }
    void update(int index, T val) {
      arr[index] = val;
      v[index + size - 1] = index;
      index += size - 1;
      while (index > 0) {
        index = (index - 1) / 2;
        if (arr[v[2 * index + 1]] < arr[v[2 * index + 2]])
          v[index] = v[2 * index + 1];
        else
          v[index] = v[2 * index + 2];
      }
    }
    T query(int l, int r) { return query(l, r, 0, size, 0); }
    T query(int l, int r, int cur_l, int cur_r, int index) {
      // [l, r)に対するクエリ
      if (cur_r <= l || r <= cur_l) return size;
      if (l <= cur_l && cur_r <= r) return v[index];

      int mid = (cur_l + cur_r) / 2;
      int l_res = query(l, r, cur_l, mid, 2 * index + 1),
          r_res = query(l, r, mid, cur_r, 2 * index + 2);
      if (arr[l_res] < arr[r_res]) return l_res;
      else return r_res;
    }
    T operator[](const int n) { return arr[n]; }
};


int main() {
  int N; cin >> N;
  int Q; cin >> Q;

  SegTree<int> T(N);

  rep(i, N) {
    int a; cin >> a;
    T.update(i, a);
  }

  rep(i, Q) {
    int q; cin >> q;
    if (q == 1) {
      int l, r; cin >> l >> r;
      --l, --r;
      int a_l = T[l],
          a_r = T[r];
      T.update(l, a_r);
      T.update(r, a_l);
    } else {
      int l, r; cin >> l >> r;
      --l, --r;
      cout << T.query(l, r+1) + 1 << endl;
    }
  }

  return 0;
}
0