P8773

· · 题解

题目大意

给定长度为 n 的序列,m 次询问 [l_i,r_i] 内有无两数异或和为 x。

思路分析

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

令 c = a\oplus x

\Leftrightarrow c \oplus b=0 \Leftrightarrow c\oplus(c\oplus b)=0\oplus c\Rightarrow b=c

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

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

考虑莫队求解即可。

代码实现

#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;
}