CF1041D Glider

原题链接:http://codeforces.com/contest/1041/problem/D

Glider

A plane is flying at a constant height of h h h meters above the ground surface. Let’s consider that it is flying from the point ( − 1 0 9 , h ) (−10^9,h) (109,h) to the point ( 1 0 9 , h ) (10^9,h) (109,h) parallel with O x Ox Ox axis.

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 O x Ox 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.

There are ascending air flows on certain segments, each such segment is characterized by two numbers x 1 x_1 x1 and x 2 ( x 1 &lt; x 2 ) x_2 (x_1&lt;x_2) x2(x1<x2) 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 O x Ox Ox axis, covering one unit of distance every second.

If the glider jumps out at 1 1 1, he will stop at 10 10 10. Otherwise, if he jumps out at 2 2 2, he will stop at 12 12 12.

Determine the maximum distance along O x Ox 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 0 0.

Input

The first line contains two integers n n n and h ( 1 ≤ n ≤ 2 ⋅ 1 0 5 , 1 ≤ h ≤ 1 0 9 ) h(1≤n≤2⋅10^5,1≤h≤10^9) h(1n2105,1h109) — the number of ascending air flow segments and the altitude at which the plane is flying, respectively.

Each of the next n n n lines contains two integers x i 1 x_{i1} xi1 and x i 2 ( 1 ≤ x i 1 &lt; x i 2 ≤ 1 0 9 ) x_{i2}(1≤x_{i1}&lt;x_{i2}≤10^9) xi2(1xi1<xi2109) — the endpoints of the i i i-th ascending air flow segment. No two segments intersect, and they are given in ascending order.

Output

Print one integer — the maximum distance along O x Ox 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.

Examples
input

3 4
2 5
7 9
10 11

output

10

input

5 10
5 7
11 12
16 20
25 26
30 33

output

18

input

1 1000000000
1 1000000000

output

1999999999

Note

In the first example if the glider can jump out at ( 2 , 4 ) (2,4) (2,4), then the landing point is ( 12 , 0 ) (12,0) (12,0), so the distance is 12 − 2 = 10 12−2=10 122=10.

In the second example the glider can fly from ( 16 , 10 ) (16,10) (16,10) to ( 34 , 0 ) (34,0) (34,0), and the distance is 34 − 16 = 18 34−16=18 3416=18.

In the third example the glider can fly from ( − 100 , 1000000000 ) (−100,1000000000) (100,1000000000) to ( 1999999899 , 0 ) (1999999899,0) (1999999899,0), so the distance is 1999999899 − ( − 100 ) = 1999999999 1999999899−(−100)=1999999999 1999999899(100)=1999999999.

题解

首先根据贪心起点一定在上升气流的左端点,就可以搞个 t w o   p o i n t e r s two\ pointers two pointers每次往右移,但是感觉贼难写,不如二分吧,直接二分飞机在哪儿落地。

代码
#include<bits/stdc++.h>
using namespace std;
const int M=2e5+5;
int le[M],ri[M],low[M],n,h,ans;
int fly(int s,int h){int pos=upper_bound(low+1,low+n+1,h+low[s-1])-low-1,d=h+low[s-1]-low[pos];return d?ri[pos+1]+d:le[pos+1];}
void in(){scanf("%d%d",&n,&h);for(int i=1;i<=n;++i)scanf("%d%d",&le[i],&ri[i]),low[i-1]=le[i]-ri[i-1];}
void ac(){low[0]=0;for(int i=2;i<n;++i)low[i]+=low[i-1];low[n]=INT_MAX;for(int i=1;i<=n;++i)ans=max(ans,fly(i,h)-le[i]);printf("%d",ans);}
int main(){in();ac();}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值