Specijacija
문제
You are given a positive integer and a sequence of positive integers, such that \frac{i(i−1)}{2} < a_i \le \frac{i(i+1)}{2}.
The sequence parameterizes a tree with vertices, consisting of levels with vertices, in the following way:
The tree parameterized by .
The -th level contains vertices \frac{i(i−1)}{2} + 1, \dots , \frac{i(i+1)}{2}. The vertex has two children, and the rest of the vertices on the level have one child each.
We want to answer queries of the form “what is the largest common ancestor of and ”, i.e. the vertex with the largest label which is an ancestor of both and .
입력
The first line contains integers , and (), the number of parameters, the number of queries, and a value which will be used to determine the labels of vertices in the queries.
The second line contains a sequence of integers (\frac{i(i−1)}{2} \le a_i \le \frac{i(i+1)}{2}) which parameterize the tree.
The -th of the following lines contains two integers and (1 ≤ \tilde{x}_i, \tilde{y}_i ≤ \frac{(n+1)(n+2)}{2}) which will be used to determine the labels of vertices in the queries.
Let be the answer to the -th query, and let . The labels in the -th query and are:
where is the remainder of integer divison.
Remark: Note that if , it holds and , so all queries are known from input. If , the queries are not known in advance, but are determined using answers to previous queries.
출력
Output lines. In the -th line, output the largest common ancestor of and .
힌트
Clarification of the examples: The tree from both examples is shown on the figure in the statement. Labels of verticies in queries in the second example are: .
예제
예제 1
3 5 0 1 2 6 7 10 8 5 6 2 9 10 2 3
1 5 1 6 1
예제 2
3 5 1 1 2 6 7 10 8 5 6 2 9 10 2 3
1 6 2 1 1