Avoid Alternating

 You have a grid with 

rows and columns in which

cells are black and the rest are white.

Let

represent the cell in the -th row from the top and the

-th column from the left.

If you are currently at cell

, you can:

  • move one cell down to
  • move one cell right to

    The cost of a path is defined as the number of times you move between two cells that have different colours.

    You are given

    queries. In the -th query, you are initially at and want to reach

    with the minimum cost.

    For each query, before you begin, you can perform the following operation any number of times (possibly zero):

    • Choose a column and flip the colour of all cells in this column (i.e. change the colour of all the black cells to white and the colour of all the white cells to black in this column)

    For each query, what is the minimum cost of a path from

    to

    after performing this operation any number of times (possibly zero)?

    Note: The operations applied in different queries are independent of each other.

    Input Format

    • The first line of the input contains a single integer
    - the number of test cases. The description of
  • test cases follows.
  • The first line of each test case contains three integers
  • , , and
  • - the number of rows, the number of columns, the number of black cells and the number of queries respectively.
  • The following
  • lines each contain two integers and - indicating that the -th black cell is at
  • .
  • The following
  • lines each contain two integers and - the destination cell of the
    • -th query.

    Output Format

    For each test case, print a single line containing

    space separated integers ― the answer for each of the

    queries.

    Constraints

  • ,
  • ,
  • the sum of
  • over all test cases does not exceed
  • the sum of
  • over all test cases does not exceed
  • the sum of
  • over all test cases does not exceed
  • the sum of
  • over all test cases does not exceed

    Sample Input 1

    5
    1 1 1 1
    1 1
    1 1
    3 1 2 2
    1 1
    3 1
    2 1
    3 1
    3 4 3 3
    1 2
    2 1
    1 4
    1 4
    3 4
    2 1
    4 3 5 2
    2 1
    2 2
    2 3
    3 2
    4 2
    4 1
    4 2
    6 3 5 2
    2 1
    3 2
    3 3
    5 2
    5 3
    3 3
    6 3
    

    Sample Output 1

    0 
    1 2 
    0 0 1 
    2 1 
    1 2 
    

    Explanation

    Test case 1:

    • Query 1: We are already at
    , so the minimum cost is
    • .

    Test case 2:

    • Initial Grid:

    • Query 1: The only sequence of possible moves is

    . So the total cost is
  • .

  • Query 2: The only sequence of possible moves is

  • . So the cost is
    • .

    It can be shown that no better answer is possible by performing any column flipping operations.

    Test case 3:

    • Initial Grid:

    • Query 1: One optimal solution is to flip columns
    and
    • to obtain the following grid.

    Then you can perform the following moves for a cost of

    -

    • Query 2: One optimal solution is to flip columns
    • .

    Then you can perform the following moves for a cost of

    -

    • Query 3: One optimal solution is to flip no columns and then perform the following moves for a cost of
    - for a total cost of
    • .

    Test case 4:

    • Initial Grid:

    • Query 1: One optimal solution is to flip no columns and then perform the following moves for a cost of

    -
  • Query 2: One optimal solution is to flip no columns and then perform the following moves for a cost of

  • -
    Previous
    Next Post »