CF1375H.Set Merging

NOI/NOI+/CTSC

通过率:0%

时间限制:4.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a permutation a1,a2,…,ana_1, a_2, \dots, a_n of numbers from 11 to nn. Also, you have nn sets S1,S2,…,SnS_1,S_2,\dots, S_n, where Si=aiS_i={a_i}. Lastly, you have a variable cntcnt, representing the current number of sets. Initially, cnt=ncnt = n.

We define two kinds of functions on sets:

f(S)=min⁡u∈Suf(S)=\min\limits_{u\in S} u;

g(S)=max⁡u∈Sug(S)=\max\limits_{u\in S} u.

You can obtain a new set by merging two sets AA and BB, if they satisfy g(A)<f(B)g(A) \lt f(B) (Notice that the old sets do not disappear).

Formally, you can perform the following sequence of operations:

  • cnt←cnt+1cnt\gets cnt+1;

  • Scnt=Su∪SvS_{cnt}=S_u\cup S_v, you are free to choose uu and vv for which 1≤u,v<cnt1\le u, v \lt cnt and which satisfy g(Su)<f(Sv)g(S_u) \lt f(S_v).

You are required to obtain some specific sets.

There are qq requirements, each of which contains two integers lil_i,rir_i, which means that there must exist a set SkiS_{k_i} (kik_i is the ID of the set, you should determine it) which equals au∣li≤u≤ri{a_u\mid l_i\leq u\leq r_i}, which is, the set consisting of all aia_i with indices between lil_i and rir_i.

In the end you must ensure that cnt≤2.2×106cnt\leq 2.2\times 10^6. Note that you don't have to minimize cntcnt. It is guaranteed that a solution under given constraints exists.

给你一个 11 到 nn 的排列 a1,a2,…,ana_1, a_2, \dots, a_n。此外,你有 nn 个集合 S1,S2,…,SnS_1, S_2, \dots, S_n,其中 Si={ai}S_i = \{a_i\}。最后,你有一个变量 cntcnt,表示当前集合的个数。初始时,cnt=ncnt = n。

我们定义两个关于集合的函数:

f(S)=min⁡u∈Suf(S) = \min\limits_{u \in S} u;

g(S)=max⁡u∈Sug(S) = \max\limits_{u \in S} u。

若两个集合 AA 和 BB 满足 g(A)<f(B)g(A) < f(B),则你可以通过合并它们得到一个新集合(注意:原来的集合不会消失)。

形式上,你可以执行如下操作序列:

  • cnt←cnt+1cnt \gets cnt + 1;

  • Scnt=Su∪SvS_{cnt} = S_u \cup S_v,其中你可以自由选择满足 1≤u,v<cnt1 \le u, v < cnt 且 g(Su)<f(Sv)g(S_u) < f(S_v) 的下标 uu 和 vv。

你需要获得若干特定的集合。

共有 qq 个要求,每个要求包含两个整数 lil_i、rir_i,表示必须存在某个集合 SkiS_{k_i}(kik_i 是该集合的编号,由你确定),使得 Ski={au∣li≤u≤ri}S_{k_i} = \{a_u \mid l_i \leq u \leq r_i\},即该集合恰好包含下标在 lil_i 到 rir_i 之间的所有 aia_i。

最终你必须保证 cnt≤2.2×106cnt \leq 2.2 \times 10^6。注意:你无需最小化 cntcnt。题目保证在给定约束下存在解。

输入格式

The first line contains two integers n,qn,q (1≤n≤212,1≤q≤216)(1\leq n \leq 2^{12},1 \leq q \leq 2^{16}) — the length of the permutation and the number of needed sets correspondently.

The next line consists of nn integers a1,a2,⋯ ,ana_1,a_2,\cdots, a_n (1≤ai≤n1\leq a_i\leq n, aia_i are pairwise distinct) — given permutation.

ii-th of the next qq lines contains two integers li,ril_i,r_i (1≤li≤ri≤n)(1\leq l_i\leq r_i\leq n), describing a requirement of the ii-th set.

第一行包含两个整数 n,qn,q(1≤n≤212, 1≤q≤2161\leq n \leq 2^{12},\,1 \leq q \leq 2^{16})—— 分别表示排列的长度以及所需集合的数量。

下一行包含 nn 个整数 a1,a2,⋯ ,ana_1,a_2,\cdots, a_n(1≤ai≤n1\leq a_i\leq n,且 aia_i 两两不同)—— 给定的排列。

接下来的 qq 行中,第 ii 行包含两个整数 li,ril_i,r_i(1≤li≤ri≤n1\leq l_i\leq r_i\leq n),描述第 ii 个集合的要求。

输出格式

It is guaranteed that a solution under given constraints exists.

The first line should contain one integer cntEcnt_E (n≤cntE≤2.2×106)(n\leq cnt_E\leq 2.2\times 10^6), representing the number of sets after all operations.

cntE−ncnt_E-n lines must follow, each line should contain two integers uu, vv (1≤u,v≤cnt′1\leq u, v\leq cnt', where cnt′cnt' is the value of cntcnt before this operation), meaning that you choose SuS_u, SvS_v and perform a merging operation. In an operation, g(Su)<f(Sv)g(S_u) \lt f(S_v) must be satisfied.

The last line should contain qq integers k1,k2,⋯ ,kqk_1,k_2,\cdots,k_q (1≤ki≤cntE)(1\leq k_i\leq cnt_E), representing that set SkiS_{k_i} is the iith required set.

Please notice the large amount of output.

在给定约束下,保证存在解。

第一行应包含一个整数 cntEcnt_E (n≤cntE≤2.2×106)(n\leq cnt_E\leq 2.2\times 10^6),表示所有操作完成后集合的总数。

随后必须有 cntE−ncnt_E-n 行,每行包含两个整数 uu、vv(1≤u,v≤cnt′1\leq u, v\leq cnt',其中 cnt′cnt' 是本次操作前的 cntcnt 值),表示你选择集合 SuS_u 和 SvS_v 并执行一次合并操作。在每次操作中,必须满足 g(Su)<f(Sv)g(S_u) \lt f(S_v)。

最后一行应包含 qq 个整数 k1,k2,⋯ ,kqk_1,k_2,\cdots,k_q(1≤ki≤cntE1\leq k_i\leq cnt_E),表示集合 SkiS_{k_i} 是第 ii 个所要求的集合。

请注意输出量较大。

输入输出样例

  • 输入#1

    3 2
    1 3 2
    2 3
    1 3

    输出#1

    6
    3 2
    1 3
    5 2
    4 6
  • 输入#2

    2 4
    2 1
    1 2
    1 2
    1 2
    1 1

    输出#2

    5
    2 1
    2 1
    2 1
    5 3 3 1

说明/提示

In the first sample:

We have S1=1,S2=3,S3=2S_1={1},S_2={3},S_3={2} initially.

In the first operation, because g(S3)=2<f(S2)=3g(S_3)=2 \lt f(S_2)=3, we can merge S3,S2S_3,S_2 into S4=2,3S_4={2,3}.

In the second operation, because g(S1)=1<f(S3)=2g(S_1)=1 \lt f(S_3)=2, we can merge S1,S3S_1,S_3 into S5=1,2S_5={1,2}.

In the third operation, because g(S5)=2<f(S2)=3g(S_5)=2 \lt f(S_2)=3, we can merge S5,S2S_5,S_2 into S6=1,2,3S_6={1,2,3}.

For the first requirement, S4=2,3=a2,a3S_4={2,3}={a_2,a_3}, satisfies it, thus k1=4k_1=4.

For the second requirement, S6=1,2,3=a1,a2,a3S_6={1,2,3}={a_1,a_2,a_3}, satisfies it, thus k2=6k_2=6

Notice that unused sets, identical sets, outputting the same set multiple times, and using sets that are present initially are all allowed.

在第一个样例中:

初始时,我们有 S1=1,S2=3,S3=2S_1={1},S_2={3},S_3={2}。

第一次操作:因为 g(S3)=2<f(S2)=3g(S_3)=2 \lt f(S_2)=3,我们可以将 S3S_3 与 S2S_2 合并为 S4=2,3S_4={2,3}。

第二次操作:因为 g(S1)=1<f(S3)=2g(S_1)=1 \lt f(S_3)=2,我们可以将 S1S_1 与 S3S_3 合并为 S5=1,2S_5={1,2}。

第三次操作:因为 g(S5)=2<f(S2)=3g(S_5)=2 \lt f(S_2)=3,我们可以将 S5S_5 与 S2S_2 合并为 S6=1,2,3S_6={1,2,3}。

对于第一个要求,S4=2,3=a2,a3S_4={2,3}={a_2,a_3} 满足条件,因此 k1=4k_1=4。

对于第二个要求,S6=1,2,3=a1,a2,a3S_6={1,2,3}={a_1,a_2,a_3} 满足条件,因此 k2=6k_2=6。

注意:允许使用未被使用的集合、重复的集合、多次输出同一集合,以及使用初始时已存在的集合。

输入解题思路,AI测评打分。不知道怎么写?

首页