PivotOJ

Specijacija

시간 제한: 4000ms메모리 제한: 1024MB출처: COCI 2020-2021BOJ 20511

문제

You are given a positive integer nn and a sequence a1,a2,,ana_1, a_2, \dots , a_n of positive integers, such that \frac{i(i&minus;1)}{2} < a_i \le \frac{i(i+1)}{2}.

The sequence parameterizes a tree with (n+1)(n+2)2\frac{(n+1)(n+2)}{2} vertices, consisting of n+1n + 1 levels with 1,2,,n+11, 2, \dots , n + 1 vertices, in the following way:

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

The ii-th level contains vertices \frac{i(i&minus;1)}{2} + 1, \dots , \frac{i(i+1)}{2}. The vertex aia_i has two children, and the rest of the vertices on the level have one child each.

We want to answer qq queries of the form “what is the largest common ancestor of xx and yy”, i.e. the vertex with the largest label which is an ancestor of both xx and yy.

입력

The first line contains integers nn, qq and tt (1n,q200000,t{0,1}1 \le n, q \le 200 000, t \in \{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 nn integers aia_i (\frac{i(i&minus;1)}{2} \le a_i \le \frac{i(i+1)}{2}) which parameterize the tree.

The ii-th of the following qq lines contains two integers x~i\tilde{x}_i and y~i\tilde{y}_i (1 &le; \tilde{x}_i, \tilde{y}_i &le; \frac{(n+1)(n+2)}{2}) which will be used to determine the labels of vertices in the queries.

Let ziz_i be the answer to the ii-th query, and let z0=0z_0 = 0. The labels in the ii-th query xix_i and yiy_i are:

xi=((x~i1+tzi1)mod(n+1)(n+2)2)+1,x_i = \left(\left(\tilde{x}_i - 1 + t \cdot z_{i-1}\right) \mod \frac{(n+1)(n+2)}{2}\right) + 1 \text{,}

yi=((y~i1+tzi1)mod(n+1)(n+2)2)+1,y_i = \left(\left(\tilde{y}_i - 1 + t \cdot z_{i-1}\right) \mod \frac{(n+1)(n+2)}{2}\right) + 1 \text{,}

where mod\text{mod} is the remainder of integer divison.

Remark: Note that if t=0t = 0, it holds xi=x~ix_i = \tilde{x}_i and yi=y~iy_i = \tilde{y}_i, so all queries are known from input. If t=1t = 1, the queries are not known in advance, but are determined using answers to previous queries.

출력

Output qq lines. In the ii-th line, output the largest common ancestor of xix_i and yiy_i.

힌트

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: x1=7,y1=10,x2=9,y2=6,x3=2,y3=8,x4=1,y4=2,x5=3,y5=4x_1 = 7, y_1 = 10, \\ x_2 = 9, y_2 = 6,\\ x_3 = 2, y_3 = 8,\\ x_4 = 1, y_4 = 2,\\ x_5 = 3, y_5 = 4.

예제

예제 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
코드를 제출하려면 로그인하세요.