結果

問題 No.875 Range Mindex Query
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-09-06 21:27:17
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 4,607 bytes
コンパイル時間 994 ms
コンパイル使用メモリ 124,460 KB
実行使用メモリ 10,228 KB
最終ジャッジ日時 2024-06-22 02:27:31
合計ジャッジ時間 3,579 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 123 ms
9,540 KB
testcase_12 AC 106 ms
7,060 KB
testcase_13 AC 92 ms
10,228 KB
testcase_14 AC 92 ms
9,116 KB
testcase_15 AC 117 ms
9,508 KB
testcase_16 AC 127 ms
9,336 KB
testcase_17 AC 140 ms
9,580 KB
testcase_18 AC 134 ms
9,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


// point update, range product
//   T: monoid
class SegmentTree(T, alias op, T ide) {
  import std.functional : binaryFun;
  alias opFun = binaryFun!op;
  int n;
  T[] ts;
  this(int n_) {
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[] = ide;
  }
  this(int n_, T[] ini) {
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[0 .. n] = ide;
    ts[n .. n + n_] = ini[];
    ts[n + n_ .. n << 1] = ide;
    foreach_reverse (a; 1 .. n) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);
  }

  // 0 <= a < n
  T get(int a) const {
    return ts[a + n];
  }
  void update(int a, in T val) {
    ts[a += n] = val;
    for (; a >>= 1; ) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);
  }
  void mulL(int a, in T val) {
    update(a, opFun(val, ts[a + n]));
  }
  void mulR(int a, in T val) {
    update(a, opFun(ts[a + n], val));
  }

  // prod of [a, b) (0 <= a <= b <= n)
  // T rangeProd(int a, int b) const {
  T rangeProd(int a, int b) {
    T prodL = ide, prodR = ide;
    for (a += n, b += n; a < b; a >>= 1, b >>= 1) {
      if (a & 1) prodL = opFun(prodL, ts[a++]);
      if (b & 1) prodR = opFun(ts[--b], prodR);
    }
    return opFun(prodL, prodR);
  }

/*
  // min b s.t. pred(prod of [a, b)) (or n + 1 if no such b)
  //   0 <= a <= n
  //   assume pred(prod of [a, b)) is non-decreasing in b
  int binarySearchR(int a, bool delegate(T) pred) const {
    if (pred(ide)) return a;
    if (a == n) return n + 1;
    T prod = ide;
    for (a += n; ; a >>= 1) {
      if (a & 1) {
        if (pred(opFun(prod, ts[a]))) {
          for (; a < n; ) {
            a <<= 1;
            if (!pred(opFun(prod, ts[a]))) {
              prod = opFun(prod, ts[a++]);
            }
          }
          return a - n + 1;
        }
        prod = opFun(prod, ts[a++]);
        if (!(a & a - 1)) return n + 1;
      }
    }
  }

  // max a s.t. pred(prod of [a, b)) (or -1 if no such a)
  //   0 <= b <= n
  //   assume pred(prod of [a, b)) is non-increasing in a
  int binarySearchL(int b, bool delegate(T) pred) const {
    if (pred(ide)) return b;
    if (b == 0) return -1;
    T prod = ide;
    for (b += n; ; b >>= 1) {
      if ((b & 1) || b == 2) {
        if (pred(opFun(prod, ts[b - 1]))) {
          for (; b <= n; ) {
            b <<= 1;
            if (!pred(opFun(prod, ts[b - 1]))) {
              prod = opFun(prod, ts[--b]);
            }
          }
          return b - n - 1;
        }
        prod = opFun(prod, ts[--b]);
        if (!(b & b - 1)) return -1;
      }
    }
  }
*/
}


enum INF = 10^^9;

int N, Q;
int[] A;
int[] T, L, R;

void main() {
  try {
    for (; ; ) {
      N = readInt();
      Q = readInt();
      A = new int[N];
      foreach (i; 0 .. N) {
        A[i] = readInt();
      }
      T = new int[Q];
      L = new int[Q];
      R = new int[Q];
      foreach (q; 0 .. Q) {
        T[q] = readInt();
        L[q] = readInt() - 1;
        R[q] = readInt() - 1;
      }
      
      auto ps = new Tuple!(int, int)[N];
      foreach (i; 0 .. N) {
        ps[i] = tuple(A[i], i);
      }
      auto seg = new SegmentTree!(Tuple!(int, int), min, tuple(INF, -1))(N, ps);
      foreach (q; 0 .. Q) {
        if (T[q] == 1) {
          swap(ps[L[q]][0], ps[R[q]][0]);
          seg.update(L[q], ps[L[q]]);
          seg.update(R[q], ps[R[q]]);
        } else {
          const res = seg.rangeProd(L[q], R[q] + 1);
          writeln(res[1] + 1);
        }
      }
      
    }
  } catch (EOFException e) {
  }
}
0