結果

問題 No.318 学学学学学
ユーザー simansiman
提出日時 2022-11-04 03:02:00
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,692 ms / 2,000 ms
コード長 2,243 bytes
コンパイル時間 56 ms
コンパイル使用メモリ 11,368 KB
実行使用メモリ 37,820 KB
最終ジャッジ日時 2023-09-25 09:06:45
合計ジャッジ時間 23,684 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 166 ms
16,900 KB
testcase_01 AC 255 ms
19,172 KB
testcase_02 AC 297 ms
19,604 KB
testcase_03 AC 213 ms
18,108 KB
testcase_04 AC 266 ms
19,180 KB
testcase_05 AC 1,326 ms
37,820 KB
testcase_06 AC 1,692 ms
34,428 KB
testcase_07 AC 1,383 ms
33,240 KB
testcase_08 AC 1,099 ms
31,804 KB
testcase_09 AC 897 ms
30,948 KB
testcase_10 AC 719 ms
30,332 KB
testcase_11 AC 1,318 ms
37,144 KB
testcase_12 AC 1,030 ms
34,284 KB
testcase_13 AC 926 ms
33,208 KB
testcase_14 AC 850 ms
31,624 KB
testcase_15 AC 774 ms
30,920 KB
testcase_16 AC 710 ms
30,300 KB
testcase_17 AC 1,389 ms
33,704 KB
testcase_18 AC 1,024 ms
33,816 KB
testcase_19 AC 1,371 ms
33,636 KB
testcase_20 AC 709 ms
29,404 KB
testcase_21 AC 81 ms
15,084 KB
testcase_22 AC 83 ms
15,312 KB
testcase_23 AC 82 ms
15,116 KB
testcase_24 AC 80 ms
15,044 KB
testcase_25 AC 81 ms
15,128 KB
testcase_26 AC 81 ms
15,052 KB
testcase_27 AC 79 ms
15,152 KB
testcase_28 AC 81 ms
15,148 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class RangeUpdateQuery
  attr_reader :n, :dat, :lazy

  # @param [Integer] len 区間の長さ
  def initialize(len, init: -1)
    @n = 1

    @n *= 2 while @n < len
    @dat = Array.new(2 * @n - 1, init)
    @lazy = Array.new(2 * @n - 1)
  end

  # @param [Integer] l 更新範囲の左端
  # @param [Integer] r 更新範囲の右端
  # @param [Integer] x 更新する値
  def update(l, r, x)
    fill(l, r, x, 0, 0, n - 1)
  end

  # @param [Integer] idx 取得したい値の箇所
  def find(idx)
    sum(idx, idx, 0, 0, n - 1)
  end

  def find_min(l, r)
    min(l, r, 0, 0, n - 1)
  end

  private

  # @param [Integer] a 探索範囲の左端
  # @param [Integer] b 探索範囲の右端
  # @param [Integer] x 更新する値
  # @param [Integer] idx 現在位置
  # @param [Integer] l 現在の探索範囲の左端
  # @param [Integer] r 現在の探索範囲の右端
  def fill(a, b, x, idx, l, r)
    return if r < a || b < l

    evaluate(idx, l, r)

    if a <= l && r <= b
      @lazy[idx] = x
    else
      fill(a, b, x, idx * 2 + 1, l, (l + r) / 2)
      fill(a, b, x, idx * 2 + 2, (l + r) / 2 + 1, r)
    end
  end

  def evaluate(idx, l, r)
    return if lazy[idx].nil?

    @dat[idx] = lazy[idx]

    if r - l > 0
      @lazy[2 * idx + 1] = lazy[idx]
      @lazy[2 * idx + 2] = lazy[idx]
    end

    @lazy[idx] = nil
  end

  def sum(a, b, idx, l, r)
    return 0 if r < a || b < l

    evaluate(idx, l, r)

    if a <= l && r <= b
      dat[idx]
    else
      vl = sum(a, b, idx * 2 + 1, l, (l + r) / 2)
      vr = sum(a, b, idx * 2 + 2, (l + r) / 2 + 1, r)

      vl + vr
    end
  end

  def min(a, b, idx, l, r)
    return 0 if r < a || b < l

    evaluate(idx, l, r)

    if a <= l && r <= b
      dat[idx]
    else
      vl = sum(a, b, idx * 2 + 1, l, (l + r) / 2)
      vr = sum(a, b, idx * 2 + 2, (l + r) / 2 + 1, r)

      vl < vr ? vl : vr
    end
  end
end

N = gets.to_i
A = gets.split.map(&:to_i)

left = Hash.new(Float::INFINITY)
right = Hash.new(-Float::INFINITY)

A.each_with_index do |a, i|
  left[a] = i if left[a] > i
  right[a] = i
end

ruq = RangeUpdateQuery.new(N)

A.uniq.sort.each do |a|
  l = left[a]
  r = right[a]
  ruq.update(l, r, a)
end

puts (0...N).map { |i| ruq.find(i) }.join(' ')
0