結果

問題 No.875 Range Mindex Query
ユーザー qsako6qsako6
提出日時 2019-09-07 18:28:05
言語 Ruby
(3.3.0)
結果
AC  
実行時間 904 ms / 2,000 ms
コード長 2,220 bytes
コンパイル時間 190 ms
コンパイル使用メモリ 11,476 KB
実行使用メモリ 31,896 KB
最終ジャッジ日時 2023-09-09 07:22:35
合計ジャッジ時間 8,489 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
15,256 KB
testcase_01 AC 81 ms
15,072 KB
testcase_02 AC 87 ms
15,196 KB
testcase_03 AC 78 ms
15,104 KB
testcase_04 AC 80 ms
15,316 KB
testcase_05 AC 79 ms
15,232 KB
testcase_06 AC 81 ms
15,092 KB
testcase_07 AC 81 ms
15,288 KB
testcase_08 AC 80 ms
15,096 KB
testcase_09 AC 80 ms
15,068 KB
testcase_10 AC 83 ms
15,104 KB
testcase_11 AC 904 ms
28,664 KB
testcase_12 AC 697 ms
23,128 KB
testcase_13 AC 650 ms
31,420 KB
testcase_14 AC 650 ms
29,420 KB
testcase_15 AC 847 ms
31,500 KB
testcase_16 AC 733 ms
31,736 KB
testcase_17 AC 779 ms
31,892 KB
testcase_18 AC 754 ms
31,896 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class SegmentTree
  # n is the size of array or array itself
  def initialize(n, unity, &func)
    raise ArgumentError if !n || !unity || !block_given?
    case n
    when Array
      @size_r = 1
      @n = n.size
      while @size_r < @n
        @size_r <<= 2
      end
      @func = func
      @unity = unity
      @data = Array.new(@size_r * 2, @unity)
      @n.times { |i| @data[i + @size_r] = n[i] }
      self.build()
    when Integer
      @size_r = 1
      @n = n
      while @size_r < @n
        @size_r <<= 2
      end
      @func = func
      @unity = unity
      @data = Array.new(@size_r * 2, @unity)
    end
  end

  # set value at index, index is 0-indexed
  def set(index, value)
    raise IndexError if index >= @n
    @data[index + @size_r] = value
  end

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

  # update the previous value at index to value, index is 0-indexed
  def update(index, value)
    raise IndexError if index >= @n
    index += @size_r
    @data[index] = value
    index >>= 1
    while index > 0
      @data[index] = @func.call(@data[index * 2], @data[index * 2 + 1])
      index >>= 1
    end
    true
  end

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

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

N, q = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
h = {}
seg = SegmentTree.new(a, 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[l]
    tmpr = seg[r]
    seg.update(l, tmpr)
    seg.update(r, tmpl)
    h[tmpr] = l
    h[tmpl] = r
  else
    ans << h[seg.get(l, r + 1)] + 1
  end
end

puts ans
0