1. 程式人生 > >Gym - 101911B Glider 貪心

Gym - 101911B Glider 貪心

A plane is flying at a constant height of hh meters above the ground surface. Let's consider that it is flying from the point (−109,h)(−109,h) to the point (109,h)(109,h) parallel with OxOx 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 OxOx 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 x1x1 and x2x2 (x1<x2x1<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 OxOx axis, covering one unit of distance every second.

 If the glider jumps out at 11, he will stop at 1010. Otherwise, if he jumps out at 22, he will stop at 1212.

Determine the maximum distance along OxOx 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 00.

Input

The first line contains two integers nn and hh (1≤n≤2⋅105,1≤h≤109)(1≤n≤2⋅105,1≤h≤109) — the number of ascending air flow segments and the altitude at which the plane is flying, respectively.

Each of the next nn lines contains two integers xi1xi1 and xi2xi2 (1≤xi1<xi2≤109)(1≤xi1<xi2≤109) — the endpoints of the ii-th ascending air flow segment. No two segments intersect, and they are given in ascending order.

Output

Print one integer — the maximum distance along OxOx 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

題意:給出的是n個氣流帶,現在所在的高度h。在氣流帶中不會下降,不在氣流帶則會每次下降一個單位。注意如果高度為0,恰好到下一個氣流帶的起點,也不能經過下一個加速帶。起點的x值可以任你選擇,最後降落下來到地面的點,離你選擇起點的最遠距離是多少。

題解:很明顯我們應該從某個氣流帶的起點開始,因為這樣能保證在任意時刻所處的高度最高,然後我們就貪心就可以了,列舉起點即可,然後二分找中點即可,這樣複雜的nlog(n),但我們這裡介紹一個O(n)的演算法,就是貪心就好了,先固定起點然後不斷向後找終點,若高度不符合了,就把起點往後移就可以了,這樣2*n 即可解決

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
struct node{
	int l,r;
}a[N];
int n,h;
int main()
{
	while(~scanf("%d%d",&n,&h))
	{
		for(int i=1;i<=n;i++)scanf("%d%d",&a[i].l,&a[i].r);
		int l=1,r=1;
		int ans=0;
		int dh=0,cnt=0;
		while(r<=n)
		{
			if(r>1)
			{
				dh+=a[r].l-a[r-1].r;
				while(dh>=h)
				{
					dh-=a[l+1].l-a[l].r;
					cnt-=a[l].r-a[l].l;
					l++;
				}
			}
			cnt+=a[r].r-a[r].l;
			r++;
			ans=max(ans,cnt);
		}
		printf("%d\n",ans+h);
	}
	return 0;
}