#P7172. [COCI 2020/2021 #3] Specijacija

    ID: 6212 远端评测题 4000ms 1024MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2020最近公共祖先,LCA主席树COCI

[COCI 2020/2021 #3] Specijacija

Description

Given a positive integer nn and a sequence of positive integers a1,a2,,ana_1, a_2, \cdots, a_n satisfying i(i1)2<aii(i+1)2\frac{i(i-1)}{2} \lt a_i \le \frac{i(i+1)}{2}.

This sequence parameterizes a tree with (n+1)(n+2)2\frac{(n+1)(n+2)}{2} nodes. The tree has n+1n+1 levels, where level 1,2,,n+11,2,\cdots,n+1 contains 1,2,,n+11,2,\cdots,n+1 nodes respectively, as shown in the figure:

It is parameterized by a=(1,2,6)a=(1,2,6).

Level ii contains nodes i(i1)2+1,,i(i+1)2\frac{i(i-1)}{2}+1, \cdots, \frac{i(i+1)}{2}. Node aia_i has two children, while all other nodes on the same level have only one child. In each level, children are assigned to nodes in increasing order of node indices, always choosing the smallest-index node in the next level that has not been assigned yet.

Answer qq queries. For each query, find the lowest common ancestor of xx and yy, i.e., the node with the largest index that is an ancestor of both xx and yy.

Input Format

The first line contains three integers n,q,tn, q, t, representing the number of parameters, the number of queries, and the parameter used to determine node indices.

The second line contains a sequence aa of length nn (for each aia_i, i(i1)2<aii(i+1)2\frac{i(i-1)}{2} \lt a_i \le \frac{i(i+1)}{2}).

Each of the next 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}$), used to determine the node indices involved in the ii-th query.

Let ziz_i be the answer to the ii-th query, and define z0=0z_0=0. The node indices involved in the ii-th query are:

$$x_i=[(\tilde x_i-1+t \times z_{i-1}) \bmod \frac{(n+1)(n+2)}{2}]+1$$$$y_i=[(\tilde y_i-1+t \times z_{i-1}) \bmod \frac{(n+1)(n+2)}{2}]+1$$

Note: When t=0t=0, we have xi=x~ix_i=\tilde x_i and yi=y~iy_i=\tilde y_i. When t=1t=1, the node indices involved in the query should be determined using previous answers.

Output Format

Output qq lines in total. On the ii-th line, output the lowest common ancestor of xix_i and yiy_i.

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

Hint

[Sample Explanation #1 / #2]

The shapes of the trees represented by the two samples are already shown in the figure in the problem description.

In the second sample, the node indices involved in each query are:

x1=7x_1=7, y1=10y_1=10;
x2=9x_2=9, y2=6y_2=6;
x3=2x_3=2, y3=8y_3=8;
x4=1x_4=1, y4=2y_4=2;
x5=3x_5=3, y5=4y_5=4.

[Constraints]

Subtask Points Constraints and Notes
11 1010 q=1,t=0q=1, t=0
22 n1000,t=0n \le 1000, t=0
33 3030 t=0t=0
44 6060 t=1t=1

For 100%100\% of the testdata, 1n,q2×1051 \le n,q \le 2 \times 10^5, and t{0,1}t \in \{0,1\}.

[Notes]

The scoring for this problem follows the original COCI problem, with a full score of 110110.

Translated from COCI2020-2021 CONTEST #4 T5 Specijacija.

Translated by ChatGPT 5