【简要题面】有四组数字 a , b , c , d a,b,c,d a,b,c,d。 a , b , c a,b,c a,b,c的个数小于1000,d的个数小于1e5。对于每个d求是否存在 a i + b i + c i = d a_i+b_i+c_i=d ai+bi+ci=d( a i 、 b i 、 c i a_i、b_i、c_i ai、bi、ci均为 a 、 b 、 c a、b、c a、b、c数组中的一个数)。
【分析】
由于
a
、
b
、
c
a、b、c
a、b、c中数字的大小未定,所以我们很可惜的不能用f数组进行预处理,实际上复杂度也不允许我们这么做。比较巧妙的做法是先将
a
、
b
a、b
a、b中的数字先相加用map存,再在读入d数组的时候枚举a数组和d数组相减,并在map中查询是否存在
d
i
−
a
j
d_i-a_j
di−aj。鉴于map自带
l
o
g
n
+
s
t
l
常
数
log_n+stl常数
logn+stl常数所以我们可以用
H
a
s
h
t
a
b
l
e
Hash table
Hashtable代为实现。
【code】
#pragma GCC optimize(2)
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1100;
const int mod=1e6+7;
const int key=1e6+9;
int la,lb,lc,ld;
int a[maxn],b[maxn],c[maxn];
struct hash_table{
struct node{
int val,nxt;
}num[mod<<1];
int head[mod],tot;
void add(int x){int p=1ll*x*key%mod;num[++tot]=(node){x,head[p]},head[p]=tot;}
bool find(int x){
int p=1ll*x*key%mod;
for(int i=head[p];i;i=num[i].nxt){
if(num[i].val==x)return 1;
}
return 0;
}
}mp;
inline void read(int &x){
x=0;char tmp=getchar();int fl=1;
while(tmp<'0'||tmp>'9'){if(tmp=='-')fl=-fl;tmp=getchar();}
while(tmp>='0'&&tmp<='9') x=(x<<1)+(x<<3)+tmp-'0',tmp=getchar();
x=x*fl;
}
inline bool check(int x){
for(int i=1;i<=la;i++){
if(x-a[i]<0) break;
if(mp.find(x-a[i]))return 1;
}
return 0;
}
int main(){
// freopen("game.in","r",stdin);
// freopen("game.out","w",stdout);
cin>>la>>lb>>lc;
for(int i=1;i<=la;i++) read(a[i]);
for(int i=1;i<=lb;i++) read(b[i]);
for(int i=1;i<=lc;i++) read(c[i]);
if(la>lb) swap(a,b),swap(la,lb);
if(la>lc) swap(a,c),swap(la,lc);
for(int i=1;i<=lb;i++)
for(int j=1;j<=lc;j++)
if(!mp.find(b[i]+c[j]))
mp.add(b[i]+c[j]);
sort(a+1,a+la+1);
cin>>ld;
while(ld--){
int x;read(x);
if(check(x)) printf("YES\n");
else printf("NO\n");
}
return 0;
}