8187: Trapped in the Haybales (Bronze) 困在干草捆中

内存限制:128 MB 时间限制:1 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:2 通过:1

题目描述

农夫约翰收到了一批 N 个干草捆,并把它们放在了通往他牛棚的路上的不同位置。

但是他忘记了奶牛贝茜正在路边吃草,现在她可能被困在干草捆中了!

道路可视为一个一维数轴。

每个干草捆 j 的尺寸为 Sj,位置为 Pj

贝茜可以沿着道路自由移动,甚至可以到达干草捆所在的位置,但是她不能越过该位置。

但有一种例外,如果她沿一个方向奔跑了 D 个单位的距离,她就会积攒足够的速度突破并破坏掉任何尺寸严格小于 D 的干草捆。

当然,这样做之后,她就有了更大的空间用来奔跑、突破并消除更多的干草捆。

如果贝茜最终可以突破最左边或最右边的干草捆,她就可以脱困重获自由。

这 N 个干草捆之间共有 N−1 个区间,这些区间中,贝茜无法从中脱困的所有区间的长度之和是多少。

输入格式

第一行包含整数 N

接下来 N 行,每行描述一个干草捆,包含两个整数(范围 1∼109),描述其尺寸和位置。

所有位置各不相同。

输出格式

输出贝茜无法从中脱困的所有区间的长度之和。

数据范围

1≤N≤4000

输入样例:

5
8 1
1 4
8 8
7 15
4 20

输出样例:

14


Farmer John has received a shipment of N large hay bales (1≤N≤4000) and placed them at various locations along the road leading to his barn. Unfortunately, he completely forgets that Bessie the cow is out grazing along the road, and she may now be trapped within the bales!

Each bale j has a size Sj and a distinct position Pj giving its location along the one-dimensional road. Bessie the cow starts at some location where there is no hay bale, and can move around freely along the road, even up to the position at which a bale is located, but she cannot cross through this position. As an exception, if she runs in the same direction for D units of distance, she builds up enough speed to break through and permanently eliminate any hay bale of size strictly less than D. Of course, after doing this, she might open up more space to allow her to make a run at other hay bales, eliminating them as well.

Bessie can escape to freedom if she can eventually break through either the leftmost or rightmost hay bale. Please compute the total area of the road consisting of real-valued starting positions from which Bessie cannot escape. For example, if Bessie cannot escape if she starts between hay bales at positions 1 and 5, then these encompass an area of size 4 from which she cannot escape.

INPUT FORMAT (file trapped.in):

The first line of input contains N. Each of the next N lines describes a bale, and contains two integers giving its size and position, each in the range 1…109.

OUTPUT FORMAT (file trapped.out):

Print a single integer, giving the area of the road from which Bessie cannot escape.

SAMPLE INPUT:

5
8 1
1 4
8 8
7 15
4 20

SAMPLE OUTPUT:

14

[Problem credits: Brian Dean, 2015]

输入样例 复制


输出样例 复制