結果

問題 No.875 Range Mindex Query
ユーザー qsako6qsako6
提出日時 2019-09-07 16:43:10
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,557 ms / 2,000 ms
コード長 1,269 bytes
コンパイル時間 238 ms
コンパイル使用メモリ 11,268 KB
実行使用メモリ 29,968 KB
最終ジャッジ日時 2023-09-09 04:40:16
合計ジャッジ時間 12,974 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
15,240 KB
testcase_01 AC 88 ms
15,156 KB
testcase_02 AC 92 ms
15,224 KB
testcase_03 AC 85 ms
15,160 KB
testcase_04 AC 87 ms
15,136 KB
testcase_05 AC 87 ms
15,248 KB
testcase_06 AC 87 ms
15,084 KB
testcase_07 AC 90 ms
15,048 KB
testcase_08 AC 88 ms
15,068 KB
testcase_09 AC 86 ms
15,232 KB
testcase_10 AC 90 ms
15,232 KB
testcase_11 AC 1,374 ms
26,908 KB
testcase_12 AC 1,083 ms
22,992 KB
testcase_13 AC 1,059 ms
29,320 KB
testcase_14 AC 1,032 ms
27,396 KB
testcase_15 AC 1,335 ms
29,616 KB
testcase_16 AC 1,455 ms
29,764 KB
testcase_17 AC 1,557 ms
29,920 KB
testcase_18 AC 1,514 ms
29,968 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class SegmentTree
  def initialize(n, inf = (1 << 30) - 1 + (1 << 30), &func)
    @n = n, @l = 1, @inf = inf
    while @l < n
      @l <<= 1
    end
    @data = Array.new(@l * 2, @inf)
    if block_given?
      @func = func
    else
      @func = lambda { |a, b| a > b ? b : a }
    end
  end

  def update(i, x)
    i += @l
    @data[i] = x
    i >>= 1
    while i > 0
      @data[i] = @func.call(@data[i * 2], @data[i * 2 + 1])
      i >>= 1
    end
    true
  end

  # minimum of [l, r)
  def query(l, r)
    _query(l, r, 1, 0, @l)
  end

  def [](k)
    @data[@l + k]
  end

  private

  def _query(l, r, k, sl, sr)
    return @inf if sr <= l || r <= sl
    return @data[k] if l <= sl && sr <= r
    dl = _query(l, r, k * 2, sl, (sl + sr) / 2)
    dr = _query(l, r, k * 2 + 1, (sl + sr) / 2, sr)
    @func.call(dl, dr)
  end
end

h = {}
n, q = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
rmq = SegmentTree.new(n) { |x, y| x > y ? y : x }
n.times do |i|
  h[a[i]] = i
  rmq.update(i, a[i])
end
ans = []
q.times do
  c, l, r = gets.split.map(&:to_i)
  l -= 1
  r -= 1
  if c == 1
    tmpl = rmq[l]
    tmpr = rmq[r]
    rmq.update(l, tmpr)
    rmq.update(r, tmpl)
    h[tmpr] = l
    h[tmpl] = r
  else
    ans << h[rmq.query(l, r + 1)] + 1
  end
end

puts ans
0