跳到正文

题解

题解:AT_abc274_h [ABC274Ex] XOR Sum of Arrays

Purslane约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年4月15日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 3 分钟
获赞
2

版本

  1. v1当前

Solution

本题显然只需要能够快速判断,给定序列 aa,bb,cc,是否有 S(a,b)=cS(a,b) =c。

想到哈希。考虑一般的哈希形如,给定基数 BB,求出 ∑i=1naiBi mod p\sum_{i=1}^n a_i B^i \bmod p 的结果,其中 pp 是一个质数。

但是这样做,知道 aa 和 bb 的哈希值并不能推出 S(a,b)S(a,b) 的哈希值。

考虑引入一些二进制运算。将 aa 看作 {0,1}k\{0,1\}^k 上的向量,使用 xor\rm xor 代替加法,and\rm and 代替乘法。

这样 ⨁i=1naiBi\bigoplus_{i=1}^n a_i B^i 就可以直接运算了。(原因:(a and c) xor (b and c)=(a xor b) and c(a \text{ and } c) \text{ xor } (b \text{ and } c) = (a \text{ xor } b) \text{ and } c,也就是有分配律)。

可以倍增实现。不过为了体现这个哈希方式是可逆的,我写了二分。 不知道为什么,二分和倍增都是两个 log 的,但是二分过不去。可能是因为矩阵乘矩阵比较多,T 飞了。

还是放了一个倍增的代码:

C++
#include<bits/stdc++.h>
#define ffor(i,a,b) for(int i=(a);i<=(b);i++)
#define roff(i,a,b) for(int i=(a);i>=(b);i--)
#define ull unsigned long long
using namespace std;
const int MAXN=5e5+10;
int n,q;
struct Mat {ull v[61];}B;
ull aa[MAXN];
Mat operator *(Mat A,Mat B) {Mat C; memset(C.v,0,sizeof(C.v));ffor(i,0,59) ffor(j,0,59) if(A.v[i]&(1ll<<j)) C.v[i]^=B.v[j];return C;}
ull operator *(ull A,Mat B) {ull C=0;ffor(j,0,59) if(A&(1ll<<j)) C^=B.v[j];return C;}
ull pre[MAXN][21];
int main() {
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>q;
	ffor(i,1,n) cin>>aa[i],pre[i][0]=aa[i];
	mt19937 myrand(time(NULL));
	ffor(i,0,59) ffor(j,0,59) if(myrand()&1) B.v[i]|=(1ll<<j);
	for(int len=1;len<=20;len++,B=B*B) for(int l=1,r=(1<<len);r<=n;l++,r++) pre[l][len]=(pre[l][len-1]*B)^pre[l+(1<<len-1)][len-1];
	ffor(i,1,q) {
		int a,b,c,d,e,f;
		cin>>a>>b>>c>>d>>e>>f;
		int lim=min(b-a+1,f-e+1),ans=0;
		roff(j,20,0) if(ans+(1<<j)<=lim&&(pre[a+ans][j]^pre[c+ans][j]^pre[e+ans][j])==0) ans+=(1<<j);
		if(ans==f-e+1) cout<<"No\n";
		else if(ans==b-a+1) cout<<"Yes\n";
		else if((aa[a+ans]^aa[c+ans])<aa[e+ans]) cout<<"Yes\n";
		else cout<<"No\n";
	}
	return 0;
}
原文