結果

問題 No.875 Range Mindex Query
ユーザー qsako6qsako6
提出日時 2019-09-06 23:15:59
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,407 ms / 2,000 ms
コード長 1,155 bytes
コンパイル時間 364 ms
コンパイル使用メモリ 11,508 KB
実行使用メモリ 30,088 KB
最終ジャッジ日時 2023-09-07 02:30:08
合計ジャッジ時間 12,663 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
15,312 KB
testcase_01 AC 85 ms
15,112 KB
testcase_02 AC 85 ms
15,064 KB
testcase_03 AC 81 ms
15,152 KB
testcase_04 AC 83 ms
15,196 KB
testcase_05 AC 83 ms
15,112 KB
testcase_06 AC 84 ms
15,060 KB
testcase_07 AC 85 ms
15,292 KB
testcase_08 AC 83 ms
15,100 KB
testcase_09 AC 81 ms
15,280 KB
testcase_10 AC 90 ms
15,124 KB
testcase_11 AC 1,211 ms
26,800 KB
testcase_12 AC 964 ms
23,024 KB
testcase_13 AC 930 ms
29,376 KB
testcase_14 AC 911 ms
27,200 KB
testcase_15 AC 1,176 ms
29,572 KB
testcase_16 AC 1,326 ms
29,884 KB
testcase_17 AC 1,407 ms
30,088 KB
testcase_18 AC 1,358 ms
29,828 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class RMQ
  def initialize(n, inf = (1 << 30) - 1 + (1 << 30))
    @n = n, @l = 1, @inf = inf
    while @l < n
      @l <<= 1
    end
    @data = Array.new(@l * 2, @inf)
  end

  def update(i, x)
    i += @l
    @data[i] = x
    i >>= 1
    while i > 0
      @data[i] = min(@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)
    min(dl, dr)
  end

  def min(a, b)
    return (a > b ? b : a)
  end
end

h = {}
n, q = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
rmq = RMQ.new(n)
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