补题记录:Codeforces Round #832 (Div. 2) D题(1747)

传送门: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)

主要思路:

  1. 首先我们需要明白一个性质,那就是对于一个区间 [ l , r ] [l,r] [l,r],如果最后全部的数字都是0的话,那就意味着对于这个区间来说有a[l]^a[l+1]^a[l+2]^....^a[r]=0
  2. 我们还需要明白此题的一个性质,那就是对于我们的每一次操作,我们都只能选择一个奇数长度的区间,假设此区间的异或和为 X X X,那么显然的我们即使异或了这个区间的每一个数,我们最后的异或和还是 X X X,因为每一对相同的数字异或得到的是0,并且0和任何数字异或相当于没有异或,所以这样的话我们最后会剩下一个 X X X,也就是说我们的最后的异或和就是原值.所以对于我们的操作是不会改变异或和的.
  3. 所以假设我们刚开始的区间异或和并不是0的话,那就意味了我们无论进行多少次操作都不会使之变为0,也就意味着答案肯定是-1
  4. 对于我们的区间异或和为0的话,假设我们的区间长度为奇数(因为我们只能改变奇数长度的序列),我们只要一次操作即可
  5. 对于我们的区间异或和为0并且我们的区间长度为偶数的话,我们需要找到区间中的一个点K,使 [ l , k ] , [ k , r ] 两个区间的区间异或和全都为 0 [l,k],[k,r]两个区间的区间异或和全都为0 [l,k],[k,r]两个区间的区间异或和全都为0,因为只有这样最后我们才能将整个序列的值都变为0.那我们要怎么才能找到这样的一个点呢.我们可以存储每一个异或和位置的奇偶性以及该异或和的位置,对于这一点我们可以使用map+vector来解决,因为显然我们的数据是离散的,并且可以存在1对多的情况,并且我们后面可能需要使用lowerbound函数所以就不用mulmap了.那么对于我们的每一个区间 [ l , r ] [l,r] [l,r],我们就直接开始搜索我们的 s u m [ l − 1 ] sum[l-1] sum[l1]即可,如果最后找到的位置大于我们的r的话,就意味我们的序列是找不到这个点的,反之即找得到,输出2即可
  6. 对于我们的偶数长度,其实还需要一个特判,因为假设我们的序列的两端点的值恰好有一个为0的话(注意只需要一个为0即可),这样就可以将其化简为奇数的情况了,此时需要输出1

下面是具体的代码部分:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <stack>
#include <deque>
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值