CF1375H.Set Merging
NOI/NOI+/CTSC
通过率:0%
时间限制:4.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a permutation a1,a2,…,an of numbers from 1 to n. Also, you have n sets S1,S2,…,Sn, where Si=ai. Lastly, you have a variable cnt, representing the current number of sets. Initially, cnt=n.
We define two kinds of functions on sets:
f(S)=u∈Sminu;
g(S)=u∈Smaxu.
You can obtain a new set by merging two sets A and B, if they satisfy g(A)<f(B) (Notice that the old sets do not disappear).
Formally, you can perform the following sequence of operations:
-
cnt←cnt+1;
-
Scnt=Su∪Sv, you are free to choose u and v for which 1≤u,v<cnt and which satisfy g(Su)<f(Sv).
You are required to obtain some specific sets.
There are q requirements, each of which contains two integers li,ri, which means that there must exist a set Ski (ki is the ID of the set, you should determine it) which equals au∣li≤u≤ri, which is, the set consisting of all ai with indices between li and ri.
In the end you must ensure that cnt≤2.2×106. Note that you don't have to minimize cnt. It is guaranteed that a solution under given constraints exists.
给你一个 1 到 n 的排列 a1,a2,…,an。此外,你有 n 个集合 S1,S2,…,Sn,其中 Si={ai}。最后,你有一个变量 cnt,表示当前集合的个数。初始时,cnt=n。
我们定义两个关于集合的函数:
f(S)=u∈Sminu;
g(S)=u∈Smaxu。
若两个集合 A 和 B 满足 g(A)<f(B),则你可以通过合并它们得到一个新集合(注意:原来的集合不会消失)。
形式上,你可以执行如下操作序列:
-
cnt←cnt+1;
-
Scnt=Su∪Sv,其中你可以自由选择满足 1≤u,v<cnt 且 g(Su)<f(Sv) 的下标 u 和 v。
你需要获得若干特定的集合。
共有 q 个要求,每个要求包含两个整数 li、ri,表示必须存在某个集合 Ski(ki 是该集合的编号,由你确定),使得 Ski={au∣li≤u≤ri},即该集合恰好包含下标在 li 到 ri 之间的所有 ai。
最终你必须保证 cnt≤2.2×106。注意:你无需最小化 cnt。题目保证在给定约束下存在解。
输入格式
The first line contains two integers n,q (1≤n≤212,1≤q≤216) — the length of the permutation and the number of needed sets correspondently.
The next line consists of n integers a1,a2,⋯,an (1≤ai≤n, ai are pairwise distinct) — given permutation.
i-th of the next q lines contains two integers li,ri (1≤li≤ri≤n), describing a requirement of the i-th set.
第一行包含两个整数 n,q(1≤n≤212,1≤q≤216)—— 分别表示排列的长度以及所需集合的数量。
下一行包含 n 个整数 a1,a2,⋯,an(1≤ai≤n,且 ai 两两不同)—— 给定的排列。
接下来的 q 行中,第 i 行包含两个整数 li,ri(1≤li≤ri≤n),描述第 i 个集合的要求。
输出格式
It is guaranteed that a solution under given constraints exists.
The first line should contain one integer cntE (n≤cntE≤2.2×106), representing the number of sets after all operations.
cntE−n lines must follow, each line should contain two integers u, v (1≤u,v≤cnt′, where cnt′ is the value of cnt before this operation), meaning that you choose Su, Sv and perform a merging operation. In an operation, g(Su)<f(Sv) must be satisfied.
The last line should contain q integers k1,k2,⋯,kq (1≤ki≤cntE), representing that set Ski is the ith required set.
Please notice the large amount of output.
在给定约束下,保证存在解。
第一行应包含一个整数 cntE (n≤cntE≤2.2×106),表示所有操作完成后集合的总数。
随后必须有 cntE−n 行,每行包含两个整数 u、v(1≤u,v≤cnt′,其中 cnt′ 是本次操作前的 cnt 值),表示你选择集合 Su 和 Sv 并执行一次合并操作。在每次操作中,必须满足 g(Su)<f(Sv)。
最后一行应包含 q 个整数 k1,k2,⋯,kq(1≤ki≤cntE),表示集合 Ski 是第 i 个所要求的集合。
请注意输出量较大。
输入输出样例
输入#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=2 initially.
In the first operation, because g(S3)=2<f(S2)=3, we can merge S3,S2 into S4=2,3.
In the second operation, because g(S1)=1<f(S3)=2, we can merge S1,S3 into S5=1,2.
In the third operation, because g(S5)=2<f(S2)=3, we can merge S5,S2 into S6=1,2,3.
For the first requirement, S4=2,3=a2,a3, satisfies it, thus k1=4.
For the second requirement, S6=1,2,3=a1,a2,a3, satisfies it, thus k2=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=2。
第一次操作:因为 g(S3)=2<f(S2)=3,我们可以将 S3 与 S2 合并为 S4=2,3。
第二次操作:因为 g(S1)=1<f(S3)=2,我们可以将 S1 与 S3 合并为 S5=1,2。
第三次操作:因为 g(S5)=2<f(S2)=3,我们可以将 S5 与 S2 合并为 S6=1,2,3。
对于第一个要求,S4=2,3=a2,a3 满足条件,因此 k1=4。
对于第二个要求,S6=1,2,3=a1,a2,a3 满足条件,因此 k2=6。
注意:允许使用未被使用的集合、重复的集合、多次输出同一集合,以及使用初始时已存在的集合。
输入解题思路,AI测评打分。不知道怎么写?