alexjercan/bugnet huggingface dataset:
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

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

int n, S[100000 + 1];

bool binarySearch(int key) {
  int left = 0, right = n - 1, mid;

  while (left <= right) {
    mid = (left + right) / 2;
    // printf("L, R, M, S[mid]: %d %d %d %d\\n", left, right, mid, S[mid]);
    if (key == S[mid])
      return true;
    else if (key < S[mid])
      right = mid - 1;
    else
      left = mid + 1;
  }
  return false;
}

int main() {
  int q, key, count = 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);
    if (binarySearch(key))
      count++;
  }

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

  return 0;
}

Error: 0

Fixed submission:

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

int n, S[10000 + 1];

bool binarySearch(int key) {
  int left = 0, right = n - 1, mid;

  while (left <= right) {
    mid = (left + right) / 2;
    // printf("L, R, M, S[mid]: %d %d %d %d\\n", left, right, mid, S[mid]);
    if (key == S[mid])
      return true;
    else if (key < S[mid])
      right = mid - 1;
    else
      left = mid + 1;
  }
  return false;
}

int main() {
  int q, key, count = 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);
    if (binarySearch(key))
      count++;
  }

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

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

#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

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

int main() {
  int n;
  cin >> n;
  vector<int> na(n);
  for (int i = 0; i < n; ++i)
    scanf("%d", &na[i]);
  int q;
  cin >> q;
  int sum = 0;
  for (int i = 0; i < q; ++i) {
    int t;
    cin >> t;
    sum += search(na, t) ? 1 : 0;
  }
  cout << sum << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int n, q;
  cin >> n;
  vector<int> na(n, 0);
  for (int i = 0; i < n; ++i)
    scanf("%d", &na[i]);
  cin >> q;
  vector<int> qa(q, 0);
  for (int i = 0; i < q; ++i)
    scanf("%d", &qa[i]);

  // linear search
  vector<int> ans;
  for (int i = 0; i < q; ++i) {
    for (int j = 0; j < n; ++j) {
      if (qa[i] == na[j]) {
        ans.push_back(qa[i]);
        break;
      }
    }
  }
  cout << ans.size() << endl;
  /*
  for(int i=0;i<ans.size();++i){
          if(i>0) cout << " ";
          cout << ans[i];
  }
  cout << endl;
  */
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int binary_search(vector<int> S, vector<int> T) {
  // S???sort??????
  int cnt = 0;

  for (int i = 0; i < T.size(); i++) {
    int left = 0;
    int right = S.size();
    int mid = (left + right) / 2;
    while (left < right) {
      mid = (left + right) / 2;
      // cout << "left, right" << left << ", " << right << endl;
      if (S[mid] == T[i]) {
        cnt++;
        break;
      }

      if (S[mid] < T[i]) {
        left = mid + 1;

      } else {
        right = mid;
      }
    }
    // cout << "i: " << i << endl;
  }
  return cnt;
}

int main() {
  int n, q;
  vector<int> S;
  vector<int> T;

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

  cin >> q;
  for (int i = 0; i < q; i++) {
    int tmp;
    cin >> tmp;
    T.push_back(tmp);
  }

  // sort
  // sort(S.begin(), S.end());

  int res = binary_search(S, T);
  cout << res << endl;
}


Error: TLE

Fixed submission:

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

using namespace std;

int binary_search(vector<int> S, vector<int> T) {
  // S???sort??????
  int cnt = 0;

  for (int i = 0; i < T.size(); i++) {
    int left = 0;
    int right = T.size();
    int mid = (left + right) / 2;
    while (left < right) {
      if (S[mid] == T[i]) {
        cnt++;
        break;
      }

      if (S[mid] < T[i]) {
        left = mid + 1;

      } else {
        right = mid;
      }
    }
  }
  return cnt;
}

int main() {
  int n, q;
  vector<int> S;
  vector<int> T;

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

  cin >> q;
  for (int i = 0; i < q; i++) {
    int tmp;
    cin >> tmp;
    T.push_back(tmp);
  }

  // sort
  // sort(S.begin(), S.end());

  int res = binary_search(S, T);
  cout << res << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
using namespace std;
int a[10010];
set<int> s;

int main() {
  int n, q, op, ans = 0;
  memset(a, 0, sizeof(a));
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &op);
    s.insert(op);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &op);
    ans += s.count(op);
  }
  printf("%d\\n", ans);
}


Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int a[10010];

int main() {
  int n, q, op;
  memset(a, 0, sizeof(a));
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &op);
    a[op]++;
  }
  scanf("%d", &q);
  int ans = 0;
  for (int i = 0; i < q; i++) {
    scanf("%d", &op);
    if (a[op] != 0) {
      ans++;
    }
  }
  printf("%d\\n", ans);
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {
  int cnt = 0;
  int x;
  cin >> x;
  long s[x];
  for (int i = 0; i < x; i++)
    cin >> s[i];
  int y;
  cin >> y;
  long t[y];
  for (int j = 0; j < y; j++) {
    cin >> t[j];
    int left = 0;
    int right = x - 1;
    while (left <= right) {
      if (t[j] == s[(left + right) / 2]) {
        cnt++;
        break;
      }
      if (t[j] > s[(left + right) / 2])
        left = (left + right) / 2 + 1;
      if (t[j] < s[(left + right) / 2])
        right = (left + right) / 2 - 1;
    }
  }
  cout << cnt << endl;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int cnt = 0;
  int x;
  cin >> x;
  long s[x];
  for (int i = 0; i < x; i++)
    cin >> s[i];
  int y;
  cin >> y;
  long t[y];
  for (int j = 0; j < y; j++) {
    cin >> t[j];
    int left = 0;
    int right = x - 1;
    while (1) {
      if (t[j] == s[(left + right) / 2]) {
        cnt++;
        break;
      }
      if (left == right)
        break;
      if (t[j] > s[(left + right) / 2])
        left = (left + right) / 2 + 1;
      if (t[j] < s[(left + right) / 2])
        right = (left + right) / 2 - 1;
    }
  }
  cout << cnt << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#define S_MAX 1000000

bool binarySearch(int S[], int s, int e, int t) {
  if (e < s)
    return false;
  int half = (s + e) / 2;
  int half_value = S[half];
  if (half_value == t) {
    return true;
  } else if (half_value < t) {
    return binarySearch(S, half + 1, e, t);
  } else if (t < half_value) {
    return binarySearch(S, s, half - 1, t);
  }
  return false;
}

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

  int q;
  scanf("%d", &q);
  int count = 0;
  for (int j = 0; j < q; j++) {
    int t;
    scanf("%d", &t);
    if (S[n - 1] < t)
      continue;
    if (t < S[0])
      continue;
    if (binarySearch(S, 0, n - 1, t))
      count++;
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#define S_MAX 1000000

bool binarySearch(int S[], int s, int e, int t) {
  int half = (s + e) / 2;
  int half_value = S[half];
  bool found = false;
  if (half_value == t) {
    return true;
  } else if (half_value < t) {
    return binarySearch(S, half, e, t);
  } else if (t < half_value) {
    return binarySearch(S, s, half, t);
  }
  return false;
}

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

  int q;
  scanf("%d", &q);
  int count = 0;
  for (int j = 0; j < q; j++) {
    int t;
    scanf("%d", &t);
    if (S[n - 1] < t)
      continue;
    if (binarySearch(S, 0, n - 1, t))
      count++;
  }

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

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

#include <iostream>
#include <vector>

typedef unsigned uint;

bool is_exist_bin_search(uint v, std::vector<uint> s) {
  uint min = 0, max = s.size();
  while (min < max) {
    uint mid = (min + max) / 2;
    uint v_mid = s[mid];
    if (v_mid == v)
      return true;

    if (v_mid >= v)
      max = mid;
    else
      min = mid + 1;
  }
  return false;
}

unsigned count(std::vector<uint> s, std::vector<uint> t) {
  uint n = 0;
  for (uint i = 0; i < t.size(); i++) {
    if (is_exist_bin_search(t[i], s))
      n++;
  }
  return n;
}

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  std::vector<uint> s, t;

  {
    uint n, buf;
    std::cin >> n;
    for (; n; n--) {
      std::cin >> buf;
      s.push_back(buf);
    }
  }

  {
    uint q, buf;
    std::cin >> q;
    for (; q; q--) {
      std::cin >> buf;
      t.push_back(buf);
    }
  }

  std::cout << count(s, t) << '\\n';
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

typedef unsigned uint;

bool is_exist_bin_search(uint v, std::vector<uint> s) {
  uint min = 0, max = s.size() - 1;
  while (min < max) {
    uint mid = (min + max) / 2;
    uint v_mid = s[mid];
    if (v_mid == v)
      return true;

    if (v_mid >= v)
      max = mid;
    else
      min = mid;
  }
  return false;
}

unsigned count(std::vector<uint> s, std::vector<uint> t) {
  uint n = 0;
  for (uint i = 0; i < t.size(); i++) {
    if (is_exist_bin_search(t[i], s))
      n++;
  }
  return n;
}

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  std::vector<uint> s, t;

  {
    uint n, buf;
    std::cin >> n;
    for (; n; n--) {
      std::cin >> buf;
      s.push_back(buf);
    }
  }

  {
    uint q, buf;
    std::cin >> q;
    for (; q; q--) {
      std::cin >> buf;
      t.push_back(buf);
    }
  }

  std::cout << count(s, t) << '\\n';
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

#include <algorithm>
#include <cmath>

using namespace std;

int S[100100];
int s, q;

bool binarySearch(int A[], int key) {
  int left = 0;
  int right = s - 1;
  int mid;

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

int main() {

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

  int count = 0;
  int temp;

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> temp;
    if (binarySearch(S, temp)) {
      count++;
    }
  }

  cout << count << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

#include <algorithm>
#include <cmath>

using namespace std;

int S[10010];
int s, q;

bool binarySearch(int A[], int key) {
  int left = 0;
  int right = s - 1;
  int mid;

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

int main() {

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

  int count = 0;
  int temp;

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> temp;
    if (binarySearch(S, temp)) {
      count++;
    }
  }

  cout << count << endl;

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

#include <iostream>

int n, S[100000];

int search(int T) {
  int l = 0, r = n;
  int mid;

  while (l < r) {
    mid = (l + r) / 2;
    if (T == S[mid])
      return 1;
    if (T > S[mid])
      l = mid + 1;
    else if (T < S[mid])
      r = mid;
  }
  return 0;
}

int main() {
  int i, q;
  int T[50000];
  int count = 0;

  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> S[i];
  }
  std::cin >> q;
  for (int i = 0; i < q; i++) {
    std::cin >> T[i];
    if (search(T[i])) {
      count++;
    }
  }
  std::cout << count << std::endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>

int n, S[100000];

int search(int T) {
  int l = 0, r = n;
  int mid;

  while (l < r) {
    mid = (l + r) / 2;
    if (T == S[mid])
      return 1;
    if (T > S[mid])
      l = mid + 1;
    else if (T < S[mid])
      r = mid;
  }
  return 0;
}

int main() {
  int i, q;
  int T[5000];
  int count = 0;

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

#include <iostream>
using namespace std;

int main() {
  int sn, tn, t, l, r, m, c = 0;
  cin >> sn;
  int *s = new int[sn];
  for (int i = 0; i < sn; i++)
    cin >> s[i];
  cin >> tn;
  for (int i = 0; i < tn; i++) {
    cin >> t;
    l = 0;
    r = sn;
    while (l < r) {
      m = (l + r) / 2;
      if (s[m] == t) {
        c++;
        break;
      } else if (t < s[m])
        r = m;
      else
        l = m + 1;
    }
  }
  cout << c << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int sn, tn, t, l, r, m, c = 0;
  cin >> sn;
  int *s = new int[sn];
  for (int i = 0; i < sn; i++)
    cin >> s[i];
  cin >> tn;
  for (int i = 0; i < tn; i++) {
    cin >> t;
    l = 0;
    r = sn - 1;
    while (l < r) {
      m = (l + r) / 2;
      if (s[m] == t) {
        c++;
        break;
      } else if (t < s[m])
        r = m + 1;
      else
        l = m - 1;
    }
  }
  cout << c << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

int binarysearch(int q[], int key, int n) {
  int left = 0;
  int right = n;

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

int main() {
  int n, a;
  scanf("%d", &n);
  int s[100000];

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

  int n_t, key;
  int sum = 0;
  scanf("%d", &n_t);
  for (int i = 0; i < n_t; i++) {
    scanf("%d", &key);
    if (binarysearch(s, key, n))
      sum++;
  }

  printf("%d\\n", sum);
}

Error: 0

Fixed submission:

#include <stdio.h>

using namespace std;

int S[10001], n, q;

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

      return -1;
  */

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

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

#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

void read_separated_string(vector<int> &v, string str) {
  istringstream istr(str);
  string tmp;
  while (getline(istr, tmp, ' ')) {
    v.push_back(stoi(tmp));
  }
}

int binary_search(const vector<int> &v, int first, int last, int key) {
  int middle = (int)((first + last) / 2);
  if (first > last) {
    return 0;
  } else if (key == v[middle]) {
    return 1;
  } else if (key < v[middle]) {
    return binary_search(v, first, middle - 1, key);
  } else {
    return binary_search(v, middle + 1, last, key);
  }
}

int main() {
  int N, Q;
  vector<int> n;
  vector<int> q;
  string str;

  cin >> N;
  cin.ignore();
  getline(cin, str);
  read_separated_string(n, str);
  cin >> Q;
  cin.ignore();
  getline(cin, str);
  read_separated_string(q, str);

  int count = 0;
  for (int i = 0; i < q.size(); ++i) {
    count += binary_search(n, 0, n.size() - 1, q[i]);
  }
  cout << count << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

void read_separated_string(vector<int> &v, string str) {
  istringstream istr(str);
  string tmp;
  while (getline(istr, tmp, ' ')) {
    v.push_back(stoi(tmp));
  }
}

int binary_search(int n, const vector<int> &v) {
  if (v.size() == 1) {
    if (v[0] == n) {
      return 1;
    } else {
      return 0;
    }
  }
  int mid = (int)(v.size() / 2);
  if (v[mid] == n) {
    return 1;
  } else if (v[mid] > n) {
    vector<int> half;
    for (int i = 0; i < mid; ++i) {
      half.push_back(v[i]);
    }
    return binary_search(n, half);
  } else {
    vector<int> half;
    for (int i = mid; i < v.size(); ++i) {
      half.push_back(v[i]);
    }
    return binary_search(n, half);
  }
}

int main() {
  int N, Q;
  vector<int> n;
  vector<int> q;
  string str;
  cin >> N;
  cin.ignore();
  getline(cin, str);
  read_separated_string(n, str);
  cin >> Q;
  cin.ignore();
  getline(cin, str);
  read_separated_string(q, str);
  int count = 0;
  for (auto i : q) {
    count += binary_search(i, n);
  }
  cout << count << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int a, b[100000], c, d[50000], e, f, g = 0;
  cin >> a;
  for (int i = 0; i < a; i++) {
    cin >> b[i];
  }
  cin >> c;
  for (int i = 0; i < c; i++) {
    cin >> d[i];
  }
  for (int i = 0; i < c; i++) {
    e = 0;
    f = a;
    while (e < f) {
      if (d[i] == b[(e + f) / 2]) {
        g++;
        break;
      }
      if (d[i] < b[(e + f) / 2]) {
        f = (e + f) / 2;
        continue;
      } else if (d[i] > b[(e + f) / 2]) {
        e = (e + f) / 2 + 1;
        continue;
      }
    }
  }
  cout << g << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int a, b[100000], c, d[50000], e = 0;
  cin >> a;
  for (int i = 0; i < a; i++) {
    cin >> b[i];
  }
  cin >> c;
  for (int i = 0; i < c; i++) {
    cin >> d[i];
  }
  for (int i = 0; i < c; i++) {
    for (int j = 0; j < a; j++) {
      if (b[j] == d[i]) {
        e += 1;
        break;
      }
    }
  }
  cout << e << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, q;
  int S[100000];
  int T[50000];
  int cnt = 0;

  fill_n(S, 100000, 0);
  fill_n(T, 50000, 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++) {
    int left = 0;
    int right = n;
    int mid;
    int ave;
    while (left < right) {
      ave = (left + right) / 2;
      mid = S[ave];
      if (mid == T[i]) {
        cnt++;
        break;
      }
      if (mid > T[i]) {
        right = ave;
      }
      if (mid < T[i]) {
        left = ave + 1;
      }
    }
  }
  cout << cnt << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, q;
  int S[100000];
  int T[50000];
  int cnt = 0;

  fill_n(S, 100000, 0);
  fill_n(T, 50000, 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++) {
    for (int j = 0; j < n; j++) {
      if (T[i] < S[j])
        break;
      if (T[i] == S[j]) {
        cnt++;
        break;
      }
    }
  }
  cout << cnt << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

typedef vector<int> int_vec;

bool linear_search(const int_vec &v, int n) {
  for (int_vec::const_iterator it = v.begin(); it != v.end(); ++it) {
    if (*it == n) {
      return true;
    }
  }
  return false;
}

bool binary_search(int_vec::iterator begin, int_vec::iterator end, int n) {
  while (begin < end) {
    int_vec::iterator mid = begin + ((end - begin) / 2);
    if (*mid == n)
      return true;
    else if (n < *mid)
      end = mid;
    else
      begin = mid + 1;
  }
  return false;
}

int main() {
  int n;
  cin >> n;
  int_vec S;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    S.push_back(x);
  }

  int q;
  cin >> q;
  int_vec T;
  for (int j = 0; j < q; ++j) {
    int x;
    cin >> x;
    T.push_back(x);
  }

  int c = 0;
  for (int_vec::iterator it = T.begin(); it != T.end(); ++it) {
    if (binary_search(S.begin(), S.end(), *it))
      c++;
  }
  cout << c << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

typedef vector<int> int_vec;

bool linear_search(const int_vec &v, int n) {
  for (int_vec::const_iterator it = v.begin(); it != v.end(); ++it) {
    if (*it == n) {
      return true;
    }
  }
  return false;
}

int main() {
  int n;
  cin >> n;
  int_vec S;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    S.push_back(x);
  }

  int q;
  cin >> q;
  int_vec T;
  for (int j = 0; j < q; ++j) {
    int x;
    cin >> x;
    T.push_back(x);
  }

  int c = 0;
  for (int_vec::iterator it = T.begin(); it != T.end(); ++it) {
    if (linear_search(S, *it))
      c++;
  }
  cout << c << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

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

  int n, q, t, count = 0;
  int left, right, center;
  int S[100000];

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

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> t;
    left = 0;
    right = n;
    while (left <= right) {
      center = (left + right) / 2;
      if (S[center] < t) {
        left = center + 1;
      } else if (S[center] > t) {
        right = center - 1;
      } else {
        count++;
        break;
      }
    }
  }

  cout << count << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n[10000], q[500], x, y, s = 0;
  int l, r, m;
  cin >> x;
  for (int i = 0; i < x; i++)
    cin >> n[i];
  cin >> y;
  for (int i = 0; i < y; i++) {
    cin >> q[i];
    l = 0;
    r = x;
    while (l <= r) {
      m = (l + r) / 2;
      if (n[m] < q[i])
        l = m + 1;
      if (n[m] > q[i])
        r = m;
      else if (q[i] == n[m]) {
        s++;
        break;
      }
    }
  }

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