Problem p03559 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define int long long
#define CONTAINS(v, n) (find((V).begin(), (V).end(), (n)) != (V).end())
#define SORT(v) sort((V).begin(), (V).end())
#define RSORT(v) sort((V).rbegin(), (V).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REMOVE(v, a) (V.erase(remove((V).begin(), (V).end(), (a)), (V).end()))
#define REVERSE(v) (reverse((V).begin(), (V).end()))
#define LOWER_BOUND(v, a) (lower_bound((V).begin(), (V).end(), (a)))
#define UPPER_BOUND(v, a) (upper_bound((V).begin(), (V).end(), (a)))
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;

int N;
int A[100000];
int B[100000];
int C[100000];

int bcnt[100000];

signed main() {
  cin >> N;
  REP(i, N) cin >> A[i];
  REP(i, N) cin >> B[i];
  REP(i, N) cin >> C[i];

  ARY_SORT(A, N);
  ARY_SORT(B, N);
  ARY_SORT(C, N);

  int c_index = N - 1;
  for (int b = N - 1; b >= 0; b--) {
    int cc = 0;
    for (int c = c_index; c >= 0; c--) {
      if (C[c] <= B[b]) {
        cc = c + 1;
        break;
      }
    }
    c_index = cc - 1;
    bcnt[b] = N - cc;
    if (b < N - 1) {
      bcnt[b] += bcnt[b + 1];
    }
  }

  int ans = 0;
  int b_index = N - 1;
  for (int a = N - 1; a >= 0; a--) {
    int bb = 0;
    for (int b = b_index; b >= 0; b--) {
      if (B[b] <= A[a]) {
        bb = b + 1;
        break;
      }
    }
    c_index = bb - 1;
    if (bb < N) {
      ans += bcnt[bb];
    }
  }

  cout << ans << endl;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define int long long
#define CONTAINS(v, n) (find((V).begin(), (V).end(), (n)) != (V).end())
#define SORT(v) sort((V).begin(), (V).end())
#define RSORT(v) sort((V).rbegin(), (V).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REMOVE(v, a) (V.erase(remove((V).begin(), (V).end(), (a)), (V).end()))
#define REVERSE(v) (reverse((V).begin(), (V).end()))
#define LOWER_BOUND(v, a) (lower_bound((V).begin(), (V).end(), (a)))
#define UPPER_BOUND(v, a) (upper_bound((V).begin(), (V).end(), (a)))
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;

int N;
int A[100000];
int B[100000];
int C[100000];

int bcnt[100000];

signed main() {
  cin >> N;
  REP(i, N) cin >> A[i];
  REP(i, N) cin >> B[i];
  REP(i, N) cin >> C[i];

  ARY_SORT(A, N);
  ARY_SORT(B, N);
  ARY_SORT(C, N);

  int c_index = N - 1;
  for (int b = N - 1; b >= 0; b--) {
    int cc = 0;
    for (int c = c_index; c >= 0; c--) {
      if (C[c] <= B[b]) {
        cc = c + 1;
        break;
      }
    }
    c_index = cc - 1;
    bcnt[b] = N - cc;
    if (b < N - 1) {
      bcnt[b] += bcnt[b + 1];
    }
  }

  int ans = 0;
  int b_index = N - 1;
  for (int a = N - 1; a >= 0; a--) {
    int bb = 0;
    for (int b = b_index; b >= 0; b--) {
      if (B[b] <= A[a]) {
        bb = b + 1;
        break;
      }
    }
    b_index = bb - 1;
    if (bb < N) {
      ans += bcnt[bb];
    }
  }

  cout << ans << endl;
}