Painting Bob's Fence

easy intervals sorting arrays 1000 ms · 256 MB

Painting Bob's Fence

Alice is bored, and the fence running along her and Bob's shared yard is endless in both directions. Over the course of the day she picks up her paint roller n times and, each time, paints the stretch of fence from meter l up to (but not including) meter r. She doesn't plan ahead, so her stretches can overlap, land exactly inside one another, or sit right end-to-end with a previous stretch.

That evening Bob walks the fence and wants two numbers: how many meters of fence now have paint on them (a meter that got painted twice still counts once), and into how many separate painted pieces the paint has split up. Two stretches that merely touch — one ending exactly where the next begins — count as one continuous piece, since there's no bare wood between them.

Input

The first line contains an integer n (1 ≤ n ≤ 2·10^5), the number of stretches Alice paints.

Each of the next n lines contains two integers l and r (-10^18 ≤ l < r ≤ 10^18) describing one stretch: every point x with l ≤ x < r gets painted.

Output

Print two integers separated by a space: the total painted length, and the number of maximal painted pieces. Both fit in a 64-bit signed integer.

Notes

In the first example, the stretches [1, 5) and [3, 7) overlap and merge into one painted piece [1, 7) of length 6, while [10, 12) stays separate, for a total length of 8 across 2 pieces.

In the second example, [1, 3) and [3, 5) only touch at the point 3, but that's still enough to join them into a single piece [1, 5) of length 4.

Example 1

Input

3
1 5
10 12
3 7

Output

8 2

Example 2

Input

2
1 3
3 5

Output

4 1

Hints · reveal one at a time if you're stuck

Hint 1

Treat each stretch as an interval on the number line. If you line the stretches up by where they start, what can you say about the piece of paint you are currently building as you move to the next stretch?

Hint 2

Sweep the stretches in order of their left end, extending the current piece whenever the next stretch touches or overlaps it and closing it otherwise. Pay close attention to the touching case, and don't forget the piece still open at the end.

report an issue Prefer your own editor? sooks prep 61 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.