結果

問題 No.875 Range Mindex Query
ユーザー kkktymkkktym
提出日時 2019-09-06 23:20:04
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 228 ms / 2,000 ms
コード長 1,528 bytes
コンパイル時間 1,126 ms
コンパイル使用メモリ 86,804 KB
実行使用メモリ 10,156 KB
最終ジャッジ日時 2023-09-07 02:35:53
合計ジャッジ時間 4,072 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 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 201 ms
8,992 KB
testcase_12 AC 159 ms
7,464 KB
testcase_13 AC 168 ms
9,404 KB
testcase_14 AC 166 ms
9,128 KB
testcase_15 AC 228 ms
10,156 KB
testcase_16 AC 187 ms
9,964 KB
testcase_17 AC 196 ms
10,152 KB
testcase_18 AC 204 ms
10,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
using namespace std;
const int inf = 1e+9;
template <typename X>
struct RMQ{
  vector<X> dat;
  int n;
  X init;
  
  RMQ(int _n,X _init)
  {
    n = 1;
    init = _init;
    while(n < _n) n *= 2;
    dat.resize(2*n-1);
    rep(i,2*n-1){
      dat[i] = init;
    }
  }
  
  void update(int k,X a)
  {
    k += n - 1;
    dat[k] = a;
    while(k > 0){
      k = (k - 1)/2;
      dat[k] = min(dat[2*k+1],dat[2*k+2]);
    }
  }
  
  X query(int a,int b,int k,int l,int r)
  {
    if(r<=a||b<=l) return init;

    if(a<=l&&r<=b) return dat[k];
    else{
      X vl = query(a,b,2*k+1,l,(l+r)/2);
      X vr = query(a,b,2*k+2,(l+r)/2,r);
      return min(vl,vr);
    }
  }
};
int n,q;
vector<int> a;
vector<int> com,l,r;
map<int,int> mp;
int main()
{
  cin >> n >> q;
  a.resize(n);
  rep(i,n){
    cin >> a[i];
    mp[a[i]] = i+1;
  }
  com.resize(q);
  l.resize(q);
  r.resize(q);
  rep(i,q){
    cin >> com[i] >> l[i] >> r[i];
    l[i]--;
    r[i]--;
  }

  
  RMQ<int> rmq(n,inf);
  rep(i,n) rmq.update(i,a[i]);
  
  rep(i,q){
    if(com[i] == 1){
      int left = rmq.dat[l[i]+rmq.n-1],right = rmq.dat[r[i]+rmq.n-1];
      rmq.update(l[i],right);
      rmq.update(r[i],left);
      int temp = mp[left];
      mp[left] = mp[right];
      mp[right] = temp;
    }
    else{
      cout << mp[rmq.query(l[i],r[i]+1,0,0,rmq.n)] << "\n";
    }
  }
  
  return 0;
}
0