Codeforces Round #340 (Div. 2) E. XOR and Favorite Number

因为x^k=y,x^x=0,所以求出异或前缀和, s u m i sum_i sumi^k= s u m j sum_j sumj,则区间i+1到j,异或值=k。所以我们运用莫队进行统计,唯一的问题在于,这次维护的区间l~r,表示的l+1~r的值,所以莫队维护的区间是左开右闭。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#pragma GCC optimize(2)
#define up(i,a,b)  for(int i=a;i<b;i++)
#define dw(i,a,b)  for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
	char ch = getchar(); ll x = 0, f = 1;
	while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 2e5 + 10;
map<int, int>mp;
int siz;
int n, m, k;
int a[N], sum[N], sum_ls[N], xor_sum[N];
ll vis[3 * N];
int belong[N];
struct qr {
	int l, r, id;
	bool operator<(const qr a)const {
		if (belong[l] == belong[a.l])
			return r < a.r;
		else return l < a.l;
	}
}m_qr[N];
int getid(int x)
{
	return mp[x] ? mp[x] : mp[x] = ++siz;
}
ll ans[N];
int l = 1, r = 0;
ll adsum = 0;
void add(int x,int y)
{
	adsum += vis[x];
	vis[y]++;
}
void del(int x,int y) {
	vis[y]--;
	adsum -= vis[x];
}
int main()
{
	n = read(); m = read(); k = read();
	int block = sqrt(n);
	upd(i, 1, n)a[i] = read(), belong[i] = (i - 1) / block + 1;
	upd(i, 1, n)sum[i] = sum[i - 1] ^ a[i], xor_sum[i] = sum[i] ^ k;
	upd(i, 1, n) { 
		sum_ls[i] = getid(sum[i]);
		xor_sum[i] = getid(xor_sum[i]);
	}
	sum_ls[0] = getid(0); vis[sum_ls[0]]++;
	xor_sum[0] = getid(k);
	int l1, r1;
	upd(i, 1, m)
	{
		l1 = read(), r1 = read();
		m_qr[i].l = l1; m_qr[i].r = r1; m_qr[i].id = i;
	}
	sort(m_qr + 1, m_qr + m + 1);
	upd(i, 1, m)
	{
		while (l < m_qr[i].l)del(xor_sum[l - 1], sum_ls[l - 1]), l++;
		while (l > m_qr[i].l)--l, add(xor_sum[l - 1], sum_ls[l - 1]);
		while (r < m_qr[i].r)r++,add(xor_sum[r], sum_ls[r]);
		while (r > m_qr[i].r)del(xor_sum[r], sum_ls[r]),r--;
		ans[m_qr[i].id] = adsum;
	}
	upd(i, 1, m)printf("%lld\n", ans[i]);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值