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

#include <iostream>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> s(n);
  FOR(i, 0, n) { cin >> s[i]; }

  int q;
  cin >> q;
  vector<int> t(q);
  int cnt = 0;
  FOR(i, 0, q) {
    cin >> t[i];
    int left = 0, right = n;
    int mid;
    while (left < right) {
      mid = (left + right) / 2;
      if (t[i] == s[mid]) {
        cnt++;
        break;
      }
      if (t[i] > s[mid]) {
        left = mid + 1;
      }
      if (t[i] < s[mid]) {
        right = mid;
      }
    }
  }
  cout << cnt << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> s(n);
  FOR(i, 0, n) { cin >> s[i]; }

  int q;
  cin >> q;
  vector<int> t(q);
  int cnt = 0;
  FOR(i, 0, q) {
    cin >> t[i];
    int left = 0, right = n - 1;
    int mid;
    while (left < right) {
      mid = (left + right) / 2;
      if (t[i] == s[mid]) {
        cnt++;
        break;
      }
      if (t[i] > s[mid]) {
        left = mid;
      }
      if (t[i] < s[mid]) {
        right = mid;
      }
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int S[100000];

int main(void) {

  int n;
  cin >> n;

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

  int q, total = 0;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int t;
    cin >> t;
    int left = 0, right = n - 1, mid = 0;
    while (left <= right) { // '<' ?????¨?????¨??§?????????
      mid = (left + right) / 2;
      if (t == S[mid]) {
        ++total;
        break;
      }
      if (t > S[mid]) {
        left = mid + 1;
      } else {
        right = mid - 1;
      }
    }
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

int main(void) {

  int n;
  cin >> n;

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

  int q;
  cin >> q;

  int total = 0;
  for (int i = 0; i < q; ++i) {
    int x;
    cin >> x;
    for (int j = 0; j < n; ++j) {
      if (x == S[j]) {
        ++total;
        break;
      }
    }
  }

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

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

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

typedef long long ll;
typedef pair<int, int> P;

int ans = 0, n;
vector<int> s;

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

int main() {
  cin >> n;
  s.resize(n);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  int q;
  cin >> q;
  vector<int> t(q);
  for (int i = 0; i < q; i++) {
    cin >> t[i];
    binarysearch(t[i]);
  }
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

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

int n, q;
vector<int> s(n);
vector<int> t(q);

int binarysearch(int key) {
  int left = 0;
  int right = n;
  int mid;
  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 ans = 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];
    if (binarysearch(t[j])) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

// 2分探索
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>

using namespace std;

const int MAX_N = 1000000;
// const int MAX_Q = 500;

bool Binary_Search(int A[], int n, int key);

int main(void) {

  int n, q, key, count = 0;
  int ary_N[MAX_N + 1];

  scanf("%d", &n);

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

  scanf("%d", &q);

  for (int i = 0; i < q; i++) {
    scanf("%d", &key);

    if (Binary_Search(&ary_N[0], n, key) == true) {
      count++;
    }
  }

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

  return 0;
}

// 2分探索
bool Binary_Search(int A[], int n, int key) {
  bool ret = false;

  int left, right, mid;

  left = 0;
  right = n;
  mid = (left + right) / 2;

  while (left < right) {
    if (A[mid] < key) {
      // 後半を探索
      left = mid + 1;
    } else if (A[mid] > key) {
      // 前半を探索
      right = mid;
    } else {
      ret = true;
      // 値が見つかったので処理終了
      break;
    }

    mid = (left + right) / 2;
  }

  return ret;
}


Error: TLE

Fixed submission:

// 線形探索
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>

using namespace std;

const int MAX_N = 100000;
// const int MAX_Q = 500;

bool linearSearch(int A[], int n, int key);

int main(void) {

  int n, q, key, count = 0;
  int ary_N[MAX_N + 1];

  scanf("%d", &n);

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

  scanf("%d", &q);

  for (int i = 0; i < q; i++) {
    scanf("%d", &key);

    if (linearSearch(&ary_N[0], n, key) == true) {
      count++;
    }
  }

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

  return 0;
}

// 線形探索
bool linearSearch(int A[], int n, int key) {
  int i = 0;
  // 終了条件の番兵の代入
  A[n] = key;

  while (A[i] != key) {
    i++;
  }

  return i != n;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
// #include<fstream>
using namespace std;

bool binarySearch(int s[], int begin, int end, int t);

int main() {
  int i, j, n, q, s[100001], t, cnt = 0;
  // ifstream fin("ALDS1_4_B-in8.txt");
  // if (!fin) {
  //	cout << "failed to open the file.";
  //	return 1;
  // }
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> s[i];
  }
  cin >> q;
  for (j = 0; j < q; j++) {
    cin >> t;
    if (binarySearch(s, 0, n, t) == true)
      cnt++;
  }
  cout << cnt << endl;
  // fin.close();
  return 0;
}

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

Error: 1

Fixed submission:

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

bool binarySearch(int s[], int begin, int end, int t);

int main() {
  int i, j, n, q, s[100001], t, cnt = 0;
  ifstream fin("ALDS1_4_B-in8.txt");
  if (!fin) {
    cout << "failed to open the file.";
    return 1;
  }
  fin >> n;
  for (i = 0; i < n; i++) {
    fin >> s[i];
  }
  fin >> q;
  for (j = 0; j < q; j++) {
    fin >> t;
    if (binarySearch(s, 0, n, t) == true)
      cnt++;
  }
  cout << cnt << endl;
  fin.close();
  return 0;
}

bool binarySearch(int s[], int begin, int end, int t) {
  int mid = (begin + end) / 2;
  if (t == s[mid])
    return true;
  else if (begin >= end)
    return false;
  else if (t < s[mid])
    return binarySearch(s, begin, mid - 1, t);
  else if (t > s[mid])
    return binarySearch(s, mid + 1, end, t);
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#define N 100000
int count = 0;
void BinarySearch(int array[], int right, int left, int key) {
  while (left < right) {
    int mid = (left + right) / 2;
    if (array[mid] == key) {
      count++;
      break;
    } else if (array[mid] < key) {
      left = mid + 1;
    } else {
      right = mid;
    }
  }
}

int main() {
  int i, n, array[N];
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &array[i]);
  int m;
  scanf("%d", &m);
  for (i = 0; i < m; i++) {
    int l;
    scanf("%d", &l);
    BinarySearch(array, n, 0, l);
  }
  printf("%d\\n", count);
}

Error: TLE

Fixed submission:

#include <stdio.h>
#define N 100000
int count = 0;
void BinarySearch(int array[], int right, int left, int key) {
  int mid = (right + left) / 2;
  if (left > right) {
    return;
  }

  if (array[mid] == key) {
    count++;
    return;
  } else if (array[mid] < key) {
    BinarySearch(array, right, mid + 1, key);
  } else {
    BinarySearch(array, mid, left, key);
  }
}

int main() {
  int i, n, array[N];
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &array[i]);
  int m;
  scanf("%d", &m);
  for (i = 0; i < m; i++) {
    int l;
    scanf("%d", &l);
    BinarySearch(array, n, 0, l);
  }
  printf("%d\\n", count);
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

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

  unsigned int list[100000];
  int numOfList;

  cin >> numOfList;

  rep(i, numOfList) cin >> list[i];

  int val;
  int counter = 0;
  cin >> val;

  while (cin >> val) {
    int high = numOfList - 1;
    int low = 0;
    int mid;

    while (low <= high) {
      mid = (low + high) / 2;
      if (list[mid] < val) {
        low = mid + 1;
      } else if (list[mid] > val) {
        high = mid - 1;
      } else if (list[mid] == val) {
        counter++;
        break;
      }
    }
  }
  cout << counter << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

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

  unsigned int list[100000];
  int numOfList;

  cin >> numOfList;

  rep(i, numOfList) cin >> list[i];

  int val;
  int counter = 0;
  cin >> val;

  while (cin >> val) {
    int high = numOfList - 1;
    int low = 0;
    int mid;

    while (low <= high) {
      if (list[mid] < val) {
        low = mid + 1;
      } else if (list[mid] > val) {
        high = mid - 1;
      } else if (list[mid] == val) {
        counter++;
        break;
      }
    }
  }
  cout << counter << endl;

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

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

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

int main() {
  int n, q;
  int S[100'000];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;
  int key;
  int count = 0;
  for (int i = 0; i < q; i++) {
    cin >> key;
    if (binarySearch(S, n, key))
      count++;
  }

  cout << count << endl;
}


Error: TLE

Fixed submission:

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

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

int main() {
  int n, q;
  int S[100'000];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;
  int key;
  int count = 0;
  for (int i = 0; i < q; i++) {
    cin >> key;
    if (binarySearch(S, n, key))
      count++;
  }

  cout << count << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int b[1000000010];

int main() {
  int n, q, s, t;
  int ans = 0;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s;
    b[s]++;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> t;
    if (b[t] >= 1)
      ans++;
  }
  cout << ans << endl;
  return (0);
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int b[10 ^ 9 + 1];

int main() {
  int n, q, s, t;
  int ans = 0;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s;
    b[s]++;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> t;
    if (b[t] >= 1)
      ans++;
  }
  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() {
  int n = 0;
  cin >> n;
  vector<long> S;
  for (int i = 0; i < n; i++) {
    long buf = 0;
    cin >> buf;
    if (find(S.begin(), S.end(), buf) == S.end())
      S.push_back(buf);
  }
  sort(S.begin(), S.end());

  int q = 0;
  cin >> q;
  vector<long> T;
  for (int i = 0; i < q; i++) {
    long buf = 0;
    cin >> buf;
    T.push_back(buf);
  }
  sort(T.begin(), T.end());

  int n_count = 0;

  vector<long>::iterator itS, itT;
  for (itS = S.begin(), itT = T.begin(); itS != S.end() || itT != T.end();
       itS++) {
    if (*itS > *itT) {
      vector<long>::iterator pos = find(itT, T.end(), *itS);
      if (pos != T.end())
        itT = pos;
    }
    if (*itS == *itT) {
      n_count++;
      itT++;
    }
  }

  cout << n_count << '\\n';

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n = 0;
  cin >> n;
  vector<long> S;
  for (int i = 0; i < n; i++) {
    long buf = 0;
    cin >> buf;
    if (find(S.begin(), S.end(), buf) == S.end())
      S.push_back(buf);
  }
  sort(S.begin(), S.end());

  int q = 0;
  cin >> q;
  vector<long> T;
  for (int i = 0; i < q; i++) {
    long buf = 0;
    cin >> buf;
    T.push_back(buf);
  }
  sort(T.begin(), T.end());

  int n_count = 0;

  vector<long>::iterator itS = S.begin();

  while (itS != S.end()) {
    if (T.end() != find(T.begin(), T.end(), *itS))
      n_count++;
    itS++;
  }

  cout << n_count << '\\n';

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

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

using namespace std;

// binary search

int main() {
  ios::sync_with_stdio(false);
  int M, N;
  vector<int> u, v;

  // input
  cin >> M;
  for (int i = 0; i < M; ++i) {
    int m;
    cin >> m;
    u.push_back(m);
  }

  cin >> N;
  for (int i = 0; i < N; ++i) {
    int n;
    cin >> n;
    v.push_back(n);
  }

  // calc
  int res = 0;

  // vector<int>& l = (M >= N ? u : v);
  // vector<int>& s = (M >= N ? v : u);
  vector<int> &l = u;
  vector<int> &s = v;

  for (auto x : s) {
    // if(find(l.begin(), l.end(), x) != l.end()){
    //   ++res;
    // }
    bool found_flg = false;
    int bgn = 0;
    int end = l.size();
    while (bgn < end) {
      int mid = (bgn + end) / 2;
      if (l[mid] > x) {
        end = mid;
      } else if (l[mid] < x) {
        bgn = mid + 1;
      } else { // l[mid] == x
        found_flg = true;
        break;
      }
    }
    if (found_flg)
      ++res;
  }

  cout << res << endl;
}

Error: TLE

Fixed submission:

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

using namespace std;

// binary search

int main() {
  ios::sync_with_stdio(false);
  int M, N;
  vector<int> u, v;

  // input
  cin >> M;
  for (int i = 0; i < M; ++i) {
    int m;
    cin >> m;
    u.push_back(m);
  }

  cin >> N;
  for (int i = 0; i < N; ++i) {
    int n;
    cin >> n;
    v.push_back(n);
  }

  // calc
  int res = 0;

  vector<int> &l = (M >= N ? u : v);
  vector<int> &s = (M >= N ? v : u);

  for (auto x : s) {
    // if(find(l.begin(), l.end(), x) != l.end()){
    //   ++res;
    // }
    bool found_flg = false;
    int bgn = 0;
    int end = l.size();
    int mid = (bgn + end) / 2;
    while (bgn < end) {
      if (l[mid] < x) {
        end = mid;
      } else if (l[mid] > x) {
        bgn = mid + 1;
      } else { // l[mid] == x
        found_flg = true;
        break;
      }
    }
    if (found_flg)
      ++res;
  }

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

#include <iostream>
#include <memory>
#include <vector>

using namespace std;

vector<int> d;

bool bin(int l, int r, int key) {
  int mid;
  while (l < r) {
    mid = (l + r) / 2;
    if (d[mid] == key) {
      return mid;
    } else if (key < d[mid]) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  return false;
}

int main() {

  int n, itmp;
  cin >> n;

  for (int i = 0; i < n; ++i) {
    cin >> itmp;
    d.push_back(itmp);
  }

  int q, count = 0;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> itmp;
    if (bin(0, n, itmp)) {
      //          cout<<" item:"<<itmp<<endl;
      count++;
    }
  }
  cout << count << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <memory>
#include <vector>

using namespace std;

bool bin(vector<int> d, int l, int r, int key) {
  // cout<<"bin r:"<<r<<" l:"<<l<<" key:"<<key<<endl;

  if (d[r] == key)
    return true;
  if (d[l] == key)
    return true;
  if (r == l)
    return false;

  int m = (l + r) / 2;

  if (d[m] < key) {
    l = m + 1;
  } else if (d[m] > key) {
    r = m;
  } else {
    return true;
  }
  if (bin(d, l, r, key))
    return true;
  return false;
}

int main() {

  vector<int> dat;
  int n, itmp;
  cin >> n;

  for (int i = 0; i < n; ++i) {
    cin >> itmp;
    dat.push_back(itmp);
  }

  int q, count = 0;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> itmp;
    if (bin(dat, 0, n - 1, itmp)) {
      //          cout<<" item:"<<itmp<<endl;
      count++;
    }
  }
  cout << count << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  cin >> n;
  vector<int> s;
  for (int i = 0; i < n; i++) {
    s.push_back(i);
    cin >> s[i];
  }
  sort(s.begin(), s.end());
  int q;
  cin >> q;
  vector<int> t;
  for (int i = 0; i < q; i++) {
    t.push_back(i);
    cin >> t[i];
  }
  sort(t.begin(), t.end());
  int c = 0;
  int i = 0;
  for (int j = 0; j < q; j++) {
    while (true) {
      if (s[i] == t[j])
        c++;
      if (s[i] >= t[j])
        break;
      i++;
      if (i == n)
        break;
    }
    if (i == n)
      break;
  }
  cout << c << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<int> s;
  for (int i = 0; i < n; i++) {
    s.push_back(i);
    cin >> s[i];
  }
  sort(s.begin(), s.end());
  int q;
  cin >> q;
  vector<int> t;
  for (int i = 0; i < q; i++) {
    t.push_back(i);
    cin >> t[i];
  }
  sort(t.begin(), t.end());
  int c = 0;
  for (int j = 0; j < q; j++) {
    for (int i = 0; i < n; i++) {
      if (s[i] == t[j])
        c++;
      if (s[i] >= t[j])
        break;
    }
  }
  cout << c << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>

using namespace std;
int A[100000], B[100000], NA, NB;

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

int main() {
  int sum = 0;
  cin >> NA;
  for (int i = 0; i < NA; i++) {
    cin >> A[i];
  }
  cin >> NB;
  for (int i = 0; i < NB; i++) {
    cin >> B[i];
    if (binarysearch(B[i]))
      sum++;
  }
  cout << sum << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;
int A[10000], B[10000], NA, NB;

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

int main() {
  int sum = 0;
  cin >> NA;
  for (int i = 0; i < NA; i++) {
    cin >> A[i];
  }
  cin >> NB;
  for (int i = 0; i < NB; i++) {
    cin >> B[i];
    if (binarysearch(B[i]))
      sum++;
  }
  cout << sum << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>}

int main(void) {
  int n, i, S[100000], q, j, T, C = 0, m, L, R, M;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &S[i]);
  }
  scanf("%d", &q);
  for (j = 0; j < q; j++) {
    scanf("%d", &T);
    L = 0;
    R = n;
    M = R / 2;
    for (i = M; L < R; i = M) {
      if (S[i] == T) {
        C++;
        break;
      }
      if (S[i] < T) {
        L = M + 1;
        M = (L + R) / 2;
      }
      if (S[i] > T) {
        R = M;
        M = (R + L) / 2;
      }
    }
  }
  printf("%d\\n", C);

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>}

int main(void) {
  int n, i, S[100000], q, j, T, C = 0, M, left, right;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &S[i]);
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      if (S[i] < S[j]) {
        M = S[i];
        S[i] = S[j];
        S[j] = M;
      }
    }
  }
  scanf("%d", &q);
  for (j = 0; j < q; j++) {
    scanf("%d", &T);
    for (i = 0; i < n; i++) {
      if (S[i] == T) {
        C++;
        break;
      }
    }
  }
  printf("%d\\n", C);

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

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

int binarySearch(int A[], int n, int key);

int main() {
  int n, S[100000], q, key, C = 0;
  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", &key);
    C += binarySearch(S, n, key);
  }
  printf("%d\\n", C);
  return 0;
}

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

Error: TLE

Fixed submission:

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

int binarySearch(int A[], int s, int t, int key);

int main() {
  int n, S[100000], q, T[50000], key, C = 0;
  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", &key);
    C += binarySearch(S, 0, n, key);
  }
  printf("%d\\n", C);
  return 0;
}

int binarySearch(int A[], int s, int t, int key) {
  int i = (t - s) / 2;
  return (A[i] == key  ? true
          : i == 0     ? false
          : A[i] < key ? binarySearch(A, i, t, key)
                       : binarySearch(A, 0, i, key));
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int n, q, count = 0, middle, sup, inf, sp, ip, x;
  int S[100001];
  bool end;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    sup = n - 1;
    inf = 0;
    end = false;
    cin >> x;
    while (!end) {
      middle = (sup + inf) / 2;
      sp = sup;
      ip = inf;
      if (n != 2 && inf == n - 2)
        middle = n - 1;
      if (x > S[middle])
        inf = middle;
      else if (x < S[middle])
        sup = middle;
      else if (x == S[middle]) {
        end = true;
        count++;
      }
      if (sp == sup && ip == inf)
        end = true;
    }
  }
  cout << count << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int main() {
  int n, q, count = 0;
  cin >> n;
  long long S[100001], T[50001];
  for (int i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;
  bool x = false;
  for (int j = 0; j < q; j++)
    cin >> T[j];
  for (int j = 0; j < q; j++) {
    x = false;
    for (int i = 0; i < n; i++) {
      if (S[i] == T[j] && x == false) {
        count++;
        x = true;
      }
    }
  }
  cout << count << endl;
}