codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1041D.md (3042B)


      1 Source: https://codeforces.com/problemset/problem/1041/D
      2 
      3 D. Glider
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 A plane is flying at a constant height of $h$ meters above the ground surface. Let's consider that it is flying from the point $(-10^9, h)$ to the point $(10^9, h)$ parallel with $Ox$ axis.
     22 
     23 A glider is inside the plane, ready to start his flight at any moment (for the sake of simplicity let's consider that he may start only when the plane's coordinates are integers). After jumping from the plane, he will fly in the same direction as the plane, parallel to $Ox$ axis, covering a unit of distance every second. Naturally, he will also descend; thus his second coordinate will decrease by one unit every second.
     24 
     25 There are ascending air flows on certain segments, each such segment is characterized by two numbers $x_1$ and $x_2$ ($x_1 \lt x_2$) representing its endpoints. No two segments share any common points. When the glider is inside one of such segments, he doesn't descend, so his second coordinate stays the same each second. The glider still flies along $Ox$ axis, covering one unit of distance every second.
     26 
     27  ![](https://espresso.codeforces.com/48923c71b04bf225eb34940d72bd23858ba6d97b.png) If the glider jumps out at $1$, he will stop at $10$. Otherwise, if he jumps out at $2$, he will stop at $12$. 
     28 
     29 Determine the maximum distance along $Ox$ axis from the point where the glider's flight starts to the point where his flight ends if the glider can choose any integer coordinate to jump from the plane and start his flight. After touching the ground the glider stops altogether, so he cannot glide through an ascending airflow segment if his second coordinate is $0$.
     30 
     31 Input
     32 
     33 The first line contains two integers $n$ and $h$ $(1 \le n \le 2\cdot10^{5}, 1 \le h \le 10^{9})$ — the number of ascending air flow segments and the altitude at which the plane is flying, respectively.
     34 
     35 Each of the next $n$ lines contains two integers $x_{i1}$ and $x_{i2}$ $(1 \le x_{i1} \lt x_{i2} \le 10^{9})$ — the endpoints of the $i$-th ascending air flow segment. No two segments intersect, and they are given in ascending order.
     36 
     37 Output
     38 
     39 Print one integer — the maximum distance along $Ox$ axis that the glider can fly from the point where he jumps off the plane to the point where he lands if he can start his flight at any integer coordinate.
     40 
     41 Examples
     42 
     43 Input
     44 
     45 ```
     46 3 4  
     47 2 5  
     48 7 9  
     49 10 11
     50 ```
     51 
     52 Output
     53 
     54 ```
     55 10
     56 ```
     57 
     58 Input
     59 
     60 ```
     61 5 10  
     62 5 7  
     63 11 12  
     64 16 20  
     65 25 26  
     66 30 33
     67 ```
     68 
     69 Output
     70 
     71 ```
     72 18
     73 ```
     74 
     75 Input
     76 
     77 ```
     78 1 1000000000  
     79 1 1000000000
     80 ```
     81 
     82 Output
     83 
     84 ```
     85 1999999999
     86 ```
     87 
     88 Note
     89 
     90 In the first example if the glider can jump out at $(2, 4)$, then the landing point is $(12, 0)$, so the distance is $12-2 = 10$.
     91 
     92 In the second example the glider can fly from $(16,10)$ to $(34,0)$, and the distance is $34-16=18$.
     93 
     94 In the third example the glider can fly from $(-100,1000000000)$ to $(1999999899,0)$, so the distance is $1999999899-(-100)=1999999999$.