#P3522. Longest Non‐Decreasing Temperature Period

    ID: 16776 Type: Default 1000ms 256MiB

Longest Non‐Decreasing Temperature Period

Longest Non‐Decreasing Temperature Period

The Byteotian Institute of Meteorology (BIM) has measured the air temperature for n consecutive days. Due to a printing mishap, the exact temperatures were lost, but for the i-th day the recorded measurement is an interval [li, ri] in which the actual temperature lies. It is known that the measurement error is such that the actual temperature can be arbitrarily chosen within the given interval.

Your task is to help BIM whitewash its negligence by determining the longest contiguous period (subarray) in which it is possible that the actual temperatures were non-decreasing – that is, you can assign for each day a temperature from its interval so that for each day (except the first) the temperature is at least that of the day before.

Input Constraints: 1 ≤ n ≤ 1,000,000, and for each day, li and ri are integers satisfying li ≤ ri.

Mathematical Formulation:

Find the maximum length of an index interval [s, t] such that there exist choices ts, ts+1, …, tt with

[ t_i \in [l_i, r_i] \quad \text{for } i=s,s+1,\ldots,t, ]

and

[ t_s \le t_{s+1} \le \cdots \le t_t. ]

inputFormat

The first line contains a single integer n (1 ≤ n ≤ 1,000,000) representing the number of days. Each of the following n lines contains two integers li and ri (with li ≤ ri), which represent the range of possible temperatures on day i.

Example:

5
3 5
4 6
5 5
4 8
8 9

outputFormat

Output a single integer representing the length of the longest contiguous period in which the temperature could have been non-decreasing.

For the above sample, the output should be:

5

sample

5
3 5
4 6
5 5
4 8
8 9
5