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:
n = int(input())
S = list(map(int,input().split()))
q = int(input())
T = list(map(int,input().split()))

ans = 0
for i in T:
    if S[0] == i or S[n-1] == i:
        ans += 1
        continue
        
    l, r = 0, n-1
    while r-l > 1:
        m = (l+r)//2
        if S[m] == i:
            ans += 1
            break
        if S[m] < i:
            l = m
        else:
            r = m

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()))
cnt = 0
for i in T:
    left = 0
    right = n
    flag = 0
    while left < right:
        mid = (left + right) // 2
        if S[mid] == i:
            flag = 1
            break
        elif S[mid] > i:
            right = mid
        else:
            left = mid+1
    if flag:
        cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_left


def main():
    input = sys.stdin.buffer.readline
    n = int(input())
    s = list(map(int, input().split()))
    _ = int(input())
    t = list(map(int, input().split()))
    cnt = 0
    for e in t:
        pos = bisect_left(s, e)

        if pos != n and s[bisect_left(s, e)] == e:
            cnt += 1
    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
from collections import deque
from itertools import *

def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

def binary_search(list, target):
    left = 0
    right = len(list) - 1
    while left <= right:
        center = int((left + right) / 2)
        if list[center] == target:
            return center
        elif list[center] < target:
            left = center + 1
        elif list[center] > target:
            right = center - 1
    return None

n = I()
s = LI()
q = I()
t = LI()
ans = 0
for t_t in t:
    if binary_search(s,t_t) is not None:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
def binary_serach(A, key):
   left = 0
   right = len(A)
   while left < right:
       mid = (left + right) //2
       if A[mid] == key:
           return mid
       elif key < A[mid]:
           right = mid
       else:
           left = mid + 1
   return 'Not Found'

if __name__ == '__main__':
    n = int(input())
    l = list(map(int, input().split()))
    q = int(input())
    t = list(map(int, input().split()))
    res = 0
    for i in t:
        if binary_serach(l,i) == 'Not Found':
            continue
        else:
            res += 1
    print(res)

Submission status: Accepted

Solution code:
import bisect

_ = int(input())
A = list(map(int, input().split()))
_ = int(input())
B = list(map(int, input().split()))

ans = 0

if len(A) > len(B):
    B, A = A, B

for a in A:
    i = bisect.bisect_left(B, a)
    if i >= 0 and i < len(B) and B[i] == a:
        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()))

cnt = 0
for t in T:
    l = 0
    r = n-1
    while l <= r:
        idx = l + (r-l) // 2
        s = S[idx]
        if t == s:
            cnt += 1
            break
        elif t < s:
            r = idx - 1
        else:
            l = idx + 1
print(cnt)

Submission status: Accepted

Solution code:
def bin_search(arr, i):
    left = 0 
    right = len(arr) - 1
    while left <= right:
        mid = (left + right) //2
        if arr[mid] == i:
            return True
        elif arr[mid] < i:
            left = mid + 1
        else:
            right = mid - 1
        
    return False
n = int(input())
S = list(map(int, input().split()))
n = int(input())
T = list(map(int, input().split()))
ans = 0
for i in T:
    if bin_search(S, i) is True:
        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()))

ans = 0
for key in t:
    left = 0
    right = n
    while (left<right):
        mid = (left + right)//2
        if ( key == s[mid]):
            ans += 1
            break
        elif ( key > s[mid]):
            left = mid + 1
        else:
            right = mid
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()))

cnt = 0
for i in t:
    l = 0
    r = n-1

    while l <= r:
        half = (l+r)//2
        if s[half] == i:
            cnt += 1
            break
        elif i < s[half]:
            r = half-1
        else:
            l = half+1

print(cnt)

Submission status: Accepted

Solution code:
import bisect

N=int(input())
NL=[0]+list(map(int,input().split()))+[10**9]
NL.sort()
M=int(input())
ML=list(map(int,input().split()))

ans=0
for i in ML:
    if NL[bisect.bisect_left(NL,i)]==i:
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
n,ss,q,tt = [list(map(int, s.split())) for s in open(0)]

import bisect

ans = 0
for t in tt:
    i = bisect.bisect_left(ss,t)
    if i < len(ss):
        ans += (ss[i] == t)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = [int(_) for _ in input().split()]
q = int(input())
t = [int(_) for _ in input().split()]
ans = 0
for i in t:
    if s[0] == i or s[n-1] == i:
        ans += 1
        continue
    l, r = 0, n-1
    while r-l > 1:
        m = (l+r)//2
        if s[m] == i:
            ans += 1
            # print(i)
            break
        if s[m] < i:
            l = m
        else:
            r = m

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()))
S.sort()

def binary_search(list,item):
    low = 0
    high = len(list)-1
    while low <= high:
        mid = (low+high)//2
        guess = list[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid-1
        else:
            low = mid+1
    return None
cnt = 0
for i in range(len(T)):
    #print(T[i])
    if binary_search(S,T[i]) != None:
        #print("i",T[i])
        cnt += 1
print(cnt)

Submission status: Accepted

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

counter=0
for x in t:
    low=0
    high=n-1
    while low<=high :
        mid=(low+high)//2 
        if x==s[mid]:
            counter+=1
            break
        if x<s[mid]:
            high=mid-1
        else :
            low=mid+1

print(counter)

Submission status: Accepted

Solution code:
import bisect
INF=10**18
n=int(input())
s=sorted(list(map(int,input().split())))+[INF]
q=int(input())
t=list(map(int,input().split()))
cnt=0
for i in t:
  if s[bisect.bisect_left(s,i)]==i:cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline


def binary_search(A: [int], e: int):
    """List A must be sorted """
    left, right = 0, len(A) - 1
    while left <= right:
        mid = (left+right) // 2
        if A[mid] > e:
            right = mid - 1
        elif A[mid] < e:
            left = mid + 1
        elif A[mid] == e:
            return mid
    return None


_ = int(input())
S = [int(i) for i in input().strip().split()]
_ = int(input())
T = [int(i) for i in input().strip().split()]

ans = 0
for t in T:
    if binary_search(S, t) != None:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
S = set(map(int,input().split()))

q = int(input())
T = list(map(int,input().split()))
ans = 0
for t in T:
    if t in 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()))
ans=0
for i in range (q):
    a=0
    b=n-1
    while True:
        c=(a+b)//2
        if c==b or c==a:
            break
        if S[c]>T[i]:
            b=c
        elif S[c]<T[i]:
            a=c
        else:
            break
    if S[a]==T[i] or S[c]==T[i] or S[b]==T[i]:
        ans+=1
print(ans)

Submission status: Accepted

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

    q = int(input())
    T = list(map(int, input().split()))
    
    res = 0
    for t in T:
        res += binary_search(S, t)
        
    print(res)
        
def binary_search(S, t):
    left = 0
    right = len(S)
    
    res = 0
    while left < right:
        mid = (left + right) // 2
    
        if t > S[mid]:
            left = mid + 1
        elif t < S[mid]:
            right = mid
        else:
            res += 1
            break
            
    return res
    
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 bisect(l,i,n):
    left = 0
    right = n
    while left < right:
        mid = int((left + right)//2)
        if l[mid] == i:
            return 1
        else:
            if i < l[mid]:
                right = mid
            else:
                left  = mid+1
    return 0

for j in range(q):
    cnt += bisect(s,t[j],n)
    
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
S = list(map(int,input().split())) #2分探索するリスト
q = int(input())
T = list(map(int,input().split()))

ans = 0

for item in T:
    low = 0
    high = len(S)-1
    while low <= high:
        mid = (low+high)//2
        guess = S[mid]
        if guess == item:
            ans += 1
            break
        elif guess > item:
            high = mid -1
        else:
            low = mid +1
print(ans)

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_left

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    res = 0
    for t in T:
        idx = bisect_left(S, t)
        if idx < n and S[idx] == t:
            res += 1
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
def bisect(l, r, x):
    while abs(r - l) > 1:
        if arr[l] == x or arr[r] == x:
            return 1
        mid = (l + r) // 2
        if arr[mid] == x:
            return 1
        if arr[mid] < x:
            l = mid
        else:
            r = mid
    return 0


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

l = 0
r = n - 1

ans = 0
for x in t:
    if bisect(l, r, x):
        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()))
ans = 0

for i in range(Q):
  def is_ok(idx):
    return S[idx] <= T[i]

  def meguru_bisect(ng, ok):
    while (abs(ok - ng) > 1):
      mid = (ok + ng) // 2
      if is_ok(mid):
        ok = mid
      else:
        ng = mid
    return ok
  
  if T[i] == S[meguru_bisect(N, -1)]:
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
def bisect_left(A, x, lo=0, hi=None):
    hi = len(A)
    while lo < hi:
        mid = (lo + hi) // 2
        if A[mid] < x:
            lo = mid + 1
        else:
            hi = mid
    return lo


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

ans = 0
for t in T:
    idx = bisect_left(S, t)

    if 0 <= idx < len(S):
        if t == S[idx]: ans += 1

print(ans)

Submission status: Accepted

Solution code:
S_len = int(input().rstrip())
S = input().rstrip().split(" ")
T_len = int(input().rstrip())
T = input().rstrip().split(" ")

S = [int(s) for s in S]
T = [int(t) for t in T]


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


cnt = 0    
for t in T:
    temp_res = binary_search(S, t)
    cnt += temp_res
print(cnt)

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()))

point = 0

for i in t:
    if bisect.bisect(s,i)-bisect.bisect_left(s,i) > 0:
        point += 1
    
print(point)

Submission status: Accepted

Solution code:
def binary_search(lst, value):
    l = 0
    r = len(lst) - 1
    while l <= r:
        mid = (r + l) // 2
        guess = lst[mid]
        if guess == value:
            return True
        elif guess > value:
            r = mid - 1
        else:
            l = mid + 1
    return False


def main():
    n = int(input())
    S = list(map(int, input().split()))
    q = int(input())
    T = list(map(int, input().split()))
    ans = 0

    for value in T:
        if binary_search(S, value):
            ans += 1
    
    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def bin_search(s, i):
    l = -1
    r = len(s)
    
    while r-l>1:
        m = (l+r)//2
        
        if s[m]>i:
            r = m
        else:
            l = m
    
    return(s[l]==i)
    
n = int(input())
S = sorted(map(int, input().split()))
q = int(input())
T = list(map(int, input().split()))

counter = 0

for t in T:
    if bin_search(S, t):
        counter += 1
        
print(counter)

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

def binary_search(li, key):
    n = len(li)
    left = 0
    right = n
    while left < right:
        mid = (left+right) // 2
        if key == li[mid]:
            return 1
        if key > li[mid]:
            left = mid + 1
        elif key < li[mid]:
            right = mid
    return 0

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

Submission status: Accepted

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

# num: 検索対象の数字, items: 検索範囲
def binary_search(num, items):
    low = 0
    high = len(s) - 1
    while low <= high:
        mid = (low + high) // 2
        guess = items[mid]
        if guess == num:
            return True
        if guess > num:
            high = mid -1
        else:
            low = mid + 1
    return False

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

Submission status: Accepted

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


def is_ok(vector, key, middle):
    if vector[middle] <= key:
        return True
    else:
        return False

def meguru_bisect(ng, ok):
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if is_ok(S, t, mid):
            ok = mid
        else:
            ng = mid
    return ok

ans = 0
for t in T:
    ind = meguru_bisect(n,0)
    if S[ind] == t:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left
n=int(input())
s=sorted(list(map(int,input().split())))
q=int(input())
t=list(map(int,input().split()))
cnt=0
for i in t:
    if s[bisect_left(s,i)]==i:
        cnt+=1
print(cnt)

Submission status: Accepted

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

    T.sort()
    S.sort()
    cnt = 0
    for i in range(n):
        if (i != 0) and (S[i] == S[i-1]):
            continue
#ここから二分探索
        left = 0
        right = q
        mid = 0
        while True:
            #print(left,right,mid,cnt)
            mid = int( (left+right)/2 )
            if T[mid] == S[i]:
                cnt += 1
                break
            elif S[i] < T[mid]:
                right = mid
            elif S[i] > T[mid]:
                left = mid + 1


            if left == right:
                break
    return cnt

print(main())

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

if __name__ == '__main__':
  n=int(input())
  S=list(map(int,input().strip("\n").split()))
  q=int(input())
  T=set(map(int,input().strip("\n").split()))

  def binarySearch(x):
    left=0
    right=n
    while left<right:
      if right-left==1:
        if S[left]==x:
          return True
        else:
          return False
      
      p=(right-left)//2
      if S[left+p]==x:
        return True
      elif x<S[left+p]:
        right=left+p
      else:
        left=left+p+1
    return False
#  def binarySearch(x,s,e):
    #print(x,s,e)
#    if s>e or e<s:
#      return False
#    elif e-s==1:
#      if S[s]==x:
#        return True
#      else:
#        return False

#    p=(e-s)//2
#    if S[s+p]==x:
#      return True
#    elif x<S[s+p]:
#      return binarySearch(x,s,s+p)
#    else:
#      return binarySearch(x,s+p+1,e)

  ans=0
  for t in T:
    #print('start:',t)
    if binarySearch(t)==True:
      ans+=1
  print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left
n = int(input())
S = list(map(int, input().split()))
q = int(input())
T = list(map(int, input().split()))
S.sort()
ans = 0
for t in T:
    i = bisect_left(S, t)
    if 0<=i<n and S[i]==t:
        ans += 1
print(ans)

Submission status: Accepted

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

print(len(A & T))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
M = int(input())
B = list(map(int,input().split()))

def bisect(key, L):
    left = 0
    right = len(L)-1
    while right - left >= 0:
        mid = left + (right - left) // 2
        if L[mid] == key:
            return mid
        if L[mid] > key:
            right = mid - 1
        else:
            left = mid +1
    return -1

count = 0
for b in B:
    if bisect(b,A) != -1:
        count += 1
print(count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
 
sys.setrecursionlimit(10 ** 7)
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

ans = 0
for t in T:
    left = -1
    right = n
    while (right - left > 1):
        mid = left + (right - left) // 2
        if S[mid] >= t:
            right = mid
        else:
            left = mid
    if S[right] == t:
        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 binary_search(data, value):
    left = 0
    right = len(data) 

    while left < right:
        mid = (left + right) // 2
        if data[mid] == value:
            return 1
        elif data[mid] < value:
            left = mid + 1
        else:
            right = mid
    return 0

count = 0
for i in t:
    exist = binary_search(s, i)
    count = count + exist

print(count)

Submission status: Accepted

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

def binary_serch(t, n, S):
    left=0
    right=n
    while left<right:
        mid=(left+right)//2
        if t == S[mid]:
            return t
        elif t > S[mid]:
            left = mid +1
        else:
            right = mid
    return -1

cnt=0
for t in T:
    if binary_serch(t,n,S)==t:
        cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin

n = int(stdin.readline().rstrip())
S = list(map(int, stdin.readline().rstrip().split()))
q = int(stdin.readline().rstrip())
T = list(map(int, stdin.readline().rstrip().split()))

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

cnt = 0
for t in T:
  if binary_search(S, t) != -1:
    cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
S = list(map(int,input().split()))
Q = int(input())
T = list(map(int,input().split()))

res = 0
for t in T: 
  l,r = 0,N-1
  while r>=l:
    m = (l+r)//2
    if S[m] == t:
      res += 1
      break
    if S[m] < t:
      l = m+1
    else:
      r = m-1

print(res)

Submission status: Accepted

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

def binary(l, key):
    left = 0
    right = len(l)
    while left < right:
        center = (left+right) // 2
        if l[center] == key:
            return True
        elif l[center] < key:
            left = center + 1
        else:
            right = center
    return False
    
ans = 0
for num in map(int, input().split()):
    if binary(S, num):
        ans += 1
        
print(ans)

Submission status: Accepted

Solution code:
def binarysearch(list,key):
    left = 0
    right = len(list)
    while left < right:
        mid = (left + right)//2
        if list[mid] == key:
            return mid + 1
        elif list[mid] > key:
            right = mid
        else:
            left = mid + 1
    return 0

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):
    if binarysearch(S,T[i]):
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = set(map(int, input().split()))
q = int(input())
t = set(map(int, input().split()))
print(len(s & t))

Submission status: Accepted

Solution code:
def bisect(lst, isok, ok, ng):
    while(abs(ok-ng)>1):
        mid = (ok+ng)//2
        if isok(lst[mid]):
            ok = mid
        else:
            ng = mid
    return [ok, ng]

n = int(input())
a = [int(i) for i in input().split()]
q = int(input())
m = [int(i) for i in input().split()]

a.sort()
print(sum([a[bisect(a, lambda x:x >= i, n, -1)[0]] == i for i in m]))

Submission status: Accepted

Solution code:
def main():
    import sys
    N = int(sys.stdin.readline())
    S = list(map(int, sys.stdin.readline().split()))
    Q = int(sys.stdin.readline())
    T = list(map(int, sys.stdin.readline().split()))
    #T.sort()
    
    def nibutan(x):
        left, right = 0, N-1
        if S[left]==x or S[right]==x:
            #print(x)
            return 1
        while True:
            mid = (left+right)//2
            if S[mid] < x:
                left = mid
            elif S[mid] > x:
                right = mid
            elif S[mid] == x:
                #print(x)
                return 1
            if right-left==1:
                return 0
                
            
    
    cnt = 0
    for t in T:
        cnt +=nibutan(t)
    print(cnt)
    



if __name__=='__main__':
    main()

Submission status: Accepted

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

    def bisect(item, list):
        cnt = 0
        # 左右のindexで考える
        l = 0
        h = len(list) - 1
        while l <= h:
            mid = (l+h) // 2
            tmp = list[mid]
            if item == tmp:
                cnt = 1
                return cnt
            elif tmp < item:
                l = mid + 1
            elif tmp > item:
                h = mid - 1
        return cnt
    
    ans = 0
    for i in t:
        ans += bisect(i, s)
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

#def binary_search()
count=0
for i in range(K):
    tmp=T[i]
    left=0
    right=N-1
    flag=False
    while left<=right:
        mid=(left+right)//2
        if S[mid]==T[i]:
            count+=1
            break
        elif S[mid]>T[i]:
            right=mid-1
        elif S[mid]<T[i]:
            left=mid+1
        else:
            print("イレギュラーな例につかまりました")
            exit()
        
print(count)

Submission status: Accepted

Solution code:
def binarysearch(S,x):
    a=0
    b=len(S)
    while b>a+1:
        m=(b+a)//2
        if S[m]>x:
            b=m
        elif S[m]<=x:
            a=m
    if S[a]==x:
        return True
    return False
            

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):
    if binarysearch(S,T[i]):
        count += 1
print(count)

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

ans = 0

for t in T:
    # 左から見ていった時に新たにtが入る位置
    left = bisect_left(S, t)
    if left < len(S) and S[left] == t:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
def binary_search(S,n,a):
    first = 0
    last = n
    # count = 0
    # x = int(math.log2(n)) + 1
    # while True:
    #     mid = (first + last) // 2
    #     if S[mid] == a:
    #         return 1
    #     elif a < S[mid]:
    #         last = mid
    #     elif S[mid] < a:
    #         first = mid
    #     count += 1
    #     if x == count:
    #         return 0

    while first < last:
        mid = (first + last) // 2
        if S[mid] == a:
            return 1
        elif a < S[mid]:
            last = mid
        elif S[mid] < a:
            first = mid + 1

    return 0

if __name__ == '__main__':
    import math
    n = int(input())
    S = [int(i) for i in input().split()]
    q = int(input())
    T = [int(i) for i in input().split()]
    count = 0

    for i in range(q):
        count += binary_search(S,n,T[i])

    print(count)

Submission status: Accepted

Solution code:
# ALDS1_4_A.py で十分通る

import sys

n = int(sys.stdin.readline())
S = [int(x) for x in sys.stdin.readline().split()]
q = int(sys.stdin.readline())
T = [int(x) for x in sys.stdin.readline().split()]

count = 0
for t in T:

    left = 0
    right = len(S)
    pmid = 0

    while left < right:
        mid = (right + left) // 2
        if mid == pmid:
            break
        pmid = mid
        m = S[mid]
        if t == m:
            count += 1
            break
        else:
            if t < m:
                right = mid
            else:
                left = mid + 1

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(' ')))


C = 0
def binarySearch(A, key):
    left = 0
    right = len(A)
    while left < right:
        mid = (left + right) // 2
        if A[mid] == key:
            global C
            C+= 1
            break
        elif A[mid] < key:
            left = mid + 1
        else:
            right = mid
    
            
for key in T:
    binarySearch(S, key)
    
print(C)

Submission status: Accepted

Solution code:
input()
S = input().split()
input()
T = input().split()
print(len(set(S)&set(T)))

Submission status: Accepted

Solution code:
n = int(input())
S = set(map(int, input().split()))
q = int(input())
T = list(map(int, input().split()))
ans = 0
for t in T:
    if t in 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()))
ans=0
for i in range(q):
    tmp=t[i]
    l=0
    r=len(s)-1
    mid=(l+r)//2
    while l<=r:
        if tmp==s[mid]:
            ans+=1
            break
        elif s[mid]>tmp:
            r=mid-1
        else:
            l=mid+1
        mid=(l+r)//2
print(ans)

Submission status: Accepted

Solution code:
def binarySearchLeft(nums, target):
    low = 0
    high = len(nums) - 1
    while low <= high:
        mid = low + high >> 1
        if target == nums[mid]:
            if mid == 0 or target != nums[mid - 1]:
                return mid
            else:
                high = mid - 1
        elif target < nums[mid]:
            high = mid - 1
        else:
            low = mid + 1
    return -1

def solve(n, S, q, T):
    counter = 0
    for t in T:
        idx = binarySearchLeft(S, t)
        counter += 1 if 0 <= idx < len(S) else 0
    print(counter)

if __name__ == "__main__":
    n = int(input())
    S = list(map(int, input().split()))
    q = int(input())
    T = list(map(int, input().split()))
    solve(n, S, q, T)

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 range(q):
    t = T[i]
    left = 0
    right = len(S) - 1
    while left <= right:
        mid = (right + left) // 2
        if S[mid] == t:
            count += 1
            break
        elif S[mid] < t:
            left = mid + 1
        else:
            right = mid - 1
print(count)

Submission status: Accepted

Solution code:
N=int(input())
N_list=list(map(int,input().split()))
S=int(input())
S_list=list(map(int,input().split()))
def search(target):
    left=-1
    right=N
    while(left<right):
        middle = (left + right) // 2
        mid=N_list[middle]
        if mid==target:
            return True
        if target>mid:
            left=middle+1
            middle =(left + right) // 2
        else:
            right=middle
            middle = (left + right) // 2
    return False
count=0
for s in S_list:
    if search(s):
        count+=1
print(count)

Submission status: Accepted

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

count = 0
for item in t:
    idx = bisect.bisect_left(s, item)
    if idx < n:
        count += s[idx] == item
print(count)