CF Round #741 (Div. 2) D2. Two Hundred Twenty One (hard version)
知识点:证明、二分查找、思维
题目
This is the hard version of the problem. The difference between the versions is that the hard version does require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are n rods arranged along one straight line and numbered from 1 to n inclusive. Each of these rods must carry an electric charge quantitatively equal to either 1 or −1 (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of n numbers characterizing the charge: either 1 or −1. Then the condition must hold: a1−a2+a3−a4+…=0, or ∑i=1n(−1)i−1⋅ai=0.
Sparky charged all n rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has q questions. In the ith question Sparky asks: if the machine consisted only of rods with numbers li to ri inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Also Sparky wants to know the numbers of these rods. Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don’t have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky’s questions!
输入
Each test contains multiple test cases.
The first line contains one positive integer t (1≤t≤10e3), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers n and q (1≤n,q≤3⋅10e5) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string s of length n, where the charge of the i-th rod is 1 if si is the “+” symbol, or −1 if si is the “-” symbol.
Each next line from the next q lines contains two positive integers li ans ri (1≤li≤ri≤n) — numbers, describing Sparky’s questions.
It is guaranteed that the sum of n over all test cases does not exceed 3⋅10e5, and the sum of q over all test cases does not exceed 3⋅10e5.
It is guaranteed that the sum of the answers (minimal number of rods that can be removed) over all test cases does not exceed 10e6.
输出
For each test case, print the answer in the following format:
In the first line print a single integer k — the minimal number of rods that can be removed.
In the second line print k numbers separated by a space — the numbers of rods to be removed.
If there is more than one correct answer, you can print any.
样例
输入
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
输出
2
5 8
2
1 11
1
9
0
1
1
2
1 2
1
2
2
1 3
1
2
2
2 3
1
3
1
3
2
3 4
1
4
提示
In the first test case for the first query you can remove the rods numbered 5 and 8, then the following set of rods will remain: ±-±-+±+±. It is easy to see that here the sign-variable sum is zero.
In the second test case:
- For the first query, we can remove the rods numbered 1 and 11, then
the following set of rods will remain: --++---++---. It is easy to
see that here the sign-variable sum is zero. - For the second query we can remove the rod numbered 9, then the
following set of rods will remain: ---++-. It is easy to see that
here the variable sum is zero. - For the third query we can not remove the rods at all.
题意
有一段只含1和-1的序列,q次询问,每次询问取一段子区间[l,r],删除区间内若干个元素(不改变原序列);设区间第一个元素的位置为1,将奇数项乘以1,偶数项乘以-1,(不改变原序列)再将区间内的数字求和(下面将把以上乘和求和的操作称为符号求和),使得和为零。求每次询问最少删除的元素的位置。
思路
本题解参考Wind_Eagle在本场比赛的官方题解。
一段区间中删去一个数后的符号求和,相当于这个数的前面一段的符号求和加上这个数的后面一段的符号求和的相反数,令sum[l,r]为l位置到r位置的符号求和,a[i]为i位置的数,即删去a[i]后的符号求和del[i]=sum[l,i-1]+(-sum[i+1,r])=sum[l,i-1]-sum[i+1,r].
证明:删去a[i]后重新排列序列,相当于将i以后所有数的位置左移一位,奇数项变为偶数项,偶数项变为奇数项,对每一个i以后的数而言,符号求和中的乘操作的变化相当于原数做乘操作再乘-1,即原数做乘操作的相反数,求和操作时提出i以后的数的负号,即为负号乘以原序列的符号求和,即i以后的数的新符号求和为原符号求和的相反数。
长度为奇数的序列符号求和为奇数
证明:乘操作后序列只包含1和-1,若符号求和为偶数,则1和-1的个数之差必须为偶数,可证个数之和也为偶数,与序列长度为奇数矛盾。
abs(del[i]-del[i+1])=2或0
证明:a[i]=a[i+1]时,若i为奇数,del[i]=sum[l,i-1]-sum[i+1,r]=sum[l,i-1]-((-a[i+1])+(-sum[i+2,r]))=sum[l,i-1]+a[i+1]+sum[i+2,r].del[i+1]=sum[l,i]-sum[i+2,r]=sum[l,i-1]+a[i]-sum[i+2,r]
(等我回去补一下,忘了)
实现思路见代码。
代码
#include"bits/stdc++.h"
#define ll long long
#define db(x) cout<<fixed<<setprecision(14)<<#x<<':'<<x<<endl
#define pb push_back
#define mk make_pair
#define x first
#define y second
#define max(a,b) (a<b?b:a)
#define mix(a,b) (a<b?a:b)
#define vec vector<ll>
using namespace std;
const int N=3e5+10,inf=1e9;
ll t,n,q;
ll arr[N],del[N];
ll lft,rgt;
vec sum,ans;
inline ll getval(ll id) {
return del[id]-sum[lft]+(sum[n]-sum[rgt]);
}
ll find(ll l,ll r) {
ll mid;
while(l!=r) {
mid=l+r>>1;
ll v=getval(mid);
if(v<=0&&getval(l)>=0||v>=0&&getval(l)<=0) r=mid;
else l=mid+1;
}
return r;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t;
cin>>t;
while(t--) {
cin>>n>>q;
sum.assign(n+1,0);
for(ll i=0,f=1; i<n; ++i,f=-f) {
char c;
cin>>c;
arr[i]=c=='+'?1:-1;
sum[i+1]=sum[i]+arr[i]*f;
}
for(ll i=0; i<n; ++i) del[i]=sum[i]-(sum[n]-sum[i+1]);
while(q--) {
ans.resize(0);
cin>>lft>>rgt;
--lft;
if(!(sum[rgt]-sum[lft])) cout<<"0"<<endl;
else {
if(!((rgt-lft)&1)) ans.pb(lft++);
ans.pb(find(lft,rgt));
cout<<ans.size()<<endl;
for(auto v:ans) cout<<v+1<<' ';
cout<<endl;
}
}
}
return 0;
}