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

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

int main() {
  int n = 0, q = 0, k = 0, max = 0, min = 0;
  long t = 0;

  cin >> n;
  vector<long> S(n);

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

  cin >> q;

  for (int i = 0; i < q; i++) {
    cin >> t;
    max = n - 1;
    min = 0;

    while (max >= min) {
      if (t > S[(min + max) / 2])
        min = (min + max) / 2 + 1;
      else if (t < S[(min + max) / 2])
        max = (min + max) / 2 - 1;
      else {
        k++;
        break;
      }
    }
  }

  cout << k << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n = 0, q = 0, k = 0;
  long t = 0;

  cin >> n;
  vector<long> S(n);
  vector<long>::iterator st;

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

  cin >> q;

  for (int i = 0; i < q; i++) {
    cin >> t;
    st = find(S.begin(), S.end(), t);
    if (st != S.end())
      k++;
  }

  cout << k << endl;

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

#include <iostream>

#define MAXN 100000
#define MAXQ 50000

using namespace std;

int n, q;
int S[MAXN + 1], T[MAXQ + 1];
int cnt = 0;

bool binarySearch(int num) {
  int lo, mi, hi;
  bool res = false;
  lo = 0;
  mi = hi = n;
  while (lo <= hi) {
    mi = (lo + hi) / 2;
    if (num < S[mi]) {
      hi = mi - 1;
    } else if (S[mi] < num) {
      lo = mi + 1;
    } else {
      res = true;
      break;
    }
  }

  return res;
}

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

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

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

#define MAXN 100000
#define MAXQ 50000

using namespace std;

int n, q;
int S[MAXN + 1], T[MAXQ + 1];
int cnt = 0;

bool binarySearch(int num) {
  int lo, mi, hi;
  bool res = false;
  lo = 0;
  mi = hi = n;
  while (1 < mi) {
    mi = (lo + hi) / 2;
    if (num < S[mi]) {
      hi = mi - 1;
    } else if (S[mi] < num) {
      lo = mi + 1;
    } else {
      res = true;
      break;
    }
  }

  return res;
}

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

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

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

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

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

int main() {
  int N;
  int S[200000];
  int q;
  int T[50000];
  int count = 0;

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

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

  cout << count << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

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

int main() {
  int N;
  int S[100000];
  int q;
  int T[20000];
  int count = 0;

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

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

  cout << count << endl;

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

#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

  int m, k, ans = 0;
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> k;
    if (*lower_bound(vec.begin(), vec.end(), k) == k)
      ans++;
  }
  cout << ans << endl;
}


Error: -11

Fixed submission:

#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

  int m, k, ans = 0;
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> k;
    if (*lower_bound(vec.begin(), vec.end(), k) == k)
      ans++;
  }
  cout << ans << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  int s[100001];
  cin >> 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;
    int left = 0, right = n;
    while (left < right) {
      int mid = (left + right) / 2;
      if (s[mid] == t) {
        count++;
        break;
      } else if (t < s[mid]) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
  }
  cout << count << endl;
  return 0;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  int s[100001];
  int t[50001];
  cin >> 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++) {
    cin >> t[i];
  }
  for (int i = 0; i < q; i++) {
    for (int j = 0; j < n; j++) {
      if (t[i] == s[j]) {
        count++;
        break;
      }
    }
  }
  cout << count << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
int main() {
  int i, n, j, k, o, count = 0;
  int a[1000001];
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  int left = 0, right = n - 1;
  int cur = n / 2;
  scanf("%d", &k);
  for (i = 0; i < k; i++) {
    scanf("%d", &o);
    left = 0, right = n - 1;
    while (1) {
      if (left == right - 1 || left == right) {
        if (a[left] == o || a[right] == o)
          count++;
        break;
      }
      if (o == a[cur] || o == a[cur + 1]) {
        count++;
        break;
      }
      if (o > a[cur]) {
        left = cur;
        cur = (left + right) / 2;
      }
      if (o < a[cur]) {
        right = cur;
        cur = (left + right) / 2;
      }
    }
  }
  printf("%d\\n", count);
}


Error: 0

Fixed submission:

#include <stdio.h>
int main() {
  int i, n, j, k, o, count = 0;
  int a[10001];
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  int left = 0, right = n - 1;
  int cur = n / 2;
  scanf("%d", &k);
  for (i = 0; i < k; i++) {
    scanf("%d", &o);
    left = 0, right = n - 1;
    while (1) {
      if (left == right - 1 || left == right) {
        if (a[left] == o || a[right] == o)
          count++;
        break;
      }
      if (o == a[cur] || o == a[cur + 1]) {
        count++;
        break;
      }
      if (o > a[cur]) {
        left = cur;
        cur = (left + right) / 2;
      }
      if (o < a[cur]) {
        right = cur;
        cur = (left + right) / 2;
      }
    }
  }
  printf("%d\\n", count);
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

#define N 100000
int S[N], T[N], count = 0, n, q, i, j, minj, temp, left, right, mid;
int binarySearch(int);
int main() {

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

  scanf("%d", &q);
  i = 0;
  while (i < q) {
    scanf("%d", &T[i]);
    if (binarySearch(T[i]) == 1) {
      count++;
    }
    i++;
  }
  printf("%d\\n", count);

  return 0;
}

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

Error: 0

Fixed submission:

#include <stdio.h>

#define N 10000
int S[N], T[N], count = 0, n, q, i, j, minj, temp, left, right, mid;
int binarySearch(int);
int main() {

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

  scanf("%d", &q);
  i = 0;
  while (i < q) {
    scanf("%d", &T[i]);
    if (binarySearch(T[i]) == 1) {
      count++;
    }
    i++;
  }
  printf("%d\\n", count);

  return 0;
}

int binarySearch(int key) {
  left = 0;
  right = n;
  while (left < right) {
    mid = (left + right) / 2;
    if (S[mid] == key) {
      return 1;
    } else if (key < S[mid]) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void bubblesort(int a[], int n) {

  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < i; j++) {
      if (a[j] > a[j + 1])
        swap(a[j], a[j + 1]);
    }
  }
}

/*--- int型オブジェクトの比較関数 ---*/
int int_cmp(const int *a, const int *b) {
  if (*a < *b)
    return (-1);
  else if (*a > *b)
    return (1);
  return (0);
}

int main() {
  int n, q;
  int S[100000], T[100000];
  int c = 0; // カウント
  int i = 0, j = 0;

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

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

  // bubblesort(T,q);
  qsort(T, q, sizeof(int), (int (*)(const void *, const void *))int_cmp);
  // cout<<"test"<<endl;
  /*cout<<"S"<<endl;
  for(int i=0;i<n;i++) cout<<S[i]<<" ";
  cout<<endl;
          cout<<"Q"<<endl;
  for(int i=0;i<q;i++) cout<<T[i]<<" ";
  cout<<endl;*/

  while (j < q) {
    if (S[i] == T[j]) {
      c++;
      i++;
      j++;
    }
    if (S[i] > T[j])
      j++;
    if (S[i] < T[j])
      i++;
  }

  cout << c << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;
void bubblesort(int a[], int n) {

  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < i; j++) {
      if (a[j] > a[j + 1])
        swap(a[j], a[j + 1]);
    }
  }
}
int main() {
  int n, q;
  int S[100000], T[100000];
  int c = 0; // カウント
  int i = 0, j = 0;

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

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

  bubblesort(T, q);

  // cout<<"test"<<endl;
  /*cout<<"S"<<endl;
  for(int i=0;i<n;i++) cout<<S[i]<<" ";
  cout<<endl;
          cout<<"Q"<<endl;
  for(int i=0;i<q;i++) cout<<T[i]<<" ";
  cout<<endl;*/

  while (j < q) {
    if (S[i] == T[j]) {
      c++;
      i++;
      j++;
    }
    if (S[i] > T[j])
      j++;
    if (S[i] < T[j])
      i++;
  }

  cout << c << endl;

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

#include <iostream>

int binarySearch(int s[], int n, int key) {

  int head = 0;
  int tail = n - 1;

  int center;

  while (head != tail) {
    center = (head + tail) / 2;
    if (s[center] == key) {
      return center;
    } else if (s[center] < key) {
      head = center + 1;
    } else {
      tail = center;
    }
  }

  if (s[head] == key) {
    return head;
  }

  return -1;
}

int main() {

  int n, q;
  int cnt = 0;

  std::cin >> n;

  int s[n];

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

  std::cin >> q;

  int key;

  for (int i = 0; i < q; i++) {
    std::cin >> key;

    if (binarySearch(s, n, key) >= 0) {
      cnt++;
    }
  }

  std::cout << cnt << "\\n";
}


Error: TLE

Fixed submission:

#include <iostream>

int binarySearch(int s[], int n, int key) {

  int head = 0;
  int tail = n - 1;

  int center;

  while (head != tail) {
    center = (head + tail) / 2;
    if (s[center] == key) {
      return center;
    } else if (s[center] < key) {
      head = center + 1;
    } else {
      tail = center - 1;
    }
  }

  if (s[head] == key) {
    return head;
  }

  return -1;
}

int main() {

  int n, q;
  int cnt = 0;

  std::cin >> n;

  int s[n];

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

  std::cin >> q;

  int key;

  for (int i = 0; i < q; i++) {
    std::cin >> key;

    if (binarySearch(s, n, key) >= 0) {
      cnt++;
    }
  }

  std::cout << cnt << "\\n";
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

using namespace std;

bool myBinarySearch(int S[], int begin, int end, int target) {
  if (begin > end) {
    return false;
  }
  int mid = (begin + end) / 2;
  if (S[mid] > target) {
    return myBinarySearch(S, begin, mid - 1, target);
  } else if (S[mid] < target) {
    return myBinarySearch(S, mid + 1, end, target);
  } else {
    return true;
  }
}

int main(void) {
  int S[100000];
  int T[50000];
  int n, m;

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

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

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (myBinarySearch(S, 0, m, T[i])) {
      ans++;
    }
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

bool myBinarySearch(int S[], int begin, int end, int target) {
  if (begin > end) {
    return false;
  }
  int mid = (begin + end) / 2;
  if (S[mid] > target) {
    return myBinarySearch(S, begin, mid - 1, target);
  } else if (S[mid] < target) {
    return myBinarySearch(S, mid + 1, end, target);
  } else {
    return true;
  }
}

int main(void) {
  int S[10000];
  int T[500];
  int n, m;

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

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

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (myBinarySearch(S, 0, m, T[i])) {
      ans++;
    }
  }
  cout << ans << endl;

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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int s[100003], t[50003];
  int n, q;
  cin >> n;
  REP(i, n) cin >> s[i];
  cin >> q;
  REP(i, q) cin >> t[i];
  int ct = 0;
  REP(i, q) {
    int l = 0;
    int r = n;
    while (abs(r - l) > 1) {
      int mid = (r + l) / 2;
      if (t[i] < s[mid])
        r = mid;
      else
        l = mid;
    }
    if (s[(r + l) / 2] == t[i])
      ct++;
  }
  cout << ct << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int s[10003], t[5003];
  int n, q;
  cin >> n;
  REP(i, n) cin >> s[i];
  cin >> q;
  REP(i, q) cin >> t[i];
  int ct = 0;
  REP(i, q) {
    int l = 0;
    int r = n;
    while (abs(r - l) > 1) {
      int mid = (r + l) / 2;
      if (t[i] < s[mid])
        r = mid;
      else
        l = mid;
    }
    if (s[(r + l) / 2] == t[i])
      ct++;
  }
  cout << ct << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int binarySearch(int n, int s[], int t) {
  int i = 0;
  int j = n - 1;
  while (i <= j) {
    int mid = i + (j - i) / 2;
    int guess = s[mid];
    if (guess == t) {
      return 1;
    }
    if (guess < t) {
      i = mid + 1;
    } else {
      j = mid - 1;
    }
  }
  return 0;
}

int main() {
  int n, q, s[100000], 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;
    ans += binarySearch(n, s, t);
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int binarySearch(int n, int s[], int t) {
  int i = 0;
  int j = n - 1;
  while (i <= j) {
    int mid = i + (j - i) / 2;
    int guess = s[mid];
    if (guess == t) {
      return 1;
    }
    if (guess < t) {
      i = mid + 1;
    } else {
      j = mid - 1;
    }
  }
  return 0;
}

int main() {
  int n, q, s[10001], 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;
    ans += binarySearch(n, s, t);
  }
  cout << ans << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int n, a[10000000];
inline int search(int key) {
  int l = 0;
  int r = n;

  while (r > l) {
    int mid = (l + r) / 2;
    if (a[mid] == key)
      return 1;
    else if (a[mid] < key)
      l = mid + 1;
    else if (a[mid] > key)
      r = mid;
  }
  return 0;
}
int main() {
  const int num = 10001;
  int q, key, sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> key;
    if (search(key))
      sum++;
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int n, a[10000000];
inline int search(int key) {
  int l = 0;
  int r = n;
  while (r > l) {
    int mid = (l + r) / 2;
    if (a[mid] = key)
      return 1;
    else if (a[mid] < key)
      l = mid + 1;
    else if (a[mid] > key)
      r = mid;
  }
  return 0;
}
int main() {
  const int num = 10001;
  int q, key, sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> key;
    if (search(key))
      sum++;
  }
  cout << sum << endl;
  return 0;
}
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 (A[mid] == key)
      return 1;
    if (key < A[mid])
      right = mid - 1;
    else if (key > A[mid])
      left = mid + 1;
  }
  return 0;
}

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 (binarySearch(key))
      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 (A[mid] == key)
      return 1;
    if (key < A[mid])
      right = mid - 1;
    else if (key > A[mid])
      left = mid;
  }
  return 0;
}

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 (binarySearch(key))
      sum++;
  }
  printf("%d\\n", sum);

  return 0;
}
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 (A[mid] == key)
      return 1;
    if (key < A[mid])
      right = mid;
    else if (key > A[mid])
      left = mid + 1;
  }
  return 0;
}

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 (binarySearch(key))
      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 (A[mid] == key)
      return 1;
    if (key < A[mid])
      right = mid - 1;
    else if (key > A[mid])
      left = mid;
  }
  return 0;
}

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 (binarySearch(key))
      sum++;
  }
  printf("%d\\n", sum);

  return 0;
}
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 - 1;
  int mid;
  while (left <= right) {
    mid = (left + right) / 2;
    if (A[mid] == key)
      return 1;
    if (key < A[mid])
      right = mid - 1;
    else if (key > A[mid])
      left = mid + 1;
  }
  return 0;
}

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 (binarySearch(key))
      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 = (right - left > 1) / 2;
    if (A[mid] == key)
      return 1;
    if (key < A[mid])
      right = mid;
    else if (key > A[mid])
      left = mid;
  }
  return 0;
}

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 (binarySearch(key))
      sum++;
  }
  printf("%d\\n", sum);

  return 0;
}