提出 #33771637


ソースコード 拡げる

class Fenwick
  attr_accessor(:size, :node)
 
  def initialize(n)
    @size = n
    @node = Array.new(@size + 1, 0)
  end
 
  def add(p, x)
    p += 1
    while p <= @size
      @node[p] += x
      p += p & (- p)
    end
  end
 
  def sum(r)
    s = 0
    while r > 0
      s += (@node[r] || 0)
      r -= r & (- r)
    end
    return s
  end
  
  def prod(l, r)
    return sum(r) - sum(l)
  end

  def bsearch_index(x)
    if x <= 0
      return 0
    else
      res = 0
      r = 1
      while r < @size
        r <<= 1
      end
      len = r
      while len > 0
        if res + len < @size && @node[res + len] < x
          x -= @node[res + len]
          res += len
        end
        len >>= 1
      end

      return res
    end
  end
end

n, q = gets.chomp.split.map(&:to_i)
queries = []
(n - 1).times do |i|
  l, r = gets.chomp.split.map(&:to_i)
  queries << [q, l, i + 1, - 1]
  queries << [q, r + 1, i + 1, 1]
end

q.times do |i|
  a, b = gets.chomp.split.map(&:to_i)
  queries << [i, a, b, 0]
end

queries.sort_by! {|t, x, _, _| x * (q + 1) + q - t }

ft = Fenwick.new(n + 2)
(n + 2).times do |i|
  ft.add(i, 1)
end

res = Array.new(q)
queries.each do |t, _, i, d|
  if t == q
    ft.add(i, d)
  else
    c = ft.sum(i)
    lb = ft.bsearch_index(c) + 1
    ub = ft.bsearch_index(c + 1)
    res[t] = ub - lb + 1
  end
end
puts res

提出情報

提出日時
問題 N - 旅行会社
ユーザ qib
言語 Ruby (2.7.1)
得点 6
コード長 1425 Byte
結果 AC
実行時間 2388 ms
メモリ 107040 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 6 / 6
結果
AC × 2
AC × 22
セット名 テストケース
Sample sample_01.txt, sample_02.txt
All core_based_random_00.txt, core_based_random_01.txt, core_based_random_02.txt, core_based_random_03.txt, core_based_random_04.txt, core_based_random_05.txt, extreme_00.txt, handmade_00.txt, max_00.txt, max_01.txt, max_02.txt, max_03.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, sample_01.txt, sample_02.txt
ケース名 結果 実行時間 メモリ
core_based_random_00.txt AC 2294 ms 106972 KiB
core_based_random_01.txt AC 2369 ms 107028 KiB
core_based_random_02.txt AC 925 ms 41020 KiB
core_based_random_03.txt AC 717 ms 47992 KiB
core_based_random_04.txt AC 753 ms 35512 KiB
core_based_random_05.txt AC 792 ms 62412 KiB
extreme_00.txt AC 2088 ms 107016 KiB
handmade_00.txt AC 58 ms 14212 KiB
max_00.txt AC 2227 ms 104120 KiB
max_01.txt AC 2244 ms 104092 KiB
max_02.txt AC 2334 ms 106396 KiB
max_03.txt AC 2291 ms 106756 KiB
random_00.txt AC 613 ms 44668 KiB
random_01.txt AC 976 ms 72548 KiB
random_02.txt AC 2388 ms 107040 KiB
random_03.txt AC 2384 ms 106872 KiB
random_04.txt AC 755 ms 35628 KiB
random_05.txt AC 806 ms 64000 KiB
random_06.txt AC 227 ms 20136 KiB
random_07.txt AC 1115 ms 63804 KiB
sample_01.txt AC 55 ms 14120 KiB
sample_02.txt AC 58 ms 14164 KiB