跳到正文

个人记录

P9533 [YsOI2023] 区间翻转区间异或和 题解

LuoFeng_Nanami约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟

版本

  1. v1当前

Solution

题目中没有规定翻转操作次数,所以题目就被转化成了区间异或和为 00 的区间个数。

考虑先求出异或前缀和,令 xorsumixorsum_i 表示前 ii 个数的异或和。如果区间 [l,r][l,r] 的异或和为 00,就说明 xorsuml−1xor⁡xorsumr=0xorsum_{l-1} \operatorname{xor} xorsum_r=0,也就是 xorsuml−1=xorsumrxorsum_{l-1}=xorsum_r。

求出前缀和后使用 map 计数每一个数字有多少个前缀和等于那个数字即可。

Code

C++
#include<bits/stdc++.h>
#define int long long

using namespace std;

const int inf = 0x3f3f3f3f,mod = 1e9 + 7;
const int maxn = 2e5 + 7;

map<int,int> m;
int n,x;
int ans,cur;//cur存当前的异或前缀和

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> n;
	m[0] = 1;
	while(n--){
		cin >> x;
		cur ^= x;
		ans += m[cur];
		m[cur]++;
	}
	cout << ans;

	return 0;
}
原文