結果

問題 No.875 Range Mindex Query
ユーザー le_panda_noirle_panda_noir
提出日時 2020-03-27 21:42:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,138 bytes
コンパイル時間 1,112 ms
コンパイル使用メモリ 116,836 KB
実行使用メモリ 4,752 KB
最終ジャッジ日時 2023-08-30 16:52:34
合計ジャッジ時間 4,250 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <iomanip>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
template<class T>using vv = vector<vector<T>>;

// #define rep(i,n) for(int i=0;i<(n);++i)
#define all(f,c,...) (([&](decltype((c)) cccc) { return (f)(begin(cccc), end(cccc), ## __VA_ARGS__); })(c))
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)

#define rrep(i,n) for(int i=(n);i>=0;--i)
const int dx[4]={1,0,-1,0}, dy[4]={0,1,0,-1};
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}

// debug
template<class T>ostream& operator<<(ostream& os,const vector<T>& vec){os<<"{";for(int i=0;i<vec.size();++i)os<<(i?", ":"")<<vec[i];os<<"}";return os;}
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>& rhs){os<<"("<<rhs.first<<", "<<rhs.second<<")";return os;}

#ifdef LOCAL
void debug() {cerr << "\n";}
template<class First> void debug(const First& first) {cerr<<first<<"\n";}
template<class First, class... Rest> void debug(const First& first, const Rest&... rest) {cerr<<first<<",";debug(rest...);}
#else
#define debug(...) 42
#endif

const int INF = 1e9+7;

#include <functional>
template<class T> struct SegTree {
  private:
    int size;
    vector<T> v, arr;
  public:
    SegTree(int n) {
      size = 1;
      while (size < n) size *= 2;
      v.resize(2 * size - 1, size);
      arr.resize(size+1, INF);
      rep(i, n) {
        v[i + size - 1] = i;
      }
      // 最小インデックスを保持
    }
    void update(int index, T val) {
      arr[index] = val;
      v[index + size - 1] = index;
      index += size - 1;
      while (index > 0) {
        index = (index - 1) / 2;
        if (arr[v[2 * index + 1]] < arr[v[2 * index + 2]])
          v[index] = v[2 * index + 1];
        else
          v[index] = v[2 * index + 2];
      }
    }
    T query(int l, int r) { return query(l, r, 0, size, 0); }
    T query(int l, int r, int cur_l, int cur_r, int index) {
      // [l, r)に対するクエリ
      if (cur_r <= l || r <= cur_l) return size;
      if (l <= cur_l && cur_r <= r) return v[index];

      int mid = (cur_l + cur_r) / 2;
      int l_res = query(l, r, cur_l, mid, 2 * index + 1);
      int r_res = query(l, r, mid, cur_r, 2 * index + 2);
      if (arr[l_res] < arr[r_res])
        return l_res;
      else
        return r_res;
    }
};


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

  SegTree<int> T(N);

  rep(i, N) {
    int a; cin >> a;
    T.update(i, a);
  }

  rep(i, Q) {
    int q; cin >> q;
    if (q == 1) {
      int l, r; cin >> l >> r;
      --l, --r;
      int a_l = T.query(l, l+1),
          a_r = T.query(r, r+1);
      T.update(l, a_r);
      T.update(r, a_l);
    } else {
      int l, r; cin >> l >> r;
      --l, --r;
      cout << T.query(l, r+1) + 1 << endl;
    }
  }

  return 0;
}
0