Yet another ProblemHint 1

You are given an array aa of nn integers a1,a2,a3,…,an.

You have to answer qq independent queries, each consisting of two integers ll and rr.

  • Consider the subarray a[l:r]a[l:r] == [al,al+1,…,ar][al,al+1,…,ar]. You can apply the following operation to the subarray any number of times (possibly zero)-
    1. Choose two integers LL, RR such that l≤L≤R≤r and R−L+1R−L+1 is odd.
    2. Replace each element in the subarray from LL to RR with the XOR of the elements in the subarray [L,R][L,R].
  • The answer to the query is the minimum number of operations required to make all elements of the subarray a[l:r]a[l:r] equal to 00 or −1−1 if it is impossible to make all of them equal to 00.

You can find more details about XOR operation here.

Input

The first line contains two integers nn and qq (1≤n,q≤2⋅105)(1≤n,q≤2⋅105)  — the length of the array aa and the number of queries.

The next line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai<230)(0≤ai<230)  — the elements of the array aa.

The ii-th of the next qq lines contains two integers lili and riri (1≤li≤ri≤n)(1≤li≤ri≤n)  — the description of the ii-th query.

Output

For each query, output a single integer  — the answer to that query.

Example

input

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

output

-1
1
1
-1
2
0

Note

In the first query, l=3,r=4l=3,r=4, subarray = [3,3][3,3]. We can apply operation only to the subarrays of length 11, which won't change the array; hence it is impossible to make all elements equal to 00.

In the second query, l=4,r=6l=4,r=6, subarray = [3,1,2][3,1,2]. We can choose the whole subarray (L=4,R=6) and replace all elements by their XOR (3⊕1⊕2)=0(3⊕1⊕2)=0, making the subarray [0,0,0].

In the fifth query, l=1,r=6l=1,r=6, subarray = [3,0,3,3,1,2][3,0,3,3,1,2]. We can make the operations as follows:

  1. Choose L=4,R=6, making the subarray[3,0,3,0,0,0].
  2. Choose L=1,R=5, making the subarray[0,0,0,0,0,0].

对于一个区间,无论选哪对l, r做多少次异或操作,因为不能自己异或自己,所以异或和总是不变的。所以要想使一个区间都变为0,这个区间必须在初始时异或和就为0

异或和不变分析:

选择L, R, 设异或之后将L - R都替换为num,这是个奇数区间,奇数个num异或后还是num。

若是查询某个奇数区间:此区间全为0则 0 次操作

                                        此区间异或和为0则 1 次操作

                                       否则无法变为0

若是查询某个偶数区间:首先此区间要异或和全为0才可能产生答案

                                        设此区间为l - r,因为只能选奇数区间异或,所以在l - r内必须存在一个奇数的异或和为0的子区间,先操作一次使这个子区间都为0,再选一个0和剩下的偶数个数异或,至此整个区间都为0

#include
#include
#include

using namespace std ;

void sol()
{
    int n, Q;
    cin >> n >> Q;
    map odd, even;
    vector last_nz(n + 1, 0), last(n + 1, -1), pxor(n + 1, 0);
    vector a(n + 1);
    even[0] = 0;
    int cur = 0;
    
    for(int i = 1; i <= n; i ++) 
	{
    	scanf("%d", &a[i]);
    	cur ^= a[i];
    	pxor[i] = cur;
    	if(a[i] == 0)//记上一个非0数出现的位置 
    		last_nz[i] = last_nz[i - 1];
    	else 
    		last_nz[i] = i;
    	
    	if(i & 1)//i是奇数 
		{
    		if(even.count(cur))//如果偶数前缀中出现过,则中间奇数个数的异或和为0 
    			last[i] = even[cur];
    		odd[cur] = i;
    	}
    	else//i是偶数 ,同理 
		{
    		if(odd.count(cur)) {
    			last[i] = odd[cur];
    		}
    		even[cur] = i;
    	}
    }
    while(Q--) 
	{
    	int l, r;
    	scanf("%d%d",&l, &r);
    	if(pxor[l - 1] != pxor[r])//区间异或和不为0 
		{
    		printf("-1\n");
    	}
    	else if(last_nz[r] < l)//区间内都是0 
		{
    		printf("0\n");
    	}
    	else if(r % 2 == l % 2)//奇数区间一次操作 
		{
    		printf("1\n");
    	}
    	else if(a[l] == 0 or a[r] == 0)//偶数区间看作奇数区间 
		{
    		printf("1\n");
    	}
    	else if(last[r] >= l)//有奇数为0的前缀 
		{
    		printf("2\n");
    	}
    	else
    		printf("-1\n");
    }
}

int main()
{ 
    sol();
    return 0;
}	 	

你可能感兴趣的:(codeforces,算法,二进制)