求异或和以及将a[x]修改成y

单求异或和

#include <bits/stdc++.h>
using namespace std;
int a[10010];
int s[10010];
int n;
int l,r;
int ans;
int main()
{
	cin >> n;
	for (int i=1;i<=n;i++) cin >> a[i];
	for (int i=1;i<=n;i++) s[i] = s[i-1]^a[i];
	cin >> l >> r;
	ans = s[l-1]^s[r];
    return 0;
}

都求

#include <bits/stdc++.h>
using namespace std;
long long a[100010];
long long c[100010];
long long n,m;
long long lowbit(long long x)
{
	return (x&(-x));
}
void add(long long w,long long x)
{
	while(w <= n)
	{
		c[w] ^= x;
		w += lowbit(w);
	}
}
long long getsum(long long w)
{
	long long res=0;
	while(w)
	{
		res ^= c[w];
		w -= lowbit(w);
	}
	return res;
}
int main()
{
	cin >> n >> m;
	for (int i=1;i<=n;i++) cin >> a[i];
	for (int i=1;i<=n;i++) add (i,a[i]);
	for (int i=1;i<=n;i++)
	{
		int op;
		cin >> op;
		long long x,y;
		cin >> x >> y;
		if (op == 1)
		{
			long long ans = getsum(x-1)^getsum(y);
			cout << ans << endl;
		}
		else
		{
			long long cnt = a[x]^y;
			add(x,cnt);
			a[x]=y;
		}
	}
    return 0;
}
posted @ 2026-03-24 11:37  msjing  阅读(8)  评论(0)    收藏  举报