传送门:CF
题目描述:
You are given an array a of n integers a1,a2,a3,…,an.
You have to answer q independent queries, each consisting of two integers l and r.
Consider the subarray a[l:r] = [al,al+1,…,ar]. You can apply the following operation to the subarray any number of times (possibly zero)-
Choose two integers L, R such that l≤L≤R≤r and R−L+1 is odd.
Replace each element in the subarray from L to R with the XOR of the elements in the subarray [L,R].
The answer to the query is the minimum number of operations required to make all elements of the subarray a[l:r] equal to 0 or −1 if it is impossible to make all of them equal to 0.
You can find more details about XOR operation here.
输入:
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
2
0
当时打比赛的时候感觉应该是需要使用异或前缀和来做的,奈何近期根本没有做过异或之类的题目,所以性质大部分都忘了,然后就坐牢了…,不然感觉还是能AC4题的…
前置知识:a[l]^a[l+1]^a[l+2]^....^a[r]=0
,并且我们称之为区间
[
l
,
r
]
[l,r]
[l,r]的异或和,并且假设我们使用sum[i]来记录[1,i]区间的异或和的话,我们有sum[l-1]
∗
*
∗sum[r]=[l,r]之间的异或和(原因也很简单,因为我们的这两个区间异或和有一段相同的部分,而对于异或来说相同的值得到0)
主要思路:
a[l]^a[l+1]^a[l+2]^....^a[r]=0
-1
lowerbound
函数所以就不用mulmap
了.那么对于我们的每一个区间
[
l
,
r
]
[l,r]
[l,r],我们就直接开始搜索我们的
s
u
m
[
l
−
1
]
sum[l-1]
sum[l−1]即可,如果最后找到的位置大于我们的r的话,就意味我们的序列是找不到这个点的,反之即找得到,输出2即可下面是具体的代码部分:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
inline ll read() {
ll x=0,w=1;char ch=getchar();
for(;ch>'9'||ch<'0';ch=getchar()) if(ch=='-') w=-1;
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
return x*w;
}
#define maxn 1000000
#define ll_maxn 0x3f3f3f3f3f3f3f3f
const double eps=1e-8;
int n,q;
int a[maxn];int sum[maxn],yihuo_sum[maxn];
map<int,vector<int> >mp[3];
int main() {
n=read();q=read();
for(int i=0;i<n;i++) {
a[i]=read();
sum[i]=a[i];yihuo_sum[i]=a[i];
if(i>0) {
sum[i]+=sum[i-1];yihuo_sum[i]^=yihuo_sum[i-1];
}
}
for(int i=0;i<n;i++) {
mp[i%2][yihuo_sum[i]].push_back(i);
}
int l,r;
for(int i=1;i<=q;i++) {
l=read();r=read();
l--,r--;
if((yihuo_sum[r]^(l==0?0:yihuo_sum[l-1]))!=0) {
printf("-1\n");
continue;
}
if((sum[r]-(l==0?0:sum[l-1]))==0) {
printf("0\n");
continue;
}
if((r-l+1)&1||a[l]==0||a[r]==0) {
printf("1\n");
continue;
}
int aha=(l==0?0:yihuo_sum[l-1]);
auto it=lower_bound(mp[l%2][aha].begin(),mp[l%2][aha].end(),l+1);
int k=(it==mp[l%2][aha].end()?n:*it);
if(k<r) {
printf("2\n");
}else {
printf("-1\n");
}
}
return 0;
}