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 ≤ 10
9
0 ≤ an element in T ≤ 10
9
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

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
    int n,q; cin>>n;
    int s[n+5], t;
    int count=0;

    for(int i =1; i<=n;i++){
        cin>>s[i];
    }
    cin>>q;
    for(int i=1;i<=q;i++){
        cin>>t;
        int l = 1, r=n; 
        while(l <= r){
            int mid = (l+r)/2;
            if(t == s[mid]){
                count++;
                break;
            }
            else if(t < s[mid]) {
                r = mid-1;
            }
            else {
                l = mid + 1;
            }
        }
    }
    cout<<count<<endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3516
final_rank: 7

code_snippet: #ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using ll = long long;
using ld = long double;
ll inf = 2e18;
#define rep(i,n) for(ll i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define el '\n'
#define sp " "
#define Yes cout << "Yes" << el
#define No cout << "No" << el
#define YN {cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
#define next_p(v) next_permutation(v.begin(),v.end())
#define mod(a,d) (a%d+d)%d
vector<ll> dx = { 1,0,-1,0 };
vector<ll> dy = { 0,1,0,-1 };
bool out_grid(ll i, ll j, ll h, ll w) {//trueならcontinue
    return (!(0 <= i && i < h && 0 <= j && j < w));
}

void solve(){

    ll n;
    cin >> n;
    vector<ll> s(n);
    rep(i,n) cin >> s[i];
    ll q;
    cin >> q;
    vector<ll> t(q);
    rep(i,q) cin >> t[i];

    ll ans = 0;
    rep(i,q){
        ll target = t[i];
        ll left = 0;
        ll right = n-1;
        if(s[left]==target){
            ans ++;
            continue;
        }
        if(s[right]==target){
            ans ++;
            continue;
        }
        ll key = 0;
        while(right-left>1){
            ll mid = left + (right-left)/2;
            if(s[mid]==target){
                ans ++;
                key = 1;
                break;
            }else if(s[mid]>target){
                right = mid;
            }else{
                left = mid;
            }
        }
        if(key==0) continue;
    }

    cout << ans << el;

}

int main(){

    ll t = 1;
    //cin >> t;
    rep(i,t) solve();

}
accuracy: 1
time_ms: 20
memory_kb: 4104
final_rank: 17

code_snippet: #include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cmath> // sqrt

using namespace std;

// 二分探索
int main() {
  int n;
  cin >> n;
  vector<int64_t> s(n);
  for(int i = 0; i < n; i++){
    cin >> s.at(i);
  }

  int q;
  cin >> q;

  int ans = 0;
  for(int i = 0; i < q; i++){
    int64_t t;
    cin >> t;

    int left = 0;
    int right = n;
    while(left < right){
      // cout << "be:" << left << " " << right << endl;
      int mid = (left + right)/2;
      if(t == s.at(mid)){
        ans++;
        break;
      }else if(t < s.at(mid)){
        right = mid;
      }else if(t > s.at(mid)){
        left = mid + 1;
      }
      // cout << "af:" << left << " " << right << endl;
    }
  }

  cout << ans << endl;

  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3768
final_rank: 14

code_snippet: #include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main(){
    int n,q,count,key;
    
    cin >> n;
    vector<int> S(n);
    for(int i = 0;i < n; ++i){
        cin >> S[i];
    }

    cin >> q;
    vector<int> T(q);
    for(int j = 0; j < q; ++j){
        cin >> T[j];
    }

    int left,right,mid;
    count = 0;

    for(int k = 0; k <= q-1; ++k){
        left = 0;
        right = n;
        key = T[k];
        while(left < right){
            mid = (left + right)/2;
            if(S[mid] == key){
                count += 1;
                break;
            }else if(key < S[mid]){
                right = mid;
            }else {
                left = mid +1;
            }     
        }
    }
    cout << count << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3436
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
int a[1000000];
int main(void){
    int n,m,ans=0;
    cin>>n;
    for(int i=0;i<n;i++){
        int b;
        cin>>b;
        a[b%1000000]=1;
    }
    cin>>m;
    for(int i=0;i<m;i++){
        int b;
        cin>>b;
        ans+=a[b%1000000];
    }
    cout<<ans<<endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3368
final_rank: 1

code_snippet: #include <iostream>
using namespace std;

int main(){
    long long n;
    cin >> n;
    long long S[n];
    for(long long i=0; i<n; ++i){
        cin >> S[i];
    }
    long long q;
    cin >> q;
    long long T[q];
    for(long long i=0; i<q; ++i){
        cin >> T[i];
    }

    long long _count = 0;

    for(int i=0; i<q; ++i){
        if((S[0] <= T[i]) || (T[i] <= S[n-1])){
            long long left = 0;
            long long right = n;
            for(long long mid=(left+right)/2; S[mid]!=T[i];mid=(left+right)/2){
                if(T[i]<S[mid]){
                    right = mid;
                }
                else left = mid+1;
                if(mid==(left+right)/2){
                    --_count; break;
                }
            }
            ++_count;
        }
    }

    cout << _count << endl;
    
    

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4608
final_rank: 2

code_snippet: #include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main(){
    int n,q,count,key;
    
    cin >> n;
    vector<int> S(n);
    for(int i = 0;i < n; ++i){
        cin >> S[i];
    }

    cin >> q;
    vector<int> T(q);
    for(int j = 0; j < q; ++j){
        cin >> T[j];
    }

    int left,right,mid;
    count = 0;

    for(int k = 0; k <= q-1; ++k){
        left = 0;
        right = n;
        key = T[k];
        while(left < right){
            mid = (left + right)/2;
            if(S[mid] == key){
                count += 1;
                break;
            }else if(key < S[mid]){
                right = mid;
            }else {
                left = mid +1;
            }     
        }
    }
    cout << count << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3472
final_rank: 5

code_snippet: #include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <cstring> 
#include <cmath>

using namespace std;

int main(){
    int n;
    int S[100000];
    int q;
    int T[50000];
    
    int i;
    int F=0;
    
    cin >> n;
    for (i=0;i<n;i++){
        cin >> S[i];
    }
    cin >> q;
    for (i=0;i<q;i++){
        cin >> T[i];
    }

    F=0;
    float L=0;
    float R=n-1;
    int M;
    int M2=-1;
    int A=0;
    M=floor((L+R)/2);
    
    for (i=0;i<q;i++){
        F = 0;
        L=0;
        R=n-1;
        M=floor((L+R)/2);
        M2=-1;
        
        
    while(F==0){
        if (S[M]==T[i]){
            A = A + 1;
            F=1;
        } else if(S[M]<T[i]){
            L=M+1;
            M=floor((L+R)/2);
            F=0;
        } else {
            R=M-1;
            M=floor((L+R)/2);
            F=0;
        }
        if (F==0 and M==M2){
            F=1;
        }
        M2=M;
    }
    }



    cout << A << endl; 




    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3948
final_rank: 18

code_snippet: #include <iostream>
#include <vector>
using namespace std;

// 二分探索（閉区間版）
bool binarySearch(const vector<int>& A, int key) {
    int left = 0;
    int right = A.size() - 1;
    while (left <= right) {
        int mid = (left + right) / 2;
        if (A[mid] == key) return true;
        else if (key < A[mid]) right = mid - 1;
        else left = mid + 1;
    }
    return false;
}

int main() {
    int n, q;
    cin >> n;
    vector<int> S(n);
    for (int i = 0; i < n; i++) cin >> S[i];

    cin >> q;
    vector<int> T(q);
    for (int i = 0; i < q; i++) cin >> T[i];

    int count = 0;
    for (int i = 0; i < q; i++) {
        if (binarySearch(S, T[i])) count++;
    }

    cout << count << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3536
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
int binary_search(int v[],int n,int target){
    //インデックスを名づける
    int left = 0;
    int right = n-1;
    while(left<=right){
        int mid = (left+right)/2;
        if(target==v[mid])return mid;
        if(target<v[mid]){
            right = mid-1;
        }
        else{
            left = mid+1;
        }
    }
    return -1;
}


int main(){
    int n;cin >> n;
    vector<int>x(n);
    for(int i=0;i<n;i++){
        cin >> x[i];
    }
    int m;cin >> m;
    vector<int>y(m);
    for(int i=0;i<m;i++){
        cin >> y[i];
    }
    
    sort(y.begin(),y.end());
    set<int>unique_x(x.begin(),x.end());
    int count=0;
    for(int val : unique_x){
        if(binary_search(y.data(), m, val) != -1) count++;
    }
    cout << count << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 5668
final_rank: 19

code_snippet: #include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main(){
    int n,q,count,key;
    
    cin >> n;
    vector<int> S(n);
    for(int i = 0;i < n; ++i){
        cin >> S[i];
    }

    cin >> q;
    vector<int> T(q);
    for(int j = 0; j < q; ++j){
        cin >> T[j];
    }

    int left,right,mid;
    count = 0;

    for(int k = 0; k <= q-1; ++k){
        left = 0;
        right = n;
        key = T[k];
        while(left < right){
            mid = (left + right)/2;
            if(S[mid] == key){
                count += 1;
                break;
            }else if(key < S[mid]){
                right = mid;
            }else {
                left = mid +1;
            }     
        }
    }
    cout << count << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3536
final_rank: 9

code_snippet: #include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;

int main() {
    int n, q;
    cin >> n;
    vector<int> S(n);
    for (int i = 0; i < n; ++i) {
        cin >> S[i];
    }

    cin >> q;
    vector<int> T(q);
    for (int i = 0; i < q; ++i) {
        cin >> T[i];
    }

    unordered_set<int> s_set(S.begin(), S.end());
    int count = 0;
    for (int i = 0; i < q; ++i) {
        if (s_set.count(T[i])) {
            ++count;
        }
    }

    cout << count << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 5816
final_rank: 20

code_snippet: #include<iostream>
using namespace std;

int main(){
    int n;
    cin >> n;

    int S[n];
    for(int i=0;i<n;i++){
        cin >> S[i];
        //cout << S[i] << endl;
    }

    int q, m, mid;
    int count=0;
    int left=0;
    int right=n;
    cin >> q;

    int T[q];
    for(int i=0;i<q;i++){
        cin >> T[i];
        //cout << T[i] << endl;
    }

    for(int i=0;i<q;i++){
        m = T[i];
        left = 0;
        right = n;
        while(left < right){
            mid = (left+right)/2;
            if(m == S[mid]){
                count++;
                break;
            } else if(m < S[mid]){
                right = mid;
            } else{
                left = mid+1;
            }
        }
    }
    cout << count << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3920
final_rank: 15

code_snippet: #include <iostream>
#include <vector>
using namespace std;
int main()
{
    int n, q, num, count = 0;
    cin >> n;
    vector<int> S, T;
    for (int i = 0; i < n; i++)
    {
        cin >> num;
        S.push_back(num);
    }
    cin >> q;
    for (int i = 0; i < q; i++)
    {
        cin >> num;
        T.push_back(num);
    }

    for (int i = 0; i < q; i++)
    {
        int left = 0;
        int right = n;
        int mid;
        int key = T[i];

        while (left < right)
        {
            mid = (left + right) / 2;

            if (S[mid] == key)
            {
                count++;
                break;
            }
            else if (S[mid] > key)
            {
                right = mid;
            }
            else

            {
                left = mid + 1;
            }
        }
    }
    cout << count << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3684
final_rank: 11

code_snippet: #include<iostream>
#include<vector>
using namespace std;

int main(){
    int n,q;
    cin >> n;
    vector<int> S(n);

    for (int i=0;i<n;i++){
        cin >> S[i];
    }
    cin >> q;
    vector<int> T(q);
    for (int i=0;i<q;i++){
        cin >> T[i];
    }
    
    int C=0;
    for (int i=0;i<q;i++){
        int left=0, right=n;
        while (left<right){
            int mid=(left+right)/2;
            if (S[mid]==T[i]){
                C+=1;
                break;
            }
            else if(T[i]<S[mid]){
                right=mid;
            }
            else{
                left=mid+1;
            }
        }
    }
    
    cout << C << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3504
final_rank: 6

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

bool Nibun_Tansaku(vector<int>&array, int N, int key,int&swap_count){
    int left = 0;
    int right = N-1;
    while(left <= right){
        swap_count ++;
        int mid = (left + right) / 2;
        if (array[mid] == key){
            return true;
        }else if(key < array[mid]){
            right = mid - 1;
        }else{
            left = mid + 1;
        }
    }
    return false;

}
int main(){
    int x;
    cin >> x;
    vector<int>S(x);
    for(int i = 0; i < x; i++ ){
        int n;
        cin >> n;
        S[i] = n;
    } 
    sort(S.begin(), S.end());
    int y;
    cin >> y;
    vector<int>T(y);
    for(int j = 0; j < y; j++){
        int m;
        cin >> m;
        T[j] = m;
    }
    int match_count = 0;
    int swap_count = 0;
    for(int key : T){
        if (Nibun_Tansaku(S, x, key,swap_count)){
            match_count++;
        }
    }
    cout <<  match_count << endl;
    return 0;
    
}
accuracy: 1
time_ms: 20
memory_kb: 3524
final_rank: 8

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    int n;
    cin >> n;
    vector<int> S(n);
    for (int i =0; i<n; ++i){
        cin >> S[i];
    }
    int q;
    cin >> q;
    vector<int> T(q);
    for (int i=0; i<q; ++i){
        cin >> T[i];
    }
    sort(S.begin(), S.end());
    int count = 0;
    for (int i=0; i<q; ++i){
        int key = T[i];
        int left = 0;
        int right =n;
        bool found = false;
        
        while(left<right){
            int mid = (left + right)/2;
            if(S[mid] == key){
                found = true;
                break;
            }
            else if (key < S[mid]){
                right = mid;
            }
            else {
                left = mid + 1;
            }
        }
        if (found) ++count;
    }
    
    cout << count << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3440
final_rank: 4

code_snippet: #include<iostream>
#include<vector>
using namespace std;

int main(){
    vector<int> S,T;
    int SN,TN,sum=0;
    cin>>SN;
    for(int i=0;i<SN;i++){
        int tmp;
        cin >>tmp;
        S.push_back(tmp);
    }
    cin>>TN;
    for(int i=0;i<TN;i++){
        int tmp;
        cin >>tmp;
        T.push_back(tmp);
    }
    for(int i=0;i<TN;i++){
        int left = 0;
        int right = SN;
        while(left<right){
            int mid = (left+right)/2;
            if (S[mid]==T[i]){
                sum++;break;
            }else if(S[mid]<T[i]){
                left=mid+1;
            }else{
                right=mid;
            }
        }
    }
    cout<<sum<<endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3712
final_rank: 12

code_snippet: using namespace std;
#include <iostream>
#include <algorithm>


int binarySearch(int A[], int n, int key){
    int left = 0;
    int right = n;
    while (left < right){
        int mid = (left + right) / 2;
        if(A[mid] == key){
            return mid;
        }
        else if (key < A[mid]){
            right = mid;
        }else{
            left = mid + 1;
        }
    }
    return -1;
}
    
int main(){
    int n;
    cin >> n;
    int S[n];
    for(int i; i < n; i++){
        cin >> S[i];
    }
    int q;
    cin >> q;
    int T[q];
    for(int i; i < q; i++){
        cin >> T[i];
    }
    int a = 0;
    for(int i = 0; i < q; i++){
        if (binarySearch(S, n, T[i]) != -1) {
            a++;
        }
    }
    cout << a << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3928
final_rank: 16

code_snippet: #include<iostream>
#include<vector>
using namespace std;

int A[10000000],n;

int binarySearch(int key){
    int left = 0;
    int right = n;
    int mid;
    while(left < right){
        mid = (left + right)/2;
        if(A[mid]==key) return mid;
        if(key<A[mid])right=mid;
        else if(key>A[mid])left=mid+1;
    }
    return 0;
}

int main(){
    int i,q,k,sum=0;
    cin >> n;
    for(i=0;i<n;i++){
        cin >> A[i];
    }
        
        cin >> q;
        for(i=0;i<q;i++){
            cin >> k;
            if(binarySearch(k)) 
            sum++;
        }
        cout << sum << endl;
        
        return 0;
    
}
accuracy: 1
time_ms: 20
memory_kb: 3732
final_rank: 13