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

#include <iostream>
using namespace std;

int BSearch(int x, int A[], int N) {
  int left = 0;
  int right = N;

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

    if (A[mid] == x) {
      return mid;
    } else if (x < A[mid]) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return -1;
}

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 ans = 0;

  for (int i = 0; i < q; i++) {
    if (BSearch(T[i], S, n) != -1) {
      ans++;
    }
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int BSearch(int x, int A[], int N) {
  int left = 0;
  int right = N;

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

    if (A[mid] == x) {
      return mid;
    } else if (x < A[mid]) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return -1;
}

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 ans = 0;

  for (int i = 0; i < q; i++) {
    if (BSearch(T[i], S, n) != -1) {
      ans++;
    }
  }

  cout << ans << endl;

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

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

int search(int first, int last, int k, int *S) {
  if (first == last) {
    if (S[first] == k) {
      return 1;
    } else {
      return 0;
    }
  } else {
    int m;
    m = (first + last) / 2;
    if (S[m] == k) {
      return 1;
    } else if (S[m] > k) {
      return search(first, m, k, S);
    } else {
      return search(m + 1, last, k, S);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int i, j, n, q;
  int count = 0;

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

  for (j = 0; j < q; j++) {
    count += search(0, n - 1, T[j], S);
  }

  cout << count << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int search(int first, int last, int k, std::vector<int> S) {
  if (first == last) {
    if (S[first] == k) {
      return 1;
    } else {
      return 0;
    }
  } else {
    int m;
    m = (first + last) / 2;
    if (S[m] == k) {
      return 1;
    } else if (S[m] > k) {
      return search(first, m, k, S);
    } else {
      return search(m + 1, last, k, S);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int i, j, n, q;
  int count = 0;

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

  for (j = 0; j < q; j++) {
    count += search(0, n - 1, T[j], S);
  }

  cout << count << endl;

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

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  // declairring
  int n, q, c = 0, tmp;
  // inputting
  cin >> n;
  int S[n];
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) { // inputting&searching&counting
    cin >> tmp;
    if (binary_search(S, S + n, tmp))
      c++;
  }
  // outputting
  cout << c << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
using namespace std;
int main() {
  // declairring
  int n, q, c = 0, tmp;
  list<int> S;
  list<int>::iterator it;
  // inputting
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    S.push_back(tmp);
  }
  cin >> q;
  for (int i = 0; i < q; i++) { // inputting&searching&counting
    cin >> tmp;
    if (binary_search(S.begin(), S.end(), tmp))
      c++;
  }
  // outputting
  cout << c << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int s[100000], n;

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

int main() {
  int m, count = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    int t;
    cin >> t;
    count += binsearch(t);
  }
  cout << count << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int s[100000], n;

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

int main() {
  int m, count = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    int t;
    cin >> t;
    count += binsearch(t);
  }
  cout << count << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n, q;
  cin >> n;
  int *S = new int[n];
  int number;
  for (int i; i < n; ++i) {
    cin >> number;
    S[i] = number;
  }
  cin >> q;
  int *T = new int[q];
  for (int i = 0; i < q; ++i) {
    cin >> number;
    T[i] = number;
  }
  int cnt = 0;

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

  cout << cnt << "\\n";
  delete[] T;
  delete[] S;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, q;
  cin >> n;
  int *S = new int[n];
  int number;
  for (int i; i < n; ++i) {
    cin >> number;
    S[i] = number;
  }
  cin >> q;
  int *T = new int[q];
  for (int i = 0; i < q; ++i) {
    cin >> number;
    T[i] = number;
  }
  int cnt = 0;

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

  cout << cnt << "\\n";
  delete[] T;
  delete[] S;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

#define MAX_N 100000
#define MAX_Q 50000

int binary_search(int *s, int n, int x) {
  int max = n - 1;
  int min = 0;

  int avg = 0;
  int avg_ = -1;
  while (avg != avg_) {
    avg_ = avg;
    avg = (max + min) / 2;

    if (s[avg] > x) {
      max = avg;
    } else if (s[avg] < x) {
      min = avg;
    } else {
      return avg;
    }
  }

  if (s[avg + 1] == x) {
    return avg + 1;
  }
  return -1;
}

int main() {
  int n, q;
  int s[MAX_N], t[MAX_Q];
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &s[i]);
  }

  scanf("%d", &q);
  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, n, t[i]) != -1)
      cnt++;
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

#define MAX_N 100000
#define MAX_Q 50000

int binary_search(int *s, int n, int x) {
  int p = n / 2;
  int searched = -1;
  while (true) {
    if (s[p] < x) {
      p = (n + p) / 2;
    } else if (s[p] > x) {
      p = p / 2;
    } else {
      return p;
    }

    if (searched == p) {
      return -1;
    }

    searched = p;
  }
}

int main() {
  int n, q;
  int s[MAX_N], t[MAX_Q];
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &s[i]);
  }

  scanf("%d", &q);
  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, n, t[i]) != -1) {
      cnt++;
    }
  }

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

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

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

int main(void) {
  int n, q;
  int *S;
  int T;
  int i;
  int result = 0;
  int l, r;

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

  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> T;
    l = 0;
    r = n;
    while (l <= r) {
      if (S[(l + r) / 2] == T) {
        result++;
        break;
      } else if (S[(l + r) / 2] < T) {
        l = (l + r) / 2 + 1;
      } else {
        r = (l + r) / 2 - 1;
      }
    }
  }

  cout << result << endl;
  delete[] S;
  S = 0;
  return 0;
}

Error: TLE

Fixed submission:

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

bool binarySearch(int, int[], int, int);

bool binarySearch(int key, int arr[], int l, int r) {
  if (arr[(l + r) / 2] == key)
    return true;
  if (l == r)
    return false;

  if (arr[(l + r) / 2] < key)
    return binarySearch(key, arr, (l + r) / 2 + 1, r);
  if (arr[(l + r) / 2] > key)
    return binarySearch(key, arr, l, (l + r) / 2 - 1);

  return false;
}

int main(void) {
  int n, q;
  int *S;
  int T;
  int i;
  int result = 0;

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

  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> T;
    if (binarySearch(T, S, 0, n))
      result++;
  }

  cout << result << endl;
  delete[] S;
  S = 0;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

bool binarySearch(vector<int> vi, int t) {
  int right = vi.size() - 1;
  int left = 0;
  bool flg = true;
  while (flg) {
    if (right == left)
      flg = false;
    int center = (left + right) / 2;
    if (vi[center] < t) {
      left = center + 1;
    } else if (vi[center] > t) {
      right = center;
    } else {
      return true;
    }
  }
  return false;
}

int main() {
  vector<int> vi;
  int n, q;
  int count = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int s;
    cin >> s;
    vi.push_back(s);
  }
  sort(vi.begin(), vi.end());
  cin >> q;
  for (int i = 0; i < q; i++) {
    int t;
    cin >> t;
    count += binarySearch(vi, t);
  }
  cout << count << endl;
  return 0;
};


Error: TLE

Fixed submission:

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

using namespace std;

bool binarySearch(vector<int> vi, int t) {
  int right = vi.size() - 1;
  int left = 0;
  bool flg = true;
  while (flg) {
    if (right == left)
      flg = false;
    int center = (left + right) / 2;
    if (vi[center] < t) {
      left = center;
    } else if (vi[center] > t) {
      right = center;
    } else {
      return true;
    }
  }
  return false;
}

int main() {
  vector<int> vi;
  int n, q;
  int count = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int s;
    cin >> s;
    vi.push_back(s);
  }
  sort(vi.begin(), vi.end());
  cin >> q;
  for (int i = 0; i < q; i++) {
    int t;
    cin >> t;
    count += binarySearch(vi, t);
  }
  cout << count << endl;
  return 0;
};
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define fori(i, k, n) for (int i = k; i < (int)n; i++)
using namespace std;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
const int dX[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          dY[8] = {1, 1, 0, -1, -1, -1, 0, 1};
struct XY {
  int x;
  int y;
};

/*
(char)'1' - (int)48 = (int)1;


*/

int main() {
  int n, q;
  vector<int> s;
  cin >> n;
  int j;
  rep(i, n) {
    cin >> j;
    s.push_back(j);
  }
  cin >> q;
  int ans = 0;
  rep(i, q) {
    cin >> j;
    int l = 0, r = n;
    if (binary_search(s.begin(), s.end(), j))
      ans++;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define fori(i, k, n) for (int i = k; i < (int)n; i++)
using namespace std;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
const int dX[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          dY[8] = {1, 1, 0, -1, -1, -1, 0, 1};
struct XY {
  int x;
  int y;
};

/*
(char)'1' - (int)48 = (int)1;


*/

int main() {
  int n, q;
  vector<int> s;
  cin >> n;
  int j;
  rep(i, n) {
    cin >> j;
    s.push_back(j);
  }
  cin >> q;
  int ans = 0;
  rep(i, q) {
    cin >> j;
    int l = 0, r = n;
    while (l != r) {
      bool ri = true;
      if (s[(l + r) / 2] > j) {
        r = (l + r) / 2 - 1;
      } else if (s[(l + r) / 2] < j) {
        l = (l + r) / 2 + 1;
      } else
        break;
    }
    if (s[(l + r) / 2] == j)
      ans++;
  }
  cout << ans << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

int S[100001];
int N;

bool binary(int q, int left, int right) {
  int mid = (left + right) / 2;
  if (right - left == 1)
    return (S[left] == q);

  // if (S[mid] == q) return true;
  else if (S[mid] > q) {
    return binary(q, left, mid);
  } else {
    return binary(q, mid, right);
  }
}

int main() {
  int q, Noq;
  cin >> N;

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

  cin >> Noq;
  int count = 0;

  for (int i = 0; i < Noq; i++) {
    scanf("%d", &q);
    if (binary(q, 0, N))
      count++;
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

int A[100001];

int main() {
  int n, q, k, count;
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    scanf("%d", &k);
    for (int j = 0; j < n; j++) {
      if (A[j] == k) {
        count++;
        break;
      }
    }
  }
  cout << count << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n, q, S[100010], T[50010], 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 a = 0, b = n - 1;
    while (a <= b) {
      int m = (a + b) / 2;
      if (T[i] == S[m]) {
        ++C;
        break;
      } else if (T[i] > S[m])
        a = m + 1;
      else
        b = m - 1;
    }
  }

  cout << C << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, q, S[100010], T, 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;
    int a = 0, b = n;
    while (b - a >= 0) {
      if (T == S[(a + b) / 2]) {
        ++C;
        break;
      } else if (T > S[(a + b) / 2]) {
        a = (a + b) / 2;
      } else {
        b = (a + b) / 2;
      }
    }
  }

  cout << C << endl;

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

#include <iostream>
using namespace std;
int main() {

  int n, a[100000], m, b, result = 0, i;

  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];
  cin >> m;
  for (i = 0; i < m; i++) {
    cin >> b;
    if (b < a[0] || a[n - 1] < b)
      continue;
    int l = 0, r = n, m;
    while (r - l) {
      m = (r + l) / 2;
      if (b == a[m]) {
        result++;
        break;
      } else if (b > a[m])
        l = m + 1;
      else
        r = m;
    }
  }
  cout << result << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {

  int n, a[100000], m, b, result = 0, i;

  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];
  cin >> m;
  for (i = 0; i < m; i++) {
    cin >> b;
    if (b < a[0] || a[n - 1] < b)
      continue;
    int l = 0, r = n, m;
    while (r - l > 0) {
      m = (r + l) / 2;
      if (b == a[m]) {
        result++;
        break;
      } else if (b > a[m])
        l = m;
      else
        r = m - 1;
    }
  }
  cout << result << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

std::vector<int> Create_input_vector() {

  std::vector<int> vector;
  int size;

  cin >> size;
  vector.reserve(size);

  while (size--) {
    int value;
    cin >> value;
    vector.emplace_back(value);
  }

  return std::move(vector);
}

bool Exist_element(const std::vector<int> &list, int value) {

  if (value < list.front() || list.back() < value) {
    return false;
  }

  int begin = 0;
  int end = list.size() - 1;
  int now;
  while (now = (begin + end) / 2, begin != end) {
    int element = list.at(now);
    if (begin + 1 == end) {
      if (value == list.at(begin) || value == list.at(end)) {
        return true;
      }
      return false;
    }
    if (element < value) {
      begin = now;
      continue;
    }
    if (element > value) {
      end = now;
      continue;
    }
    return true;
  }

  return false;
}

int Count_same_element(const std::vector<int> &base,
                       const std::vector<int> &other) {

  int count = 0;
  for (const auto element : base) {
    if (Exist_element(other, element)) {
      ++count;
    }
  }

  return count;
}

int main() {

  std::vector<int> S{Create_input_vector()};
  std::vector<int> T{Create_input_vector()};

  cout << Count_same_element(T, S) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

std::vector<int> Create_input_vector() {

  std::vector<int> vector;
  int size;

  cin >> size;
  vector.reserve(size);

  while (size--) {
    int value;
    cin >> value;
    vector.emplace_back(value);
  }

  return std::move(vector);
}

bool Exist_element(const std::vector<int> &list, int value) {

  if (value < list.front() || list.back() < value) {
    return false;
  }

  int begin = 0;
  int end = list.size() - 1;
  int now;
  while (now = (begin + end) / 2, begin != end) {
    int element = list.at(now);
    if (element < value) {
      begin = now;
      continue;
    }
    if (element > value) {
      end = now;
      continue;
    }
    return true;
  }

  return false;
}

int Count_same_element(const std::vector<int> &base,
                       const std::vector<int> &other) {

  int count = 0;
  for (const auto base_element : base) {
    if (Exist_element(other, base_element)) {
      ++count;
    }
  }

  return count;
}

int main() {

  std::vector<int> S{Create_input_vector()};
  std::vector<int> T{Create_input_vector()};

  cout << Count_same_element(T, S) << endl;

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

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

int search(int);

int n, S[100000];

int main() {

  int q, T[50000], counter = 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];

  sort(S, S + n);

  for (int i = 0; i < q; i++)
    counter += search(T[i]);

  cout << counter << endl;

  return 0;
}

int search(int t) {

  int left = 0;
  int right = n;

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

  return 0;
}

Error: TLE

Fixed submission:

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

int search(int);

int n, S[100000];

int main() {

  int q, T[50000], counter = 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];

  sort(S, S + n);

  for (int i = 0; i < q; i++)
    counter += search(T[i]);

  cout << counter << endl;

  return 0;
}

int search(int t) {

  int left = 0;
  int right = n;

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

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

#include <iostream>
using namespace std;

int a[100000], b[100000];
int binary(int l, int r, int c) {
  while (l <= r) {
    int p = (l + r) / 2, x, y;
    if (a[p] == c)
      return 1;
    else if (c < a[p])
      r = p - 1;
    else
      l = p + 1;
  }
  return 0;
}

int ar(int n) {
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    for (int j = 0; j < i; j++)
      if (b[i] == b[j])
        i--, n--;
  }
  return n;
}

int main() {
  int p, q, n = 0;
  cin >> p;
  for (int i = 0; i < p; i++)
    cin >> a[i];
  cin >> q, q = ar(q);
  for (int i = 0; i < q; i++)
    n += binary(0, p - 1, b[i]);
  cout << n << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int a[10000], b[10000];
int binary(int l, int r, int c) {
  while (l <= r) {
    int p = (l + r) / 2, x, y;
    if (a[p] == c)
      return 1;
    else if (c < a[p])
      r = p - 1;
    else
      l = p + 1;
  }
  return 0;
}

int ar(int n) {
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    for (int j = 0; j < i; j++)
      if (b[i] == b[j])
        i--, n--;
  }
  return n;
}

int main() {
  int p, q, n = 0;
  cin >> p;
  for (int i = 0; i < p; i++)
    cin >> a[i];
  cin >> q, q = ar(q);
  for (int i = 0; i < q; i++)
    n += binary(0, p - 1, b[i]);
  cout << n << endl;

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

#include <iostream>
using namespace std;

int n, S[100010], q, key, cnt = 0;

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

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> key;
    cnt += search();
  }
  cout << cnt << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int n, S[10010], q, key, cnt = 0;

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

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> key;
    cnt += search();
  }
  cout << cnt << endl;
  return 0;
}