結果

問題 No.875 Range Mindex Query
ユーザー tjaketjake
提出日時 2019-09-07 08:25:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 259 ms / 2,000 ms
コード長 2,933 bytes
コンパイル時間 979 ms
コンパイル使用メモリ 82,988 KB
実行使用メモリ 6,332 KB
最終ジャッジ日時 2023-09-08 09:06:57
合計ジャッジ時間 3,770 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
5,396 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 4 ms
5,512 KB
testcase_11 AC 185 ms
6,332 KB
testcase_12 AC 155 ms
4,620 KB
testcase_13 AC 130 ms
5,804 KB
testcase_14 AC 128 ms
5,680 KB
testcase_15 AC 179 ms
5,776 KB
testcase_16 AC 239 ms
5,752 KB
testcase_17 AC 259 ms
6,324 KB
testcase_18 AC 248 ms
5,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<functional>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cassert>
#include<ctime>
using namespace std;

#define mind(a,b) (a>b?b:a)
#define maxd(a,b) (a>b?a:b)
#define absd(x) (x<0?-(x):x)
#define pow2(x) ((x)*(x))
#define rep(i,n) for(int i=0; i<n; ++i)
#define repr(i,n) for(int i=n-1; i>=0; --i)
#define repl(i,s,n) for(int i=s; i<=n; ++i)
#define replr(i,s,n) for(int i=n; i>=s; --i)
#define repf(i,s,n,j) for(int i=s; i<=n; i+=j)
#define repe(e,obj) for(auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X,Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i,n,val) for(int i=0; i<n; ++i) cout << val << " "; cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 1000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define P_TYPE int
typedef pair<P_TYPE, P_TYPE> P;
typedef pair<P, P_TYPE> PI;
typedef pair<P_TYPE, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P> > pvqueue;

#define N 100005

class SegmentTree {
  const static ll inf = (1LL << 31) - 1;
  int n, n0;
  int data[4*N], idx[4*N];

  void build(int k) {
    if(data[2*k+1] < data[2*k+2]) {
      data[k] = data[2*k+1];
      idx[k] = idx[2*k+1];
    } else {
      data[k] = data[2*k+2];
      idx[k] = idx[2*k+2];
    }
  }

public:
  SegmentTree(int n, int *a) {
    n0 = 1;
    while(n0 < n) n0 <<= 1;
    for(int i=0; i<n; ++i) idx[i+n0-1] = i, data[i+n0-1] = a[i];
    for(int i=n; i<n0; ++i) idx[i+n0-1] = i, data[i+n0-1] = inf;
    for(int i=n0-2; i>=0; --i) build(i);
  }

  void update(int k, int x) {
    k += n0-1;
    data[k] = x;
    while(k > 0) {
      k = (k - 1) / 2;
      build(k);
    }
  }

  void swap(int a, int b) {
    int x = data[n0-1+a], y = data[n0-1+b];
    update(a, y); update(b, x);
  }

  int query(int l, int r) {
    int l0 = l + n0, r0 = r + n0;
    ll s = inf; int rr = -1;
    while(l0 < r0) {
      if(r0 & 1) {
        --r0;
        if(data[r0-1] < s) {
          s = data[r0-1];
          rr = idx[r0-1];
        }
      }
      if(l0 & 1) {
        if(data[l0-1] < s) {
          s = data[l0-1];
          rr = idx[l0-1];
        }
        ++l0;
      }
      l0 >>= 1; r0 >>= 1;
    }
    return rr;
  }
};

int a[N];

int main() {
  int n, q;
  cin >> n >> q;
  rep(i, n) cin >> a[i];

  SegmentTree st(n, a);
  while(q--) {
    int t, l, r;
    cin >> t >> l >> r;
    switch(t) {
      case 1:
        st.swap(l-1, r-1);
        break;
      case 2:
        cout << st.query(l-1, r)+1 << endl;
        break;
    }
  }

  return 0;
}
0