传送门:点我

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 (−10^9,h)(−10^9,h) to the point (10^9,h)(10^9,h) parallel with 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 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 x1 x1 and x2 x2 (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 Ox 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⋅10^5,1≤h≤10^9)(1≤n≤2⋅10^5,1≤h≤10^9) — 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≤10^9)(1≤xi1<xi2≤10^9) — 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 42 57 910 11

Output
10

Input
5 105 711 1216 2025 2630 33

Output
18

Input
1 10000000001 1000000000

Output
1999999999

NOTE

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

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

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

大概题意:

给出的是n个气流带(按顺序给出),和你现在所在的高度m。在气流带中不会下降,即维持进入气流带的高度。不在气流带则会每次下降一个单位。

下面n行是气流带的起点和终点。注意如果高度为0,恰好到下一个气流带的起点,也不能经过下一个加速带。

询问的是,起点的x值可以任你选择,最后降落下来到地面的点,离你选择起点的最远距离是多少。

因为是可以任意选起点,所以会有一个最大值。题目插图画的就是样例1的示例。

思路:看到题目给2秒,大暴力二分找✔。算了下复杂度是O(NlogN),n是2e5,也可以接受。

首先,要明确的一点是,起点肯定是某个加速带的起点,因为这样才能保证有更多的空间,留给后面经过更多的加速带(简单的贪心)

   其次,既然是二分,那么肯定要是有序的,在这题我二分的是从我选定的起点,最远到能到的终点,中间可以经过多少加速带。因为距离是递增的,所以可以二分。

     即,把每个加速带中间的间隔,抽取出来作为一个数组,对这个数组进行二分,然后同时维护一个本身加速带的长度的一个前缀和,用来快速计算,经过加速带的距离。

     最后答案肯定是 :能经过的加速带的距离+m   这个值的最大值

不知道还有没有更好的办法呀,还想学习一下。

暂时先这样子吧。

最后是代码:

#include<stdio.h>
#include<string.h>
#include<string>
#include<math.h>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
struct note{int x,y;
}p[200010];
int a[200010];
int main()
{vector<int>v;int sum = 0,cnt = 0,n,m;a[cnt++] = 0;//a数组维护加速带的前缀和 scanf("%d %d",&n,&m);for(int i = 0 ; i < n ; i ++){scanf("%d %d",&p[i].x,&p[i].y);a[cnt] = a[cnt-1] + p[i].y - p[i].x;cnt++;if(i > 0){sum += p[i].x-p[i-1].y;v.push_back(sum);}//v向量用来保存加速带的前缀和
    }int ans = 0;for(int i = 0 ;i <= v.size(); i++){int  pos = lower_bound(v.begin(),v.end(),(i==0?0:v[i-1])+m)-v.begin();sum = a[pos+1]-a[i] + m;//加速带的长度+m ,即要更新的答案 ans = max(sum,ans);}printf("%d\n",ans);
}
/*
3 4
2 5
7 9
10 115 10
5 7
11 12
16 20
25 26
30 331 1000000000
1 1000000000
*/

转载于:https://www.cnblogs.com/Esquecer/p/9960996.html

Gym - 101911B Glider(前缀和+二分)相关推荐

  1. Gym - 101911B Glider【尺取】

    参考大佬的博客https://blog.csdn.net/mmk27_word/article/details/85063325?ops_request_misc=%257B%2522request% ...

  2. Gym - 101911B Glider 贪心

    A plane is flying at a constant height of hh meters above the ground surface. Let's consider that it ...

  3. Glider(前缀和+二分)

    题目链接:Glider Gym-101911B 解题分析:下落的高度一定,是h.在没有气流的地方每秒下落1:所以可以转化为经过无气流地带的时间总长为h. 那么很显然从一个有气流地带的开始,选择下落,那 ...

  4. Glider Gym - 101911B

    A plane is flying at a constant height of h meters above the ground surface. Let's consider that it ...

  5. poj3061尺取法/前缀和 二分(java)

    今天遇到这题因为以前没见到过,当时就是想着应该有着一个很简单的方法可以过但是奈何就是没思路.后来看了别人思路写了下来.学习了尺取法 poj3061 题目介绍: Description A sequen ...

  6. [Leedcode][JAVA][第209题][长度最小的子数组][滑动窗口][前缀和][二分查找][双指针]

    [问题描述][中等] 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组,并返回其长度.如果不存在符合条件的连续子数组,返回 0.示例: 输入: ...

  7. LeetCode 528. 按权重随机选择(前缀和+二分查找)

    文章目录 1. 题目 2. 解题 1. 题目 给定一个正整数数组 w ,其中 w[i] 代表下标 i 的权重(下标从 0 开始),请写一个函数 pickIndex ,它可以随机地获取下标 i,选取下标 ...

  8. LeetCode 497. 非重叠矩形中的随机点(前缀和+二分查找)

    文章目录 1. 题目 2. 解题 1. 题目 给定一个非重叠轴对齐矩形的列表 rects,写一个函数 pick 随机均匀地选取矩形覆盖的空间中的整数点. 提示: 整数点是具有整数坐标的点. 矩形周边上 ...

  9. 209 长度最小的子数组(前缀和+二分查找、滑动窗口)

    1. 问题描述: 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组,并返回其长度.如果不存在符合条件的子数组,返回 0. 示例: 输入:s = ...

最新文章

  1. 27岁华裔小伙一战成名!搞出美国新冠最准预测模型,一人干翻专业机构,彭博:Superstar...
  2. MaterialDesign动画
  3. java 获取sqlsession_获取Java的MyBatis框架项目中的SqlSession的方法
  4. swfupload 进度条 提示 中文乱码
  5. js中同时得到整数商及余数_js和vue实现时分秒倒计时的方法
  6. 设计代码说明什么是多态性?如何实现多态?(代码中要写注释解释)_狗屎一样的代码!快,重构我...
  7. Linux 内存管理之 SLUB分配器 (4):slub page大小计算方法
  8. I/O资源如何映射到内核虚拟空间
  9. 2020数据分析人才及CDA持证人行业报告
  10. PHP如何实现解析抖音短视频链接中的无水印视频
  11. 精密型工业级UHF超高频RFID读写器|读卡器JT7300的MODBUS协议说明
  12. 高通平台ITS:scene2_a/test_effects fail
  13. mysql 分位数 知乎_分位数的意义是什么?
  14. mysql 启动 错误1053:服务没有及时响应启动或者控制请求
  15. excel高级筛选怎么用_如何用excel在筛选状态下怎么复制粘贴?
  16. 微信公众平台开发之Java实现群发消息
  17. Java实现 LeetCode第30场双周赛 (题号5177,5445,5446,5447)
  18. 伟平在重阳节给深爱的父母鞠一躬
  19. CAJ文件转换成PDF文件:教你如何实现文件格式转换
  20. 适合初学者入门的Java基础视频

热门文章

  1. 各地实时公交查询软件GPS
  2. 《Spring源码深度解析 郝佳 第2版》SpringBoot体系分析、Starter的原理
  3. APP自动化测试+稳定性测试-Appetizer
  4. 了解计算机技术的课件,认识计算机第一信息技术PPT课件.ppt
  5. 测试显示器蓝光软件,iPhone11光谱测试揭露屏幕真相 蓝光伤害爆表
  6. 进程内服务器,进程外服务器,远程服务器
  7. 用简单的办法在c++程序做一个老板键(万能老板键和隐藏固定程序的老板键)
  8. 人工智能识别植物准确率高达80% 植物学家轻松了
  9. Nodejs+vue+Elementui英语单词学习网站express前端源码
  10. python读取文件特定内容_Python基础知识之文件的读取操作