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

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

bool IsExist(vector<int> &S, int Number);

bool IsExist(vector<int> &S, int Number) {
  int left = 0;
  int right = S.size();

  while (left < right) {
    int Index = (left + right) / 2;
    if (S[Index] == Number) {
      return true;
    } else if (Number < S[Index]) {
      right = Index;
    } else {
      left = Index + 1;
    }
  }
  return false;
}

int main(void) {
  int n, q, Number, count;
  bool Exist;
  vector<int> S;

  std::cin >> n;
  S.clear();
  for (int i = 0; i < n; i++) {
    std::cin >> Number;
    if (S.empty()) {
      S.push_back(Number);
    } else {
      if (S[S.size() - 1] != Number)
        S.push_back(Number);
    }
  }

  std::cin >> q;
  count = 0;
  for (int i = 0; i < q; i++) {
    std::cin >> Number;
    if (IsExist(S, Number) == true)
      count++;
  }

  std::cout << count << std::endl;
}

Error: TLE

Fixed submission:

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

int main(void) {
  int n, q, Number, count;
  bool Exist;
  vector<int> S;

  std::cin >> n;
  S.clear();
  for (int i = 0; i < n; i++) {
    std::cin >> Number;
    if (S.empty()) {
      S.push_back(Number);
    } else {
      Exist = false;
      for (int j = 0; j < S.size(); j++) {
        if (Number == S[j])
          Exist = true;
      }
      if (Exist == false)
        S.push_back(Number);
    }
  }

  std::cin >> q;
  count = 0;
  for (int i = 0; i < q; i++) {
    std::cin >> Number;
    for (int j = 0; j < S.size(); j++) {
      if (Number == S[j])
        count++;
    }
  }

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

#include <cstdio>
#include <cstdlib>
#include <iostream>

int main() {
  int n, q;
  std::cin >> n;
  int *S = new int[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &S[i]);
  }
  std::cin >> q;
  int *T = new int[q];
  for (int i = 0; i < q; i++) {
    scanf("%d", &T[i]);
  }

  int cnt = 0;
  for (int i = 0; i < q; i++) {
    int head = 0;
    int tail = n - 1;
    int j = (head + tail) / 2;
    while (1) {
      if (S[j] == T[i]) {
        cnt++;
        break;
      } else if (tail - head == 1) {
        if (S[head] == T[i] || S[tail] == T[i]) {
          cnt++;
        }
        break;
      } else if (S[j] > T[i]) {
        tail = j;
        j = (head + tail) / 2;
      } else if (S[j] < T[i]) {
        head = j;
        j = (head + tail) / 2;
      }
    }
  }

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


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <iostream>

int main() {
  int n, q;
  std::cin >> n;
  int *S = new int[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &S[i]);
  }
  std::cin >> q;
  int *T = new int[q];
  for (int i = 0; i < q; i++) {
    scanf("%d", &T[i]);
  }

  int cnt = 0;
  for (int i = 0; i < q; i++) {
    int head = 0;
    int tail = n - 1;
    int j = (head + tail) / 2;
    while (1) {
      if (S[j] == T[i]) {
        cnt++;
        break;
      } else if (head - tail == 1) {
        break;
      } else if (S[j] > T[i]) {
        tail = j;
        j = (head + tail) / 2;
      } else if (S[j] < T[i]) {
        head = j;
        j = (head + tail) / 2;
      }
    }
  }

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

#include <iostream>
#include <vector>

const int N = 100000;
const int Q = 50000;

int main() {
  std::ios_base::sync_with_stdio(false);

  int n = 0;
  std::vector<int> S;

  S.reserve(N);

  std::cin >> n;

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

    std::cin >> x;

    if (i == 0) {
      S.push_back(x);

      continue;
    }

    if ((i > 0) && (x != S[S.size() - 1])) {
      S.push_back(x);
    }
  }

  int q = 0;
  std::vector<int> T;

  T.reserve(Q);

  std::cin >> q;

  for (int i = 0; i < q; i++) {
    int x = 0;

    std::cin >> x;

    T.push_back(x);
  }

  int count = 0;

  std::vector<int>::iterator j = T.begin();

  while (j != T.end()) {
    int left = 0;
    int right = S.size() - 1;

    bool found = false;

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

      if (S[middle] == *j) {
        count++;

        found = true;

        break;
      } else if (S[middle] < *j) {
        left = middle + 1;
      } else {
        right = middle - 1;
      }
    }

    if (found) {
      j = T.erase(j);
    } else {
      ++j;
    }
  }

  std::cout << count << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

const int N = 100000;
const int Q = 50000;

int main() {
  std::ios_base::sync_with_stdio(false);

  int n = 0;
  std::vector<int> S;

  S.reserve(N);

  std::cin >> n;

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

    std::cin >> x;

    S.push_back(x);
  }

  int q = 0;
  std::vector<int> T;

  T.reserve(Q);

  std::cin >> q;

  for (int i = 0; i < q; i++) {
    int x = 0;

    std::cin >> x;

    T.push_back(x);
  }

  int count = 0;

  for (std::vector<int>::iterator i = S.begin(); i != S.end(); ++i) {
    std::vector<int>::iterator j = T.begin();

    while (j != T.end()) {
      int left = 0;
      int right = n - 1;

      bool found = false;

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

        //	std::cout << middle << std::endl;

        if (S[middle] == *j) {
          count++;

          found = true;

          break;
        } else if (S[middle] < *j) {
          left = middle + 1;
        } else {
          right = middle - 1;
        }
      }

      if (found) {
        j = T.erase(j);
      } else {
        ++j;
      }
    }
  }

  std::cout << count << std::endl;

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

#include <iostream>

using namespace std;

//?????????????????????????????¢?????´?????????
int BinarySearch(int n, int A[], int T) {
  // T?????????dataS?????????????????????????´¢?????????????????????????????°0????????????

  int left = 0, right = n, mid;

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

  return 0;
}

int main() {

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

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

  //????????????????????????
  for (int j = 0; j < q; ++j) {

    if (BinarySearch(n, S, T[j]) == 1) {
      cnt++;
    }
  }
  cout << cnt << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int i, j, n, q, count = 0;
  int dataS[100000], dataT[50000];
  cin >> n;
  for (i = 0; i < n; ++i) {
    cin >> dataS[i];
  }

  cin >> q;
  for (i = 0; i < q; ++i) {
    cin >> dataT[i];
  }
  for (int j = 0; j < q; ++j) {
    for (int i = 0; i < n; ++i) {
      if (dataT[j] == dataS[i]) {
        count++;
        break;
      }
    }
  }
  cout << count << "\\n";

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

#include <iostream>
using namespace std;

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

  int S[100000], T, n, q, i, c = 0;

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

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

    if (binarySearch(n, q, S, T))
      c++;
  }

  cout << c << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int LinearSearch(int n, int q, int S[], int T) {
  int i = 0;
  S[n] = T;
  while (S[i] != T)
    i++;

  return i != n;
}
int main() {

  int S[100000], T, n, q, i, c = 0;

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

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

    if (LinearSearch(n, q, S, T))
      c++;
  }

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

#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define vi vector<int>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  // q=50000????????§????????????????????¨????????????
  // C++??????binary_search??¨?????????????????¢??°?????????
  // b2.cpp??§??????????????°??????????????????????£?
  int n, q;
  ll int cnt = 0;
  cin >> n;
  ll int data[100000];
  REP(i, n) cin >> data[i];
  sort(data, data + n);
  cin >> q;
  REP(i, q) {
    ll int tmp;
    cin >> tmp;
    if (binary_search(data, data + n, tmp))
      cnt++;
  }
  cout << cnt << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define vi vector<int>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int n, q;
ll int data[100000];

bool mybinary_serach(int l, int r, ll int tg) { // left,right,target
  if (l == r)
    return false;
  int nowpos = (l + r) / 2;
  if (data[nowpos] == tg)
    return true;
  else if (data[nowpos] > tg)
    return mybinary_serach(l, nowpos, tg);
  else if (data[nowpos] < tg)
    return mybinary_serach(nowpos, r, tg);
}
int main() {
  ll int cnt = 0;
  cin >> n;
  REP(i, n) cin >> data[i];
  sort(data, data + n);
  cin >> q;
  REP(i, q) {
    ll int tmp;
    cin >> tmp;
    if (mybinary_serach(0, n, tmp))
      cnt++;
  }
  cout << cnt << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, q, nk = 0, goukei = 0;
  cin >> n;
  int s[100000], t[50000];
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    if (s[i] == s[i - 1]) {
      i--;
      n--;
    }
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> t[i];
  }
  for (int i = 0; i < q; i++) {
    int right = n - 1, left = 0, count = 0;
    while (left < right) {
      nk = right + left + count % 2 * 2;
      ;
      nk /= 2;

      if (s[nk] > t[i]) {
        if (right == nk) {
          count++;
          if (count == 2) {
            break;
          }
        } else {
          count = 0;
        }
        right = nk;
      } else if (s[nk] == t[i]) {
        goukei++;
        break;
      } else {
        if (left == nk) {
          count++;
          if (count == 2) {
            break;
          }
        } else {
          count = 0;
        }
        left = nk;
      }
    }
  }
  cout << goukei + 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, q, nk = 0, goukei = 0;
  cin >> n;
  int s[100000], t[50000];
  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 right = n - 1, left = 0;
    ;
    while (left < right) {
      nk = right + left + 1;
      nk /= 2;
      /*if (nk == k) {
              cout << "ohmy" << endl;
              break;
      }*/
      if (s[nk] > t[i]) {
        right = nk;
      } else if (s[nk] == t[i]) {
        goukei++; /* cout << t[i] << " " << goukei<<endl;*/
        break;
      } else {
        left = nk;
      } // cout << left << "  " << right <<" "<<nk<< endl;
      int lk = left, rk = right;
    }
  }
  cout << goukei << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

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

int s[100000];
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) scanf("%d", &s[i]);
  int q;
  scanf("%d", &q);
  int cnt = 0;
  rep(i, q) {
    int t;
    scanf("%d", &t);
    if (binary_search(s, s + n, t))
      cnt++;
  }
  printf("%d\\n", cnt);
}

Error: 0

Fixed submission:

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

int s[10000];
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) scanf("%d", &s[i]);
  int q;
  scanf("%d", &q);
  int cnt = 0;
  rep(i, q) {
    int t;
    scanf("%d", &t);
    if (binary_search(s, s + n, t))
      cnt++;
  }
  printf("%d\\n", cnt);
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

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

int main() {

  int n, m, cnt = 0;
  cin >> n;
  int a[n];

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

  sort(a, a + n);

  cin >> m;
  for (int i = 0; i < m; i++) {
    int v;
    scanf("%d", &v);
    if (binary_search(a, a + n, v))
      cnt++;
  }
  cout << cnt << endl;
}

Error: 0

Fixed submission:

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

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

  const int MAX = 10000;
  int n, m, a[MAX], b, cnt = 0;
  cin >> n;

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

  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> b;
    if (binary_search(a, a + n, b))
      cnt++;
  }
  cout << cnt << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {

  int N;
  cin >> N;
  int a[N]; // sorted
  for (int i = 0; i < N; i++)
    scanf("%d", &a[i]);
  int Q;
  cin >> Q;
  int ans = 0;
  while (Q--) {
    int b;
    cin >> b;
    int l = 0, r = N;
    while (r - l > 1) {
      int m = (l + r) / 2;
      if (a[m] <= b) {
        l = m;
      } else {
        r = m;
      }
    }
    if (a[l] == b)
      ans++; // lower_bound
  }

  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {

  int N;
  cin >> N;
  int a[N];
  for (int i = 0; i < N; i++)
    scanf("%d", &a[i]);
  int Q;
  cin >> Q;
  int b[Q];
  for (int i = 0; i < Q; i++)
    scanf("%d", &b[i]);
  bool c[Q];
  for (int i = 0; i < Q; i++)
    c[i] = false;
  int ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < Q; j++) {
      if (a[i] == b[j] && !c[j])
        c[j] = true, ans++;
    }
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool binary_search(int *seq, int n, int key) {
  int left = 0, right = n, mid;
  int flag = false;
  while (left < right) {
    mid = (left + right) / 2;
    if (seq[mid] == key) {
      flag = true;
      break;
    } else if (seq[mid] > key)
      right = mid;
    else
      left = mid + 1;
  }
  return flag;
}

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

  int q;
  cin >> q;

  int num = 0;
  int key = 0;
  for (i = 0; i < q; i++) {
    cin >> key;
    if (binary_search(seq, n, key))
      num += 1;
  }

  cout << num << endl;
  //	system("pause");
  return 0;
}


Error: TLE

Fixed submission:

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

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

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

  int q;
  cin >> q;

  int num = 0;
  int key = 0;
  for (i = 0; i < q; i++) {
    cin >> key;
    if (binary_search(seq, n, key))
      num += 1;
  }

  cout << num << endl;
  //	system("pause");
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

void solve() {
  int n, q, a[100000], b[50000], i, j;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> b[i];
  }
  int r, l;
  int ans = 0;
  for (i = 0; i < q; i++) {
    r = 0;
    l = n - 1;
    if (b[i] == a[0])
      ans++;
    else {
      while (l - r > 1) {
        int m;
        if ((l + r) % 2 == 1)
          m = (l + r + 1) / 2;
        else
          m = (l + r) / 2;
        if (a[m] >= b[i])
          l = m;
        else
          r = m;
      }
      if (b[i] == a[l])
        ans++;
    }
  }
  cout << ans << endl;
}

int main() {
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

void solve() {
  int n, q, a[100000], b[50000], i, j;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> b[i];
  }
  int r, l;
  int ans = 0;
  for (i = 0; i < q; i++) {
    r = 0;
    l = n - 1;
    if (b[i] == a[0])
      ans++;
    else {
      while (l - r > 1) {
        int m;
        if ((l + r) % 2 == 1)
          m = (l + r + 1) / 2;
        else
          m = (l + r) / 2;
        if (a[m] >= b[i])
          l = a[m];
        else
          r = a[m];
      }
      if (b[i] == a[l])
        ans++;
    }
  }
  cout << ans;
}

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

#include <cstdio>
using namespace std;
int S[1000000], n;
int serch(int key) {
  int left = 0, right, mid;
  right = n;
  while (left < right) {
    mid = (left + right) / 2;
    if (key == S[mid])
      return 1;
    if (key > S[mid])
      left = mid + 1;
    else if (key < S[mid])
      right = mid;
  }
  return 0;
}
int main() {
  int q, key;
  int i, sum = 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", &key);
    if (serch(key))
      sum++;
  }
  printf("%d\\n", sum);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
using namespace std;
int S[1000000], n;
int serch(int key) {
  int left = 0, right, mid;
  right = n;
  while (left < right) {
    mid = (left + right) / 2;
    if (key == S[mid])
      return 1;
    if (key > S[mid])
      left = mid;
    else if (key < S[mid])
      right = mid;
  }
  return 0;
}
int main() {
  int q, key;
  int i, sum = 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", &key);
    if (serch(key))
      sum++;
  }
  printf("%d\\n", sum);
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

bool BinarySerach(const vector<int> &S, int &key) {
  int start = 0;
  int end = S.size() - 1;
  int middle;

  while (1) {
    middle = (end - start) / 2 + start;
    if (S[middle] == key) {
      return true;
    } else if (start == end) {
      return false;
    } else if (S[middle] < key) {
      start = middle + 1;
    } else if (S[middle] > key) {
      end = middle;
    }
  }
}

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

  int n, q;
  vector<int> S;

  cin >> n;
  S.resize(n);

  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  cin >> q;
  int count = 0;
  for (int i = 0; i < q; i++) {
    int key;
    cin >> key;
    if (BinarySerach(S, key)) {
      count++;
      //			cout << "key:" << key << endl;
    }
  }
  cout << count << endl;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

bool BinarySerach(const vector<int> &S, int &key) {
  int start = 0;
  int end = S.size() - 1;
  int middle;

  while (1) {
    middle = ceil((end - start) / 2.) + start;
    if (S[middle] == key) {
      return true;
    } else if (start == end) {
      return false;
    } else if (S[middle] < key) {
      start = middle + 1;
    } else if (S[middle] > key) {
      end = middle;
    }
  }
}

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

  int n, q;
  vector<int> S;

  cin >> n;
  S.resize(n);

  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  cin >> q;
  int count = 0;
  for (int i = 0; i < q; i++) {
    int key;
    cin >> key;
    if (BinarySerach(S, key)) {
      count++;
      //			cout << "key:" << key << endl;
    }
  }
  cout << count << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <math.h>
#include <stdio.h>
#include <string.h>

int main() {
  long i, j;
  long n;
  long q;
  long cnt = 0;
  long h;
  long left, right, mid;
  static long S[100001];
  static long T[50001];

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

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

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

  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
#include <string.h>

int main() {
  long i, j;
  long n;
  long q;
  long cnt = 0;
  static long S[100001];
  static long T[50001];

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

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

  for (i = 0; i < q; i++) {
    for (j = 0; j < n; j++) {
      if (T[i] == S[j]) {
        cnt++;
        break;
      }
    }
  }

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

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

#include <iostream>
using namespace std;

int A[100000], n;

int binarySearch(int key) {
  int left = 0;
  int right = n;
  int i = 0;
  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() {
  int q, i, k, ans = 0;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> A[i];

  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> k;
    if (binarySearch(k))
      ans++;
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int A[10001], n;

int binarySearch(int key) {
  int left = 0;
  int right = n;
  int i = 0;
  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() {
  int q, i, k, ans = 0;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> A[i];

  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> k;
    if (binarySearch(k))
      ans++;
  }

  cout << ans << endl;

  return 0;
}