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

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

namespace shamal {
template <typename F, typename IndexType, typename TargetType>
pair<bool, IndexType> lower_bound(F const &func, IndexType left,
                                  IndexType right, TargetType const &t) {
  do {
    auto const mid = left + (right - left) / 2;
    auto const val = func(mid);
    if (val > t) {
      right = mid - 1;
    } else if (val < t) {
      left = mid + 1;
    } else {
      right = mid;
    }
  } while (left <= right);

  return {func(left) == t, left};
}
template <typename F, typename IndexType, typename TargetType>
pair<bool, IndexType> binary_search(F const &func, IndexType left,
                                    IndexType right, TargetType const &t) {
  do {
    auto const mid = left + (right - left) / 2;
    auto const val = func(mid);
    if (val > t) {
      right = mid - 1;
    } else if (val < t) {
      left = mid + 1;
    } else {
      return {true, mid};
    }
  } while (left <= right);
  return {false, IndexType()};
}
}; // namespace shamal

int main(void) {
  int ns, nt;
  cin >> ns;
  vector<int> s(ns);
  for (int i = 0; i < ns; i++) {
    scanf("%d", &s[i]);
  }

  cin >> nt;
  vector<int> t(nt);
  for (int i = 0; i < nt; i++) {
    scanf("%d", &t[i]);
  }

  int r = 0;
  for (auto const &x : t) {
    if (shamal::binary_search([&](int i) { return s[i]; }, 0, ns - 1, x).first)
      r++;
  }
  cout << r << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

namespace shamal {
template <typename F, typename IndexType, typename TargetType>
pair<bool, IndexType> lower_bound(F const &func, IndexType left,
                                  IndexType right, TargetType const &t) {
  while (left != right) {
    auto const mid = left + (right - left) / 2;
    auto const val = func(mid);
    if (val > t) {
      right = mid - 1;
    } else if (val < t) {
      left = mid + 1;
    } else {
      right = mid;
    }
  }
  return {func(left) == t, left};
}
}; // namespace shamal

int main(void) {
  int ns, nt;
  cin >> ns;
  vector<int> s(ns);
  for (int i = 0; i < ns; i++) {
    scanf("%d", &s[i]);
  }

  cin >> nt;
  vector<int> t(nt);
  for (int i = 0; i < nt; i++) {
    scanf("%d", &t[i]);
  }

  int r = 0;
  for (auto const &x : t) {
    if (shamal::lower_bound([&](int i) { return s[i]; }, 0, ns - 1, x).first)
      r++;
  }
  cout << r << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int A[1000000], n;

int binarySearch(int key) {
  int left = 0;
  int right = n;
  int mid;

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

int main(void) {
  int i, q, k, sum = 0;

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

  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d", &k);
    if (binarySearch(k))
      sum++;
  }
  printf("%d\\n", sum);
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int A[1000000], n;

int binarySearch(int key) {
  int left = 0;
  int right = n;
  int mid;

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

int main(void) {
  int i, q, k, sum = 0;

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

  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d", &k);
    if (binarySearch(k))
      sum++;
  }
  printf("%d\\n", sum);
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <stdlib.h>

int main() {
  int a, m, n, q, i, S[100000], T[50000], r, l;

  m = 0;

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

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

  for (i = 0; i < q; i++) {
    l = 0;
    r = n;

    while (l < r) {
      a = (l + r) / 2;
      if (S[a] == T[i]) {
        m++;
        break;
      } else if (S[a] < T[i]) {
        l = a + 1;
      } else {
        r = a;
      }
    }
  }
  printf("%d\\n", m);
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>

int main() {
  int a, m, n, q, i, j, S[10000], T[500], r, l;
  m = 0;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &S[i]);
  }

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

  for (i = 0; i < q; i++) {
    l = 0;
    r = n;

    while (l <= r) {
      a = (r + l) / 2;
      if (S[a] == T[i]) {
        m++;
        break;
      } else if (S[a] < T[i]) {
        l = a + 1;
      } else if (S[a] > T[i]) {
        r = a;
      }
    }
  }
  printf("%d\\n", m);
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int S[100000];

bool binaryS(int key, int n);

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

  int q, sum = 0;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int key;
    cin >> key;
    if (binaryS(key, n)) {
      sum++;
    }
  }

  cout << sum << endl;
  return 0;
}

bool binaryS(int key, int n) {

  int l = 0;
  int t = n;

  while (l <= t) {
    int s = (l + t) / 2;
    if (S[s] == key) {
      return true;
    } else if (S[s] < key) {
      l = s + 1;
    } else if (S[s] > key) {
      t = s - 1;
    }
  }

  return false;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int S[100000];

bool binaryS(int key, int n);

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

  int q, sum = 0;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int key;
    cin >> key;
    if (binaryS(key, n)) {
      sum++;
    }
  }

  cout << sum << endl;
  return 0;
}

bool binaryS(int key, int n) {

  int l = 0;
  int t = n;

  while (l < t) {
    int s = (l + t) / 2;
    if (S[s] < key) {
      l = s;
    } else if (S[s] > key) {
      t = s;
    } else if (S[s] == key) {
      return true;
    }
  }

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

#include <iostream>
#include <vector>

constexpr int NOT_FOUND = 8101919;

bool IsNotElem(std::vector<int> &done, int x) {
  for (const auto &g : done) {
    if (g == x) {
      return false;
    }
  }
  done.push_back(x);
  return true;
}

void MySwap(int &a, int &b) {
  int tmp = std::move(a);
  a = std::move(b);
  b = std::move(tmp);
}

bool BinarySearch(std::vector<int> &v, int key) {
  int left = 0;
  int right = v.size();
  int mid;
  // std::cout << key << ": ";
  while (left < right) {
    mid = (left + right) / 2;
    if (v[mid] == key) {
      // std::cout << key;
      return true;
    } else if (key < v[mid]) {
      right = mid;
    } else {
      left = mid + 1;
    }

    if (key == 1) {
      // std::cout << v[mid] << " ";
    }
  }
  return false;
}

int main() {
  int n;
  std::cin >> n;
  std::vector<int> S(n);
  for (auto &s : S) {
    std::cin >> s;
  }

  int q;
  std::cin >> q;
  std::vector<int> T(q);
  for (auto &t : T) {
    std::cin >> t;
  }

  int num = 0;
  std::vector<int> done;
  for (auto &t : T) {
    if (BinarySearch(S, t)) {
      ++num;
    }
  }
  std::cout << num << "\\n";
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

constexpr int NOT_FOUND = 8101919;

bool IsNotElem(std::vector<int> &done, int x) {
  for (const auto &g : done) {
    if (g == x) {
      return false;
    }
  }
  done.push_back(x);
  return true;
}

void MySwap(int &a, int &b) {
  int tmp = std::move(a);
  a = std::move(b);
  b = std::move(tmp);
}

void BubbleSort(std::vector<int> &vec) {
  int n = vec.size();
  for (int i = 0; i < n; ++i) {
    for (int j = n - 1; j > i; --j) {
      if (vec[j] < vec[j - 1]) {
        MySwap(vec[j], vec[j - 1]);
      }
    }
  }
}

bool BinarySearch(std::vector<int> &v, int key) {
  int left = 0;
  int right = v.size();
  int mid;
  // std::cout << key << ": ";
  while (left < right) {
    mid = (left + right) / 2;
    if (v[mid] == key) {
      // std::cout << key;
      return true;
    } else if (key < v[mid]) {
      right = mid;
    } else {
      left = mid + 1;
    }

    if (key == 1) {
      // std::cout << v[mid] << " ";
    }
  }
  return false;
}

int main() {
  int n;
  std::cin >> n;
  std::vector<int> S(n);
  for (auto &s : S) {
    std::cin >> s;
  }

  int q;
  std::cin >> q;
  std::vector<int> T(q);
  for (auto &t : T) {
    std::cin >> t;
  }

  BubbleSort(T);

  int num = 0;
  std::vector<int> done;
  for (auto &t : T) {
    if (BinarySearch(S, t)) {
      ++num;
    }
    // std::cout << "\\n";
  }
  std::cout << num << "\\n";
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

int A[1000000], n;

bool search(int *A, int key) {
  int mid, r, l;
  l = 0;
  r = n;
  while (l < r) {
    mid = (r + l) / 2;
    if (A[mid] == key)
      return true;
    if (A[mid] < key)
      l = mid + 1;
    else if (A[mid] > key)
      r = mid;
  }
  return false;
}

int main() {
  int i, q, key, sum = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);

  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &key);
    if (search(A, key))
      sum++;
  }
  printf("%d\\n", sum);
  return 0;
}


Error: 0

Fixed submission:

#include <stdio.h>
bool search(int *A, int n, int key) {
  int mid, r, l;
  l = 0;
  r = n;
  while (l < r) {
    mid = (r + l) / 2;
    if (A[mid] == key)
      return true;
    if (A[mid] < key)
      l = mid + 1;
    else if (A[mid] > key)
      r = mid;
  }
  return false;
}

int main() {
  int i, n, A[10000 + 1], q, key, sum = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);

  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &key);
    if (search(A, n, key))
      sum++;
  }
  printf("%d\\n", sum);
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
long long S[100001], key;
long n;

bool binarySearch() {
  long left, right, mid;
  left = 0;
  right = n;
  while (left < right) {
    mid = (left + right) / 2;
    if (S[mid] == key)
      return true;
    else if (key < S[mid])
      right = mid;
    else
      left = mid + 1;
  }
  return false;
}

int main(void) {
  long q, count = 0;
  long long T[50001];
  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++) {
    key = T[i];
    if (binarySearch())
      count++;
  }
  cout << count << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;
long long S[10001], key;
int n;

bool binarySearch() {
  int left, right, mid;
  left = 0;
  right = n;
  while (left < right) {
    mid = (left + right) / 2;
    if (S[mid] == key)
      return true;
    else if (key < S[mid])
      right = mid;
    else
      left = mid + 1;
  }
  return false;
}

int main(void) {
  int q, count = 0;
  long long T[501];
  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++) {
    key = T[i];
    if (binarySearch())
      count++;
  }
  cout << count << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <stack>
#include <vector>

using namespace std;

int main() {
  int n, q, key;
  int count = 0;
  vector<int> S;
  cin >> n;
  S.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> S[i];
  }
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    cin >> key;
    if (*lower_bound(S.begin(), S.end(), key) == key) {
      ++count;
    }
  }
  printf("%d\\n", count);
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <stack>
#include <vector>

using namespace std;

bool search_ele(int n, vector<int> &S, int ele) {
  int right = 0;
  int left = n;
  while (right < left) {
    int mid = (right + left) / 2;
    if (S[mid] == ele)
      return true;
    if (S[mid] > ele)
      left = mid;
    else
      right = mid;
  }
  return false;
}

int main() {
  int n, q, key;
  int count = 0;
  vector<int> S;
  scanf("%d", &n);
  S.resize(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &S[i]);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    scanf("%d", &key);
    if (search_ele(n, S, key)) {
      ++count;
    }
  }
  printf("%d\\n", count);
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int calcAve(int bottom, int top) {
  if ((bottom + top) % 2 == 1) {
    return (bottom + top - 1) / 2;
  } else {
    return (bottom + top) / 2;
  }
}
int main() {
  int n;
  cin >> n;
  int S[n];
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  int q;
  cin >> q;
  int T[q];
  for (int i = 0; i < q; ++i) {
    cin >> T[i];
  }
  int score = 0;
  for (int i = 0; i < q; ++i) {

    int bottom = 0;
    int top = n - 1;
    while (top >= bottom) {
      if (S[calcAve(bottom, top)] == T[i]) {
        score++;
        break;
      }
      if (S[calcAve(bottom, top)] > T[i]) {
        top = calcAve(bottom, top) - 1;
      } else if (S[calcAve(bottom, top)] < T[i]) {
        bottom = calcAve(bottom, top) + 1;
      }

      // cout << top << " " << bottom << endl;
    }
  }
  cout << score << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int calcAve(int bottom, int top) {
  if ((bottom + top) % 2 == 1) {
    return (bottom + top - 1) / 2;
  } else {
    return (bottom + top) / 2;
  }
}
int main() {
  int n;
  cin >> n;
  int S[n];
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  int q;
  cin >> q;
  int T[q];
  for (int i = 0; i < q; ++i) {
    cin >> T[i];
  }
  int score = 0;
  for (int i = 0; i < q; ++i) {

    int bottom = 0;
    int top = n - 1;
    while (top != bottom) {
      if (S[calcAve(bottom, top)] >= T[i]) {
        top = calcAve(bottom, top) - 1;
      } else if (S[calcAve(bottom, top)] < T[i]) {
        bottom = calcAve(bottom, top) + 1;
      }
      if (S[calcAve(bottom, top)] == T[i]) {
        score++;
        break;
      }
      // cout << top << " " << bottom << endl;
    }
  }
  cout << score << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

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

int main() {
  int n, s[100100], q, key, count = 0;
  int i, j, k;

  cin >> n;

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

  cin >> q;

  for (i = 0; i < q; i++) {
    scanf("%d", &key);
    if (binarySearch(s, n, key))
      count++;
  }

  cout << count << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

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

int main() {
  int n, s[10100], q, key, count = 0;
  int i, j, k;

  cin >> n;

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

  cin >> q;

  for (i = 0; i < q; i++) {
    scanf("%d", &key);
    if (binarySearch(s, n, key))
      count++;
  }

  cout << count << endl;

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

#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, q;
  vector<int> s;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    int t;
    scanf("%d", &t);
    s.push_back(t);
  }
  cin >> q;
  int c = 0;
  while (q--) {
    int t;
    scanf("%d", &t);
    int left = 0, right = n;
    bool flag = false;
    while (left < right) {
      int mid = (left + right) / 2;
      if (s[mid] == t) {
        flag = true;
        break;
      } else if (s[mid] < t) {
        left = mid + 1;
      } else {
        right = mid;
      }
    }
    if (flag) {
      c++;
    }
  }
  cout << c << endl;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, q;
  vector<int> s;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    int t;
    scanf("%d", &t);
    s.push_back(t);
  }
  cin >> q;
  int c = 0;
  while (q--) {
    int t;
    scanf("%d", &t);
    int left = 0, right = n + 1;
    bool flag = false;
    while (left <= right) {
      int mid = (left + right) / 2;
      if (s[mid] == t) {
        flag = true;
        break;
      } else if (s[mid] < t) {
        left = mid + 1;
      } else {
        right = mid;
      }
    }
    if (flag) {
      c++;
    }
  }
  cout << c << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>

using namespace std;

int main() {
  int n, q;
  int s[100010];
  int t[50010];
  int ans = 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 ti = 0; ti < q; ti++) {
    int left = 0;
    int right = n - 1;
    while (1) {
      int mid = (left + right) / 2;
      if (t[ti] < s[mid])
        right = mid;
      if (t[ti] > s[mid])
        left = mid + 1;
      if (t[ti] == s[mid]) {
        ans++;
        break;
      }
      if (left == right) {
        if (s[left] == t[ti]) {
          ans++;
        }
        break;
      }
    }
  }

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

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>

using namespace std;

int main() {
  int n, q;
  int s[100010];
  int t[50010];
  int ans = 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 ti = 0; ti < q; ti++) {
    int left = 0;
    int right = n - 1;
    while (1) {
      int mid = (left + right) / 2;
      if (t[ti] < s[mid])
        right = mid;
      else
        left = mid;
      if (left == right)
        break;
    }
    if (s[left] == t[ti])
      ans++;
  }

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

#include <algorithm>
#include <stdio.h>

int s[100000];

int main() {
  int n, q;
  int cnt = 0;

  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &s[i]);
  }
  std::sort(s, s + n);
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    int t;
    scanf("%d", &t);
    int top = 0, end = n;
    while (top < end) {
      int i2 = top + (end - top) / 2;
      if (t == s[i2]) {
        ++cnt;
        break;
      }
      if (t < s[i2]) {
        end = i2;
      } else {
        top = i2 + 1;
      }
    }
  }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>

int s[100000];

int main() {
  int n, q;
  int cnt = 0;

  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &s[i]);
  }
  std::sort(s, s + n);
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    int t;
    scanf("%d", &t);
    int top = 0, end = n;
    while (top < end) {
      int i2 = (end - top) / 2;
      if (t == s[i2]) {
        ++cnt;
        break;
      }
      if (t < s[i2]) {
        end = i2;
      } else {
        top = i2 + 1;
      }
    }
    if (top == end) {
      if (t = s[top]) {
        ++cnt;
      }
    }
  }

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

#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
int center = 0;

using namespace std;

int binarySearch(int *S, int from, int to, int t) {

  center = floor((from + to) / 2);

  //	printf("(from, center, to) = (%d, %d, %d)\\n", from, center, to);

  if (from >= to) {
    return t == S[center];
  } else if (t == S[center]) {
    return 1;
  } else if (S[center] < t) {
    return binarySearch(S, center + 1, to, t);
  } else if (t < S[center]) {
    return binarySearch(S, from, center - 1, t);
  }
}

int main() {

  int *S, *T;
  int S_size, T_size, temp;

  scanf("%d", &S_size);
  S = new int[S_size];
  for (int i = 0; i < S_size; i++) {
    scanf("%d", &temp);
    S[i] = temp;
  }
  scanf("%d", &T_size);
  T = new int[T_size];
  for (int i = 0; i < T_size; i++) {
    scanf("%d", &temp);
    T[i] = temp;
  }

  int ans = 0;

  for (int i = 0; i < T_size; i++) {

    ans += binarySearch(S, 0, S_size - 1, T[i]);
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
int center = 0;

using namespace std;

int binarySearch(vector<int> S, int from, int to, int t) {

  center = floor((from + to) / 2);

  //	printf("(from, center, to) = (%d, %d, %d)\\n", from, center, to);

  if (from >= to) {
    return t == S[center];
  } else if (t == S[center]) {
    return 1;
  } else if (S[center] < t) {
    return binarySearch(S, center + 1, to, t);
  } else if (t < S[center]) {
    return binarySearch(S, from, center - 1, t);
  }
}

int main() {

  vector<int> S;
  vector<int> T; // T?????¢???????????????
  int t, temp;

  scanf("%d", &t);
  for (int i = 0; i < t; i++) {
    scanf("%d", &temp);
    S.push_back(temp);
  }
  scanf("%d", &t);
  for (int i = 0; i < t; i++) {
    scanf("%d", &temp);
    T.push_back(temp);
  }

  int ans = 0;

  for (unsigned int i = 0; i < T.size(); i++) {

    ans += binarySearch(S, 0, S.size() - 1, T[i]);
  }
  cout << ans << endl;

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

#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
int center = 0;

using namespace std;

int binarySearch(int *S, int S_size, int t) {

  int from = 0, to = S_size - 1;

  while (from <= to) {
    center = (from + to) / 2;

    if (S[center] == t) {
      return 1;
    } else if (t < S[center]) {
      to = center - 1;
    } else if (S[center] < t) {
      from = center + 1;
    }
  }

  return 0;

  //	printf("(from, center, to) = (%d, %d, %d)\\n", from, center, to);
}

int main() {

  int *S, *T;
  int S_size, T_size, temp;

  scanf("%d", &S_size);
  S = new int[S_size];
  for (int i = 0; i < S_size; i++) {
    scanf("%d", &temp);
    S[i] = temp;
  }
  scanf("%d", &T_size);
  T = new int[T_size];
  for (int i = 0; i < T_size; i++) {
    scanf("%d", &temp);
    T[i] = temp;
  }

  int ans = 0;

  for (int i = 0; i < T_size; i++) {

    ans += binarySearch(S, S_size, T[i]);
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
int center = 0;

using namespace std;

int binarySearch(int *S, int S_size, int t) {

  int from = 0, to = S_size - 1, center = (from + to) / 2;

  while (from <= to) {

    if (S[center] == t) {
      return 1;
    } else if (t < S[center]) {
      to = center - 1;
    } else if (S[center] < t) {
      from = center + 1;
    }
  }

  return 0;

  //	printf("(from, center, to) = (%d, %d, %d)\\n", from, center, to);
}

int main() {

  int *S, *T;
  int S_size, T_size, temp;

  scanf("%d", &S_size);
  S = new int[S_size];
  for (int i = 0; i < S_size; i++) {
    scanf("%d", &temp);
    S[i] = temp;
  }
  scanf("%d", &T_size);
  T = new int[T_size];
  for (int i = 0; i < T_size; i++) {
    scanf("%d", &temp);
    T[i] = temp;
  }

  int ans = 0;

  for (int i = 0; i < T_size; i++) {

    ans += binarySearch(S, S_size, T[i]);
  }
  cout << ans << endl;

  return 0;
}