problems / 239

Suki and Biscuit Share the Garden

medium intervals sorting two-pointers sweep-line 1000 ms · 256 MB

Suki and Biscuit Share the Garden

Suki, the curly black dog with the grey beard, shares a garden with Biscuit, the terrier from next door. Each dog's family lets their dog out several times a day, and the family diary lists these garden windows in no particular order. The two families wrote their windows down carelessly, so one dog's windows may overlap, touch, or even repeat. Suki wants to know when she can play with Biscuit.

Time is measured in minutes from the start of the day and flows continuously. A window [l, r] means the dog is in the garden at every moment t with l ≤ t ≤ r (when l = r the dog only pops out for a single moment). A dog is in the garden at moment t if t lies in at least one of its windows.

A shared stretch is a maximal closed interval [a, b] of moments at which both dogs are in the garden: both are outside at every moment from a to b, and the interval cannot be extended in either direction. A shared stretch may be a single moment (a = b), and its length is b − a minutes.

Find all shared stretches, their total length, and the length of the longest one.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 2·10^5): the number of Suki's windows and of Biscuit's windows.

Each of the next n lines contains two integers l and r (0 ≤ l ≤ r ≤ 10^9): one of Suki's windows.

Each of the next m lines contains two integers l and r (0 ≤ l ≤ r ≤ 10^9): one of Biscuit's windows.

Output

On the first line print three integers: the number k of shared stretches, their total length, and the length of the longest one. If there are no shared stretches, print 0 0 0.

Then print k lines, each with two integers a b: the shared stretches in increasing order of a.

Notes

In the first example Suki's windows [1, 4] and [3, 7] overlap, so she is outside from minute 1 to minute 7, and her windows [10, 12] and [12, 15] touch, so she is also outside from 10 to 15. Biscuit is outside during [5, 11] and [15, 20]. Both dogs are outside during [5, 7], during [10, 11], and at the single moment 15, which gives three stretches of total length 2 + 1 + 0 = 3; the longest has length 2.

In the second example the dogs are never in the garden at the same moment: Biscuit goes out at minute 5 only after Suki came in at minute 4, and goes back in before Suki's next window starts.

Example 1

Input

4 2
3 7
12 15
1 4
10 12
15 20
5 11

Output

3 3 2
5 7
10 11
15 15

Example 2

Input

2 2
9 12
1 4
6 7
5 8

Output

0 0 0

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

Hint 1

The question gets much easier if each dog's garden time is described by a few disjoint stretches instead of a pile of overlapping windows. Careful: two of Suki's own windows overlapping is not a moment she shares with Biscuit.

Hint 2

Turn each dog's windows into a sorted list of disjoint stretches. Time is continuous here: should [1, 3] and [3, 5] stay separate?

Hint 3

Two sorted lists of disjoint stretches can be intersected in a single linear pass with two pointers. Think about when the current stretch of one dog can no longer meet anything in the other dog's list.

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

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