CODEVS:http://codevs.cn/problem/1230/
题目描述 Description
给出n个正整数,然后有m个询问,每个询问一个整数,询问该整数是否在n个正整数中出现过。
输入描述 Input Description
第一行两个整数 n 和m。
第二行n个正整数(1<=n<= 100000)
第三行m个整数(1<=m<=100000)
输出描述 Output Description
一共m行,若出现则输出YES,否则输出NO
样例输入 Sample Input
4 2
2 1 3 4
1 9
样例输出 Sample Output
YES
NO
数据范围及提示 Data Size & Hint
所有数据都不超过10^8
题解
就是把一个数对一个大质数取模,但是这样还是会有冲突于是就建立一个链表把元素存在里面。
/*
作者:WZH
题目:p1230 元素查找
*/
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int MOD = 350899;
int head[400000],a[1000000],ans,n,m,e = 1;
unsigned long long b,x;
struct node{
int v,next;
}D[1000000];
inline unsigned long long read(){
long long x = 0, f = 1;char ch = getchar();
while(ch < '0' || '9' < ch) { ch = getchar(); }
while('0' <=ch && ch<='9') { x = x * 10 + ch - '0';ch = getchar(); }
return x * f;
}
inline void add(int u,int v){
D[e].v = v;D[e].next = head[u];head[u] = e++;
}
inline void hash(unsigned long long x){
b = x % MOD;
add(b,x);
}
inline bool check(int u,unsigned long long x){
for(int i = head[u];i;i = D[i].next)
if(D[i].v == x) return false;
return true;
}
inline void init(){
n = read();m = read();
for(int i = 1;i <= n;i++)
a[i] = read();
for(int i = 1;i <= n;i++)
hash(a[i]);
for(int i = 1;i <= m;i++){
x = read();b = x % MOD;
if(!check(b,x))puts("YES");else puts("NO");
}
}
int main(){
init();
}