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 or 1. Call a binary string decent if it has an equal number of 0s and 1s.
Initially, you have an infinite binary string t whose characters are all 0s. You are given a sequence a of n updates, where ai indicates that the character at index ai will be flipped (0↔1). You need to keep and modify after each update a set S of disjoint ranges such that:
- for each range [l,r], the substring tl…tr is a decent binary string, and
- for all indices i such that ti=1, there exists [l,r] in S such that l≤i≤r.
You only need to output the ranges that are added to or removed from S after each update. You can only add or remove ranges from S at most 106 times.
More formally, let Si be the set of ranges after the i-th update, where S0=∅ (the empty set). Define Xi to be the set of ranges removed after update i, and Yi to be the set of ranges added after update i. Then for 1≤i≤n, Si=(Si−1∖Xi)∪Yi. The following should hold for all 1≤i≤n:
- ∀a,b∈Si,(a=b)→(a∩b=∅);
- Xi⊆Si−1;
- (Si−1∖Xi)∩Yi=∅;
- i=1∑n(∣Xi∣+∣Yi∣)≤106.
二进制字符串是指每个字符均为 0 或 1 的字符串。若一个二进制字符串中 0 和 1 的个数相等,则称其为“优雅的”(decent)。
初始时,你拥有一个无限长的二进制字符串 t,其所有字符均为 0。你将收到一个包含 n 次更新的序列 a,其中每次更新 ai 表示将下标为 ai 处的字符翻转(0↔1)。你需要在每次更新后维护并调整一个互不相交的区间集合 S,使得:
- 对于每个区间 [l,r]∈S,子串 tl…tr 是一个优雅的二进制字符串;
- 对所有满足 ti=1 的下标 i,均存在某个 [l,r]∈S,使得 l≤i≤r。
你只需在每次更新后输出被加入或被移除的那些区间。在整个过程中,对集合 S 执行的添加或删除操作总次数至多为 106 次。
更形式化地,令 Si 表示第 i 次更新后的区间集合,且定义 S0=∅(空集)。设 Xi 为第 i 次更新后被移除的区间集合,Yi 为第 i 次更新后被加入的区间集合。则对所有 1≤i≤n,有 Si=(Si−1∖Xi)∪Yi。以下条件必须对所有 1≤i≤n 成立:
- ∀a,b∈Si, (a=b)→(a∩b=∅);
- Xi⊆Si−1;
- (Si−1∖Xi)∩Yi=∅;
- i=1∑n(∣Xi∣+∣Yi∣)≤106。
输入格式
The first line contains a single integer n (1≤n≤2⋅105) — the number of updates.
The next n lines each contain a single integer ai (1≤ai≤2⋅105) — the index of the i-th update to the string.
第一行包含一个整数 n(1≤n≤2⋅105)—— 更新操作的次数。
接下来的 n 行每行包含一个整数 ai(1≤ai≤2⋅105)—— 第 i 次更新操作所对应的字符串下标。
输出格式
After the i-th update, first output a single integer xi — the number of ranges to be removed from S after update i.
In the following xi lines, output two integers l and r (1≤l<r≤106), which denotes that the range [l,r] should be removed from S. Each of these ranges should be distinct and be part of S.
In the next line, output a single integer yi — the number of ranges to be added to S after update i.
In the following yi lines, output two integers l and r (1≤l<r≤106), which denotes that the range [l,r] should be added to S. Each of these ranges should be distinct and not be part of S.
The total number of removals and additions across all updates must not exceed 106.
After processing the removals and additions for each update, all the ranges in S should be disjoint and cover all ones.
It can be proven that an answer always exists under these constraints.
在第 i 次更新后,首先输出一个整数 xi —— 表示第 i 次更新后需从集合 S 中移除的区间数量。
接下来的 xi 行中,每行输出两个整数 l 和 r(满足 1≤l<r≤106),表示应将区间 [l,r] 从 S 中移除。这些被移除的区间必须互不相同,且均属于 S。
随后的一行中,输出一个整数 yi —— 表示第 i 次更新后需加入集合 S 的区间数量。
接下来的 yi 行中,每行输出两个整数 l 和 r(满足 1≤l<r≤106),表示应将区间 [l,r] 加入 S。这些被加入的区间必须互不相同,且均不属于 S。
所有更新操作中移除与添加的区间总数不得超过 106。
每次更新完成所有移除与添加操作后,集合 S 中的所有区间必须两两不相交,且其并集恰好覆盖所有值为 1 的位置。
可以证明,在上述约束条件下,答案恒存在。
输入输出样例
输入#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=1 is 1. The interval [1,2] is added, so S1=[1,2], which has one 0 and one 1.
After the second update, the set of indices where ai=1 is 1,6. The interval [5,6] is added, so S2=[1,2],[5,6], each of which has one 0 and one 1.
After the third update, the set of indices where ai=1 is 1,5,6. The interval [5,6] is removed and the intervals [4,5] and [6,7] are added, so S3=[1,2],[4,5],[6,7], each of which has one 0 and one 1.
After the fourth update, the set of indices where ai=1 is 1,6. The interval [4,5] is removed, so S4=[1,2],[6,7], each of which has one 0 and one 1.
After the fifth update, the set of indices where ai=1 is 1. The interval [6,7] is removed, so S5=[1,2], which has one 0 and one 1.
样例中的换行仅为了清晰起见,您在输出中无需打印这些换行。
第一次更新后,满足 ai=1 的下标集合为 {1}。此时加入区间 [1,2],因此 S1={[1,2]},该区间包含一个 0 和一个 1。
第二次更新后,满足 ai=1 的下标集合为 {1,6}。此时加入区间 [5,6],因此 S2={[1,2],[5,6]},其中每个区间均包含一个 0 和一个 1。
第三次更新后,满足 ai=1 的下标集合为 {1,5,6}。此时移除区间 [5,6],并加入区间 [4,5] 和 [6,7],因此 S3={[1,2],[4,5],[6,7]},其中每个区间均包含一个 0 和一个 1。
第四次更新后,满足 ai=1 的下标集合为 {1,6}。此时移除区间 [4,5],因此 S4={[1,2],[6,7]},其中每个区间均包含一个 0 和一个 1。
第五次更新后,满足 ai=1 的下标集合为 {1}。此时移除区间 [6,7],因此 S5={[1,2]},该区间包含一个 0 和一个 1。
输入解题思路,AI测评打分。不知道怎么写?