XOR and Favorite Number

版权声明:菜鸟一枚~~ 有想法可在下面评论, 转载标明出处即可。 https://blog.csdn.net/KLFTESPACE/article/details/88752961

参见https://blog.csdn.net/Tc_To_Top/article/details/50736291

有个WA问题, 就是数组大小要开大............

#include<iostream>
#include<cmath>
#include<algorithm>

using namespace std;

const int MAX = 1000000+5;
const int MAXK = 10000000+5;

typedef long long LL;

int tt = sqrt(MAX);
LL pre[MAX], a[MAX], be[MAX], ans[MAX], num;
LL cnt[MAXK];
int n, m, k;

struct Query{
	int l, r, id;
}q[MAX];

bool cmp(Query a, Query b)
{	
	if(be[a.l] == be[b.l])
		return be[a.r] < be[b.r];
		
	return be[a.l] < be[b.l];

	/*
    if(a.l / tt == b.l / tt)
        return a.r / tt < b.r / tt;
    return a.l / tt < b.l / tt;
	*/
}

void Add(int x)
{
	num += cnt[x^k];
	cnt[x]++;	
}

void Sub(int x)
{
	cnt[x]--;
	num -= cnt[x^k];
}

int main()
{
	
	cin >> n >> m >> k;
	
	for(int i=1; i<=n; i++){
		cin >> a[i];
		
		be[i] = (i-1)/tt+1;
		pre[i] = pre[i-1]^a[i];
	}
	
	for(int i=0; i<m; i++){
		cin >> q[i].l >> q[i].r;
		q[i].id = i;
		
		q[i].l -= 1;
	}
	
	sort(q, q+m, cmp);

	int l = 0, r = 0;
	num = 0;
	cnt[0] = 1;
	
	for(int i=0; i <m; i++){
		while(l < q[i].l){
			Sub(pre[l]);
			l++;
		}
		while(l > q[i].l){
			l--;
			Add(pre[l]);
		}
		while(r < q[i].r){
			r++;
			Add(pre[r]);
		}
		while(r > q[i].r){
			Sub(pre[r]);
			r--;
		}
		
		ans[q[i].id] = num;
	}
	
	for(int i=0; i<m; i++){
		cout << ans[i] << endl;
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/KLFTESPACE/article/details/88752961
今日推荐