4507. 子数组异或和 异或前缀和+位运算

4507. 子数组异或和 - AcWing题库

 关于异或运算,有以下性质:

① 0^a=a

② a^a=0

③ a^b^b=a

④ a^b=c  =>  a^b^b = c^b   =>  a=c^b

⑤ sum[i]表示i结尾的异或前缀和

sum[l-r]=sum[r]^sum[l-1]

利用以上性质,可以将原问题转换为两端点的异或前缀和要相等。

#include<bits/stdc++.h>
using namespace std;
#pragma warning(disable:4996);
#define int long long 
#define endl '\n'
#define rep(j,b,e) for(int j=(b);j<=(e);j++)
#define drep(j,e,b) for(int j=(e);j>=(b);j--)
const int N = 2e5 + 10;
int mod = (1e7) + 7;
int T = 1;
int n, m, x, k;
vector<int>arr(1);
vector<int>ans;
signed main() {
#ifndef ONLINE_JUDGE
	//freopen("out.txt", "w", stdout);
#endif
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	//|____|___|
	//s-2m s-m s
	//s^s-m=s-m^s-2m =>s^s-2m=s-m^s-m=0 =>s=s-2m
	cin >> n;
	unordered_map<int, int>even, odd;
	rep(j, 1, n) {
		cin >> k;
		arr.push_back(arr[j-1]^k);//异或前缀和
	}
	int ans = 0;
	even[0]++;
	rep(j, 1, n) {
		k = arr[j];
		if (j % 2 == 0) {
			ans += even[k];
		}
		else {
			ans += odd[k];
		}
		if (j % 2 == 0)even[k]++;
		else odd[k]++;
	}
	//区间长为偶,须左右端点同奇同偶
	cout << ans << endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值