Problem p03559 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////
const long long int INF = 100000000000;
const long long int Mod = 1000000007;
using ll = long long int; // long long int は64bit
using vi = vector<int>;
using Vi = vector<long long int>;
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define rep(i, N) for (long long int i = 0; i < N; i++)
#define np nullptr
template <class T> bool chmax(T &former, const T &b) {
  if (former <= b) {
    former = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &former, const T &b) {
  if (b < former) {
    former = b;
    return true;
  }
  return false;
}
template <class T> T sqar(T x) { return x * x; } // sqrt(x)は平方根;
#define Sort(v)                                                                \
  std::sort(v.begin(), v.end(),                                                \
            std::greater<decltype(v[0])>()) // 降順でVをソート
#define fill(x, num) memset(x, num, sizeof(x));
typedef vector<vector<ll>> matrix;
typedef pair<ll, ll> Pair;
bool p_compare(const Pair &firstP, const Pair &secondP) {
  return firstP.second < secondP.second;
} // sort(all(v),p_compare)で昇順ソート
//////////////////////////^^ *Emile
///^^^//////////////////////////////////////////////////////
// Snipet -> Caps Lock

ll n;
vi a, b, c;

ll memo[1000000] = {0};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  ll numn;
  rep(i, n) {
    cin >> numn;
    a.pb(numn);
  }
  rep(i, n) {
    cin >> numn;
    b.pb(numn);
  }
  rep(i, n) {
    cin >> numn;
    c.pb(numn);
  }
  sort(all(a));
  sort(all(b));
  sort(all(c));
  ll ans = 0;
  bool flg = true;
  rep(i, n) {
    flg = true;
    for (auto b2 = upper_bound(all(b), a[i]); flg; b2++) {
      if (memo[b2 - b.begin()] == 0) {
        auto c2 = upper_bound(all(c), *b2);
        if (*b2 > a[i] && *c2 > *b2)
          memo[b2 - b.begin()] = c.end() - c2;
        ans += memo[b2 - b.begin()];
      } else {
        ans += memo[b2 - b.begin()];
      }
      if (b2 == b.end())
        flg = false;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////
const long long int INF = 100000000000;
const long long int Mod = 1000000007;
using ll = long long int; // long long int は64bit
using vi = vector<int>;
using Vi = vector<long long int>;
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define rep(i, N) for (long long int i = 0; i < N; i++)
#define np nullptr
template <class T> bool chmax(T &former, const T &b) {
  if (former <= b) {
    former = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &former, const T &b) {
  if (b < former) {
    former = b;
    return true;
  }
  return false;
}
template <class T> T sqar(T x) { return x * x; } // sqrt(x)は平方根;
#define Sort(v)                                                                \
  std::sort(v.begin(), v.end(),                                                \
            std::greater<decltype(v[0])>()) // 降順でVをソート
#define fill(x, num) memset(x, num, sizeof(x));
typedef vector<vector<ll>> matrix;
typedef pair<ll, ll> Pair;
bool p_compare(const Pair &firstP, const Pair &secondP) {
  return firstP.second < secondP.second;
} // sort(all(v),p_compare)で昇順ソート
//////////////////////////^^ *Emile
///^^^//////////////////////////////////////////////////////
// Snipet -> Caps Lock

ll n;
vi a, b, c;

ll memo[1000000] = {0};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  ll numn;
  rep(i, n) {
    cin >> numn;
    a.pb(numn);
  }
  rep(i, n) {
    cin >> numn;
    b.pb(numn);
  }
  rep(i, n) {
    cin >> numn;
    c.pb(numn);
  }
  sort(all(a));
  sort(all(b));
  sort(all(c));
  ll ans = 0;
  bool flg = true;
  rep(i, n) {
    auto c2 = upper_bound(all(c), b[i]);
    auto a2 = lower_bound(all(a), b[i]);
    ans += (c.end() - c2) * (a2 - a.begin());
  }
  cout << ans << endl;
  return 0;
}