1230 Element Lookup
Title Description Description
Given n positive integers, then there are M queries, each asking an integer asking if the integer has occurred in n positive integers.
Enter a description input Description
The first line is two integers n and M.
Second row n positive integers (1<=n<= 100000)
The third row of M integers (1<=m<=100000)
outputs description output Description
Total M line, if present, output yes, otherwise output no
sample input to sample
4 2
2 1 3 4
1 9
Sample output Sample outputs
YES
NO
data size & Hint
All data are not more than 10^8
——————————————— Split Line ———————————————
Hash Table Exercises
#include"bits/stdc++.h"using namespacestd;Const intMOD =10000007 ;Const intMAXN = 1e5 +1e3; typedefLong LongQaq;intCNT;intnext[MOD <<1], head[MOD <<1], end[MOD <<1 ] ;inta[MAXN]; inlineintINPUT () {intx =0, F =1;CharCH =GetChar (); while(Ch <'0'||'9'< CH) {if(ch = ='-') F =-1; CH =GetChar ();} while('0'<= CH && Ch <='9') {x = (x <<1) + (x <<3) + CH-'0'; CH =GetChar ();} returnX *F;} InlineintHash_function (intx) {return((x MoD) + MoD)%MOD;} InlineBOOLFind (Const inttmp) { intpos =hash_function (TMP), y; for(inti=head[POS]; y = end[i], I; i = next[i])if(y = = tmp)return true ; return false ; } InlinevoidHash_link (Const intTmpConst intPOS) {next[++CNT] =head[POS]; head[POS]=CNT; end[CNT]=tmp;} InlinevoidHash_add (Const inttmp) { intpos =hash_function (TMP), y; for(inti=head[POS]; y = end[i], I; i = next[i])if(y = = tmp)return ; Hash_link (TMP, POS); }intMain () {intN = INPUT (), Q =INPUT (); for(intI=1; I<=n; ++i) {a[i]=INPUT (); if( !Find (a[i])) Hash_add (a[i]); } while(Q-- ) { if(Find (INPUT ())) cout <<"YES"<<Endl; Elsecout <<"NO"<<Endl; } return 0 ; }
View Code
2016-10-26 00:14:00
CODE[VS] 1230