题解:P17667 [IO 2025 #3] 帕格斯利的神秘实验
题意
给定一个长度为
思路
首先我们需要了解有关异或的一些性质:
-
交换律:
x \oplus y = y \oplus x 。 -
结合律:
x \oplus y \oplus z = x \oplus (y \oplus z) 。 -
恒等律:
x \oplus 0 = x 。 -
归零率:
x \oplus x = 0 。
回到题目,令
这时我们发现,因为
所以
所以
又因为
所以
令
这时我们发现 map当一个桶统计数量即可。
复杂度分析
时间复杂度为
代码
#include<bits/stdc++.h>
using namespace std;
const int N=200005;
int n,q,tot,a[N];
map<int,int>num;
int main()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++) tot^=a[i],num[a[i]]++;
cin>>q;
while(q--)
{
int k,i,v;
cin>>k;
if(k==1)
{
cin>>i>>v;
i++;
num[a[i]]--,num[v]++;
tot^=a[i],tot^=v;
a[i]=v;
}
else cout<<num[tot]<<'\n';
}
}