Under construction

Still being built. Things may change

Physics

Time limit1sMemory limit1024 MB

Summary
Given the max and min of two piecewise linear velocity graphs over time T, restore both graphs, each covering the same distance.
Level

Hard8 of 10

Topics
Implementation, Two pointers, Brute force
Solved
No attempts yet

Statement

Android Vasya reads the book Amusing physics for the smallest children. Recently he read a chapter about uniformly accelerated motion and decided to set up some experiments. For this purpose, Vasya made a mechanical turtle that can be given a different acceleration by remote control.

Vasya launched his turtle into the lobby and changed its acceleration several times. After plotting the piecewise linear velocity function v1(t)v_1(t), he repeated the experiment. This time he got a piecewise linear continuous function v2(t)v_2(t) and plotted its graph too.

Before the third try, Vasya realized that plotting v1(t)v_1(t) and v2(t)v_2(t) on the same graph was a mistake. Now it is unclear which segments belong to which function. Help Vasya restore the graphs. Both experiments lasted the same time TT, and in both runs the turtle covered the same distance, which is the length of the lobby.

Input

The first line contains an integer TT, the duration of each experiment (2≤T≤1062 \le T \le 10^6). Then the description of the functions h(t)=max⁡(v1(t),v2(t))h(t) = \max(v_1(t), v_2(t)) and g(t)=min⁡(v1(t),v2(t))g(t) = \min(v_1(t), v_2(t)) follows. The second line contains an integer nn, the number of kink points of h(t)h(t). The next nn lines contain pairs of integers tit_i and h(ti)h(t_i), the kink points of h(t)h(t) (0=t1<t2<⋯<tn−1<tn=T0 = t_1 < t_2 < \dots < t_{n-1} < t_n = T, 0≤h(ti)≤1060 \le h(t_i) \le 10^6). No three consecutive kink points lie on the same straight line. The following lines describe g(t)g(t) in the same format. For any x∈[0;T]x \in [0; T], g(t)≤h(t)g(t) \le h(t), and g(t)=h(t)g(t) = h(t) holds at no more than 30 points (in particular, the graphs share no common segments). Each function has between 2 and 100000 kink points.

Output

Output the functions v1(t)v_1(t) and v2(t)v_2(t) in the same format as h(t)h(t) and g(t)g(t). No three consecutive kink points may lie on the same straight line. All numbers in the output must be integers. If there are several solutions, print any of them. At least one solution is guaranteed to exist.

Examples1

  1. Example 1

    Input
    6
    6
    0 2
    1 1
    2 2
    3 1
    4 2
    6 0
    6
    0 0
    1 1
    2 0
    3 1
    4 0
    6 0
    
    Expected output
    5
    0 2
    1 1
    2 2
    4 0
    6 0
    5
    0 0
    1 1
    2 0
    4 2
    6 0