Gadgets Factory

Time limit3sMemory limit256 MB

Summary
Given m sorted factories each producing one of n part types, find all coordinates t minimizing the sum over parts of squared distance to the nearest factory of that part, expressed as exact fractions.
Level

Hard8 of 10

Topics
Math, Binary search, Prefix sum, Implementation
Solved
No attempts yet

Problem

Mr. Smith is a very wealthy gadget enthusiast. When he realized that he cannot buy the gadgets he wants simply because they have not been produced yet, he decided to build his own gadget factory.

The factory will stand on a street called Silicon Road, which is lined with plants that manufacture the high-tech parts gadgets are made of. Silicon Road is perfectly straight and the plants sit right next to it, so we model the road as a number line and each plant as a point on it. We call each such plant a factory.

Producing a gadget requires nn different parts, and there are mm factories along the road, each producing exactly one kind of part. If Mr. Smith builds his gadget factory at coordinate tt, its cost is the sum, over all nn required parts, of the squared distance from tt to the nearest factory that produces that part.

Find every coordinate tt at which this cost is minimized.

Input

The first line contains two integers nn and mm (1≤n≤100001 \le n \le 10000; n≤m≤100000n \le m \le 100000).

Each of the next mm lines contains two integers xix_i and pip_i: the coordinate of the ii-th factory and the identifier of the part it produces (∣xi∣≤100000|x_i| \le 100000; xi≤xi+1x_i \le x_{i+1}; 1≤pi≤n1 \le p_i \le n).

Every required part is produced by at least one factory.

Output

Let f(t)f(t) be the sum, over all nn parts, of the squared distance from tt to the nearest factory producing that part. Every point at which ff attains its minimum is a rational number whose denominator divides nn.

On the first line print kk, the number of points at which f(t)f(t) is minimal. Then print those kk points in ascending order, one per line, each written exactly as a fraction p/q in lowest terms with q>0q > 0, or as the integer p when q=1q = 1.

Examples2

  1. Example 1

    Input
    3 5
    -1 3
    0 1
    2 3
    4 2
    5 2
    
    Expected output
    1
    2
    
  2. Example 2

    Input
    2 5
    1 1
    2 2
    3 1
    4 2
    5 1
    
    Expected output
    4
    3/2
    5/2
    7/2
    9/2