結果

問題 No.875 Range Mindex Query
ユーザー beetbeet
提出日時 2021-02-05 14:48:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 2,123 bytes
コンパイル時間 2,231 ms
コンパイル使用メモリ 205,620 KB
実行使用メモリ 4,456 KB
最終ジャッジ日時 2023-09-14 23:12:30
合計ジャッジ時間 4,762 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 69 ms
4,380 KB
testcase_12 AC 55 ms
4,380 KB
testcase_13 AC 48 ms
4,380 KB
testcase_14 AC 48 ms
4,456 KB
testcase_15 AC 65 ms
4,376 KB
testcase_16 AC 86 ms
4,384 KB
testcase_17 AC 94 ms
4,380 KB
testcase_18 AC 93 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// verification-helper: PROBLEM https://yukicoder.me/problems/3277

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

#define call_from_test
template<typename T=int>
vector<T> read(size_t n){
  vector<T> ts(n);
  for(size_t i=0;i<n;i++) cin>>ts[i];
  return ts;
}

template <typename T>
struct SegmentTree{
  using F = function<T(T,T)>;
  int n;
  F f;
  T ti;
  vector<T> dat;

  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){
    if(a>=b) return ti;
    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 and !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);
  }
};

#undef call_from_test

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  const char newl = '\n';

  int n,q;
  cin>>n>>q;
  auto as=read(n);

  auto f=[](int a,int b){return min(a,b);};
  SegmentTree<int> seg(f,n+1);
  seg.build(as);

  for(int i=0;i<q;i++){
    int t,l,r;
    cin>>t>>l>>r;
    if(t==1){
      l--;r--;
      swap(as[l],as[r]);
      seg.set_val(l,as[l]);
      seg.set_val(r,as[r]);
    }
    if(t==2){
      l--;
      int m=seg.query(l,r);
      auto check=[&](int x){return x<=m;};
      cout<<seg.find(l,check)+1<<newl;
    }
  }
  return 0;
}
0