Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
hackercup / 2016 /round2 /snakes_and_ladders.md
wjomlex's picture
2016 Problems
07f695f verified
|
raw
history blame
2.41 kB
As the owner of a vast collection of fine ladders, you would like to display
them to potential onlookers. You've decided to stand your **N** ladders up
vertically, with their bases at distinct points on a horizontal number line.
The **i**th ladder's base is at position **Xi**, and it has height **Hi**.
The local snake population has taken an interest in your ladders. As everyone
knows, snakes love nothing more than to suspend themselves horizontally above
the ground! In particular, a snake of length **L** is able to suspend itself
between the tops of two ladders **a** and **b** if and only if they meet the
following conditions:
* \- the ladders are exactly **L** units apart (|**Xa** \- **Xb**| = **L**)
* \- the ladders are of equal height (**Ha** = **Hb**)
* \- there are no taller ladders in between them (there exists no ladder **c** such that min{**Xa**, **Xb**} < **Xc** < max{**Xa**, **Xb**} and **Hc** > **Ha**)
A number of snakes are planning to take up residence amongst your ladders. In
particular, for every position in which a snake could suspend itself (in other
words, for every distinct, unordered pair of ladders **a** and **b**), one
snake will move in of the appropriate length.
You'll have no choice but to take care of these snakes, of course. To feed a
snake of length **L**, it'll cost you **L**2 dollars daily. To prepare your
budget, you'd like to calculate how many dollars you'll be spending each day
on your new pets!
### Input
Input begins with an integer **T**, the number of sets of ladders you own. For
each set, there is first a line containing the integer **N**. Then, **N**
lines follow, the **i**th of which contains two space-separated integers,
**Xi** and **Hi**.
### Output
For the **i**th set of ladders, print a line containing "Case #**i**: "
followed by the daily feeding cost of all the snakes that move in, modulo 109
\+ 7.
### Constraints
1 ≤ **T** ≤ 50
1 ≤ **N** ≤ 200,000
0 ≤ **Xi**, **Hi** ≤ 1,000,000,000
### Explanation of Sample
In the first case, one snake will move in between your two ladders. It will
have length 20, so it will cost 202 = 400 dollars a day to feed. In the third
case, one snake will move in between the ladders of height 3, and another will
move in between the ladders at X = 2 and X = 3. The first snake has length 3,
and the second has length 1. The total cost is therefore 32 \+ 12 = 10.