, 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? 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!
Input
Each test contains multiple test cases.
The first line contains one positive integer t (1≤t≤103), 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⋅105) — 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⋅105, and the sum of q over all test cases does not exceed 3⋅105.
Output
For each test case, print a single integer — the minimal number of rods that can be removed.
Example
input
Copy
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
output
Copy
2 2 1 0 1 2 1 2 1 2 1 1 2 1
Note
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.
思路:
f[i]表示去掉i点。根据以上推理我们可以得出 L与R有相反性,说明了sum的变化有对称性(对称点)(即关于i点对称),对称了说明能将左右两端折叠而相互抵消,使之变为0(和为0)。当L到R之间有奇数个时,刚好可以去掉一个数使之变为0(即对折),当为偶数时我们能够先去掉一个数,使之在折叠
ps:当证明过有对称行之后,我们能够将sum的变化简化为一条直线的变化( 也可变为sin函数等图像的变化,只不过这种图像没有直线直观,只要能相互抵消,就能模拟为一条直线,且有单调性)
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=3e5+10;
int sum[N],f[N];
int main()
{
int t;
cin>>t;
while(t--)
{
memset(sum,0,sizeof sum);
int n,q;
cin>>n>>q;
char x;
for(int i=1;i<=n;i++)
{
cin>>x;
if(i%2==1)
{
if(x=='+') sum[i]=sum[i-1]+1;
else sum[i]=sum[i-1]-1;
}
else
{
if(x=='+') sum[i]=sum[i-1]-1;
else sum[i]=sum[i-1]+1;
}
}
while(q--)
{
int dif;
int l,r;
cin>>l>>r;
dif=sum[r]-sum[l-1];
if(dif==0)
{
cout<<"0"<<endl;
}
else if(dif%2)//可以写为(R-L+1)%2
{
cout<<"1"<<endl;
}
else
{
cout<<"2"<<endl;
}
}
}
return 0;
}