import java.util.* import kotlin.collections.ArrayDeque import kotlin.collections.component1 import kotlin.collections.component2 fun main() { val testCase = readln().toInt() for (case in 1 .. testCase) { val (v, m) = readln().split(' ').map(String::toInt) val pickup = readln().split(' ').map(String::toInt) val deliver = readln().split(' ').map(String::toInt) val operation = readln().split(' ').map(String::toInt) val result = solve(v, m, pickup, deliver, operation) println("Case #$case: ${result.joinToString( " ")}") } } fun solve(v: Int, m: Int, pickup: List, deliver: List, operation: List): List { val greaterQueue = PriorityQueue(reverseOrder()) val smallerQueue = PriorityQueue() val pickupPosition = ArrayDeque>((0 until v).map { i -> pickup[i] to deliver[i] }.sortedBy { it.first }) val result = mutableListOf() var currentPosition = 0L for (x in operation) { var count = 0 currentPosition += x if (x < 0) { while (greaterQueue.isNotEmpty() && currentPosition <= greaterQueue.first()) { ++count greaterQueue.poll() } }else { var hasDeliverEvent = (smallerQueue.isNotEmpty() && smallerQueue.first() <= currentPosition) var hasPickupEvent = (pickupPosition.isNotEmpty() && pickupPosition.first().first <= currentPosition) while (hasDeliverEvent || hasPickupEvent) { if (!hasDeliverEvent || (hasPickupEvent && pickupPosition.first().first <= smallerQueue.first())) { val (p, d) = pickupPosition.removeFirst() if (p < d) { smallerQueue.add(d) }else { greaterQueue.add(d) } hasPickupEvent = (pickupPosition.isNotEmpty() && pickupPosition.first().first <= currentPosition) hasDeliverEvent = (smallerQueue.isNotEmpty() && smallerQueue.first() <= currentPosition) }else { smallerQueue.poll() ++count hasDeliverEvent = (smallerQueue.isNotEmpty() && smallerQueue.first() <= currentPosition) } } } result.add(count) } return result }