Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;

vector<int> merge_sort_merge(vector<int> v1, vector<int> v2) {
  vector<int> ret;
  int i = 0, j = 0;
  while (i < v1.size() && j < v2.size()) {
    if (v1[i] < v2[j]) {
      ret.push_back(v1[i++]);
    } else {
      ret.push_back(v2[j++]);
    }
  }
  while (i < v1.size()) {
    ret.push_back(v1[i++]);
  }
  while (j < v2.size()) {
    ret.push_back(v2[j++]);
  }
  return ret;
}

void merge_sort(vector<int> &vec) {
  if (vec.size() < 2) {
    return;
  }
  vector<int> v1(vec.size() / 2 + vec.size() % 2);
  vector<int> v2(vec.size() / 2);
  for (int i = 0; i < vec.size(); i++) {
    if (i < vec.size() / 2 + vec.size() % 2) {
      v1[i] = vec[i];
    } else {
      v2[i - vec.size() / 2 - vec.size() % 2] = vec[i];
    }
  }
  merge_sort(v1);
  merge_sort(v2);
  vec = merge_sort_merge(v1, v2);
}
int S[100000];
int n;
bool binary_search(int target, int l, int r) {
  int mid = (l + r) / 2;
  // cout<<target<<" "<<l<<" "<<r<<endl;
  if (S[mid] == target) {
    return true;
  }
  if (S[mid] > target) {
    r = mid - 1;
  }
  if (S[mid] < target) {
    l = mid + 1;
  }
  if (l > n - 1 || r < 0 || l > r) {
    return false;
  }
  return binary_search(target, l, r);
  ;
}

int main() {
  int q;
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", S + i);
  }
  cin >> q;
  int t;
  int ans = 0;
  for (int i = 0; i < q; i++) {
    cin >> t;
    if (binary_search(t, 0, n - 1)) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;

vector<int> merge_sort_merge(vector<int> v1, vector<int> v2) {
  vector<int> ret;
  int i = 0, j = 0;
  while (i < v1.size() && j < v2.size()) {
    if (v1[i] < v2[j]) {
      ret.push_back(v1[i++]);
    } else {
      ret.push_back(v2[j++]);
    }
  }
  while (i < v1.size()) {
    ret.push_back(v1[i++]);
  }
  while (j < v2.size()) {
    ret.push_back(v2[j++]);
  }
  return ret;
}

void merge_sort(vector<int> &vec) {
  if (vec.size() < 2) {
    return;
  }
  vector<int> v1(vec.size() / 2 + vec.size() % 2);
  vector<int> v2(vec.size() / 2);
  for (int i = 0; i < vec.size(); i++) {
    if (i < vec.size() / 2 + vec.size() % 2) {
      v1[i] = vec[i];
    } else {
      v2[i - vec.size() / 2 - vec.size() % 2] = vec[i];
    }
  }
  merge_sort(v1);
  merge_sort(v2);
  vec = merge_sort_merge(v1, v2);
}

bool binary_search(vector<int> vec, int target, int l, int r) {
  int mid = (l + r) / 2;
  // cout<<target<<" "<<l<<" "<<r<<endl;
  if (vec[mid] == target) {
    return true;
  }
  if (vec[mid] > target) {
    r = mid - 1;
  }
  if (vec[mid] < target) {
    l = mid + 1;
  }
  if (l > vec.size() - 1 || r < 0 || l > r) {
    return false;
  }
  return binary_search(vec, target, l, r);
  ;
}

int main() {
  int n;

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

  cin >> q;
  T.resize(q);
  int ans = 0;
  for (int i = 0; i < q; i++) {
    cin >> T[i];
    if (binary_search(S, T[i], 0, n - 1)) {
      ans++;
    }
    //   cout<<i<<endl;
  }
  cout << ans << endl;

  return 0;
}