当前位置:网站首页>XOR prefix and +map maintenance

XOR prefix and +map maintenance

2022-06-13 04:17:00 csx_ zzh

give n and n Number , Seek differences or make peace 0 Number of sub segments of
set up a[i] Number entered for
b[i] Is an XOR prefix and
b[i] = a[1] ^ a[2] ^ … ^ a[i - 1] ^ a[i]
A number is known x^y = 0 If and only if x == y It was established
So for a paragraph [1,r] Come on , The XOR prefix and are b[r], So if you want to r Is exclusive or is 0 The right half of the sub segment of , Then you only need a number in front of you b[i] == b[r], that [i + 1,r] This XOR and sum is 0
Like
about x1,x2,x3…xr, If you want to find a range [i,r] Make the exclusive or sum of its intervals be 0, set up x1,x2,x3…xj The exclusive or and are b[j] So if b[j] == b[r], in other words b[j] ^ b[j+1,r] = b[r] And only x ^ y == x If and only if y = 0 It was established .

Then you just need the prefix XOR and +map Maintenance is enough

#include <iostream>
#include <map>
#define ll long long
using namespace std;
const int maxn = 2e5 + 5;
std::map<ll, ll> mp;
ll a[maxn],b[maxn];
int main(){
    
	int n;
	cin >> n;
	ll ans = 0;
	for(int i = 1; i <= n; i++){
    
		cin >> a[i];
		b[i] = a[i] ^ b[i - 1];
		if(b[i] == 0)ans++;
		ans += mp[b[i]];
		mp[b[i]]++;
		cout << ans << endl;
	}
	cout << ans << endl;
	return 0;
}
原网站

版权声明
本文为[csx_ zzh]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280524206852.html