COCI '20 Contest 3 #5 Specijacija

View as PDF

Submit solution


Points: 25 (partial)
Time limit: 4.0s
Memory limit: 1G

Problem types

You are given a positive integer n and a sequence a1,a2,,an of positive integers, such that i(i1)2<aii(i+1)2.

The sequence parameterizes a tree with (n+1)(n+2)2 vertices, consisting of n+1 levels with 1,2,,n+1 vertices, in the following way:

The tree parameterized by a=(1,2,6).

The i-th level contains vertices i(i1)2+1,,i(i+1)2. The vertex ai has two children, and the rest of the vertices on the level have one child each.

We want to answer q queries of the form "what is the largest common ancestor of x and y", i.e. the vertex with the largest label which is an ancestor of both x and y.

Input

The first line contains integers n,q and t (1n,q200000,t{0,1}), 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 n integers ai (i(i1)2<aii(i+1)2) which parameterize the tree.

The i-th of the following q lines contains two integers xi~ and yi~ (1xi~,yi~(n+1)(n+2)2) which will be used to determine the labels of vertices in the queries.

Let zi be the answer to the i-th query, and let z0=0. The labels in the i-th query xi and yi are:

xi=((xi~1+tzi1)mod(n+1)(n+2)2)+1,yi=((yi~1+tzi1)mod(n+1)(n+2)2)+1,

where mod is the remainder of integer division.

Remark: Note that if t=0, it holds xi=xi~ and yi=yi~, so all queries are known from input. If t=1, the queries are not known in advance, but are determined using answers to previous queries.

Output

Output q lines. In the i-th line, output the largest common ancestor of xi and yi.

Scoring

Subtask Score Constraints
1 10 q=1,t=0
2 10 n1000,t=0
3 30 t=0
4 60 t=1

Sample Input 1

Copy
3 5 0
1 2 6
7 10
8 5
6 2
9 10
2 3

Sample Output 1

Copy
1
5
1
6
1

Explanation for Sample Output 1

The tree from Sample Input 1 is shown in the figure in the statement.

Sample Input 2

Copy
3 5 1
1 2 6
7 10
8 5
6 2
9 10
2 3

Sample Output 2

Copy
1
6
2
1
1

Explanation for Sample Output 2

The tree from Sample Input 2 is shown in the figure in the statement.

Labels of vertices in queries in the second example are:

x1=7,y1=10,
x2=9,y2=6,
x3=2,y3=8,
x4=1,y4=2,
x5=3,y5=4.


Comments

There are no comments at the moment.