Advanced Causal Measurements (ACM)

Time limit1sMemory limit128 MB

Summary
Given n observed events and m causes, place the m causes so all events are causally reachable and the earliest cause time is maximized.
Level

Hard8 of 10

Topics
Binary search, Greedy, Geometry, Sorting
Solved
No attempts yet

Problem

Causality is a very important concept in theoretical physics. The basic elements in a discussion of causality are events. An event ee is described by its time of occurrence tt and its location xx, and we write e=(t,x)e = (t, x). For our concerns, all events happen in a one-dimensional geometric space, so a location is a single real number xx (a coordinate on the xx-axis). Theoretical physicists like to set the speed of light to 11, so that time and space share the same units.

One event e1=(t1,x1)e_1 = (t_1, x_1) is a possible cause of a second event e2=(t2,x2)e_2 = (t_2, x_2) if a signal emitted at e1e_1 could reach e2e_2. A signal cannot travel faster than light, so this condition can be written as

e1 is a possible cause of e2  ⟺  t2≥t1+∣x2−x1∣.e_1 \text{ is a possible cause of } e_2 \iff t_2 \ge t_1 + |x_2 - x_1|.

Thus an event at (−1,1)(-1, 1) could cause events at (0,0)(0, 0), (1,2)(1, 2), and (1,3)(1, 3), but could not have caused events at (1,4)(1, 4) or (−2,1)(-2, 1). Note that a single event can cause several others.

Light-cone illustration of the first case

Scientists have observed several unusual events in this one-dimensional universe. From current theory they know how many causes were responsible for these observations, but they know nothing about the times and locations of those causes. There are exactly mm causes, and every observed event must have at least one of these mm causes as a possible cause.

Write a program that determines the latest time at which the earliest cause could have occurred — that is, the largest integer TT such that, no matter how the mm causes are placed, at least one cause occurs at time TT or earlier. Equivalently, place the mm causes so that they are possible causes of all observed events while making the earliest cause time as large as possible, and report that time.

All observed events have integer coordinates with −1000000≤t,x≤1000000-1000000 \le t, x \le 1000000.

Input

The first line contains the number of test cases. Each test case begins with a line containing the number of events nn and the number of causes mm (1≤n,m≤1000001 \le n, m \le 100000). The next nn lines each contain the coordinates tt and xx of one event.

Output

For each test case, print a single line Case k: a, where kk is the test case number (starting from 11) and aa is the latest time at which the earliest cause could have occurred. This value is always an integer, because our time units are not divisible.

Examples1

  1. Example 1

    Input
    4
    4 1
    1 -1
    1 3
    1 4
    2 6
    4 2
    1 -1
    1 3
    1 4
    2 6
    4 3
    1 -1
    1 3
    1 4
    2 6
    4 4
    1 -1
    1 3
    1 4
    2 6
    
    Expected output
    Case 1: -2
    Case 2: 0
    Case 3: 0
    Case 4: 1