結果

問題 No.875 Range Mindex Query
ユーザー SSRSSSRS
提出日時 2020-11-11 23:48:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 270 ms / 2,000 ms
コード長 1,512 bytes
コンパイル時間 2,180 ms
コンパイル使用メモリ 169,504 KB
実行使用メモリ 5,240 KB
最終ジャッジ日時 2023-09-30 00:58:06
合計ジャッジ時間 5,480 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 3 ms
4,384 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,376 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 190 ms
5,116 KB
testcase_12 AC 154 ms
4,380 KB
testcase_13 AC 134 ms
5,184 KB
testcase_14 AC 130 ms
5,144 KB
testcase_15 AC 178 ms
5,096 KB
testcase_16 AC 247 ms
5,236 KB
testcase_17 AC 270 ms
5,176 KB
testcase_18 AC 261 ms
5,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 10000000;
struct segment_tree{
  int N;
  vector<int> ST;
  segment_tree(vector<int> a){
    int n = a.size();
    N = 1;
    while (N < n){
      N *= 2;
    }
    ST = vector<int>(N * 2 - 1, INF);
    for (int i = 0; i < n; i++){
      ST[N - 1 + i] = a[i];
    }
    for (int i = N - 2; i >= 0; i--){
      ST[i] = min(ST[i * 2 + 1], ST[i * 2 + 2]);
    }
  }
  void update(int i, int x){
    i += N - 1;
    ST[i] = x;
    while (i > 0){
      i = (i - 1) / 2;
      ST[i] = min(ST[i * 2 + 1], ST[i * 2 + 2]);
    }
  }
  int query(int L, int R, int i, int l, int r){
    if (r <= L || R <= l){
      return INF;
    } else if (L <= l && r <= R){
      return ST[i];
    } else {
      int m = (l + r) / 2;
      return min(query(L, R, i * 2 + 1, l, m), query(L, R, i * 2 + 2, m, r));
    }
  }
  int query(int L, int R){
    return query(L, R, 0, 0, N);
  }
};
int main(){
  int N, Q;
  cin >> N >> Q;
  vector<int> a(N);
  for (int i = 0; i < N; i++){
    cin >> a[i];
    a[i]--;
  }
  vector<int> b(N);
  for (int i = 0; i < N; i++){
    b[a[i]] = i;
  }
  segment_tree ST(a);
  for (int i = 0; i < Q; i++){
    int t;
    cin >> t;
    if (t == 1){
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      swap(b[a[l]], b[a[r]]);
      swap(a[l], a[r]);
      ST.update(l, a[l]);
      ST.update(r, a[r]);
    }
    if (t == 2){
      int l, r;
      cin >> l >> r;
      l--;
      cout << b[ST.query(l, r)] + 1 << endl;
    }
  }
}
0