結果

問題 No.2650 [Cherry 6th Tune *] セイジャク
ユーザー tomeruntomerun
提出日時 2024-02-23 21:44:10
言語 Crystal
(1.11.2)
結果
TLE  
実行時間 -
コード長 1,840 bytes
コンパイル時間 13,013 ms
コンパイル使用メモリ 293,008 KB
実行使用メモリ 13,336 KB
最終ジャッジ日時 2024-02-23 21:44:39
合計ジャッジ時間 21,065 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 845 ms
13,336 KB
testcase_03 AC 1,360 ms
10,196 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

n, a = read_line.split.map(&.to_i)
x = read_line.split.map(&.to_i)
t = read_line.to_i
lrs = Array.new(t) { read_line.split.map(&.to_i) }
coords = x.dup
lrs.each { |lr| coords += lr }
c2i = Hash(Int32, Int32).new
coords.sort.uniq.each.with_index do |c, i|
  c2i[c] = i
end
segtree = RangeSetSegTree(Int32).new(Array.new(coords.size, -1), ->(x : Int32, y : Int32) { {x, y}.max }, 0)
lrs.each.with_index do |lr, i|
  l, r = lr.map { |x| c2i[x] }
  segtree.set(l, r + 1, i + 1)
end
n.times do |i|
  ans = segtree.get(c2i[x[i]])
  puts ans <= 0 ? -1 : ans
end

class RangeSetSegTree(T)
  @ar : Array(T)
  @size : Int32
  @op : Proc(T, T, T)
  @zero : T

  def initialize(s : Int32, @op : Proc(T, T, T), @zero : T)
    @size = 1
    while @size < s
      @size *= 2
    end
    @ar = Array.new(@size * 2, @zero)
  end

  def initialize(init : Array(T), @op : Proc(T, T, T), @zero : T)
    @size = 1
    while @size < init.size
      @size *= 2
    end
    @ar = Array.new(@size, @zero)
    @ar.concat(init)
    @ar.concat([@zero] * (@size - init.size))
    (@size - 1).downto(1) do |i|
      @ar[i] = @op.call(@ar[i * 2], @ar[i * 2 + 1])
    end
  end

  def get(pos : Int32)
    ret = @zero
    bit = 1
    base = @size
    while base > 0
      ret = @op.call(ret, @ar[base + (pos >> (bit - 1))])
      bit += 1
      base >>= 1
    end
    return ret
  end

  def set(lo : Int32, hi : Int32, v : T)
    # [lo, hi)
    bit = @size.trailing_zeros_count
    len = @size
    base = 1
    while len > 0
      nlo = (lo + len - 1) & ~(len - 1)
      phi = hi & ~(len - 1)
      if nlo + len <= hi
        i = base + (nlo >> bit)
        @ar[i] = @op.call(v, @ar[i])
      end
      if lo <= phi - len
        i = base + (phi >> bit) - 1
        @ar[i] = @op.call(v, @ar[i])
      end
      bit -= 1
      len >>= 1
      base <<= 1
    end
  end
end
0