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

#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
using ll = long long;
using ld = long double;
#define int ll
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
#define mp make_pair
signed main() {
  int n;
  cin >> n;
  vi v(n);
  rep(i, n) cin >> v[i];
  int m;
  cin >> m;
  int ans = 0;
  rep(i, m) {
    int t;
    cin >> t;
    int imax = n - 1;
    int imin = 0;
    int mid;
    while (imax >= imin) {
      mid = (imax + imin) / 2;
      if (v[mid] > t)
        imax = mid - 1;
      else if (v[mid] < t)
        imin = mid + 1;
      else {
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
using ll = long long;
using ld = long double;
#define int ll
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
#define mp make_pair
signed main() {
  int n;
  cin >> n;
  vi v(n);
  rep(i, n) cin >> v[i];
  int m;
  cin >> m;
  int ans = 0;
  rep(i, m) {
    int t;
    cin >> t;
    int imax = n - 1;
    int imin = 0;
    int mid;
    while (imax >= imin) {
      mid = (imax + imin) / 2;
      if (v[mid] > t)
        imax = mid + 1;
      else if (v[mid] < t)
        imin = mid - 1;
      else {
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
using ll = long long;
using ld = long double;
#define int ll
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
#define mp make_pair
signed main() {
  int n;
  cin >> n;
  vi v(n);
  rep(i, n) cin >> v[i];
  int m;
  cin >> m;
  int ans = 0;
  rep(i, m) {
    int t;
    cin >> t;
    int imax = n;
    int imin = -1;
    int mid;
    while (imax - imin > 1) {
      mid = (imax + imin) / 2;
      if (v[mid] > t)
        imax = mid;
      else
        imin = mid;
    }
    if (v[imin] == t)
      ans++;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
using ll = long long;
using ld = long double;
#define int ll
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
#define mp make_pair
signed main() {
  int n;
  cin >> n;
  vi v(n);
  rep(i, n) cin >> v[i];
  int m;
  cin >> m;
  int ans = 0;
  rep(i, m) {
    int t;
    cin >> t;
    int imax = n - 1;
    int imin = 0;
    int mid;
    while (imax - imin > 1) {
      mid = (imax + imin) / 2;
      if (v[mid] > t)
        imax = mid;
      else if (v[mid] < t)
        imin = mid;
    }
    if (v[imin] == t)
      ans++;
  }
  cout << ans << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
using ll = long long;
using ld = long double;
// #define int ll
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
#define mp make_pair
signed main() {
  int n;
  cin >> n;
  vector<int> v(n);
  rep(i, n) cin >> v[i];
  int ans = 0;
  int a;
  cin >> a;
  vector<int> u(a);
  rep(i, a) cin >> u[i];
  rep(i, a) {
    int f = u[i];
    int imin = 0;
    int imax = n;
    while (imax - imin > 1) {
      int mid = (imax + imin) / 2;
      if (v[mid] > f)
        imax = mid;
      else
        imin = mid;
    }
    if (v[imin] == f)
      ans++;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
using ll = long long;
using ld = long double;
// #define int ll
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
#define mp make_pair
signed main() {
  int n;
  cin >> n;
  vector<int> v(n);
  rep(i, n) cin >> v[i];
  int ans = 0;
  int a;
  cin >> a;
  vector<int> u(a);
  rep(i, a) cin >> u[i];
  rep(i, a) {
    int f = u[i];
    int imin = 0;
    int imax = n - 1;
    while (imax > imin) {
      int mid = (imax + imin) / 2;
      if (v[mid] > f)
        imax = mid + 1;
      else
        imin = mid - 1;
    }
    if (v[imin] == f)
      ans++;
  }
  cout << ans << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

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 cnt = 0;
  for (int ti = 0; ti < q; ti++) {
    int left = 0, right = n, mid;
    while (left < right) {
      mid = (left + right) / 2;
      if (S[mid] == T[ti]) {
        cnt++;
        break;
      } else if (T[ti] < S[mid])
        right = mid;
      else
        left = mid + 1;
    }
  }
  cout << cnt << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

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 cnt = 0;
  for (int ti = 0; ti < q; ti++) {
    int left = 0, right = n, mid;
    while (left < right) {
      mid = (left + right) / 2;
      if (S[mid] == T[ti]) {
        cnt++;
        break;
      } else if (T[ti] < S[mid])
        right = mid;
      else
        left = mid - 1;
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {

  int n, q, t, size, cnt = 0;

  cin >> n;

  int s[n];

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

  cin >> q;

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

    int l = 0, r = n, m;
    while (l <= r) {
      m = (l + r) / 2;
      if (s[m] == t) {
        ++cnt;
        break;
      } else if (s[m] < t) {
        l = m + 1;
      } else if (s[m] > t) {
        r = m - 1;
      }
    }
  }

  cout << cnt << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {

  int n, q, t, cnt = 0;

  cin >> n;

  int s[n];

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

  cin >> q;

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

    int l = 0, r = n, m;
    while (1) {
      m = (l + r) / 2;
      if (s[m] == t) {
        // cout << "found" << endl;
        ++cnt;
        break;
      } else if (l == r) {
        break;
      } else if (s[m] < t) {
        l = m + 1;
      } else if (s[m] > t) {
        r = m - 1;
      }
      // cout << m << ' ' << l << ' ' << r << endl;
    }
  }

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

#include <iostream>
using namespace std;

int main() {
  int *S;
  int n, q, t;
  int cnt;

  cin >> n;
  S = new int[n];
  for (int i = 0; i < n; ++i) {
    cin >> S[i];
  }
  cin >> q;
  cnt = 0;
  for (int i = 0; i < q; ++i) {
    cin >> t;
    int st = 0, ed = n - 1, mid;
    if (S[st] == t || S[ed] == t)
      ++cnt;
    else {
      while (ed - st > 1) {
        mid = (st + ed) / 2;
        if (S[mid] == t) {
          ++cnt;
          break;
        } else if (S[mid] > t) {
          ed = mid;
        } else {
          st = mid;
        }
      }
    }
  }
  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, q, *S, *T, c = 0, i, j;

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

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

  for (i = q; i; --i)
    for (j = n; j; --j)
      if (S[j - 1] == T[i - 1]) {
        ++c;
        break;
      }

  cout << c << endl;

  delete[] S;
  delete[] T;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int ans = 0;

bool answer(int n, long num[], int p) {
  if (n == num[p]) {
    ans++;
    return true;
  }
  return false;
}

int main() {
  int s, t, n;

  cin >> s;
  long num[s];

  for (int i = 0; i < s; i++)
    cin >> num[i];

  cin >> t;
  for (int i = 0; i < t; i++) {
    int p = 0, pp = s;
    bool br = false;
    cin >> n;
    while (true) {
      int a = p + ((pp - p) / 2);
      if (answer(n, num, a))
        break;
      else if (n > num[a]) {
        if (answer(n, num, a + 1))
          break;
        p = a + 1;
        if (p == pp)
          break;
      } else if (answer(n, num, a - 1))
        break;
      else {
        pp = a - 2;
        if (pp <= p)
          break;
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int ans = 0;

bool answer(int n, long num[], int p) {
  if (n == num[p]) {
    ans++;
    return true;
  }
  return false;
}

int main() {
  int s, t, n;

  cin >> s;
  long num[s];

  for (int i = 0; i < s; i++)
    cin >> num[i];

  cin >> t;
  for (int i = 0; i < t; i++) {
    int p = 0, pp = s;
    bool br = false;
    cin >> n;
    while (true) {
      int a = p + ((pp - p) / 2);
      if (answer(n, num, a))
        break;
      else if (n > num[a]) {
        if (answer(n, num, a + 1))
          break;
        p = a;
        if (p >= s)
          break;
      } else if (answer(n, num, a - 1))
        break;
      else {
        pp = a - 2;
        if (pp <= p)
          break;
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

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

int main() {
  int n, A[100000 + 5], 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);
    sum += BsearchQ(A, n, key);
  }
  printf("%d\\n", sum);
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>

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

int main() {
  int 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);
    sum += BsearchQ(A, n, key);
  }
  printf("%d\\n", sum);
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdlib>
#include <iostream>
#include <vector>
/*与えられた数列Sの中に含まれている数列Tの要素の数を出力する*/
using namespace std;
int main() {
  vector<int> s, t;
  int n, n2;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    s.push_back(tmp);
  }
  cin >> n2;
  for (int i = 0; i < n2; i++) {
    int tmp;
    cin >> tmp;
    t.push_back(tmp);
  }
  int ans = 0;
  for (int t_i = 0; t_i < n2; t_i++) {
    int first = 0, last = n, middle;
    while (first < last) {
      middle = (first + last) / 2;
      if (s[middle] == t[t_i]) {
        ans++;
        break;
      } else if (s[middle] > t[t_i]) {
        last = middle;
      } else {
        first = middle + 1;
      }
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <vector>
/*与えられた数列Sの中に含まれている数列Tの要素の数を出力する*/
using namespace std;
int main() {
  vector<int> s, t;
  int n, n2;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    s.push_back(tmp);
  }
  cin >> n2;
  for (int i = 0; i < n2; i++) {
    int tmp;
    cin >> tmp;
    t.push_back(tmp);
  }
  int ans = 0;
  for (int t_i = 0; t_i < n2; t_i++) {
    int first = 0, last = n, middle = last / s.size();
    while (first <= last) {
      if (s[middle] == t[t_i]) {
        ans++;
        break;
      } else if (s[middle] > t[t_i]) {
        first = middle + 1;
      } else if (s[middle] < t[t_i]) {
        last = middle - 1;
      }
    }
  }
  cout << ans << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define PB push_back

int main(int argc, char *argv[]) {
  int n, q, x, ans = 0, i;
  vector<int> vis, vit;

  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> x;
    vis.PB(x);
  }
  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> x;
    vit.PB(x);
  }

  sort(vis.begin(), vis.end());
  sort(vit.begin(), vit.end());

  vector<int>::iterator itvis, itvit;
  itvis = vis.begin();
  itvit = vit.begin();

  while (itvis != vis.end() && itvit != vit.end()) {
    if (*itvis == *itvit) {
      ans++;
      itvis++;
      itvit++;
    } else if (*itvis > *itvit) {
      itvit++;
    } else if (*itvis < *itvit) {
      itvis++;
    }
  }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <list>
using namespace std;

#define PB push_back

int main(int argc, char *argv[]) {
  int n, q;
  list<int> lis, lit;
  int i, x, ans = 0;

  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> x;
    lis.PB(x);
  }
  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> x;
    lit.PB(x);
  }
  // 重複要素を削除
  lis.sort();
  lis.unique();
  lit.sort();
  lit.unique();

  list<int>::iterator itlit;
  for (itlit = lit.begin(); itlit != lit.end(); itlit++) {
    if (find(lis.begin(), lis.end(), *itlit) != lis.end())
      ans++;
  }

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

#include <iostream>
#include <vector>

using namespace std;

bool binary_search(vector<int> &v, int &n) {
  int left = 0;
  int right = v.size();
  while (left < right) {
    int middle = (left + right) / 2;
    if (v[middle] == n)
      return true;
    if (n < v[middle]) {
      right = middle;
    } else {
      left = middle + 1;
    }
  }
  return false;
}

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

  int q;
  cin >> q;

  int count = 0;
  for (int i = 0; i < q; ++i) {
    int t;
    cin >> t;
    if (binary_search(S, t))
      ++count;
  }
  cout << count << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

bool binary_search(vector<int> &v, int &n) {
  int left = 0;
  int right = v.size() - 1;
  int middle = (right - left) / 2;
  while (true) {
    if (v[middle] == n)
      return true;
    if (v[middle] < n) {
      left = middle;
      middle = (right + left + 1) / 2;
    } else {
      right = middle;
      middle = (right + left) / 2;
    }
    if (left == right)
      break;
  }
  return false;
}

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

  int q;
  cin >> q;

  int count = 0;
  for (int i = 0; i < q; ++i) {
    int t;
    cin >> t;
    if (binary_search(S, t))
      ++count;
  }
  cout << count << endl;

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

#include <iostream>
using namespace std;
int main() {
  int n, q, s[100001], t, 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;
    int l = 0, r = n, m;
    while (l < r) {
      m = (l + r) / 2;
      if (t < s[m])
        r = m;
      else if (t > s[m])
        l = m + 1;
      else {
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, q, s[100001], t, 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;
    int l = 0, r = n, m;
    while (l < r) {
      m = (l + r) / 2;
      if (t < s[m])
        r = m;
      else if (t > s[m])
        l = m;
      else {
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  vector<int> o1(100000);
  vector<int> o2(50000);
  int n1;
  int n2;
  int count = 0;
  int n;
  cin >> n1;
  for (int i = 0; i < n1; i++) {
    cin >> o1[i];
  }
  sort(o1.begin(), o1.end());
  cin >> n2;
  for (int j = 0; j < n2; j++) {
    cin >> o2[j];
  }
  for (int i = 0; i < n2; i++) {
    if (binary_search(o1.begin(), o1.end(), o2[i])) {
      count++;
    }
  }
  cout << count << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  vector<int> o1(100000, 0);
  vector<int> o2(50000, 0);
  int n1;
  int n2;
  int count = 0;
  cin >> n1;
  for (int i = 0; i < n1; i++) {
    cin >> o1[i];
  }
  cin >> n2;
  for (int j = 0; j < n2; j++) {
    cin >> o2[j];
  }
  for (int i = 0; i < n2; i++) {
    for (int j = 0; j < n1; j++) {
      if (o2[i] == o1[j]) {
        count++;
        break;
      }
    }
  }
  cout << count << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool binary_search(int target, int low, int high, vector<int> &S) {
  int mid = low + (high - low) / 2;
  if (S.at(mid) == target) {
    return true;
  }
  if (low >= high)
    return false;
  if (S.at(mid) > target) {
    return binary_search(target, low, mid - 1, S);
  } else {
    return binary_search(target, mid + 1, high, S);
  }
}

int main() {
  int n, q, a, c = 0, i, j;
  vector<int> S;
  scanf("%d", &n);
  S.resize(n);
  for (i = 0; i < n; ++i) {
    scanf("%d", &S[i]);
  }

  scanf("%d", &q);
  for (j = 0; j < q; ++j) {
    scanf("%d", &a);
    if (binary_search(a, 0, n - 1, S))
      ++c;
  }
  printf("%d\\n", c);
  return 0;
}

Error: TLE

Fixed submission:

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

bool binary_search(int target, int low, int high, vector<int> &S) {
  int mid = low + (high - low) / 2;
  if (S.at(mid) == target) {
    return true;
  }
  if (low == high)
    return false;
  if (S.at(mid) > target) {
    return binary_search(target, low, mid - 1, S);
  } else {
    return binary_search(target, mid + 1, high, S);
  }
}

int main() {
  int n, q, a, c = 0, i, j;
  vector<int> S;
  scanf("%d", &n);
  S.resize(n);
  for (i = 0; i < n; ++i) {
    scanf("%d", &S[i]);
  }

  scanf("%d", &q);
  for (j = 0; j < q; ++j) {
    scanf("%d", &a);
    if (binary_search(a, 0, n - 1, S))
      ++c;
  }
  printf("%d\\n", c);
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ll long long

int binarysearch(int A[], int n, int key) {
  int left = 0, right = n;
  while (left < right) {
    int 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 s[100001];
  int n, q, key;

  cin >> n;
  REP(i, n) cin >> s[i];
  cin >> q;

  int sum = 0;
  REP(i, q) {
    cin >> key;
    if (binarysearch(s, n, key))
      sum++;
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

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

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ll long long

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

int main() {
  int s[100001];
  int n, q, key;

  cin >> n;
  REP(i, n) cin >> s[i];
  cin >> q;

  int sum = 0;
  REP(i, q) {
    cin >> key;
    if (binarysearch(s, n, key))
      sum++;
  }
  cout << sum << endl;
}