Artificial Lake

Time limit1sMemory limit128 MB

Summary
Water fills a terrain of N distinct-height platforms at 1 unit per minute; report when each platform first has 1 unit of water above it.
Level

Hard8 of 10

Topics
Stack, Simulation, Greedy, Math
Solved
No attempts yet

Problem

On the hottest summer days, Farmer John decides to build an artificial lake. He models the lake as a two-dimensional landscape: a contiguous row of NN platforms numbered 1…N1 \dots N from left to right (1≤N≤100,0001 \le N \le 100{,}000).

Platform ii is described by two integers: its width WiW_i (1≤Wi≤1,0001 \le W_i \le 1{,}000) and its height (a relative elevation) HiH_i (1≤Hi≤1,000,0001 \le H_i \le 1{,}000{,}000). All heights are distinct. Infinitely tall walls enclose the landscape on the left and the right. One example landscape profile is shown below.

         *            *  :
         *             *  :
         *             *  8
         *    ***      *  7
         *    ***      *  6
         *    ***      *  5
         *    **********  4 <- height
         *    **********  3
         ***************  2
         ***************  1
Level    |  1 |2|  3   |

Water pours into the bottom of the lowest platform at a rate of 11 square unit per minute. The water falls straight down until it lands on something, and then it spreads out sideways as water always does. Assume that both the falling and the spreading happen instantly.

For each platform, determine the number of minutes after sunrise at which it first becomes covered by a layer of water exactly 11 unit tall.

     WATER              WATER OVERFLOWS
       |                       |
     * |          *      *     |      *      *            *
     * V          *      *     V      *      *            *
     *            *      *    ....    *      *~~~~~~~~~~~~*
     *    **      *      *~~~~** :    *      *~~~~**~~~~~~*
     *    **      *      *~~~~** :    *      *~~~~**~~~~~~*
     *    **      *      *~~~~**~~~~~~*      *~~~~**~~~~~~*
     *    *********      *~~~~*********      *~~~~*********
     *~~~~*********      *~~~~*********      *~~~~*********
     **************      **************      **************
     **************      **************      **************

     After 4 mins        After 26 mins       After 50 mins
     Lvl 1 submerged     Lvl 3 submerged     Lvl 2 submerged

Note: the answer will not always fit in 32 bits.

Input

  • Line 1: a single integer NN.
  • Lines 2 to N+1N+1: line i+1i+1 describes platform ii with two space-separated integers, WiW_i and HiH_i.

Output

  • Lines 1 to NN: line ii contains a single integer, the number of minutes after sunrise at which platform ii is first covered by a 11-unit-tall layer of water.

Examples1

  1. Example 1

    Input
    3
    4 2
    2 7
    6 4
    
    Expected output
    4
    50
    26