Specialization
Time limit4sMemory limit1024 MB
Given a layered tree where one vertex per level has two children, answer online queries for the largest common ancestor of two labeled vertices.
- Level
Hard8 of 10
- Topics
- Tree, Implementation
- Solved
- No attempts yet
문제
You are given a positive integer and a sequence of positive integers, such that .
The sequence defines a tree with vertices. The tree has levels with vertices each.

The tree defined by .
The -th level contains vertices . Vertex has two children. Every other vertex on that level has one child.
You must answer queries. Each query asks for the largest common ancestor of and , that is, the ancestor of both with the largest label.
Input
The first line contains three integers , , and (, ). They are the number of parameters, the number of queries, and a value used to compute the query labels.
The second line contains integers () that define the tree.
The next lines each contain two integers and (). The query labels are computed as follows.
Here is the answer to the -th query, and . The mod operator is the remainder of integer division.
If , then and , so every query is known from the input. If , each query depends on the previous answer, so the queries cannot be known in advance.
Output
Print lines. The -th line contains the largest common ancestor of and .
Hint
The figure shows the tree defined by .