Count how many moving interval attacks pierce a wall that rises after each success to the strength that would have stopped it.
Hard8Segment treeIntervalsSortingNo attempts yetTime limit15sMemory limit512 MBYou are studying the history of the Great Wall of China, built to hold off military incursions from the north. In this problem the Great Wall runs along a straight line, from infinity in the east to minus infinity in the west. That is a lot of distance to cover, so the Wall was not built all at once. The builders worked reactively instead: whenever a stretch of the border was attacked successfully, the Wall on that stretch was raised to the height that would have stopped an identical attack.
The northern border was attacked often by nomadic tribes. Each tribe attacks some interval with a strength S. To repel the attack, the Wall must be at least S high everywhere on the defended interval. If a single point inside the interval is lower than S, the attack breaches the Wall there and succeeds. Only the interior of the interval counts. Two intervals that meet at one endpoint do not overlap.
A successful attack does no damage to the Wall. After the attack, every attacked part of the Wall that was lower than S is raised to height S, so the Wall grows in the minimal way that would have stopped the attack. If two or more attacks happen on the same day, the Wall is raised only after all of them resolve, and it is raised in the minimal way that would stop all of them.
Nomadic tribes do not stay in one place. They move east or west and attack the Wall periodically. In this problem each tribe moves at a constant speed and attacks at constant intervals, and the strength of its attacks changes by a constant amount after each attack. The strength can shrink from attrition or grow from experience.
In 250 BC the Wall did not exist yet and its height was zero everywhere. Given the full description of every nomadic tribe that attacked the Wall, determine how many attacks succeeded.
The first line contains the number of test cases T. Each test case begins with a line containing the number of tribes N that attacked the Wall. N lines follow. The ith of them describes one tribe with eight space separated integers di, ni, wi, ei, si, Δdi, Δpi, Δsi.
Limits
For each test case, print one line in the form Case #x: y, where x is the test case number starting from 1 and y is the number of attacks that succeed.
In the first test case of the first example, tribe 1 attacks three times: on day 0 it hits the interval [0,2] with strength 10, on day 2 it hits [3,5] with strength 8, and on day 4 it hits [6,8] with strength 6. All three succeed. Tribe 2 attacks three times with strength 8 each time. On day 10 it hits [2,3] and succeeds (at position 2.5, for example, the Wall is still zero). On day 17 it hits [4,5] and fails, because the Wall on [3,5] is already 8 high and covers [4,5]. On day 24 it hits [6,7] and succeeds, because the Wall there was 6 high.
In the second test case the attacks of three tribes interleave.