This page is still under construction.

Parts of this page are still being built. What you see may change.

Plotter

Time limit5sMemory limit128 MB

Summary
Given the recursively defined order-n bytecurve and m integer points, report how many times and at which seconds the pen visits each point.
Level

Hard8 of 10

Topics
Recursion, Divide and conquer, Implementation, Math
Solved
No attempts yet

Problem

To test his newly bought plotter, Byteasar decides to draw a few bytecurves.

A bytecurve of order nn consists of 2n2^n segments, each of length 2\sqrt{2}. The very first segment joins the points (0,0)(0, 0) and (1,1)(1, 1). The bytecurve of order nn is described by a word LnL_n of length 2n−12^n - 1 over the two-letter alphabet {L,R}\{L, R\}. The ii-th letter tells the pen, right after the ii-th segment has been drawn, to turn by 90°90° and continue at a right angle either to the left (letter LL) or to the right (letter RR) before drawing the next segment.

L1L_1 is the single letter LL (one left turn), and L2=LLRL_2 = LLR (two left turns followed by one right turn). In general, LnL_n is built from Ln−1L_{n-1} like this: write the letters of Ln−1L_{n-1} separated by single spaces and add one extra space before the first letter and one after the last, then fill the newly created gaps from left to right with the alternating letters L,R,L,R,…L, R, L, R, \dots starting with LL. For example,

L2=LLR  ⟶  _ L _ L _ R _  ⟶  LLRLLRR=L3,L_2 = LLR \;\longrightarrow\; \_\,L\,\_\,L\,\_\,R\,\_ \;\longrightarrow\; LLRLLRR = L_3,

and in the same way L4=LLRLLRRLLLRRLRRL_4 = LLRLLRRLLLRRLRR (drawn in the figure below).

Drawing one segment takes exactly one second, and the pen starts at (0,0)(0, 0) at time 00. While the plotter works, Byteasar wonders: for a given point (x,y)(x, y), at which moments is the pen located there? For example, on the order-44 bytecurve above the pen is at (−3,−1)(-3, -1) after 77 seconds and again after 1111 seconds. Answer Byteasar's question.

Input

The first line contains two integers nn and mm (1≤n,m≤20001 \le n, m \le 2000): the curve is LnL_n and there are mm query points. Each of the next mm lines contains two integers xix_i and yiy_i (−109≤xi,yi≤109-10^9 \le x_i, y_i \le 10^9), the coordinates of the ii-th query point. A query point need not lie on the curve, and no point appears twice in the input.

Output

Print mm lines, one per query in order. For the ii-th query print a nonnegative integer kik_i, the number of times the pen is at (xi,yi)(x_i, y_i) while the order-nn curve is drawn (the starting position at time 00 counts as a visit), followed by those kik_i visit times in increasing order, in seconds since drawing began. Separate all numbers on a line by single spaces, with no leading or trailing space.

Hint

If you enjoyed this problem, try the harder variant that asks the same question under tighter limits.

Examples4

  1. Example 1

    Input
    4 3
    -3 -1
    1 1
    -1 0
    
    Expected output
    2 7 11
    1 1
    0
    
  2. Example 2

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

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

    Input
    6 4
    0 0
    1 1
    0 2
    -2 -4
    
    Expected output
    1 0
    1 1
    1 2
    2 14 22