結果

問題 No.875 Range Mindex Query
ユーザー syawachasyawacha
提出日時 2019-09-06 21:51:25
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 280 ms / 2,000 ms
コード長 2,191 bytes
コンパイル時間 1,097 ms
コンパイル使用メモリ 105,104 KB
実行使用メモリ 10,424 KB
最終ジャッジ日時 2023-09-06 23:38:50
合計ジャッジ時間 4,031 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 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,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 229 ms
9,896 KB
testcase_12 AC 192 ms
6,688 KB
testcase_13 AC 164 ms
10,364 KB
testcase_14 AC 162 ms
9,852 KB
testcase_15 AC 220 ms
10,232 KB
testcase_16 AC 262 ms
10,424 KB
testcase_17 AC 280 ms
10,224 KB
testcase_18 AC 270 ms
10,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
using namespace std;

typedef pair<ll, int> P;
#define fs first
#define sc second

P min(P a, P b){
  if(a.fs < b.fs){
    return a;
  } else {
    return b;
  }
}

class SegmentTree{
public:
  int N;
  vector<P> node;
  P INF = P(1LL << 40, 100000000);

  //コンストラクタ
  SegmentTree(vector<P> v){
    int sz = v.size();
    N = 1;
    while(N < sz) N *= 2;
    node.resize(2 * N - 1, INF);

    for(int i = 0; i < sz; i++){
      int k = i + N - 1;
      node[k] = v[i];
      while(k > 0){
        k = (k - 1) / 2;
        node[k] = min(node[k * 2 + 1], node[k * 2 + 2]);
      }
    }
  }

  //k番目の値(0_indexed)をaに変更
  void update(int k, P a){
    //葉の節点
    k += N - 1;
    node[k] = a;
    //登りながら更新
    while(k > 0){
      k = (k - 1) / 2;
      node[k] = min(node[k * 2 + 1], node[k * 2 + 2]);
    }
  }

  //[a,b)の最小値
  P query(int a, int b){
    return Query(a, b, 0, 0, N);
  }

  P Query(int a, int b, int k, int l, int r){
    if(r <= a || b <= l) return INF;
    if(a <= l && r <= b) return node[k];
    else{
      P v1 = Query(a, b, k * 2 + 1, l, (l + r) / 2);
      P v2 = Query(a, b, k * 2 + 2, (l + r) / 2, r);
      return min(v1, v2);
    }
  }

  void print(){
    for(int i = 0; i < 2 * N - 1; i++){
      //cout << "node[" << i << "] " << node[i] << endl;
    }
  }
};

int main(){
  int N, Q;
  cin >> N >> Q;

  vector<P> a(N);
  for(int i = 0; i < N; i++){
    ll tmp;
    cin >> tmp;
    a[i] = P(tmp, i);
  }

  SegmentTree sg = SegmentTree(a);

  vector<int> ans;
  for(int i = 0; i < Q; i++){
    int q, l, r;
    cin >> q >> l >> r;
    l--;
    r--;

    if(q == 1){
      P L = sg.query(l, l + 1);
      P R = sg.query(r, r + 1);
      sg.update(l, P(R.fs, L.sc));
      sg.update(r, P(L.fs, R.sc));
    } else {
      //cout << (sg.query(l, r + 1)).sc << endl;
      ans.push_back((sg.query(l, r + 1)).sc + 1);
    }
  }

  for(int i : ans){
    cout << i << endl;
  }
  return 0;
}
0