結果

問題 No.875 Range Mindex Query
ユーザー qsako6qsako6
提出日時 2019-09-07 17:03:23
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 1,350 bytes
コンパイル時間 215 ms
コンパイル使用メモリ 11,460 KB
実行使用メモリ 31,940 KB
最終ジャッジ日時 2023-09-09 05:07:46
合計ジャッジ時間 7,798 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
15,028 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 742 ms
31,668 KB
testcase_17 WA -
testcase_18 AC 767 ms
31,896 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class SegmentTree
  def initialize(n, unity, &func)
    @size_r = 1
    while @size_r < n
      @size_r <<= 2
    end
    @func = func
    @unity = unity
    @dat = Array.new(@size_r * 2, @unity)
  end

  # set, a is 0-indexed
  def set(a, v)
    @dat[a + @size_r] = v
  end

  def build
    (@size_r - 1).downto(1) do |k|
      @dat[k] = @func.call(@dat[k * 2], @dat[k * 2 + 1])
    end
  end

  # get [a, b), a and b are 0-indexed
  def get(a, b)
    vleft = vright = @unity
    left = a + @size_r
    right = b + @size_r
    while (left < right)
      if (left & 1) == 1
        vleft = @func.call(vleft, @dat[left])
        left += 1
      end
      if (right & 1) == 1
        right -= 1
        vright = @func.call(@dat[right], vright)
      end
      left >>= 1
      right >>= 1
    end
    return @func.call(vleft, vright)
  end

  def [](k)
    @dat[@size_r + k]
  end
end

N, q = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
h = {}
seg = SegmentTree.new(N, 1 << 31) { |x, y| x > y ? y : x }

N.times do |i|
  h[a[i]] = i
  seg.set(i, a[i])
end
seg.build
ans = []
q.times do
  c, l, r = gets.split.map(&:to_i)
  l -= 1
  r -= 1
  if c == 1
    tmpl = seg.get(l, l + 1)
    tmpr = seg.get(r, r + 1)
    seg.set(l, tmpr)
    seg.set(r, tmpl)
    h[tmpr] = l
    h[tmpl] = r
  else
    ans << h[seg.get(l, r + 1)] + 1
  end
end

puts ans
0