Problem:


Search II

You are given a sequence of n integers S and a sequence of different q integers T. Write a program which outputs C, the number of integers in T which are also in the set S.

Input

In the first line n is given. In the second line, n integers are given. In the third line q is given. Then, in the fourth line, q integers are given.

Output

Print C in a line.

Constraints

Elements in S is sorted in ascending order

n ≤ 100000

 q ≤ 50000 

0 ≤ an element in S ≤ 109

0 ≤ an element in T ≤ 109

Sample Input 1

5
1 2 3 4 5
3
3 4 1

Sample Output 1

3

Sample Input 2

3
1 2 3
1
5

Sample Output 2

0

Sample Input 3

5
1 1 2 2 3
2
1 2

Sample Output 3

2

Notes

Template in C
-->



Sample input:
5
1 2 3 4 5
3
3 4 1


Sample output:
3

Submission status: Accepted

Solution code:
from bisect import bisect_left

ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

# https://docs.python.org/ja/3/library/bisect.html
def index(a, x):
    'Locate the leftmost value exactly equal to x'
    i = bisect_left(a, x)
    if i != len(a) and a[i] == x:
        return 1
    return 0

n = ii()
s = li()

q = ii()
t = li()

cnt = 0

for i in t:
    cnt += index(s,i)

print(cnt)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8 ff=unix


def find(S, val):
    left = 0
    right = len(S) - 1
    while left < right:
        mid = (left+right) // 2
        if S[mid] == val:
            return True
        elif S[mid] < val:
            left = mid+1
        else:
            right = mid-1
    return S[left] == val


def solv(S, T):
    count = 0
    for val in T:
        a = find(S, val)
        count += a
    return count


if __name__ == "__main__":

    _ = input()
    S = list(map(int, input().split()))
    _ = input()
    T = map(int, input().split())

    ans = solv(S, T)

    print(ans)

Submission status: Accepted

Solution code:
def bisearch(li, tgt):
    upper, lower = len(li)-1, 0
    while upper>=lower:
        mid = lower + (upper-lower)//2
        if(li[mid]==tgt):
            return mid
        elif(li[mid]>tgt):
            upper = mid-1
        else:
            lower = mid+1
        
    return -1

N = int(input())
S = [int(i) for i in input().split()]
Q = int(input())
T = [int(i) for i in input().split()]
cnt = 0
for t in T:
    if(bisearch(S, t)!=-1):
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    s = LI()
    q = I()
    t = LI()
    ans = 0
    for i in t:
        j = bisect.bisect_left(s,i)
        if j < n and i == s[j]:
            ans += 1
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
n = int(input())
S = list(map(int, input().split()))
q = int(input())
T = list(map(int, input().split()))

def func(x,btm,top):
  while btm < top:
    ctr = btm + (top-btm)//2 #中央値の添え字
    if x == S[ctr]:
      return 1
    elif x < S[ctr]:
      top = ctr
    elif x > S[ctr]:
      btm = ctr + 1
  return 0

C = 0
btm = 0
top = n
for i in T:
  C += func(i,btm,top)

print(C)

Submission status: Accepted

Solution code:
#Binary search
N=int(input())
S=list(map(int,input().split()))
q=int(input())
T=list(map(int,input().split()))

def Binary_Search(item,list):
    low=0
    high=len(list)-1
    while low<=high :
        mid=(high+low)//2
        if item == list[mid]:
            return 1
        elif item < list[mid]:
            high=mid-1
        else:
            low=mid+1
    return 0

cnt=0
for i in T:
    cnt+=Binary_Search(i,S)
print(cnt)

Submission status: Accepted

Solution code:

s_num = int(input())
s = list(map(int, input().split()))
s = set(s)
q_num = int(input())
q = list(map(int ,input().split()))
q = set(q)

r = s & q

print(len(r))

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def MI(): return map(int, input().split())


def S(): return input()


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


def print_matrix(mat):
    for i in range(len(mat)):
        print(*mat[i])


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']


# show_flg = True
show_flg = False


def f(a, x):
    return a[x]


# 二分探索 ok ng
def main():
    N = I()
    S = LI()
    Q = I()
    T = LI()
    ans = 0

    for i in range(Q):
        s = 0
        t = N
        ok = s - 1
        ng = t
        target = T[i]
        while ng - ok > 1:
            mid = (ng + ok) // 2
            # print(mid, S[mid], target)
            if f(S, mid) <= target:
                ok = mid
            else:
                ng = mid

        ans += 1 if target == S[ok] else 0

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
S = list(map(int,input().split()))
q = int(input())
T = list(map(int,input().split()))

def bs(t,s,e):

    while e-s > 1:
        #print(s,e)
        m = (s+e)//2
        if S[m] <= t:
            s = m
        else:
            e = m
    if S[s] == t or S[e] == t:
        return True
    else:
        return False

ans = 0
for t in T:
    if bs(t,0,n-1):
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
*S, = map(int, input().split())
q = int(input())
*T, = map(int, input().split())
print(len(set(S) & set(T)))

Submission status: Accepted

Solution code:
import bisect

if __name__ == "__main__":
    N = int(input())
    num_list = list(map(int, input().split()))
    M = int(input())
    target_list = list(map(int, input().split()))

    count = 0
    for target in target_list:
        target_idx = bisect.bisect_left(num_list, target)
        if(num_list[target_idx] == target):
            count += 1
    print(count)

Submission status: Accepted

Solution code:
def bs(a, key):
    left = -1
    right = len(a)

    while right - left > 1:
        mid = left + (right - left) // 2

        if a[mid] >= key:
            right = mid
        else:
            left = mid

    return a[right] == key


n = int(input())
S = sorted(list(map(int, input().split())))
q = int(input())
T = list(map(int, input().split()))

print(sum([bs(S, t) for t in T]))

Submission status: Accepted

Solution code:
_ = int(input())

S = list(map(int, input().split()))
# S = list(map(int, '0 0 0 0 0 0 0 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 8 8 8 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10 10 10 11 11 11 11 11 12 12 12 12 13 13 13 14 14 14 14 14 15 15 16 16 16 16 16 16 16 16 17 17 17 17 17 17 17 17 18 18 18 18 19 19 19 19 19 19 19 19 20 20 20 20 20 21 22 23 23 23 23 24 24 24 24 24 24 25 25 25 25 26 26 26 26 27 27 27 27 27 28 28 28 28 28 29 29 29 29 29 29 29 30 30 30 30 30 30 30 30 30 30 30 30 31 31 31 31 31 31 32 32 32 32 32 32 32 32 33 33 33 34 34 34 34 35 35 35 36 36 36 37 37 37 37 37 38 38 38 40 40 40 40 41 41 41 42 42 42 42 42 43 43 43 43 43 43 43 43 44 44 44 45 45 45 45 46 46 47 47 47 47 47 47 47 47 48 48 48 48 49 49 49 49 49 49 49 49 50 50 50 51 51 51 51 51 52 52 52 52 52 53 53 53 53 53 53 53 54 54 54 54 54 54 54 55 55 55 55 55 55 55 55 56 56 56 56 56 56 56 56 57 57 57 58 58 58 58 58 58 59 59 59 59 60 60 60 60 60 61 61 61 62 62 62 63 63 63 63 63 63 63 64 64 64 65 65 65 65 65 66 66 66 67 67 67 67 68 68 68 68 68 69 69 70 70 70 71 71 71 71 71 71 71 72 72 72 72 73 73 73 73 73 73 74 74 74 74 74 74 75 75 75 76 76 76 76 77 77 77 77 77 78 78 78 79 79 79 79 79 79 79 80 80 80 80 80 80 81 81 81 82 82 82 83 83 83 83 83 83 84 84 84 85 85 85 86 86 86 87 87 87 87 87 87 88 88 88 88 88 88 89 89 89 90 90 90 90 90 91 91 91 91 92 92 92 92 92 93 93 93 93 93 93 93 93 94 94 94 94 95 95 95 95 95 95 95 95 96 96 96 96 96 96 97 97 97 97 97 97 97 98 98 98 98 99'.split()))
_ = int(input())
T = list(map(int, input().split()))
# T = list(map(int, '76 31 21 17 28 60 6 62 7 35 78 39 12 90 14 3 46 0 41 10 96 59 22 92 24 99 58 47 51 38 77 79 36 33 34 45 55 37 88 18 52 57 73 32 44 54 91 8 48 11 61 67 9 16 84 85 97 70 75 2 15 95 50 94 89 66 19 40 68 65 5 56 72 49 74 64 4 25 43 42 69 27 82 13 80 23 53 26 29 63 81 86 83 98 71 1 30 20 93 87'.split()))


def binary_search(num_list, num, left, right):
    mid = (left + right) // 2
    # print(left, mid, right)
    if num_list[mid] == num:
        # print(f"{num}がある")
        return True
    if left >= right:
        return False
    elif num_list[mid] > num:
        return binary_search(num_list, num, left, mid - 1)
    else:
        return binary_search(num_list, num, mid + 1, right)


count = 0
for num in T:
    if binary_search(S, num, 0, len(S) - 1):
        count += 1

print(count)

Submission status: Accepted

Solution code:
n = int(input())
s = [int(i) for i in input().split()]

q = int(input())
t = [int(i) for i in input().split()]

ans = 0
for i in range(q):

    left = 0
    right = n-1

    while left <= right:
        mid = (right+left) // 2

        if t[i] < s[mid]:
            right = mid -1
        elif t[i] > s[mid]:
            left = mid + 1
        else:
            ans += 1
            break

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
S = [item for item in map(int, input().split())]
q = int(input())
T = [item for item in map(int, input().split())]

c = 0
for i in range(len(T)):
    lo = 0
    hi = n
    while lo < hi:
        mid = (lo + hi) // 2
        if S[mid] == T[i]:
            c += 1
            break
        elif T[i] < S[mid]:
            hi = mid
        else:
            lo = mid+1
print(c)

Submission status: Accepted

Solution code:
n = int(input())
s = list(map(int,input().split()))
q = int(input())
t = map(int,input().split())
cnt = 0

def binary(lft,rgt,m):
    global s,n
    while lft <= rgt:
        mid = (rgt+lft)//2
        if s[mid] == m:
            return True
        elif s[mid] > m:
            rgt = mid - 1
        else:
            lft = mid + 1
    return False
    
     

for i in t:
    if binary(0,n-1,i) and (s[0]<=i and s[n-1]>=i):
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
def b_search(A, a, left=None, right=None):
    if left is None:
        left = 0
    if right is None:
        right = len(A)
    if left == right:
        return False
    mid = (left + right) // 2
    if a == A[mid]:
        return True
    elif a > A[mid]:
        return b_search(A, a, mid + 1, right)
    else:
        return b_search(A, a, left, mid)


N = int(input())
S = list(map(int, input().split()))
Q = int(input())
T = list(map(int, input().split()))
ans = 0
for t in T:
    ans += b_search(S, t)
print(ans)

Submission status: Accepted

Solution code:
n =int(input())
s = list(map(int, input().split()))

q =int(input())
t = list(map(int, input().split()))

ans = 0
for i in range(q):
    key = t[i]
    left = 0
    right = n
    mid = (left + right) // 2
    while left <= right:
        if s[mid] == key:
            ans += 1
            break
        elif s[mid] < key:
            left = mid + 1
        else:
            right = mid - 1
        mid = (left + right) // 2
print(ans)

Submission status: Accepted

Solution code:
import bisect

n = int(input())
S = list(map(int, input().split()))
q = int(input())
T = list(map(int, input().split()))

count = 0
for t in T:
    l, r = bisect.bisect_left(S, t), bisect.bisect_right(S, t)
    if l < r:
        count += 1
print(count)

Submission status: Accepted

Solution code:
import bisect

n = int(input())
S = list(map(int, input().split()))
q = int(input())
T = list(map(int, input().split()))

C = 0
for t in T:
    index = bisect.bisect_left(S, t)
    if index < len(S) and S[index] == t:
        C += 1

print(C)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
S = [int(item) for item in input().split()]
q = int(input())
T = [int(item) for item in input().split()]

cnt = 0


for i in range(q):
    temp_index = bisect.bisect_left(S, T[i])
    if len(S) != temp_index:
        if T[i] == S[temp_index]:
            cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
import copy
n=int(input())
S=list(map(int,input().split()))
q=int(input())
T=list(map(int,input().split()))

def linearSearch(A,key,n):
    i=0
    B=copy.copy(A)
    B.append(key)
    while B[i] != key:
        i=i+1
    if i==n:
        return 0
    return 1

def BinarySearch(A,key,start,end):
    if start==end:
        if A[start]!=key:
            return 0
        else:
            return 1
    mid=(start+end)//2
    if A[mid]>=key:
        return BinarySearch(A,key,start,mid)
    else:
        return BinarySearch(A,key,mid+1,end)

ans=0
for i in range(q):
    if BinarySearch(S,T[i],0,n-1):
        ans=ans+1

print(ans)

Submission status: Accepted

Solution code:
n, s = int(input()), list(set(map(int, input().split())))
q, t = int(input()), list(set(map(int, input().split())))
l = set()
tl = set()
for tt in (t):
    if tt in tl:
        l.add(tt)
        continue
    mi, md, ma = 0, len(s) // 2, len(s) - 1
    for i in range(mi, ma, 1):
        tl.add(s[mi])
        tl.add(s[md])
        tl.add(s[ma])
        if tt in tl:
            l.add(tt)
            break
        elif mi == md or md == ma:
            break
        elif s[md] < tt:
            mi = md
            md = md + ((ma - md) // 2)
        elif s[md] > tt:
            ma = md
            md = md - ((md - mi) // 2)
print(len(l))

Submission status: Accepted

Solution code:
n=int(input())
s=list(map(int,input().split()))
q=int(input())
t=list(map(int,input().split()))
ans=0

def f(i,s):
    r=n-1
    l=0
    m=(r+l)//2
    while r-l>-1:
        if s[m]==i:
            return True
        elif s[m]>i:
            r=m-1
            m=(r+l)//2
        else:
            l=m+1
            m=(r+l)//2
for i in t:
    if f(i,s):
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
S=list(map(int,input().split()))
q=int(input())
T=list(map(int,input().split()))

def bs(ken,lst):
    left=0
    right=len(lst)
    while (left<right):
        middle=(left+right)//2
        if lst[middle]==ken:
            return 1
        elif lst[middle]<ken:
            left=middle+1
        elif lst[middle]>ken:
            right=middle
    else: return 0

cnt=0
for i in T:
    cnt+=bs(i,S)
print(cnt)

Submission status: Accepted

Solution code:
def binary_search(s, t):
    top = 0
    tail = len(s)
    while top < tail:
        mid = (top + tail) // 2
        if s[mid] == t:
            return True
        elif s[mid] < t:
            top = mid + 1
        else:
            tail = mid
    return False

n = int(input())
S = [int(n) for n in input().split()]
q = int(input())
T = [int(q) for q in input().split()]

count = 0

for t in T:
    if binary_search(S, t):
        count += 1

print(count)

Submission status: Accepted

Solution code:
import sys, math
def input():
    return sys.stdin.readline()[:-1]
from itertools import permutations, combinations
from collections import defaultdict, Counter
from math import factorial
from bisect import bisect_left # bisect_left(list, value)
#from fractions import gcd

sys.setrecursionlimit(10**7)
n = int(input())
S = list(map(int, input().split()))
q = int(input())
T = list(map(int, input().split()))

#S.sort()
T.sort()
setS = set(S)

cnt = 0
for t in T:
    if t in setS:
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
import bisect

n = int(input())
S = set(list(map(int, input().split(" "))))
q = int(input())
T = set(map(int, input().split(" ")))

A = S & T
print(len(A))

Submission status: Accepted

Solution code:
import bisect
n = int(input())
s = list(map(int, input().split()))
q = int(input())
t = list(map(int, input().split()))
s,t = sorted(s), sorted(t)

ans = 0
for i in t:
    up_index = bisect.bisect_left(s,i)
    if up_index != n:
        if s[up_index] == i:
            ans += 1
    
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import copy
from typing import Tuple, List, Union
input = sys.stdin.readline


def binary_search(t, S):
    left = 0
    right = len (S)
    while left < right:
        mid = int ((left + right) / 2)
        if S [mid] == t:
            return True
        elif t < S[mid]:
            right = mid
        else:
            left = mid + 1
    return False


def main():
    n = int(input())
    S = tuple(map(int, input().split()))
    q = int(input())
    T = tuple(map(int, input().split()))
    return [binary_search(t, S) for t in T]



if __name__ == '__main__':
    print(sum(main()))

Submission status: Accepted

Solution code:
def search(s:list, target:int) -> int:
    left=0
    right=len(s)
    while left < right:
        mid = int((left+right)/2)
        if s[mid] == target:
            return mid
        if target > s[mid]:
            left=mid+1
        elif target < s[mid]:
            right=mid
    return -1

n=int(input())
s=list(map(int, input().split()))
q=int(input())
t=list(map(int, input().split()))

ans=0
for _t in t:
    if search(s, _t) != -1:
        ans+=1

print(ans)

Submission status: Accepted

Solution code:
def nibu(a,A): #a in A(sorted)
    mi = 0
    ma = len(A)-1
    while ma-mi>1:
        mid = (ma+mi)//2
        if a < A[mid]:
            ma = mid
        else:
            mi = mid
    if A[ma]==a:
        return True
    elif A[mi]==a:
        return True
    else:
        return False #a not in A
    
n = int(input())
S = list(map(int,input().split())) #sorted
q = int(input())
T = list(map(int,input().split()))


ans = 0
for i in range(q):
    if nibu(T[i],S):
        ans +=1
print(ans)

Submission status: Accepted

Solution code:
def main():
    import sys
    input=sys.stdin.readline

    n=int(input())
    s=set(map(int,input().split()))
    q=int(input())
    t=list(map(int,input().split()))
    
    ans=0
    for num in t:
      if num in s:
        ans+=1
    print(ans)
if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import bisect
import math
import sys
from collections import Counter, defaultdict, deque
from copy import copy, deepcopy
from heapq import heapify, heappop, heappush
from itertools import combinations, permutations
from queue import Queue

read = sys.stdin.read
readline = sys.stdin.readline 
readlines = sys.stdin.readlines 

def SI():
    return int(readline())
def MI():
    return map(int, readline().split())
def MLI():
    return map(int, open(0).read().split())

inf = float("inf")


def main():
    n = int(input())
    S = [i for i in MI()]
    
    q = int(input())
    T = [i for i in MI()]
    
    ret = 0
    for t in T:
        index = bisect.bisect_left(S, t)
        if index < n and S[index] == t:
            ret += 1
            
    print(ret)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
S = [int(x) for x in input().split()]
S.sort()
q = int(input())
T = [int(x) for x in input().split()]
T.sort()

import bisect

ans = 0
s = set()
for i in S:
    if i not in s:
        idx_left = bisect.bisect_left(T, i)
        idx_right = bisect.bisect_right(T, i)
        ans += idx_right - idx_left
        s.add(i)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
S=list(map(int,input().split()))
q=int(input())
T=list(map(int,input().split()))

count=0
for i in T:
    left=0
    right=n
    while left<right:
        mid=(left+right)//2
        if S[mid]==i:
            count+=1
            break
        elif i<S[mid]:
            right=mid
        else:
            left=mid + 1

print(count)

Submission status: Accepted

Solution code:
def func(A, x, lower, upper):
    if lower+1 == upper:
        if (A[lower] == x)or(A[upper] == x):
            return 1
        else:
            return 0
    idx = (upper+lower)//2
    if x < A[idx]:
        return func(A, x, lower, idx)
    else:
        return func(A, x, idx, upper)


N = int(input())
S = list(map(int, input().split()))
Q = int(input())
T = list(map(int, input().split()))
ans = 0
for t in T:
    ans += func(S, t, 0, N)
print(ans)

Submission status: Accepted

Solution code:
# not found suitable BinarySearch module

def binary_search(a, x, lo=0, hi=None):
    """Return True if list a in item x, assuming a is sorted.
    """
    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if a[mid] < x:
            lo = mid+1
        elif a[mid] > x:
            hi = mid
        else:
            return True
    return False

same_num_count = 0
firstlist_num = int(input())
sorted_firstlist = sorted(set(input().split()))
secondlist_num = int(input())
secondlist = set(input().split())

for n in secondlist:
    if binary_search(sorted_firstlist, n):
        same_num_count += 1
print(same_num_count)

Submission status: Accepted

Solution code:
n = int(input())
S = set(list(map(int,input().split())))
q = int(input())
T = list(map(int,input().split()))
c = 0
for t in T:
  if t in S:
    c += 1
print(c)

Submission status: Accepted

Solution code:
import sys
import math
import fractions
import itertools
from collections import deque
import copy
import bisect

MOD = 10 ** 9 + 7
INF = 10 ** 18 + 7
input = lambda: sys.stdin.readline().strip()
n = int(input())
S = set(map(int,input().split()))
q = int(input())
T = list(map(int,input().split()))
ans = 0
for i in T:
    if i in S:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
import bisect

n = int(input())
S = list(map(int,input().split(" ")))
q = int(input())
T = list(map(int, input().split(" ")))

count = 0
for t in T:
    l = bisect.bisect_left(S, t)
    if l < n and S[l] == t:
        count += 1
print(count)

Submission status: Accepted

Solution code:
import bisect

n=int(input())
arr=list(map(int,input().split()))
q=int(input())
arr2=list(map(int,input().split()))
ans=0
for i in range(q):
  pos=bisect.bisect_left(arr,arr2[i])
  if pos==n:
    continue
  else:
    if arr[pos]==arr2[i]:
      ans+=1
print(ans)

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_left
input = sys.stdin.readline

n = int(input())
s = list(map(int,input().split()))
q = int(input())
t = list(map(int,input().split()))

cnt = 0
for x in t:
    c = bisect_left(s,x)
    if s[c] == x:
        cnt += 1
#    big = n-1
#    small = 0
#    if x < s[0] or x > s[n-1]:
#        continue
#    while big > small + 1:
#        m = (small + big) // 2
#        if s[m] < x:
#            small = m
#        else:
#            big = m
#    if s[small] == x or s[big] == x:
#        cnt += 1
print (cnt)

Submission status: Accepted

Solution code:
n = int(input())
s = [int(i) for i in input().split(" ")]
q = int(input())
t = [int(i) for i in input().split(" ")]

print(len(set(s) & set(t)))

Submission status: Accepted

Solution code:
def binary():
    # データを格納
    n = int(input())
    s = list(map(int, input().split()))
    q = int(input())
    t = list(map(int, input().split()))

    # 二分探索法
    count = 0
    for i in range(q):
        left = 0
        right = n - 1
        while left <= right:
            mid = (left + right) // 2
            if s[mid] == t[i]:
                count += 1
                break
            elif s[mid] > t[i]:
                right = mid - 1
            else:
                left = mid + 1
    print(count)

if __name__ == "__main__":
    binary()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    S = [int(a) for a in input().split()]

    q = int(input())
    T = [int(a) for a in input().split()]

    count = 0

    for t in T:
        l = 0
        r = n

        while l < r:
            m = int((l + r) / 2)

            if S[m] == t:
                count += 1
                break
            elif S[m] < t:
                l = m + 1
            elif S[m] > t:
                r = m

    print(count)    

main()

Submission status: Accepted

Solution code:
def binarySerch(A, key):
    left = 0
    right = n
    while left <= right:
        mid = (left + right) // 2
        if int(A[mid]) == int(key):
            return True
        elif int(A[mid]) < int(key):
            left = mid+1
        else :
            right = mid-1
    return False

n = int(input())
S = input().split()
q = int(input())
T = input().split()

count = 0
for i in T:
    if (binarySerch(S, i)) == True:
        count += 1

print(count)

Submission status: Accepted

Solution code:
# Algorithms and Data Structures 1
# Binary Search
# Name: Ryuya Asada
# ID: s1260064


def binary_search(A: list, key: int):
    left = 0
    right = len(A)
    while left < right:
        mid = int((left + right) / 2)
        if A[mid] == key:
            return mid
        elif key < A[mid]:
            right = mid
        else:
            left = mid + 1

    return -1


def main():
    number_of_A = int(input())
    A = [int(a) for a in input().split(" ")]

    number_of_B = int(input())
    B = [int(b) for b in input().split(" ")]

    num = 0
    for b in B:
        result = binary_search(A, b)
        if result != -1:
            num += 1

    print(num)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = list(map(int, input().split()))
q = int(input())
t = list(map(int, input().split()))

cnt = 0

def binary_search (a, key):
    left = 0
    right = len(a)
    while left < right:
        mid = (left + right) // 2
        if a[mid] == key:
            return True
        elif key < a[mid]:
            right = mid
        else:
            left = mid + 1
    return False

for i in range(q):
    if binary_search(s, t[i]):
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
#アルゴリズム実装
def binarySearch(list, find_number):
    left = 0
    right = len(list)
    while left < right:
        mid = (left + right) // 2
        if list[mid] == find_number:
            return 1
        elif list[mid] > find_number:
            right = mid
        else:
            left = mid + 1
    return 0

#インプットdata
n_number = int(input())
n_list = list(map(int, input().split()))
q_number = int(input())
q_list = list(map(int, input().split()))
result = 0

#処理の実行
for i in range(q_number):
        result += binarySearch(n_list, q_list[i])

#結果の表示
print(result)

Submission status: Accepted

Solution code:
n = int(input())
S = list(map(int, input().split()))
q = int(input())
T = list(map(int, input().split()))
C = 0
for t in T:
    right = n-1
    left = 0
    mid = 1
    while left <= right:
        mid = (right + left) // 2
        # if t == 11:
        #     print(left)
        #     print(right)
        #     print(mid)
        #     print(S[mid])
        #     print('-----------')
        if t == S[mid]:
            C += 1
            break
        elif t > S[mid]:
            left = mid+1
        else:
            right = mid-1
print(C)

Submission status: Accepted

Solution code:
#  search

n = int(input())
S = list(set(map(int,input().split())))
q = int(input())
T = list(map(int,input().split()))

S.sort()
T.sort()
ans = 0

i = 0
for elm in S:
    if elm > T[i]:
        while i < q-1 and elm > T[i]:
            i += 1
        
        if elm == T[i]:
            ans += 1
    elif elm == T[i]:
        ans += 1
    elif elm < T[i]:
        pass

print(ans)

Submission status: Accepted

Solution code:
input()
s = set(list(map(int,input().split())))
input()
t = set(list(map(int,input().split())))
print(len(s.intersection(t)))

Submission status: Accepted

Solution code:
import math

n = int(input())
S = list(map(int, input().split()))
q = int(input())
T = list(map(int, input().split()))

count = 0

for v in T:
  top = 0
  tail = len(S)
  while top < tail:
    i = math.floor((top + tail)/2)
    if v == S[i]:
      count += 1
      break
    elif v > S[i]:
      top = i + 1
    else :
      tail = i

print (count)

Submission status: Accepted

Solution code:
n=int(input())
S=list(map(int,input().split()))

q=int(input())
T=list(map(int,input().split()))

#2分探索　昇順、降順になっているときに高速
def binarysearch(key,S):
    left = 0
    right = len(S)
    while left<right :
        mid = (left+right)//2
        if S[mid] == key:
            return 1
        elif key<S[mid]:
            right = mid
        elif key>S[mid]:
            left=mid+1
    return 0

count = 0
key=0
for i in range(q):
    key = T[i]
    count += binarysearch(key,S)
print(count)

Submission status: Accepted

Solution code:
print(input() and len(set(input().split()) & set(input() and input().split())))

Submission status: Accepted

Solution code:
n = int(input())
s = list(map(int,input().split()))
q = int(input())
t = list(map(int,input().split()))
count = 0
for i in t:
    left = 0;right = n
    while left < right:
        mid = (left+right)//2
        if s[mid] == i:
            count += 1
            break
        elif i < s[mid]:
            right = mid
        else:
            left = mid + 1
print(count)

Submission status: Accepted

Solution code:
# Binary Search
from bisect import bisect_left

def main():
  n = int(input())
  S = list(map(int, input().split()))
  m = int(input())
  T = list(map(int, input().split()))
  count = 0
  for i in T:
    index = bisect_left(S, i)
    if index != len(S) and S[index] == i:
      count += 1
  print(count)

main()

Submission status: Accepted

Solution code:
def binary(s, t):
    c = 0
    left = 0
    right = len(s)
    while left < right:
        m = int((left + right)/2)
        if s[m] == t:
            c = 1
            return c
        elif s[m] > t:
            right = m
        else:
            left = m+1
    return c


n = int(input())
count = 0

s = list(map(int, input().split()))
q = int(input())
t = list(map(int, input().split()))
for i in range(q):
    count += binary(s, t[i])
print(count)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int, input().split()))
q=int(input())
Key = list(map(int, input().split()))
def BinarySearch(A, n, key):
	left = 0
	right = n
	while (left < right):
		mid = (left + right) // 2
		if (key == A[mid]):
			return 1
		if (key > A[mid]):
			left = mid + 1
		elif (key < A[mid]):
			right = mid
	return 0
cnt = 0
for i in range(q):
	if (BinarySearch(A, n, Key[i])):
		cnt+=1
print(cnt)