[ABC185F] Range Xor Query 题解

思路

这道题,线段树模版题。

  • 操作 \(1\):将 \(A_X\) 替换为 \(A_X \oplus Y\)(单点修改)
  • 操作 \(2\):输出 \(A_{X}\oplus A_{X+1}\oplus A_{X+2}\oplus\dots\oplus A_Y\)(区间异或和)。

于是这道题只需要维护一个支持单点修改,区间求和的数据结构即可,直接在线段树模版上改一改就行了。

代码如下

#include <bits/stdc++.h>

using namespace std;

// #define int long long
#define rep(i, l, r) for(int i = l; i <= r; ++ i)
#define per(i, r, l) for(int i = r; i >= l; -- i)
#define me0(a); memset(a, 0, sizeof a);
#define me3(a); memset(a, 0x3f, sizeof a);
#define PII pair<int, int>

const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;

inline void read(int &n)
{
    bool w = 0;
    char c = getchar();
    for(; c < 48 || c > 57; c = getchar())
        w = c == 45;
    for(n = 0; c >= 48 && c <= 57; c = getchar())
        n = n * 10 + c - 48;
    n = w ? -n : n;
}

inline void write(int x, char a)
{
    char c[40], s = 0;
    if(x < 0) putchar(45), x = -x;
    for(; x ;) c[s ++] = x % 10, x /= 10;
    if(!s) putchar(48);
    for(; s -- ;) putchar(c[s] + 48);
    putchar(a);
}

const int MAXN = 1000001;
#define ls num + num
#define rs num + num + 1
int sum[MAXN << 2], p[MAXN], addans;
void update(int num) {sum[num] = sum[ls] ^ sum[rs];}
void build(int num, int l, int r)
{
	if(l == r)
	{
		sum[num] = p[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(ls, l, mid);
	build(rs, mid + 1, r);
	update(num);
}
void find(int num, int l, int r, int x, int y)
{
	if(x <= l && r <= y)
	{
		addans ^= sum[num];
		return;
	}
	int mid = (l + r) >> 1;
	if(mid >= x) find(ls, l, mid, x, y);
	if(mid < y) find(rs, mid + 1, r, x, y);
}
void change(int num, int l, int r, int pos, int val)
{
	if(l == r)
	{
		sum[num] ^= val;
		return;
	}
	int mid = (l + r) >> 1;
	if(pos <= mid) change(ls, l, mid, pos, val);
	else change(rs, mid + 1, r, pos, val);
	update(num);
}
int n, Q;
main()
{
	read(n); read(Q);
    rep(i, 1, n) read(p[i]);
    build(1, 1, n);
	rep(i, 1, Q)
	{
		int c, x, y;
		read(c); read(x); read(y);
		if(c == 1)
			change(1, 1, n, x, y);
		else
		{
			addans = 0;
			find(1, 1, n, x, y);
			write(addans, '\n');
		}
	}
}
posted @ 2023-07-01 10:53  liukejie  阅读(11)  评论(0编辑  收藏  举报