Where Are My Genes

Time limit1sMemory limit128 MB

Summary
Apply a sequence of reversals to the identity genome and report the final position of each queried gene.
Level

Medium4 of 10

Topics
Simulation, Array, Implementation
Solved
No attempts yet

Problem

Scientists study how one species evolved into another by tracing how an ancestor's genome changed into a descendant's. Closely related species share many genes, and a good way to compare them is to see how the shared genes changed position.

One of the most common mutations that reorder a genome's genes is the reversal. Model a genome as a sequence of NN genes, where each gene is an integer from 11 to NN. A reversal reverses the order of a block of consecutive genes. It is described by two indices (i,j)(i, j) with 1≤i≤j≤N1 \le i \le j \le N and reverses the genes at positions ii through jj. Applied to the genome [g1,…,gi−1,gi,gi+1,…,gj−1,gj,gj+1,…,gN][g_1, \dots, g_{i-1}, g_i, g_{i+1}, \dots, g_{j-1}, g_j, g_{j+1}, \dots, g_N], it produces [g1,…,gi−1,gj,gj−1,…,gi+1,gi,gj+1,…,gN][g_1, \dots, g_{i-1}, g_j, g_{j-1}, \dots, g_{i+1}, g_i, g_{j+1}, \dots, g_N].

For example, applying the reversal (3,6)(3, 6) to [1, 2, 3, 4, 5, 6, 7] gives [1, 2, 6, 5, 4, 3, 7]. Applying the reversal (1,3)(1, 3) after that gives [6, 2, 1, 5, 4, 3, 7].

A scientist wants to apply a series of reversals to a genome and then query the final position of several genes. Answer those queries.

Input

The input contains several test cases.

  • The first line of a test case contains an integer NN (1≤N≤500001 \le N \le 50000), the number of genes in the genome. The genome initially holds the integers from 11 to NN in increasing order.
  • The second line contains an integer RR (0≤R≤10000 \le R \le 1000), the number of reversals to apply.
  • Each of the next RR lines contains two integers ii and jj (1≤i≤j≤N1 \le i \le j \le N) separated by a single space, describing one reversal.
  • The next line contains an integer QQ (0≤Q≤1000 \le Q \le 100), the number of gene queries.
  • Each of the next QQ lines contains one integer, a gene whose final position you must report.

The reversals are applied in the given order. The end of the input is a line containing N=0N = 0, which must not be processed.

Output

For each test case, print Q+1Q + 1 lines. The first line must contain the word Genome, then a single space, then the test case number (test cases are numbered starting from 11). Each of the following QQ lines must contain a single integer: the final position of the corresponding queried gene.

Examples3

  1. Example 1

    Input
    9
    1
    3 6
    4
    1
    3
    5
    1
    5
    2
    1 2
    1 5
    2
    5
    2
    0
    
    Expected output
    Genome 1
    1
    6
    4
    1
    Genome 2
    1
    5
    
  2. Example 2

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

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