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 = Set(Int32).new q = PriorityQueue(Int32).new(n) n.times do |i| if a[i] > b[i] return false end set << a[i] q.add(a[i]) while q.size > 0 && q.top > b[i] set.delete(q.top) q.pop end c[i] ||= set.includes?(b[i]) end set.clear q.clear (n - 1).downto(0) do |i| set << a[i] q.add(a[i]) while q.size > 0 && q.top > b[i] set.delete(q.top) q.pop end c[i] ||= set.includes?(b[i]) end return c.all? { |v| v } end class PriorityQueue(T) def initialize(capacity : Int32) @elem = Array(T).new(capacity) end def initialize(list : Enumerable(T)) @elem = list.to_a 1.upto(size - 1) { |i| fixup(i) } end def size @elem.size end def add(v) @elem << v fixup(size - 1) end def top @elem[0] end def pop ret = @elem[0] last = @elem.pop if size > 0 @elem[0] = last fixdown(0) end ret end def clear @elem.clear end def decrease_top(new_value : T) @elem[0] = new_value fixdown(0) end def to_s(io : IO) io << @elem end private def fixup(index : Int32) while index > 0 parent = (index - 1) // 2 break if @elem[parent] >= @elem[index] @elem[parent], @elem[index] = @elem[index], @elem[parent] index = parent end end private def fixdown(index : Int32) while true left = index * 2 + 1 break if left >= size right = index * 2 + 2 child = right >= size || @elem[left] > @elem[right] ? left : right if @elem[child] > @elem[index] @elem[child], @elem[index] = @elem[index], @elem[child] index = child else break end end end end