alexjercan/bugnet huggingface dataset:
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> S(n);
  REP(i, n) cin >> S[i];

  int q;
  cin >> q;
  vector<int> T(q);
  REP(i, q) cin >> T[i];

  int ans = 0;

  for (int i = 0; i < q; i++) {
    int key = T[i];
    int left = 0;
    int right = n;
    while (left < right) {
      int mid = (left + right) / 2;
      if (S[mid] == key) {
        ans++;
        break;
      } else if (S[mid] > key) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
  }

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> S(n);
  REP(i, n) cin >> S[i];

  int q;
  cin >> q;
  vector<int> T(q);
  REP(i, q) cin >> T[i];

  int ans = 0;

  for (int i = 0; i < n; i++) {
    int key = S[i];
    int left = 0;
    int right = q;
    while (left < right) {
      int mid = (left + right) / 2;
      if (T[mid] == key) {
        ans++;
      } else if (T[mid] > key) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdlib.h>
using namespace std;
bool binarysearch(long int x, long int *D, long int n) {
  int B = 0;
  int T = n - 1;

  while (B <= T) {
    int mid = (B + T) / 2;
    if (D[mid] == x) {
      return true;
    } else if (D[mid] < x) {
      B = mid + 1;
    } else
      T = mid - 1;
  }
  return false;
}

int main() {
  long int n;
  cin >> n;
  long int S[n];
  for (int i = 0; i < n; i++)
    cin >> S[i];
  long int q;
  cin >> q;
  long int T[q];
  for (int i = 0; i < q; i++)
    cin >> T[i];
  int cnt = 0;
  for (int i = 0; i < q; i++) {
    if (binarysearch(T[i], S, n))
      cnt++;
  }
  cout << cnt << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdlib.h>
using namespace std;

void quick_sort(long int *D, long int B, long int T) {
  if (B >= T)
    return;
  long int i = B, j = T;
  long int div = D[B];

  while (i < j) {
    while (i <= j && D[i] <= div)
      i++;
    while (i <= j && D[j] > div)
      j--;
    if (i < j)
      swap(D[i], D[j]);
  }
  long int tmp = D[B];
  D[B] = D[j];
  D[j] = tmp;
  quick_sort(D, B, j - 1);
  quick_sort(D, j + 1, T);
}

bool binarysearch(long int x, long int *D, long int n) {
  int B = 0;
  int T = n - 1;

  while (B <= T) {
    int mid = (B + T) / 2;
    if (D[mid] == x) {
      return true;
    } else if (D[mid] < x) {
      B = mid + 1;
    } else
      T = mid - 1;
  }
  return false;
}

int main() {
  long int n;
  cin >> n;
  long int S[n];
  for (int i = 0; i < n; i++)
    cin >> S[i];
  long int q;
  cin >> q;
  long int T[q];
  for (int i = 0; i < q; i++)
    cin >> T[i];
  quick_sort(S, 0, n - 1);
  int cnt = 0;
  for (int i = 0; i < q; i++) {
    if (binarysearch(T[i], S, n))
      cnt++;
  }
  cout << cnt << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
void A() {
  int S[10000];
  int T[500];
  int N;
  int Q;
  int C = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }

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

  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < N; j++) {
      if (T[i] == S[j]) {
        C++;
        break;
      }
    }
  }

  cout << C << endl;
}

void B() {
  int S[100001];
  int T[50000];
  int N;
  int Q;
  int C = 0;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }

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

  for (int i = 0; i < Q; i++) {
    int left = 0;
    int right = N;
    while (left < right) {
      int center = (left + right) / 2;

      if (T[i] == S[center]) {
        C++;
        break;
      }

      if (T[i] > S[center]) {
        left = center + 1;
      } else {
        right = center;
      }
    }
  }

  cout << C << endl;
}

int main() {
  // A();
  B();
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
void A() {
  int S[10000];
  int T[500];
  int N;
  int Q;
  int C = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }

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

  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < N; j++) {
      if (T[i] == S[j]) {
        C++;
        break;
      }
    }
  }

  cout << C << endl;
}

void B() {
  int S[100001];
  int T[50000];
  int N;
  int Q;
  int C = 0;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }

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

  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      if (S[i] > S[j]) {
        int tmp = S[i];
        S[i] = S[j];
        S[j] = tmp;
      }
    }
  }

  for (int i = 0; i < Q; i++) {
    int left = 0;
    int right = N;
    while (left < right) {
      int center = (left + right) / 2;

      if (T[i] == S[center]) {
        C++;
        break;
      }

      if (T[i] > S[center]) {
        left = center + 1;
      } else {
        right = center;
      }
    }
  }

  cout << C << endl;
}

int main() {
  // A();
  B();
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void output(vector<int> a);
int main(void) {
  int n;
  int q;
  // int index=0;

  int count = 0;
  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];
  }

  /*for(int i=0 ; i< q; i++){
          if(i!=0){
                  if(T[i-1]>T[i]){
                          n=index;
                          index
                  }

          }
  }*/

  // for(int j=index ; j< n; j++){

  for (int i = 0; i < q; i++) {
    if (std::binary_search(S.begin(), S.end(), T[i])) {
      count++;
      // break;
      // index =j;
    }
  }

  cout << count << endl;
}

Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void output(vector<int> a);
int main(void) {
  int n;
  int q;
  int index = 0;

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

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

  /*for(int i=0 ; i< q; i++){
          if(i!=0){
                  if(T[i-1]>T[i]){
                          n=index;
                          index
                  }

          }
  }*/

  // for(int j=index ; j< n; j++){

  for (int i = 0; i < q; i++) {
    if (std::binary_search(S.begin(), S.end(), T[i])) {
      count++;
      break;
      // index =j;
      //}
    }
  }

  cout << count << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
// #include <conio.h>
using namespace std;

int binary(int value, const int *array, int size) {
  int lower, upper, mid;
  lower = 0;
  upper = size - 1;

  while (lower <= upper) {

    mid = (lower + upper) / 2;

    if (array[mid] == value) {
      // cout << array[mid] << endl;
      return array[mid];
    } else if (array[mid] < value) {
      lower = mid + 1;
    } else {
      upper = mid - 1;
    }
  }
  return -1;
}

int main() {
  int n, q, sum = 0, size = 0;
  cin >> n;
  vector<int> S(n);
  /*for(int i=0; i<n; ++i){
      int tmp;
      bool check=true;
      cin >> tmp;
      for(int j=0; j<size; ++j){
          if(tmp == S[j]) check = false;
      }
      if(check == true){
          //cout << check << " " << tmp <<  endl;
          S.push_back(tmp);
          size++;
      }
  }*/
  for (int i = 0; i < n; ++i) {
    cin >> S[i];
  }
  /*for(int i=0; i<size; ++i){
      cout << S[i] << endl;
  }*/
  cin >> q;
  vector<int> T(q);
  for (int i = 0; i < q; ++i) {
    cin >> T[i];
  }
  sort(S.begin(), S.end());
  // sort(T.begin(),T.end());
  for (int i = 0; i < q; ++i) {
    // cout << binary(T[i],&S.front(),S.size()) << endl;
    if (binary(T[i], &S.front(), S.size()) != -1)
      ++sum;
  }
  cout << sum << endl;
  // getch();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
// #include <conio.h>
using namespace std;

int binary(int value, const int *array, int size) {
  int lower, upper, mid;
  lower = 0;
  upper = size - 1;

  while (lower <= upper) {

    mid = (lower + upper) / 2;

    if (array[mid] == value) {
      // cout << array[mid] << endl;
      return array[mid];
    } else if (array[mid] < value) {
      lower = mid + 1;
    } else {
      upper = mid - 1;
    }
  }
  return -1;
}

int main() {
  int n, q, sum = 0, size = 0;
  cin >> n;
  vector<int> S;
  for (int i = 0; i < n; ++i) {
    int tmp;
    bool check = true;
    cin >> tmp;
    for (int j = 0; j < size; ++j) {
      if (tmp == S[j])
        check = false;
    }
    if (check == true) {
      // cout << check << " " << tmp <<  endl;
      S.push_back(tmp);
      size++;
    }
  }
  /*for(int i=0; i<size; ++i){
      cout << S[i] << endl;
  }*/
  cin >> q;
  vector<int> T(q);
  for (int i = 0; i < q; ++i) {
    cin >> T[i];
  }
  // sort(S.begin(),S.end());
  // sort(T.begin(),T.end());
  for (int i = 0; i < q; ++i) {
    // cout << binary(T[i],&S.front(),S.size()) << endl;
    if (binary(T[i], &S.front(), S.size()) != -1)
      ++sum;
  }
  cout << sum << endl;
  // getch();
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

constexpr int Nmax = 100'000;

bool binarySearch(int *array, int value, int n) {
  int left = 0;
  int right = n - 1;
  int center = (left + right) / 2;
  while (array[center] != value) {
    if (left >= right) {
      return false;
    }
    if (array[center] > value) {
      right = center - 1;
    } else {
      left = center + 1;
    }
    center = (left + right) / 2;
  }
  return true;
}

int main() {
  ios::sync_with_stdio(false);

  int same = 0;

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

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int value;
    cin >> value;
    if (binarySearch(S, value, n)) {
      same++;
    }
  }
  cout << same << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

constexpr int Nmax = 100'000;

bool binarySearch(int *array, int value, int n) {
  int left = 0;
  int right = n - 1;
  int center = (left + right) / 2;
  while (array[center] != value) {
    if (left == right) {
      return false;
    }
    if (array[center] > value) {
      right = center - 1;
    } else {
      left = center + 1;
    }
    center = (left + right) / 2;
  }
  return true;
}

int main() {
  ios::sync_with_stdio(false);

  int same = 0;

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

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int value;
    cin >> value;
    if (binarySearch(S, value, n)) {
      same++;
    }
  }
  cout << same << endl;

  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

bool includeNum(int fig, std::vector<int> S) {
  int left = 0;
  int right = S.size();
  int center;

  while (right > left) {
    center = (right + left) / 2;
    if (S[center] < fig) {
      left = center + 1;
    } else if (S[center] > fig) {
      right = center;
    } else {
      return true;
    }
  }

  return false;
}

int main(int argc, char const *argv[]) {

  int N, fig;
  int cnt = 0;

  std::vector<int> S;
  std::cin >> N;
  for (int i = 0; i < N; ++i) {
    std::cin >> fig;
    S.push_back(fig);
  }
  std::cin >> N;
  for (int i = 0; i < N; ++i) {
    std::cin >> fig;
    if (includeNum(fig, S))
      cnt++;
  }

  std::cout << cnt << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

bool includeNum(int fig, std::vector<int> S) {
  int left = 0;
  int right = S.size() - 1;
  int center;

  do {
    center = (right + left) / 2;
    if (S[center] < fig) {
      left = center;
    } else if (S[center] > fig) {
      right = center;
    } else {
      return true;
    }
  } while (right - left > 0);

  return false;
}

int main(int argc, char const *argv[]) {

  int N, fig;
  int cnt = 0;

  std::vector<int> S;
  std::cin >> N;
  for (int i = 0; i < N; ++i) {
    std::cin >> fig;
    S.push_back(fig);
  }

  std::cin >> N;
  for (int i = 0; i < N; ++i) {
    std::cin >> fig;
    if (includeNum(fig, S))
      cnt++;
  }

  std::cout << cnt << std::endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <assert.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define loop(i, a, b) for (i = a; i < b; i++)
#define rloop(i, a, b) for (i = a; i >= b; i--)
const int inf = 1e8;
const ll INF = 1e16;
#define MOD 1000000007
#define mod 1000000009

int search(int *s, int t, int left, int right) {
  while (left < right) {
    int k = (right + left) / 2;
    if (s[k] == t) {
      return 1;
    } else if (s[k] > t) {
      right = k;
    } else {
      left = k + 1;
    }
  }
  return 0;
}

int main() {
  int n, q, i, j;
  cin >> n;
  int s[n];
  int cnt = 0;
  loop(i, 0, n) { cin >> s[i]; }
  cin >> q;
  int t;
  loop(i, 0, q) {
    cin >> t;
    if (search(s, t, 0, n) != 0) {
      cnt++;
    }
  }
  cout << cnt << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define loop(i, a, b) for (i = a; i < b; i++)
#define rloop(i, a, b) for (i = a; i >= b; i--)
const int inf = 1e8;
const ll INF = 1e16;
#define MOD 1000000007
#define mod 1000000009

int search(int *s, int t, int left, int right) {

  while (left < right) {
    int k = (right + left) / 2;
    if (s[k] == t) {
      return 1;
    } else if (s[k] > t) {
      right = k;
    } else {
      left = k;
    }
  }
  return 0;
}

int main() {
  int n, q, i, j;
  cin >> n;
  int s[n];
  int cnt = 0;
  loop(i, 0, n) { cin >> s[i]; }
  cin >> q;
  int t;
  loop(i, 0, q) {
    cin >> t;
    if (search(s, t, 0, n) != 0) {
      cnt++;
    }
  }
  cout << cnt << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>

using namespace std;

int count = 0;

int binary_search(int A[], int n, int key) {

  int left, right, j;

  int origin = 0;

  left = 0;
  right = n;

  while (left < right) {

    j = (left + right) / 2;

    if (A[j] == key)
      return 1;

    else if (A[j] > key)
      right = j;

    else
      left = j + 1;
  }

  return 0;
}

int main() {

  int i, n, count;

  scanf("%d", &n);

  int S[n];

  for (i = 0; i < n; i++) {

    scanf("%d", &S[i]);
  }

  int m;

  scanf("%d", &m);

  int key;

  count = 0;

  for (i = 0; i < m; i++) {

    scanf("%d", &key);

    count += binary_search(S, n, key);
  }

  printf("%d\\n", count);
}

Error: TLE

Fixed submission:

#include <cstdio>

using namespace std;

int count = 0;

int binary_search(int A[], int n, int key) {

  int j, count, length;

  int origin = 0;

  length = n;

  if (n % 2 == 0) {

    j = n / 2;

    while (length > 0) {

      if (A[j - 1] == key) {

        count++;

        break;

      } else if (A[j - 1] > key) {

        length = length / 2;

        j = origin + length / 2;

      } else {

        length = length / 2;

        origin = j;

        j += length / 2;
      }
    }

  } else {

    j = n / 2;

    while (length > 0) {

      if (A[j - 1] == key) {

        count++;

        break;

      } else if (A[j - 1] > key) {

        length = length / 2;

        j = origin + length / 2;

      } else {

        length = length / 2 + 1;

        origin = j;

        j += length / 2;
      }
    }
  }

  return count;
}

int main() {

  int i, n, count;

  scanf("%d", &n);

  int S[n];

  for (i = 0; i < n; i++) {

    scanf("%d", &S[i]);
  }

  int m;

  scanf("%d", &m);

  int key;

  count = 0;

  for (i = 0; i < m; i++) {

    scanf("%d", &key);

    count += binary_search(S, n, key);
  }

  printf("%d\\n", count);
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
int main() {
  using namespace std;
  int len1, len2;
  cin >> len1;
  int *arr1 = new int[len1];
  for (int i = 0; i < len1; i++)
    cin >> arr1[i];
  cin >> len2;
  int *arr2 = new int[len2];
  for (int i = 0; i < len2; i++)
    cin >> arr2[i];
  int cnt = 0;
  for (int i = 0; i < len2; i++) {
    int l = 0;
    int r = len1 - 1;
    int m = (l + r) / 2;
    while (l < r) {
      if (arr1[m] == arr2[i]) {
        cnt++;
        break;
      } else if (arr1[m] < arr2[i]) {
        l = m + 1;
        m = (l + r) / 2;
      } else {
        r = m - 1;
        m = (l + r) / 2;
      }
    }
    if (arr1[l] == arr1[r] && arr1[l] == arr2[i] && l == r)
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
int main() {
  using namespace std;
  int len1, len2;
  cin >> len1;
  int *arr1 = new int[len1];
  for (int i = 0; i < len1; i++)
    cin >> arr1[i];
  cin >> len2;
  int *arr2 = new int[len2];
  for (int i = 0; i < len2; i++)
    cin >> arr2[i];
  int cnt = 0;
  for (int i = 0; i < len2; i++) {
    int l = 0;
    int r = len1 - 1;
    int m = (l + r) / 2;
    while (l != r) {
      if (arr1[m] == arr2[i]) {
        cnt++;
        break;
      } else if (arr1[m] < arr2[i]) {
        l = m + 1;
        m = (l + r) / 2;
      } else {
        r = m - 1;
        m = (l + r) / 2;
      }
    }
    if (arr1[l] == arr1[r] && arr1[l] == arr2[i])
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;
bool search(int A[], int n, int key) {
  int i = 0;
  A[n] = key;
  while (A[i] != key)
    i++;
  return i < n;
}
int BinarySearch(int A[], int n, int key) {
  int left = 0;
  int right = n;
  int mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (A[mid] == key)
      return 1;
    if (A[mid] > key)
      right = mid;
    else
      left = mid + 1;
  }
  return 0;
}
int main() {
  int n, m;
  cin >> n;
  int *S = new int[n + 1];
  for (int i = 0; i < n; ++i)
    cin >> S[i];
  cin >> m;
  int *T = new int[m];
  for (int i = 0; i < m; ++i)
    cin >> T[i];
  int sum = 0;
  for (int j = 0; j < m; ++j)
    if (BinarySearch(S, n, T[j]))
      sum++;
  cout << sum << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;
bool search(int A[], int n, int key) {
  int i = 0;
  A[n] = key;
  while (A[i] != key)
    i++;
  return i < n;
}
int main() {
  int n, m;
  cin >> n;
  int *S = new int[n + 1];
  for (int i = 0; i < n; ++i)
    cin >> S[i];
  cin >> m;
  int *T = new int[m];
  for (int i = 0; i < m; ++i)
    cin >> T[i];
  int sum = 0;
  for (int j = 0; j < m; ++j)
    if (search(S, n, T[j]))
      sum++;
  cout << sum << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

int get_middle(int head, int tail) {
  if ((tail - head) % 2) {
    return (tail + head) / 2;
  } else {
    return (tail + head + 1) / 2;
  }
}

bool binary_search(int *s, int num, int head, int tail) {
  if (head == tail) {
    if (s[head] == num) {
      return true;
    } else {
      return false;
    }
  } else if (tail - head == 1) {
    if (s[head] == num || s[tail] == num) {
      return true;
    } else {
      return false;
    }
  }
  int middle = get_middle(head, tail);
  if (s[middle] == num) {
    return true;
  } else if (s[middle] > num) {
    return binary_search(s, num, head, middle - 1);
  } else if (s[middle] < num) {
    return binary_search(s, num, middle + 1, tail);
  }
  printf("error\\n");
  return false;
}

int main(void) {
  int n;
  scanf("%d", &n);

  int s[100000];
  for (int i = 0; i < n; i++) {
    scanf("%d", &s[i]);
  }

  int q;
  scanf("%d", &q);

  int t[50000];
  for (int i = 0; i < q; i++) {
    scanf("%d", &t[i]);
  }

  int cnt = 0;
  for (int i = 0; i < q; i++) {
    if (binary_search(s, t[i], 0, n - 1)) {
      cnt++;
    }
  }

  printf("%d\\n", cnt);
}


Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

int get_middle(int head, int tail) {
  if ((tail - head) % 2) {
    return (tail + head) / 2;
  } else {
    return (tail + head + 1) / 2;
  }
}

bool binary_search(vector<int> s, int num, int head, int tail) {
  if (head == tail) {
    if (s[head] == num) {
      return true;
    } else {
      return false;
    }
  } else if (tail - head == 1) {
    if (s[head] == num || s[tail] == num) {
      return true;
    } else {
      return false;
    }
  }
  int middle = get_middle(head, tail);
  if (s[middle] == num) {
    return true;
  } else if (s[middle] > num) {
    return binary_search(s, num, head, middle - 1);
  } else if (s[middle] < num) {
    return binary_search(s, num, middle + 1, tail);
  }
  printf("error\\n");
  return false;
}

int main(void) {
  int n;
  scanf("%d", &n);

  vector<int> s;
  for (int i = 0; i < n; i++) {
    int s_;
    scanf("%d", &s_);
    s.push_back(s_);
  }

  int q;
  scanf("%d", &q);

  int t[50000];
  for (int i = 0; i < q; i++) {
    scanf("%d", &t[i]);
  }

  int cnt = 0;
  for (int i = 0; i < q; i++) {
    if (binary_search(s, t[i], 0, s.size() - 1)) {
      cnt++;
    }
  }

  printf("%d\\n", cnt);
}