跳到正文

题解

P8773

0xFF约 4 分钟
原文
文章信息与版本

文章信息

发布
2022年11月22日
收录
2026年1月6日
更新
2026年11月10日
篇幅
约 4 分钟
题目
P8773

版本

  1. v1当前

题目大意


给定长度为 nn 的序列,mm 次询问 [li,ri][l_i,r_i] 内有无两数异或和为 xx。

思路分析


a⊕b=x⇒a⊕b⊕x=x⊕x=0a \oplus b=x \Rightarrow a \oplus b \oplus x=x\oplus x= 0

令 c=a⊕xc = a\oplus x

⇔c⊕b=0\Leftrightarrow c \oplus b=0 ⇔c⊕(c⊕b)=0⊕c⇒b=c\Leftrightarrow c\oplus(c\oplus b)=0\oplus c\Rightarrow b=c

综上,可以由 a⊕b=x⇒a⊕x=ba\oplus b=x\Rightarrow a\oplus x = b

原题就转化成了给定一个区间 [li,ri][l_i,r_i] 对于 aa 有无 a⊕xa\oplus x 与之对应。

考虑莫队求解即可。

代码实现


C++
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<deque>
#include<queue>
#include<map>
#include<vector>
#define int long long//
using namespace std;
const int INF = 2e15;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
const int M = (1<<20) + 10;

inline int read(){
	int x=0,f=1;char ch=getchar();
	for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-1;
	for(; isdigit(ch);ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
	return x*f;
}
struct Node{
	int id,l,r;
}q[N<<1];
int a[N],cnt[M],ans[N],belong[N];
bool cmp(Node x,Node y){
	if(belong[x.l] == belong[y.l]) return x.r < y.r;
	else return belong[x.l] < belong[y.l];
}
int n,m,x,res = 0;
void Add(int t){
	if(a[t] > M) return;
	cnt[a[t]]++;
	res += cnt[a[t] ^ x];
}
void Del(int t){
	if(a[t] > M) return;
	cnt[a[t]]--;
	res -= cnt[a[t] ^ x];
}
signed main(){
	n = read() , m = read() , x = read();
	int siz = sqrt(n);
	for(int i=1;i<=n;i++) a[i] = read() , belong[i] = i / siz;
	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 L = 1 , R = 0;
	for(int i=1;i<=m;i++){
		while(L > q[i].l) Add(--L);
		while(L < q[i].l) Del(L++);
		while(R < q[i].r) Add(++R);
		while(R > q[i].r) Del(R--);
		ans[q[i].id] = res;
	}
	for(int i=1;i<=m;i++){
		if(ans[i] > 0) cout<<"yes\n";
		else cout<<"no\n";
	}
	return 0;
}
原文
存档评论2 条抓取于