luogu#P4188. [USACO18JAN] Lifeguards S

    ID: 8215 远端评测题 1000ms 125MiB 尝试: 3 已通过: 0 难度: 5 上传者: 标签>2018线段树USACO枚举暴力概率论统计离散化

[USACO18JAN] Lifeguards S

题目描述

Farmer John has opened a swimming pool for his cows, figuring it will help them relax and produce more milk.

To ensure safety, he hires NN cows as lifeguards, each of which has a shift that covers some contiguous interval of time during the day. For simplicity, the pool is open from time t=0t=0 until time t=1,000,000,000t = 1,000,000,000 on a daily basis, so each shift can be described by two integers, giving the time at which a cow starts and ends her shift. For example, a lifeguard starting at time t=4t = 4 and ending at time t=7t = 7 covers three units of time (note that the endpoints are "points" in time).

Unfortunately, Farmer John hired 1 more lifeguard than he has the funds to support. Given that he must fire exactly one lifeguard, what is the maximum amount of time that can still be covered by the shifts of the remaining lifeguards? An interval of time is covered if at least one lifeguard is present.

输入格式

The first line of input contains NN (1N100,0001 \leq N \leq 100,000). Each of the next NN lines describes a lifeguard in terms of two integers in the range 01,000,000,0000 \ldots 1,000,000,000, giving the starting and ending point of a lifeguard's shift. All such endpoints are distinct. Shifts of different lifeguards might overlap.

输出格式

Please write a single number, giving the maximum amount of time that can still be covered if Farmer John fires 1 lifeguard.

题目大意

FJ 为他的奶牛们建造了一个游泳池,FJ 认为这将有助于他们放松身心以及生产更多牛奶。

为了确保奶牛们的安全,FJ 雇佣了 NN 头牛,作为泳池的救生员,每一个救生员在一天内都会有一定的事情,并且这些事情都会覆盖一天内的一段时间。为了简单起见,泳池从时间 t=0t=0 时开门,直到时间 t=109t=10^9 关门,所以每个事情都可以用两个整数来描述,给出奶牛救生员开始以及结束事情的时间。例如,一个救生员在时间 t=4t=4 时开始事情并且在时间 t=7t=7 时结束事情,那么这件事情就覆盖了 33 个单位时间。(注意:结束时间是“点”的时间)

不幸的是,FJ 多雇佣了一名的救生员,但他没有足够的资金来雇佣这些救生员。因此他必须解雇一名救生员,求可以覆盖剩余救生员的轮班时间的最大总量是多少?如果当时至少有一名救生员的事情已经开始,则这个时段被覆盖。

输入格式

输入的第一行包括一个整数 N (1N100000)N\ ( 1 \le N \le 100000)。接下来 NN 行中,每行告诉了我们一个救生员在 01090 \sim 10^9 范围内的开始以及结束时间。所有的结束时间都是不同的。不同的救生员的事情覆盖的时间可能会重叠。

输出格式

输出一行一个整数,如果 FJ 解雇一名救生员仍能覆盖的最大时间。

3
5 9
1 4
3 7
7