Under construction

Still being built. Things may change

Specialization

Time limit4sMemory limit1024 MB

Summary
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 nn and a sequence a1,a2,…,ana_1, a_2, \dots, a_n of positive integers, such that i(i−1)2<ai≤i(i+1)2\frac{i(i-1)}{2} < a_i \le \frac{i(i+1)}{2}.

The sequence defines a tree with (n+1)(n+2)2\frac{(n+1)(n+2)}{2} vertices. The tree has n+1n+1 levels with 1,2,…,n+11, 2, \dots, n+1 vertices each.

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

The ii-th level contains vertices i(i−1)2+1,…,i(i+1)2\frac{i(i-1)}{2} + 1, \dots, \frac{i(i+1)}{2}. Vertex aia_i has two children. Every other vertex on that level has one child.

You must answer qq queries. Each query asks for the largest common ancestor of xx and yy, that is, the ancestor of both with the largest label.

Input

The first line contains three integers nn, qq, and tt (1≤n,q≤200 0001 \le n, q \le 200\,000, t∈{0,1}t \in \{0, 1\}). They are the number of parameters, the number of queries, and a value used to compute the query labels.

The second line contains nn integers aia_i (i(i−1)2≤ai≤i(i+1)2\frac{i(i-1)}{2} \le a_i \le \frac{i(i+1)}{2}) that define the tree.

The next qq lines each contain two integers x~i\tilde{x}_i and y~i\tilde{y}_i (1≤x~i,y~i≤(n+1)(n+2)21 \le \tilde{x}_i, \tilde{y}_i \le \frac{(n+1)(n+2)}{2}). The query labels are computed as follows.

xi=((x~i−1+t⋅zi−1) mod (n+1)(n+2)2)+1x_i = \left(\left(\tilde{x}_i - 1 + t \cdot z_{i-1}\right) \bmod \frac{(n+1)(n+2)}{2}\right) + 1

yi=((y~i−1+t⋅zi−1) mod (n+1)(n+2)2)+1y_i = \left(\left(\tilde{y}_i - 1 + t \cdot z_{i-1}\right) \bmod \frac{(n+1)(n+2)}{2}\right) + 1

Here ziz_i is the answer to the ii-th query, and z0=0z_0 = 0. The mod operator is the remainder of integer division.

If t=0t = 0, then xi=x~ix_i = \tilde{x}_i and yi=y~iy_i = \tilde{y}_i, so every query is known from the input. If t=1t = 1, each query depends on the previous answer, so the queries cannot be known in advance.

Output

Print qq lines. The ii-th line contains the largest common ancestor of xix_i and yiy_i.

Hint

The figure shows the tree defined by a=(1,2,6)a = (1, 2, 6).

Examples2

  1. Example 1

    Input
    3 5 0
    1 2 6
    7 10
    8 5
    6 2
    9 10
    2 3
    
    Expected output
    1
    5
    1
    6
    1
    
  2. Example 2

    Input
    3 5 1
    1 2 6
    7 10
    8 5
    6 2
    9 10
    2 3
    
    Expected output
    1
    6
    2
    1
    1