CF1758F.Decent Division

NOI/NOI+/CTSC

通过率:0%

时间限制:5.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

A binary string is a string where every character is 0\texttt{0} or 1\texttt{1}. Call a binary string decent if it has an equal number of 0\texttt{0}s and 1\texttt{1}s.

Initially, you have an infinite binary string tt whose characters are all 0\texttt{0}s. You are given a sequence aa of nn updates, where aia_i indicates that the character at index aia_i will be flipped (0↔1\texttt{0} \leftrightarrow \texttt{1}). You need to keep and modify after each update a set SS of disjoint ranges such that:

  • for each range [l,r][l,r], the substring tl…trt_l \dots t_r is a decent binary string, and
  • for all indices ii such that ti=1t_i = \texttt{1}, there exists [l,r][l,r] in SS such that l≤i≤rl \leq i \leq r.

You only need to output the ranges that are added to or removed from SS after each update. You can only add or remove ranges from SS at most 106\mathbf{10^6} times.

More formally, let SiS_i be the set of ranges after the ii-th update, where S0=∅S_0 = \varnothing (the empty set). Define XiX_i to be the set of ranges removed after update ii, and YiY_i to be the set of ranges added after update ii. Then for 1≤i≤n1 \leq i \leq n, Si=(Si−1∖Xi)∪YiS_i = (S_{i - 1} \setminus X_i) \cup Y_i. The following should hold for all 1≤i≤n1 \leq i \leq n:

  • ∀a,b∈Si,(a≠b)→(a∩b=∅)\forall a,b \in S_i, (a \neq b) \rightarrow (a \cap b = \varnothing);
  • Xi⊆Si−1X_i \subseteq S_{i - 1};
  • (Si−1∖Xi)∩Yi=∅(S_{i-1} \setminus X_i) \cap Y_i = \varnothing;
  • ∑i=1n(∣Xi∣+∣Yi∣)≤106\displaystyle\sum_{i = 1}^n {(|X_i| + |Y_i|)} \leq 10^6.

二进制字符串是指每个字符均为 0\texttt{0} 或 1\texttt{1} 的字符串。若一个二进制字符串中 0\texttt{0} 和 1\texttt{1} 的个数相等,则称其为“优雅的”(decent)。

初始时,你拥有一个无限长的二进制字符串 tt,其所有字符均为 0\texttt{0}。你将收到一个包含 nn 次更新的序列 aa,其中每次更新 aia_i 表示将下标为 aia_i 处的字符翻转(0↔1\texttt{0} \leftrightarrow \texttt{1})。你需要在每次更新后维护并调整一个互不相交的区间集合 SS,使得:

  • 对于每个区间 [l,r]∈S[l,r] \in S,子串 tl…trt_l \dots t_r 是一个优雅的二进制字符串;
  • 对所有满足 ti=1t_i = \texttt{1} 的下标 ii,均存在某个 [l,r]∈S[l,r] \in S,使得 l≤i≤rl \leq i \leq r。

你只需在每次更新后输出被加入或被移除的那些区间。在整个过程中,对集合 SS 执行的添加或删除操作总次数至多为 106\mathbf{10^6} 次。

更形式化地,令 SiS_i 表示第 ii 次更新后的区间集合,且定义 S0=∅S_0 = \varnothing(空集)。设 XiX_i 为第 ii 次更新后被移除的区间集合,YiY_i 为第 ii 次更新后被加入的区间集合。则对所有 1≤i≤n1 \leq i \leq n,有 Si=(Si−1∖Xi)∪YiS_i = (S_{i - 1} \setminus X_i) \cup Y_i。以下条件必须对所有 1≤i≤n1 \leq i \leq n 成立:

  • ∀a,b∈Si, (a≠b)→(a∩b=∅)\forall a,b \in S_i,\ (a \neq b) \rightarrow (a \cap b = \varnothing);
  • Xi⊆Si−1X_i \subseteq S_{i - 1};
  • (Si−1∖Xi)∩Yi=∅(S_{i-1} \setminus X_i) \cap Y_i = \varnothing;
  • ∑i=1n(∣Xi∣+∣Yi∣)≤106\displaystyle\sum_{i = 1}^n {(|X_i| + |Y_i|)} \leq 10^6。

输入格式

The first line contains a single integer nn (1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5) — the number of updates.

The next nn lines each contain a single integer aia_i (1≤ai≤2⋅1051 \leq a_i \leq 2 \cdot 10^5) — the index of the ii-th update to the string.

第一行包含一个整数 nn(1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5)—— 更新操作的次数。

接下来的 nn 行每行包含一个整数 aia_i(1≤ai≤2⋅1051 \leq a_i \leq 2 \cdot 10^5)—— 第 ii 次更新操作所对应的字符串下标。

输出格式

After the ii-th update, first output a single integer xix_i — the number of ranges to be removed from SS after update ii.

In the following xix_i lines, output two integers ll and rr (1≤l<r≤1061 \leq l \lt r \leq 10^6), which denotes that the range [l,r][l,r] should be removed from SS. Each of these ranges should be distinct and be part of SS.

In the next line, output a single integer yiy_i — the number of ranges to be added to SS after update ii.

In the following yiy_i lines, output two integers ll and rr (1≤l<r≤1061 \leq l \lt r \leq 10^6), which denotes that the range [l,r][l,r] should be added to SS. Each of these ranges should be distinct and not be part of SS.

The total number of removals and additions across all updates must not exceed 106\mathbf{10^6}.

After processing the removals and additions for each update, all the ranges in SS should be disjoint and cover all ones.

It can be proven that an answer always exists under these constraints.

在第 ii 次更新后,首先输出一个整数 xix_i —— 表示第 ii 次更新后需从集合 SS 中移除的区间数量。

接下来的 xix_i 行中,每行输出两个整数 ll 和 rr(满足 1≤l<r≤1061 \leq l \lt r \leq 10^6),表示应将区间 [l,r][l,r] 从 SS 中移除。这些被移除的区间必须互不相同,且均属于 SS。

随后的一行中,输出一个整数 yiy_i —— 表示第 ii 次更新后需加入集合 SS 的区间数量。

接下来的 yiy_i 行中,每行输出两个整数 ll 和 rr(满足 1≤l<r≤1061 \leq l \lt r \leq 10^6),表示应将区间 [l,r][l,r] 加入 SS。这些被加入的区间必须互不相同,且均不属于 SS。

所有更新操作中移除与添加的区间总数不得超过 106\mathbf{10^6}。

每次更新完成所有移除与添加操作后,集合 SS 中的所有区间必须两两不相交,且其并集恰好覆盖所有值为 11 的位置。

可以证明,在上述约束条件下,答案恒存在。

输入输出样例

  • 输入#1

    5
    1
    6
    5
    5
    6

    输出#1

    0
    1
    1 2
    
    0
    1
    5 6
    
    1
    5 6
    2
    6 7
    4 5
    
    1
    4 5
    0
    
    1
    6 7
    0

说明/提示

Line breaks are provided in the sample only for the sake of clarity, and you don't need to print them in your output.

After the first update, the set of indices where ai=1a_i = 1 is 1{1}. The interval [1,2][1, 2] is added, so S1=[1,2]S_1 = {[1, 2]}, which has one 0\texttt{0} and one 1\texttt{1}.

After the second update, the set of indices where ai=1a_i = 1 is 1,6{1, 6}. The interval [5,6][5, 6] is added, so S2=[1,2],[5,6]S_2 = {[1, 2], [5, 6]}, each of which has one 0\texttt{0} and one 1\texttt{1}.

After the third update, the set of indices where ai=1a_i = 1 is 1,5,6{1, 5, 6}. The interval [5,6][5, 6] is removed and the intervals [4,5][4, 5] and [6,7][6, 7] are added, so S3=[1,2],[4,5],[6,7]S_3 = {[1, 2], [4, 5], [6, 7]}, each of which has one 0\texttt{0} and one 1\texttt{1}.

After the fourth update, the set of indices where ai=1a_i = 1 is 1,6{1, 6}. The interval [4,5][4, 5] is removed, so S4=[1,2],[6,7]S_4 = {[1, 2], [6, 7]}, each of which has one 0\texttt{0} and one 1\texttt{1}.

After the fifth update, the set of indices where ai=1a_i = 1 is 1{1}. The interval [6,7][6, 7] is removed, so S5=[1,2]S_5 = {[1, 2]}, which has one 0\texttt{0} and one 1\texttt{1}.

样例中的换行仅为了清晰起见,您在输出中无需打印这些换行。

第一次更新后,满足 ai=1a_i = 1 的下标集合为 {1}\{1\}。此时加入区间 [1,2][1, 2],因此 S1={[1,2]}S_1 = \{[1, 2]\},该区间包含一个 0\texttt{0} 和一个 1\texttt{1}。

第二次更新后,满足 ai=1a_i = 1 的下标集合为 {1,6}\{1, 6\}。此时加入区间 [5,6][5, 6],因此 S2={[1,2],[5,6]}S_2 = \{[1, 2], [5, 6]\},其中每个区间均包含一个 0\texttt{0} 和一个 1\texttt{1}。

第三次更新后,满足 ai=1a_i = 1 的下标集合为 {1,5,6}\{1, 5, 6\}。此时移除区间 [5,6][5, 6],并加入区间 [4,5][4, 5] 和 [6,7][6, 7],因此 S3={[1,2],[4,5],[6,7]}S_3 = \{[1, 2], [4, 5], [6, 7]\},其中每个区间均包含一个 0\texttt{0} 和一个 1\texttt{1}。

第四次更新后,满足 ai=1a_i = 1 的下标集合为 {1,6}\{1, 6\}。此时移除区间 [4,5][4, 5],因此 S4={[1,2],[6,7]}S_4 = \{[1, 2], [6, 7]\},其中每个区间均包含一个 0\texttt{0} 和一个 1\texttt{1}。

第五次更新后,满足 ai=1a_i = 1 的下标集合为 {1}\{1\}。此时移除区间 [6,7][6, 7],因此 S5={[1,2]}S_5 = \{[1, 2]\},该区间包含一个 0\texttt{0} 和一个 1\texttt{1}。

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

首页