Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
const ll INF = (ll)1e18 + 1;
const ll DIV = 1000000007;
// #define TEST
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
#ifdef TEST
  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();
#endif
  long N;
  cin >> N;
  vector<ll> A(N);
  vector<ll> B(N);
  vector<ll> C(N);
  for (size_t i = 0; i < N; i++)
    cin >> A[i];
  for (size_t i = 0; i < N; i++)
    cin >> B[i];
  for (size_t i = 0; i < N; i++)
    cin >> C[i];

  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  sort(C.begin(), C.end());

  ll comb_count = 0;
  auto B_it = lower_bound(B.begin(), B.end(), A[0]);
  size_t B_start = distance(B.begin(), B_it);
  auto C_it = lower_bound(C.begin(), C.end(), B[B_start]);
  size_t C_start = distance(C.begin(), C_it);
  for (size_t i = 0; i < N; i++) {
    auto B_it = lower_bound(B.begin(), B.end(), A[i] + 1);
    size_t B_start = distance(B.begin(), B_it);
    cerr << i << ":" << B_start << endl;
    for (size_t j = B_start; j < N; j++) {
      auto C_it = lower_bound(C.begin(), C.end(), B[j] + 1);
      size_t C_start = distance(C.begin(), C_it);
      comb_count += C.size() - C_start;
    }
  }
  cout << comb_count << endl;
#ifdef TEST
  end = chrono::system_clock::now();
  cerr << static_cast<double>(
              chrono::duration_cast<chrono::microseconds>(end - start).count() /
              1000.0)
       << "[ms]" << endl;
#endif
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
const ll INF = (ll)1e18 + 1;
const ll DIV = 1000000007;
// #define TEST
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
#ifdef TEST
  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();
#endif
  long N;
  cin >> N;
  vector<ll> A(N);
  vector<ll> B(N);
  vector<ll> C(N);
  for (size_t i = 0; i < N; i++)
    cin >> A[i];
  for (size_t i = 0; i < N; i++)
    cin >> B[i];
  for (size_t i = 0; i < N; i++)
    cin >> C[i];

  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  sort(C.begin(), C.end());

  ll comb_count = 0;
  for (size_t j = 0; j < N; j++) {
    auto A_it = upper_bound(A.begin(), A.end(), B[j] - 1);
    size_t A_end = distance(A.begin(), A_it);
    auto C_it = lower_bound(C.begin(), C.end(), B[j] + 1);
    size_t C_start = distance(C.begin(), C_it);
    comb_count += A_end * (C.size() - C_start);
  }
  cout << comb_count << endl;
#ifdef TEST
  end = chrono::system_clock::now();
  cerr << static_cast<double>(
              chrono::duration_cast<chrono::microseconds>(end - start).count() /
              1000.0)
       << "[ms]" << endl;
#endif
  return 0;
}