結果

問題 No.875 Range Mindex Query
ユーザー _____TAB__________TAB_____
提出日時 2020-04-18 20:56:34
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 283 ms / 2,000 ms
コード長 1,440 bytes
コンパイル時間 1,505 ms
コンパイル使用メモリ 93,972 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 21:13:12
合計ジャッジ時間 4,957 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 3 ms
6,816 KB
testcase_02 AC 4 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 3 ms
6,944 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 4 ms
6,944 KB
testcase_11 AC 212 ms
6,940 KB
testcase_12 AC 179 ms
6,940 KB
testcase_13 AC 152 ms
6,940 KB
testcase_14 AC 146 ms
6,944 KB
testcase_15 AC 201 ms
6,944 KB
testcase_16 AC 254 ms
6,940 KB
testcase_17 AC 283 ms
6,944 KB
testcase_18 AC 270 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <functional>
using namespace std;


template <typename T>
struct SegmentTree{
private:
  using F = function<T(T,T)>;
  int n;
  F f;
  T ti;
  vector<T> dat;
public:
  SegmentTree(){};
  SegmentTree(F f,T ti) : f(f),ti(ti) {}
  void build(int n_){
    n = n_;
    dat.assign(2*n,ti);
  }
  void build(const vector<T> &v){
    int n_ = v.size();
    build(n_);
    for(int i = 0; i < n; ++i) dat[n+i]=v[i];
    for(int i = n-1; i >= 0; --i)
      dat[i]=f(dat[2*i+0],dat[2*i+1]);
  }
  void set_val(int k,T x){
    dat[k+=n] = x;
    while(k > 0){
      k = k/2;
      dat[k]=f(dat[2*k+0],dat[2*k+1]);
    }
  }
  T query(int a,int b){
    T vl = ti, vr = ti;
    for(int l = a+n, r = b+n; l < r; l >>= 1, r >>= 1){
      if(l&1) vl = f(vl,dat[l++]);
      if(r&1) vr = f(dat[--r],vr);
    }
    return f(vl,vr);
  }
};

int main(){
  int N, Q;
  cin >> N >> Q;
  vector<pair<int,int>> A(N);
  for(int i = 0; i < N; ++i) cin >> A[i].first, A[i].second = i+1;
  int INF = 1e9;
  using T = pair<int,int>;
  T ti(INF,-1);
  function<T(T,T)> f = [](T a, T b){return min(a,b);};
  
  SegmentTree<T> st(f,ti);
  st.build(A);
  while(Q--){
    int t, l, r;
    cin >> t >> l >> r;
    if(t == 1){
      T vl = st.query(l-1,l), vr = st.query(r-1,r);
      swap(vl.first,vr.first);
      st.set_val(l-1,vl);
      st.set_val(r-1,vr);
    }else{
      cout << st.query(l-1,r).second << endl;
    }
  }
}
0