#P13508. [OOI 2024] Burenka and Pether

    ID: 15390 远端评测题 3000ms 1024MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>线段树倍增2024分块Moscow Olympiad

[OOI 2024] Burenka and Pether

题目描述

Once upon a time, the princess of Burlyandia, Burenka, decided to please her friend ReLu. Knowing that ReLu shares her interest in cryptocurrency, Burenka decided to create her own blockchain cryptocurrency called Pether\texttt{Pether}.

After taking courses and training from an expert coach in personal growth in cybersecurity, Burenka decided that the currency Pether\texttt{Pether} should be protected in the best possible way. As a result, due to incredibly complex and convoluted restrictions, not all users can exchange Pether\texttt{Pether} with each other.

The structure of the Pether\texttt{Pether} blockchain currency is indeed complex and convoluted. All users are numbered with integers from 11 to nn. Each user is assigned a unique\textbf{unique} identifier aia_{i}. Also, the currency has a security parameter dd.

User ii can directly transfer currency to user jj only if i<ji < j and ai<aja_i < a_j. But that's not enough! Direct currency transfer between users occurs through a chain of transactions involving some number of intermediate users. During each transaction, the number of each subsequent intermediate user (including the last user jj) must increase, but not by more than dd. Also, all intermediate users except ii and jj must have an identifier strictly less than aia_i.

More formally, user ii can directly transfer cryptocurrency to user jj if the following conditions are met:

  • It is satisfied that i<ji < j
  • It is satisfied that ai<aja_{i} < a_{j}
  • There exists a sequence of intermediate users xx of length kk such that:
    • i=x1<x2<<xk1<xk=ji = x_1 < x_2 < \ldots < x_{k-1} < x_{k} = j
    • For all 1tk11 \le t \le k - 1, it is true that xt+1xtdx_{t + 1} - x_{t} \le d
    • For all 2tk12 \le t \le k - 1, it is true that axt<aia_{x_t} < a_{i}

Burenka asks you, her acquaintance programmer, to understand this system and find out for some pairs of users how to transfer Pether\texttt{Pether} to each other.

You need to answer qq queries. In each query, you need to determine whether there is a sequence of direct currency transfers (possibly through intermediate users) that allows transferring Pether\texttt{Pether} from user uiu_i to user viv_i. In some queries, it is also necessary to minimize the number of direct currency transfers in the process of sending currency from uiu_i to viv_i. Please note that it is not necessary to minimize the number of transactions during each direct transfer.

输入格式

The first line contains three integers nn, dd, and gg (1n,d300000,0g12)(1 \leq n, d \leq 300\,000, 0 \leq g \leq 12) --- the number of users, the security parameter, and the test group number.

The second line contains nn integers a1, a2, , ana_1,\ a_2,\ \ldots,\ a_n (1ain)(1 \leq a_{i} \leq n) --- user identifiers. It is guaranteed that all numbers aia_i are distinct\textbf{distinct}.

The third line contains a single integer qq (1q300000)(1 \leq q \leq 300\,000) --- the number of queries.

The next qq lines contain three integers each ti, ui, vit_{i},\ u_{i},\ v_{i} (ti{1,2},1ui<vin)(t_{i}\in \{1, 2\}, 1 \leq u_{i} < v_{i} \leq n), where uiu_i is the user who should transfer the currency, and viv_i is the user who should receive the currency. If ti=1t_i = 1, then it is necessary to determine whether it is possible to transfer the currency, and if ti=2t_i = 2, then it is also necessary to minimize the number of direct currency transfers.

输出格式

Output qq lines, where the ii-th line should contain the answer to the ii-th query.

If it is not possible to transfer the currency from user uiu_i to user viv_i, then output 0 as the answer to the ii-th query. Otherwise, if ti=1t_i = 1, output 1, and if ti=2t_i = 2, output the minimum number of direct currency transfers required to transfer Pether\texttt{Pether} from uiu_i to viv_i.

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

提示

Note

In the first example, the following direct transfers between users are possible:

In the first query, user with index 11 can directly transfer Pether\texttt{Pether} to user with index 33, making 2 transactions through intermediate user 2.

In the second query, a direct transfer between users with indices 11 and 22 is not possible, as a1=2>a2=1a_{1} = 2 > a_{2} = 1.

In the third query, it is possible to transfer the currency from user 11 to user 44 with two direct transfers, first transferring the currency from user 11 to user 33, and then from 33 to 44. Since t3=1t_3 = 1, it is only necessary to determine the possibility of transferring the currency, so the answer to the query is 1.

In the fourth query, it is possible to manage with three direct transfers: from 11 to 33, from 33 to 44, and from 44 to 55.

In the second example, the following direct transfers between users are possible:

In the third example, the following direct transfers between users are possible:

Scoring

The tests for this problem consist of twelve groups. Points for each group are given only if all tests of the group and all tests of the required groups are passed. Please note that passing the example tests is not required for some groups. Offline-evaluation\textbf{Offline-evaluation} means that the results of testing your solution on this group will only be available after the end of the competition.

Group Points Additional constraints < Required groups Comment
nn qq vi,an,tiv_i, a_n, t_i
0 -- -- -- Examples.
1 10 n100n \le 100 q100q \le 100
2 7 n1000n \le 1000 -- 1
3 14 -- an=n,vi=na_n = n, v_i = n --
4 10 q=1q = 1 vi=nv_i = n
5 9 -- 3, 4
6 7 ti=2t_i=2 -- The answer does not exceed 1010
7 1, 6 The answer does not exceed 150150
8 13 ti=1t_i = 1 --
9 10 n50000n \le 50\,000 q50000q \le 50\,000 -- 1
10 4 n100000n \le 100\,000 q100000q \le 100\,000 1, 9
11 n200000n \le 200\,000 q200000q \le 200\,000 1, 9, 10
12 5 -- 0--11 Offline-evaluation.