Where Are My Genes
Time limit1sMemory limit128 MB
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 genes, where each gene is an integer from to . A reversal reverses the order of a block of consecutive genes. It is described by two indices with and reverses the genes at positions through . Applied to the genome , it produces .
For example, applying the reversal to [1, 2, 3, 4, 5, 6, 7] gives [1, 2, 6, 5, 4, 3, 7]. Applying the reversal 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 (), the number of genes in the genome. The genome initially holds the integers from to in increasing order.
- The second line contains an integer (), the number of reversals to apply.
- Each of the next lines contains two integers and () separated by a single space, describing one reversal.
- The next line contains an integer (), the number of gene queries.
- Each of the next 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 , which must not be processed.
Output
For each test case, print lines. The first line must contain the word Genome, then a single space, then the test case number (test cases are numbered starting from ). Each of the following lines must contain a single integer: the final position of the corresponding queried gene.