Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
#define print(x) cout << x << endl;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define REP(i, a) for (int i = 0; i < a; i++)
#define printall(n, array)                                                     \
  {                                                                            \
    for (int i = 0; i < n; i++) {                                              \
      cout << array[i] << " ";                                                 \
    }                                                                          \
    cout << endl;                                                              \
  }
#define U() cout << endl;
typedef long long ll;
typedef pair<int, int> PI;
typedef pair<int, PI> V;
typedef vector<int> VE;
const ll mod = 10000000007; // 10^9+7

int main() {
  int n;
  cin >> n;
  VE a, b, c;
  ll A, B, C;
  ll temp;
  REP(i, n) {
    cin >> temp;
    a.push_back(temp);
  }
  REP(i, n) {
    cin >> temp;
    b.push_back(temp);
  }
  REP(i, n) {
    cin >> temp;
    c.push_back(temp);
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  // REP(i,a.size())cout<<a[i]<<" ";cout<<endl;///
  // auto e=lower_bound(a.begin(),a.end(),6)-a.begin();///
  // print(a[e])///
  ll ans = 0;
  REP(i, n) {
    A = a[i];
    auto B = upper_bound(b.begin(), b.end(), A) - b.begin();
    rep(j, B, n) {
      auto C = upper_bound(c.begin(), c.end(), b[j]) - c.begin();
      ans += (n - C);
      // print("jans "<<j<<" "<<ans);///
    }
  }
  print(ans);
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define print(x) cout << x << endl;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define REP(i, a) for (int i = 0; i < a; i++)
#define printall(n, array)                                                     \
  {                                                                            \
    for (int i = 0; i < n; i++) {                                              \
      cout << array[i] << " ";                                                 \
    }                                                                          \
    cout << endl;                                                              \
  }
#define U() cout << endl;
typedef long long ll;
typedef pair<int, int> PI;
typedef pair<int, PI> V;
typedef vector<int> VE;
const ll mod = 10000000007; // 10^9+7

int main() {
  int n;
  cin >> n;
  VE a, b, c;
  ll A, B, C;
  ll temp;
  REP(i, n) {
    cin >> temp;
    a.push_back(temp);
  }
  REP(i, n) {
    cin >> temp;
    b.push_back(temp);
  }
  REP(i, n) {
    cin >> temp;
    c.push_back(temp);
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  // REP(i,a.size())cout<<a[i]<<" ";cout<<endl;///
  // auto e=lower_bound(a.begin(),a.end(),6)-a.begin();///
  // print(a[e])///
  ll ans = 0;
  REP(i, n) {
    B = b[i];
    auto A = lower_bound(a.begin(), a.end(), B) - a.begin();
    auto C = upper_bound(c.begin(), c.end(), B) - c.begin();
    ans += A * (n - C);
    // print(i<<" "<<A<<" "<<n-C<<" ans "<<ans);///
  }
  print(ans);
}