CF703D Mishka and Interesting sum

Posted by bl00dshooter on Wed, 03 Nov 2021 07:09:14 +0100

General idea of the topic

given n n Sequence of n numbers a a a.

m m m operations.

There is one type of operation:

  • l r: find a l ∼ a r a_l\sim a_r In al ∼ ar ∼ the XOR sum of numbers occurs even times.

1 ≤ n , m ≤ 1 0 6 1\le n,m\le 10^6 1≤n,m≤106, 1 ≤ a i ≤ 1 0 9 1\le a_i\le 10^9 1≤ai​≤109.

Problem solving ideas

Try to change the meaning of the question. You can see that there are even numbers of XOR and = = =The XOR and sum of a number of occurrences (multiple occurrences count as one) xor ⁡ \operatorname{xor} xor is the exclusive or sum of all numbers.
∵ x xor ⁡ x = 0 ∴ place have number of different or and in = Out present even number second of number of different or and xor ⁡ Out present odd number second of number of different or and . ∴ Out present even number second of number of different or and = Out present too number ( Out present many second count one individual ) of different or and xor ⁡ place have number of different or and . \Because x \ operatorname{xor} x = 0 \ \ \ \ therefore among the exclusive or sum of all numbers = the exclusive or sum of numbers with even times and \ operatorname{xor} the exclusive or sum of numbers with odd times. \ \ n\ Thereforethe XOR sum of numbers that occur even times = the XOR sum of numbers that occur (count one for multiple occurrences) and the XOR sum of \ operatorname{xor} all numbers. ∵ xxorx=0 ∵ in the xor sum of all numbers = the xor sum of numbers with even times and xor sum of numbers with odd times. ‡ xor sum of numbers with even number of occurrences = xor sum of numbers with even number of occurrences (multiple occurrences count as one) and xor sum of all numbers.
All XORs and can be prefixed and maintained.

The XOR sum of the number of occurrences (counting one for multiple occurrences) can linearly record the position of each number before it occurs p r e pre pre.

If he is the first number to appear, his previous position is 0 0 0.

Then use the tree array to maintain.

That is, every time this point x x x plus a [ x ] a[x] a[x], and p r e [ x ] pre[x] pre[x] plus a [ x ] a[x] a[x].

So if a number has appeared in this interval, then in this interval of the tree array, it only xor ⁡ \operatorname{xor} xor has passed an odd number of times.

Queries can be considered offline and sorted according to the right endpoint O ( n log ⁡ n ) \mathcal{O}(n \log n) The time complexity of O(nlogn).

Refer to code for details.

CODE

#include <bits/stdc++.h>

using namespace std;

inline int read()
{
	int x = 0, f = 1;
	char c = getchar();
	while(c < '0' || c > '9')
	{
		if(c == '-') f = -1;
		c = getchar();
	}
	while(c >= '0' && c <= '9')
	{
		x = x * 10 + c - '0';
		c = getchar();
	}
	return x * f;
}

inline void write(int x)
{
	if(x < 0)
	{
		putchar('-');
		x = -x;
	}
	if(x > 9) write(x / 10);
	putchar(x % 10 + '0');
}

const int _ = 1000007;

int c[_];

int n, m;

int a[_];

int sum[_];

int pre[_];

map<int, int> d;

int ans[_];

inline int lowbit(int x)
{
	return x & -x;
}

inline void update(int x, int val)
{
	if(x == 0) return;
	for(int i = x; i <= n; i += lowbit(i))
		c[i] ^= val;
}

inline int query(int x)
{
	if(x == 0) return 0;
	int res = 0;
	for(int i = x; i; i -= lowbit(i)) res ^= c[i];
	return res;
}

struct abc
{
	int l, r, id;
} q[_];

bool cmp(abc a, abc b)
{
	return a.r < b.r;
}

signed main()
{
	n = read();
	for(int i = 1; i <= n; ++i)
	{
		a[i] = read();
		sum[i] = sum[i - 1] ^ a[i];
		pre[i] = d[a[i]];
		d[a[i]] = i;
	}
	m = read();
	for(int i = 1; i <= m; ++i)
	{
		q[i].l = read();
		q[i].r = read();
		q[i].id = i;
	}
	sort(q + 1, q + m + 1, cmp);
	int tot = 0;
	for(int i = 1; i <= m; i++)
	{
		while(tot < q[i].r)
		{
			tot++;
			update(tot, a[tot]);
			update(pre[tot], a[pre[tot]]);
		}
		ans[q[i].id] = query(q[i].r) ^ query(q[i].l - 1) ^ sum[q[i].r] ^ sum[q[i].l - 1];
	}
	for(int i = 1; i <= m; ++i)
		cout << ans[i] << "\n";
	return 0;
}

Topics: Binary Indexed Tree