結果

問題 No.875 Range Mindex Query
ユーザー tnakao0123tnakao0123
提出日時 2019-09-07 00:57:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 2,126 bytes
コンパイル時間 831 ms
コンパイル使用メモリ 91,356 KB
実行使用メモリ 5,780 KB
最終ジャッジ日時 2023-09-07 05:13:25
合計ジャッジ時間 2,629 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,572 KB
testcase_01 AC 4 ms
5,616 KB
testcase_02 AC 4 ms
5,572 KB
testcase_03 AC 3 ms
5,524 KB
testcase_04 AC 3 ms
5,508 KB
testcase_05 AC 3 ms
5,572 KB
testcase_06 AC 4 ms
5,616 KB
testcase_07 AC 4 ms
5,572 KB
testcase_08 AC 4 ms
5,780 KB
testcase_09 AC 4 ms
5,568 KB
testcase_10 AC 4 ms
5,576 KB
testcase_11 AC 80 ms
5,756 KB
testcase_12 AC 66 ms
5,640 KB
testcase_13 AC 58 ms
5,640 KB
testcase_14 AC 57 ms
5,572 KB
testcase_15 AC 78 ms
5,572 KB
testcase_16 AC 92 ms
5,692 KB
testcase_17 AC 100 ms
5,560 KB
testcase_18 AC 95 ms
5,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 875.cc:  No.875 Range Mindex Query - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_E2 = 1 << 18; // = 262144
const int INF = 1 << 30;

/* typedef */

typedef pair<int,int> pii;

template <typename T, const int MAX_E2>
struct SegTreeMin {
  int n, e2;
  T nodes[MAX_E2], def;
  SegTreeMin() {}

  void init(int _n, T _def) {
    n = _n; def = _def;
    for (e2 = 1; e2 < n; e2 <<= 1);
    fill(nodes, nodes + MAX_E2, def);
  }

  T &get(int i) { return nodes[e2 - 1 + i]; }

  void set(int i, T v) {
    int j = e2 - 1 + i;
    nodes[j] = v;
    while (j > 0) {
      j = (j - 1) / 2;
      nodes[j] = min(nodes[j * 2 + 1], nodes[j * 2 + 2]);
    }
  }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = min(nodes[j * 2 + 1], nodes[j * 2 + 2]);
  }

  T min_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return def;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    int im = (i0 + i1) / 2;
    T v0 = min_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = min_range(r0, r1, k * 2 + 2, im, i1);
    return min(v0, v1);
  }
  T min_range(int r0, int r1) { return min_range(r0, r1, 0, 0, e2); }
};

/* global variables */

SegTreeMin<pii,MAX_E2> st;

/* subroutines */

/* main */

int main() {
  int n, qn;
  scanf("%d%d", &n, &qn);

  st.init(n, pii(INF, INF));

  for (int i = 0; i < n; i++) {
    int ai;
    scanf("%d", &ai);
    st.get(i) = pii(ai, i);
  }
  st.setall();

  while (qn--) {
    int op, l, r;
    scanf("%d%d%d", &op, &l, &r);
    l--;

    if (op == 1) {
      r--;
      pii al = st.get(l), ar = st.get(r);
      swap(al.first, ar.first);
      st.set(l, al), st.set(r, ar);
    }
    else {
      printf("%d\n", st.min_range(l, r).second + 1);
    }
  }
  return 0;
}
0