File size: 2,654 Bytes
761e24d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
<p>
Today, Mr. Fox is taking it easy by playing with some blocks in a 2D world. Each block is an inch-by-inch square,
and there are <strong>N</strong> stacks of blocks in a row, with the <strong>i</strong>th stack having <strong>H<sub>i</strong> blocks.
For example, if <strong>N</strong>=6 and <strong>H</strong>={3, 1, 5, 4, 1, 6}, then the collection of blocks looks like this
(where an "X" denotes a block):
<p>
<p>
<pre>
.....X
..X..X
..XX.X
X.XX.X
X.XX.X
XXXXXX
</pre>
</p>
<p>
Ever curious, Mr. Fox would like to answer <strong>Q</strong> questions about his blocks (without actually modifying them),
the <strong>i</strong>th one being as follows:
</p>
<p>
"If I were to consider only the stacks from <strong>A<sub>i</sub></strong> to <strong>B<sub>i</sub></strong> inclusive,
getting rid of all of the other blocks, how many square inches of water would my block structure be able to hold?"
</p>
<p>
As one might imagine, a given square inch can hold water if it doesn't contain a block itself, but there is a block both somewhere to its left
and somewhere to its right at the same height. For example, if you were to take
<strong>A<sub>i</sub></strong>=2 and <strong>B<sub>i</sub></strong>=6, you would be left with the
following block structure to consider (where an "*" denotes an inch-by-inch square which can hold water):
</p>
<p>
<pre>
....X
.X**X
.XX*X
.XX*X
.XX*X
XXXXX
</pre>
</p>
<h3>Constraints</h3>
<p>
1 ≤ <strong>T</strong> ≤ 20 <br/>
1 ≤ <strong>N</strong> ≤ 300,000 <br />
1 ≤ <strong>Q</strong> ≤ 300,000 <br />
1 ≤ <strong>H<sub>i</sub></strong> ≤ 10<sup>9</sup> <br />
1 ≤ <strong>A<sub>i</sub></strong> ≤ <strong>B<sub>i</sub></strong> ≤ <strong>N</strong> <br />
</p>
<h3>Input</h3>
<p>
Input begins with an integer <strong>T</strong>, the number of block structures Mr. Fox has.
For each structure, there is first a line containing the space-separated integers <strong>N</strong> and <strong>Q</strong>.
The next line contains the space-separated integers <strong>H<sub>i</sub></strong>.
Then follow <strong>Q</strong> lines, the <strong>i</strong>th of which contains the space-separated integers
<strong>A<sub>i</sub></strong> and <strong>B<sub>i</sub></strong>.
</p>
<h3>Output</h3>
<p>
For the <strong>i</strong>th structure, print a line containing "Case #<strong>i</strong>: " followed by
the sum of the answers to the <strong>Q</strong> questions modulo 10<sup>9</sup>+7.
</p>
<h3>Explanation of Sample</h3>
<p>
In the first case, we consider prefixes of the block structure. The answers to the queries are 0, 0, 0, 0, 0, 5, 5, 7, 7, 18, 18 for a total of 60.
</p>
|