glider
D. glider
time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output
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 OxOxaxis, 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
Note
In the first example if the glider can jump out at (2,4)(2,4), then the landing point is (12,0)(12,0), so the distance is 12−2=1012−2=10.
In the second example the glider can fly from (16,10)(16,10) to (34,0)(34,0), and the distance is 34−16=1834−16=18.
In the third example the glider can fly from (−100,1000000000)(−100,1000000000) to (1999999899,0)(1999999899,0), so the distance is 1999999899−(−100)=19999999991999999899−(−100)=1999999999.
#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)
#define x first
#define y second
typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
const int INF = int(1e9);
const li INF64 = li(1e18);
const ld EPS = 1e-9;
const int N = 200 * 1000 + 555;
int n, h;
pt a[N];
inline bool read() {
if(!(cin >> n >> h))
return false;
fore(i, 0, n)
assert(scanf("%d%d", &a[i].x, &a[i].y) == 2);
sort(a, a + n);
return true;
}
int ps[N];
int getH(int lf, int rg) { // 从 x=lf 到 x=rg 会下降多少
int l = int(lower_bound(a, a + n, pt(lf, -1)) - a);
int r = int(lower_bound(a, a + n, pt(rg, -1)) - a);
int sum = ps[r] - ps[l];
if(l > 0)
sum += max(0, a[l - 1].y - lf);
assert(rg - lf - sum >= 0);
return rg - lf - sum;
}
inline void solve() {
ps[0] = 0;
fore(i, 0, n)
ps[i + 1] = ps[i] + (a[i].y - a[i].x);
int ans = 0;
fore(i, 0, n) {
int lx = a[i].y + 1;
int lf = -(h + 1), rg = lx;
while(rg - lf > 1) {
int mid = (lf + rg) / 2;
if(getH(mid, lx) > h)
lf = mid;
else
rg = mid;
}
assert(getH(rg, lx) == h);
ans = max(ans, lx - rg);
}
cout << ans << endl;
}
int main() {
if(read()) {
solve();
}
return 0;
}
相关阅读
sqlserver中set IDENTITY_INSERT on 和 off 的设置方
前言 最近频繁用数据库,发现数据库并没有想想的那么好操作,今天遇到了一个插入的问题,表格建好了,往里边插入数据的时候就报错。说se
declare-styleable是给自定义控件添加自定义属性用的1.首先,先写attrs.xml<?xml version="1.0" encoding="utf-8"?> <resources>
ADB 操作命令详解及用法大全一、ADB是什么?二、ADB有什么作用?三、ADB命令语法单一设备/模拟器连接多个设备/模拟器连接四、ADB常用
效果图package com.example.liuqidemo.view;import java.text.DateFormat;import java.text.ParseException;import java.text.Si
Ulead GIF Animator如何裁剪 Ulead GIF Animator裁剪
友立公司出版的动画GIF制作软件,内建的Plugin有许多现成的特效可以立即套用,可将AVI文件转成动画GIF文件,而且还能将动画GIF图片最佳