結果

問題 No.1768 The frog in the well knows the great ocean.
ユーザー tomeruntomerun
提出日時 2021-11-27 00:03:27
言語 Crystal
(1.11.2)
結果
AC  
実行時間 208 ms / 3,000 ms
コード長 5,595 bytes
コンパイル時間 12,933 ms
コンパイル使用メモリ 295,468 KB
実行使用メモリ 31,616 KB
最終ジャッジ日時 2024-06-29 19:10:45
合計ジャッジ時間 15,996 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 5 ms
5,376 KB
testcase_02 AC 6 ms
5,376 KB
testcase_03 AC 5 ms
5,376 KB
testcase_04 AC 5 ms
5,376 KB
testcase_05 AC 5 ms
5,376 KB
testcase_06 AC 64 ms
10,240 KB
testcase_07 AC 65 ms
13,312 KB
testcase_08 AC 62 ms
10,496 KB
testcase_09 AC 64 ms
10,240 KB
testcase_10 AC 64 ms
15,232 KB
testcase_11 AC 66 ms
17,792 KB
testcase_12 AC 68 ms
17,792 KB
testcase_13 AC 68 ms
17,792 KB
testcase_14 AC 67 ms
17,792 KB
testcase_15 AC 69 ms
17,792 KB
testcase_16 AC 79 ms
31,416 KB
testcase_17 AC 79 ms
31,488 KB
testcase_18 AC 78 ms
31,432 KB
testcase_19 AC 83 ms
31,488 KB
testcase_20 AC 79 ms
31,616 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 168 ms
5,376 KB
testcase_25 AC 196 ms
27,392 KB
testcase_26 AC 208 ms
27,376 KB
testcase_27 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

read_line.to_i.times do
  puts solve() ? "Yes" : "No"
end

def solve
  n = read_line.to_i
  a = read_line.split.map(&.to_i)
  b = read_line.split.map(&.to_i)
  c = Array.new(n, false)
  set = RBST(Int32).new
  n.times do |i|
    if a[i] > b[i]
      return false
    end
    set.insert(a[i])
    while true
      lo = set.lower(a[i])
      break if !lo
      set.remove(lo)
    end
    while true
      hi = set.upper(b[i])
      break if !hi
      set.remove(hi)
    end
    c[i] ||= !set.find(b[i]).nil?
  end
  set.clear
  (n - 1).downto(0) do |i|
    set.insert(a[i])
    while true
      lo = set.lower(a[i])
      break if !lo
      set.remove(lo)
    end
    while true
      hi = set.upper(b[i])
      break if !hi
      set.remove(hi)
    end
    c[i] ||= !set.find(b[i]).nil?
  end
  return c.all? { |v| v }
end

class RBST(T)
  class Node(T)
    @left : Node(T) | Nil
    @right : Node(T) | Nil
    property :left, :right
    getter :val, :size

    def initialize(@val : T)
      @left = nil
      @right = nil
      @size = 1
    end

    def height
      if left = @left
        lh = left.height
      else
        lh = 0
      end
      if right = @right
        rh = right.height
      else
        rh = 0
      end
      return {lh, rh}.max + 1
    end

    def fix_size
      @size = 1
      if left = @left
        @size += left.size
      end
      if right = @right
        @size += right.size
      end
    end

    def to_s(io : IO)
      to_s(io, 0)
    end

    def to_s(io : IO, level : Int32)
      if left = @left
        left.to_s(io, level + 1)
      end
      io << " " * level * 2 << @val << "\n"
      if right = @right
        right.to_s(io, level + 1)
      end
    end
  end

  @root : Node(T) | Nil

  def initialize
    @root = nil
    @rnd = Random.new
  end

  def insert(v : T)
    @root = insert(@root, v)
  end

  def insert(node : Node(T) | Nil, v : T) : Node(T)
    return Node(T).new(v) if !node
    if @rnd.rand(node.size + 1) == 0
      return insert_root(node, v)
    end
    if v < node.val
      node.left = insert(node.left, v)
    else
      node.right = insert(node.right, v)
    end
    node.fix_size
    return node
  end

  def insert_root(node : Node(T) | Nil, v : T) : Node(T)
    return Node(T).new(v) if !node
    if v < node.val
      node.left = insert_root(node.left, v)
      return rotate_right(node)
    else
      node.right = insert_root(node.right, v)
      return rotate_left(node)
    end
  end

  def rotate_right(node : Node(T)) : Node(T)
    top = node.left.not_nil!
    mid = top.right
    top.right = node
    node.left = mid
    node.fix_size
    top.fix_size
    return top
  end

  def rotate_left(node : Node(T)) : Node(T)
    top = node.right.not_nil!
    mid = top.left
    top.left = node
    node.right = mid
    node.fix_size
    top.fix_size
    return top
  end

  def clear
    @root = nil
  end

  def remove(v : T) : Bool
    new_root, found = remove(@root, v)
    if found
      @root = new_root
      return true
    else
      return false
    end
  end

  def remove(node : Node(T) | Nil, v : T) : Tuple(Node(T) | Nil, Bool)
    return {nil, false} if !node
    if v == node.val
      return {meld(node.left, node.right), true}
    elsif v < node.val
      new_child, found = remove(node.left, v)
      if found
        node.left = new_child
        node.fix_size
      end
    else
      new_child, found = remove(node.right, v)
      if found
        node.right = new_child
        node.fix_size
      end
    end
    return {node, found}
  end

  def meld(left : Node(T) | Nil, right : Node(T) | Nil) : Node(T) | Nil
    return right if !left
    return left if !right
    if @rnd.rand(left.size + right.size) < left.size
      left.right = meld(left.right, right)
      left.fix_size
      return left
    else
      right.left = meld(left, right.left)
      right.fix_size
      return right
    end
  end

  def find(v : T) : Node(T) | Nil
    cur = @root
    while cur
      if v == cur.val
        return cur
      elsif v < cur.val
        cur = cur.left
      else
        cur = cur.right
      end
    end
    return nil
  end

  def nth(n : Int32) : T
    # 0-indexed
    cur = @root
    while cur
      ln = cur.left
      lc = ln ? ln.size : 0
      if lc > n
        cur = cur.left
      elsif lc < n
        cur = cur.right
        n -= lc + 1
      else
        break
      end
    end
    if cur
      return cur.val
    else
      raise Exception.new("cannot fine")
    end
  end

  def rank(v : T) : Int32
    # 0-indexed, not count equivalent values
    cur = @root
    ret = 0
    while cur
      if v <= cur.val
        cur = cur.left
      else
        if left = cur.left
          ret += left.size
        end
        ret += 1
        cur = cur.right
      end
    end
    return ret
  end

  def lower(v : T) : T | Nil
    # largest value smaller than v
    cur = @root
    ret = nil
    while cur
      if cur.val < v
        ret = cur.val
        cur = cur.right
      else
        cur = cur.left
      end
    end
    return ret
  end

  def upper(v : T) : T | Nil
    # smallest value larger than v
    cur = @root
    ret = nil
    while cur
      if cur.val > v
        ret = cur.val
        cur = cur.left
      else
        cur = cur.right
      end
    end
    return ret
  end

  def size
    if root = @root
      return root.size
    else
      return 0
    end
  end

  def height
    if root = @root
      root.height
    else
      0
    end
  end

  def to_s(io : IO)
    if root = @root
      root.to_s(io, 0)
    else
      io << "nil\n"
    end
  end
end
0