CF BUDDY
← Problems·

1747D · Yet Another Problem

1900 · binary search, bitmasks, constructive algorithms

Problem: You are given an array aa of nn integers a1,a2,a3,,ana_1, a_2, a_3, \ldots, a_n.

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][a_l, a_{l+1}, \ldots, a_r]. You can apply the following operation to the subarray any number of times (possibly zero)- Choose two integers LL, RR such that lLRrl \le L \le R \le r and RL+1R - L + 1 is odd. 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 Format: The first line contains two integers nn and qq (1n,q2105)(1 \le n, q \le 2 \cdot 10^5)  — the length of the array aa and the number of queries.

The next line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (0ai<230)(0 \le a_i \lt 2^{30})  — the elements of the array aa.

The ii-th of the next qq lines contains two integers lil_i and rir_i (1lirin)(1 \le l_i \le r_i \le n)  — the description of the ii-th query.

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

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)(L = 4, R = 6) and replace all elements by their XOR (312)=0(3 \oplus 1 \oplus 2) = 0, making the subarray [0,0,0][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=6L = 4, R = 6, making the subarray [3,0,3,0,0,0][3, 0, 3, 0, 0, 0].
  2. Choose L=1,R=5L = 1, R = 5, making the subarray [0,0,0,0,0,0][0, 0, 0, 0, 0, 0].

Sample Cases

Case 1

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

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback