http://codeforces.com/gym/101911/problem/B
题意:给n个区间和高度h,区间水平移动,非区间每一步就向下移动一步,起点任取,问最远到哪;
思路:区间不相交的!,前缀和求区间长度和间隙长度,枚举每个区间起点,二分找到最远到哪里;
#include<algorithm>
#include<set>
#include<vector>
#include<queue>
#include<cmath>
#include<cstring>
#include<iostream>
#include<set>
#include<vector>
#include<queue>
#include<cmath>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<map>
#include<stack>
#include<string>
#include<ctime>
using namespace std;
#define sfi(i) scanf("%d",&i)
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-14
#define PI acos(-1)
#define lowbit(x) ((x)&(-x))
#define zero(x) (((x)>0?(x):-(x))<eps)
#define fl() printf("flag\n")
#define MOD(x) ((x%mod)+mod)%mod
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
ll gcd(ll a,ll b){while(b^=a^=b^=a%=b);return a;}
const int maxn=3e5+9;
const int mod=1000000007;
inline ll read()
{
ll f=1,x=0;
char ss=getchar();
while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
return f*x;
}
struct node
{
int l,r;
}p[maxn];
ll sumi[maxn],sumo[maxn];
int main()
{
ll n,h;
cin>>n>>h;
for(int i=1;i<=n;i++)
{
cin>>p[i].l>>p[i].r;
}
sumi[1]=p[1].r-p[1].l;
sumo[1]=0;
for(int i=2;i<=n;i++)
{
sumi[i]=sumi[i-1]+p[i].r-p[i].l;
sumo[i]=sumo[i-1]+p[i].l-p[i-1].r;
}
sumo[n+1]=1e18;
ll Max=0;
for(int i=1;i<=n;i++)
{
int pos=lower_bound(sumo+1,sumo+n+1,sumo[i]+h)-sumo;
Max=max(Max,sumi[pos-1]-sumi[i-1]);
}
cout<<Max+h<<endl;
return 0;
}