結果

問題 No.875 Range Mindex Query
ユーザー snow39snow39
提出日時 2019-09-06 21:40:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 289 ms / 2,000 ms
コード長 2,193 bytes
コンパイル時間 1,119 ms
コンパイル使用メモリ 106,344 KB
実行使用メモリ 7,392 KB
最終ジャッジ日時 2023-09-06 23:13:46
合計ジャッジ時間 4,200 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 3 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,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,380 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 214 ms
6,680 KB
testcase_12 AC 174 ms
5,344 KB
testcase_13 AC 148 ms
7,064 KB
testcase_14 AC 144 ms
7,116 KB
testcase_15 AC 200 ms
7,020 KB
testcase_16 AC 266 ms
7,196 KB
testcase_17 AC 289 ms
7,088 KB
testcase_18 AC 284 ms
7,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;

#include <functional>
#include <climits>

//SegmentTree<int> seg(N,[](int a,int b){return min(a,b);},INT_MAX);
template< typename T>
class SegmentTree{
public:

  using F = function<T(T,T)>;

  int n;
  vector<T> tree;
  const F operation;
  const T def;

  SegmentTree(int size,F _operation,T _def):operation(_operation),def(_def){
    n=1;
    while(n<size) n*=2;
    tree.resize(2*n-1,def);
  }

  void initialize(vector<T> v){
    int vSize=v.size();
    n=1;
    while(n<vSize) n*=2;
    tree.resize(2*n-1,def);

    for(int i=0;i<vSize;i++) tree[i+n-1]=v[i];
    for(int i=n-2;i>=0;i--) tree[i]=operation(tree[2*i+1],tree[2*i+2]);
  }

  void update(int index,T value){
    index+=n-1;

    tree[index]=value;
    while(index>0){
      index=(index-1)/2;
      tree[index]=operation(tree[2*index+1],tree[2*index+2]);
    }
  }

  T query(int a,int b,int k=0,int l=0,int r=-1){//[a,b),getMin(a,b,0,0,-1)
    if(r<0) r=n;

    if(r<=a||b<=l) return def;
    else if(a<=l&&r<=b) return tree[k];
    else{
      T lval=query(a,b,2*k+1,l,(l+r)/2);
      T rval=query(a,b,2*k+2,(l+r)/2,r);
      return operation(lval,rval);
    }
  }

  T getVal(int index){
    return tree[index+n-1];
  }
};

int N,Q;
vector<int> A;

int main(){
  cin>>N>>Q;
  A.resize(N);
  for(int i=0;i<N;i++){
    cin>>A[i];
    A[i]--;
  }
  vector<pair<int,int>> v;
  for(int i=0;i<N;i++) v.push_back(mkp(A[i],i));

  SegmentTree<pair<int,int>> seg(N,[](pair<int,int> a,pair<int,int> b){return min(a,b);},mkp(N,N));
  seg.initialize(v);
  for(int i=0;i<Q;i++){
    int t,l,r;
    cin>>t>>l>>r;

    l--;r--;
    if(t==1){
      auto a=seg.getVal(l),b=seg.getVal(r);
      auto nl=mkp(b.first,a.second);
      auto nr=mkp(a.first,b.second);
      seg.update(l,nl);
      seg.update(r,nr);
    }else{
      auto f=seg.query(l,r+1);
      cout<<f.second+1<<endl;
    }
  }

  return 0;
}
0