Problem p03559 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repl(i, s, e) for (int i = s; i < e; i++)
#define reple(i, s, e) for (int i = s; i <= e; i++)
#define revrep(i, n) for (int i = n - 1; i >= 0; i--)
#define all(x) (x).begin(), (x).end()

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int N;
  cin >> N;

  map<ll, ll> A;
  rep(i, N) {
    ll tmp;
    cin >> tmp;
    A[tmp]++;
  }

  map<ll, ll> B;
  rep(i, N) {
    ll tmp;
    cin >> tmp;
    B[tmp]++;
  }

  map<ll, ll> C;
  rep(i, N) {
    ll tmp;
    cin >> tmp;
    C[tmp]++;
  }

  map<ll, ll> sumC;
  sumC[C.rbegin()->first] = C.rbegin()->second;

  for (auto iter = next(C.rbegin()); iter != C.rend(); ++iter) {
    sumC[iter->first] = sumC[prev(iter)->first] + iter->second;
  }

  map<ll, ll> countB;

  // B[i]より大きいCの要素の個数を数える。
  for (auto b : B) {
    auto iter = sumC.upper_bound(b.first);

    if (iter != sumC.end())
      countB[b.first] = b.second * iter->second;
  }

  // countBの累積和
  map<ll, ll> sumB;
  sumB[countB.rbegin()->first] = countB.rbegin()->second;

  for (auto iter = next(countB.rbegin()); iter != countB.rend(); ++iter) {
    sumB[iter->first] = sumB[prev(iter)->first] + iter->second;
  }

  ll ans = 0;

  for (auto a : A) {
    auto iter = sumB.upper_bound(a.first);

    if (iter != sumB.end())
      ans += a.second * iter->second;
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repl(i, s, e) for (int i = s; i < e; i++)
#define reple(i, s, e) for (int i = s; i <= e; i++)
#define revrep(i, n) for (int i = n - 1; i >= 0; i--)
#define all(x) (x).begin(), (x).end()

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int N;
  cin >> N;

  map<ll, ll> A;
  rep(i, N) {
    ll tmp;
    cin >> tmp;
    A[tmp]++;
  }

  map<ll, ll> B;
  rep(i, N) {
    ll tmp;
    cin >> tmp;
    B[tmp]++;
  }

  map<ll, ll> C;
  rep(i, N) {
    ll tmp;
    cin >> tmp;
    C[tmp]++;
  }

  map<ll, ll> sumC;
  sumC[C.rbegin()->first] = C.rbegin()->second;

  for (auto iter = next(C.rbegin()); iter != C.rend(); ++iter) {
    sumC[iter->first] = sumC[prev(iter)->first] + iter->second;
  }

  map<ll, ll> countB;

  // B[i]より大きいCの要素の個数を数える。
  for (auto b : B) {
    countB[b.first] = 0;

    auto iter = sumC.upper_bound(b.first);

    if (iter != sumC.end())
      countB[b.first] = b.second * iter->second;
  }

  // countBの累積和
  map<ll, ll> sumB;
  sumB[countB.rbegin()->first] = countB.rbegin()->second;

  for (auto iter = next(countB.rbegin()); iter != countB.rend(); ++iter) {
    sumB[iter->first] = sumB[prev(iter)->first] + iter->second;
  }

  ll ans = 0;

  for (auto a : A) {
    auto iter = sumB.upper_bound(a.first);

    if (iter != sumB.end())
      ans += a.second * iter->second;
  }

  cout << ans << endl;

  return 0;
}