Problem p03559 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

using namespace std;

int binary_search(const int x, const vector<int> v, const int s, const int e) {
  if (x <= v[s])
    return (s - 1);
  if (v[e] < x) {
    return (e);
  }
  int idx = (e - s) / 2 + s;
  if (v[idx] < x) {
    if (e == idx + 1) {
      return (idx);
    }
    return (binary_search(x, v, idx, e));
  } else {
    if (s == idx - 1) {
      return (idx - 1);
    }
    return (binary_search(x, v, s, idx));
  }
}

int main() {
  long long result = 0;
  int n;
  vector<int> a, b, c;
  vector<long long> acc;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    a.push_back(tmp);
  }
  sort(a.begin(), a.end());
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    b.push_back(tmp);
  }
  sort(b.begin(), b.end());
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    c.push_back(tmp);
  }
  sort(c.begin(), c.end());
  long long prev = 0;
  for (int i = 0; i < n; i++) {
    int tmp1 = binary_search(b[i], a, 0, n - 1) + 1;
    vector<int>::iterator tmp_iter = lower_bound(a.begin(), a.end(), b[i]);
    int tmp = tmp_iter - a.begin();
    acc.push_back((long long)tmp + prev);
    prev += (long long)tmp;
  }
  for (int i = 0; i < n; i++) {
    int tmp1 = binary_search(c[i], b, 0, n - 1);
    vector<int>::iterator tmp_iter = lower_bound(b.begin(), b.end(), c[i]);
    int tmp = tmp_iter - b.begin();
    tmp--;
    if (tmp >= 0)
      result += acc[tmp];
  }
  cout << result << endl;
}


Fixed submission:
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

using namespace std;

int binary_search(const int x, const vector<int> &v, const int s, const int e) {
  if (x <= v[s])
    return (s - 1);
  if (v[e] < x) {
    return (e);
  }
  int idx = (e - s) / 2 + s;
  if (v[idx] < x) {
    if (e == idx + 1) {
      return (idx);
    }
    return (binary_search(x, v, idx, e));
  } else {
    if (s == idx - 1) {
      return (idx - 1);
    }
    return (binary_search(x, v, s, idx));
  }
}

int main() {
  long long result = 0;
  int n;
  vector<int> a, b, c;
  vector<long long> acc;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    a.push_back(tmp);
  }
  sort(a.begin(), a.end());
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    b.push_back(tmp);
  }
  sort(b.begin(), b.end());
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    c.push_back(tmp);
  }
  sort(c.begin(), c.end());
  long long prev = 0;
  for (int i = 0; i < n; i++) {
    int tmp1 = binary_search(b[i], a, 0, n - 1) + 1;
    vector<int>::iterator tmp_iter = lower_bound(a.begin(), a.end(), b[i]);
    int tmp = tmp_iter - a.begin();
    acc.push_back((long long)tmp + prev);
    prev += (long long)tmp;
  }
  for (int i = 0; i < n; i++) {
    int tmp1 = binary_search(c[i], b, 0, n - 1);
    vector<int>::iterator tmp_iter = lower_bound(b.begin(), b.end(), c[i]);
    int tmp = tmp_iter - b.begin();
    tmp--;
    if (tmp >= 0)
      result += acc[tmp];
  }
  cout << result << endl;
}