CF1747D.Yet Another Problem

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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)-
    1. Choose two integers LL, RR such that l≤L≤R≤rl \le L \le R \le 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.

给你一个包含 nn 个整数 a1,a2,a3,…,ana_1, a_2, a_3, \ldots, a_n 的数组 aa。

你需要回答 qq 个相互独立的查询,每个查询由两个整数 ll 和 rr 组成。

  • 考虑子数组 a[l:r]=[al,al+1,…,ar]a[l:r] = [a_l, a_{l+1}, \ldots, a_r]。你可以对该子数组执行以下操作任意多次(包括零次):
    1. 选择两个整数 LL、RR,满足 l≤L≤R≤rl \le L \le R \le r,且 R−L+1R - L + 1 为奇数;
    2. 将子数组 [L,R][L, R] 中的每个元素替换为该子数组 [L,R][L, R] 内所有元素的异或(XOR)值。
  • 查询的答案为:使子数组 a[l:r]a[l:r] 中所有元素均变为 00 所需的最少操作次数;若无法使所有元素变为 00,则答案为 −1-1。

关于异或(XOR)运算的更多细节,请参见此处。

输入格式

The first line contains two integers nn and qq (1≤n,q≤2⋅105)(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 (0≤ai<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 (1≤li≤ri≤n)(1 \le l_i \le r_i \le n) — the description of the ii-th query.

第一行包含两个整数 nn 和 qq (1≤n,q≤2⋅105)(1 \le n, q \le 2 \cdot 10^5) —— 分别表示数组 aa 的长度和查询次数。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai<230)(0 \le a_i \lt 2^{30}) —— 表示数组 aa 的元素。

接下来的 qq 行中,第 ii 行包含两个整数 lil_i 和 rir_i (1≤li≤ri≤n)(1 \le l_i \le r_i \le n) —— 表示第 ii 个查询的描述。

输出格式

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

对于每个查询,输出一个整数——该查询的答案。

输入输出样例

  • 输入#1

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

    输出#1

    -1
    1
    1
    -1
    2
    0

说明/提示

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 (3⊕1⊕2)=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].

在第一次查询中,l=3,r=4l = 3, r = 4,子数组为 [3,3][3, 3]。我们只能对长度为 11 的子数组执行操作,而该操作不会改变数组;因此无法使所有元素变为 00。

在第二次查询中,l=4,r=6l = 4, r = 6,子数组为 [3,1,2][3, 1, 2]。我们可以选择整个子数组(即 L=4,R=6L = 4, R = 6),并将所有元素替换为其异或值 (3⊕1⊕2)=0(3 \oplus 1 \oplus 2) = 0,从而使子数组变为 [0,0,0][0, 0, 0]。

在第五次查询中,l=1,r=6l = 1, r = 6,子数组为 [3,0,3,3,1,2][3, 0, 3, 3, 1, 2]。我们可以按如下方式执行操作:

  1. 选择 L=4,R=6L = 4, R = 6,使子数组变为 [3,0,3,0,0,0][3, 0, 3, 0, 0, 0]。
  2. 选择 L=1,R=5L = 1, R = 5,使子数组变为 [0,0,0,0,0,0][0, 0, 0, 0, 0, 0]。

输入解题思路,AI测评打分。不知道怎么写?

首页