結果

問題 No.875 Range Mindex Query
ユーザー shotshot
提出日時 2019-09-06 21:46:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 207 ms / 2,000 ms
コード長 2,115 bytes
コンパイル時間 2,726 ms
コンパイル使用メモリ 153,700 KB
実行使用メモリ 9,320 KB
最終ジャッジ日時 2023-09-06 23:25:59
合計ジャッジ時間 4,692 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 165 ms
9,236 KB
testcase_12 AC 133 ms
6,136 KB
testcase_13 AC 108 ms
9,176 KB
testcase_14 AC 109 ms
9,256 KB
testcase_15 AC 151 ms
9,176 KB
testcase_16 AC 197 ms
9,236 KB
testcase_17 AC 207 ms
9,252 KB
testcase_18 AC 199 ms
9,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define int long long

template <typename T>
struct SegmentTree{
  using F = function<T(T,T)>;
  int n;
  F f;
  T ti;
  vector<T> dat;
  SegmentTree(){};
  SegmentTree(F f,T ti):f(f),ti(ti){}
  void init(int n_){
    n=1;
    while(n<n_) n<<=1;
    dat.assign(n<<1,ti);
  }
  void build(const vector<T> &v){
    int n_=v.size();
    init(n_);
    for(int i=0;i<n_;i++) dat[n+i]=v[i];
    for(int i=n-1;i;i--)
      dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
  }
  void set_val(int k,T x){
    dat[k+=n]=x;
    while(k>>=1)
      dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|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);
  }
  template<typename C>
  int find(int st,C &check,T &acc,int k,int l,int r){
    if(l+1==r){
      acc=f(acc,dat[k]);
      return check(acc)?k-n:-1;
    }
    int m=(l+r)>>1;
    if(m<=st) return find(st,check,acc,(k<<1)|1,m,r);
    if(st<=l&&!check(f(acc,dat[k]))){
      acc=f(acc,dat[k]);
      return -1;
    }
    int vl=find(st,check,acc,(k<<1)|0,l,m);
    if(~vl) return vl;
    return find(st,check,acc,(k<<1)|1,m,r);
  }
  template<typename C>
  int find(int st,C &check){
    T acc=ti;
    return find(st,check,acc,1,0,n);
  }
};

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int N, Q;
  cin >> N >> Q;

  using P = pair<int, int>;
  int INF = 1e9;  
  auto f=[](P a, P b){return min(a,b);};
  SegmentTree<P> rmq(f, P(INF, INF));  
  rmq.init(N);

  vector<P> v;  
  for ( int i = 0; i < N; i++ ) {
    int a;
    cin >> a;
    v.emplace_back(P(a, i+1));    
  }

  rmq.build(v);

  for ( int i = 0; i < Q; i++ ) {
    int q, l, r;
    cin >> q >> l >> r;
    l--; r--;    
    if ( q == 1 ) {
      P a = rmq.query(l, l+1);    
      P b = rmq.query(r, r+1);      
      rmq.set_val(l, P(b.first, a.second));
      rmq.set_val(r, P(a.first, b.second));
    } else {
      cout << rmq.query(l, r+1).second << endl;      
    }
  }
  
  return 0;
}
0