Problem:
Score : 300 points

Problem Statement

The season for Snuke Festival has come again this year. First of all, Ringo will perform a ritual to summon Snuke. For the ritual, he needs an altar, which consists of three parts, one in each of the three categories: upper, middle and lower.

He has N parts for each of the three categories. The size of the i-th upper part is A_i, the size of the i-th middle part is B_i, and the size of the i-th lower part is C_i.

To build an altar, the size of the middle part must be strictly greater than that of the upper part, and the size of the lower part must be strictly greater than that of the middle part. On the other hand, any three parts that satisfy these conditions can be combined to form an altar.

How many different altars can Ringo build? Here, two altars are considered different when at least one of the three parts used is different.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9(1\leq i\leq N)

1 \leq B_i \leq 10^9(1\leq i\leq N)

1 \leq C_i \leq 10^9(1\leq i\leq N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N
B_1 ... B_N
C_1 ... C_N

Output

Print the number of different altars that Ringo can build.

Sample Input 1

2
1 5
2 4
3 6

Sample Output 1

3

The following three altars can be built:

Upper: 1-st part, Middle: 1-st part, Lower: 1-st part

Upper: 1-st part, Middle: 1-st part, Lower: 2-nd part

Upper: 1-st part, Middle: 2-nd part, Lower: 2-nd part

Sample Input 2

3
1 1 1
2 2 2
3 3 3

Sample Output 2

27

Sample Input 3

6
3 14 159 2 6 53
58 9 79 323 84 6
2643 383 2 79 50 288

Sample Output 3

87

Solution in C++11 (GCC 4.8.1) (judge verdict: Time Limit Exceeded)

#include <iostream>
#include <iomanip>
#include <vector>
#include <valarray>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <bitset>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <complex>
#include <string>
#include <sstream>

#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cmath>

using namespace std;

#define all(c) c.begin(),c.end()
#define repeat(i,n) for(int i=0;i<static_cast<int>(n);i++)
#define debug(x) #x << "=" << (x)
#define dump(x) cerr << debug(x) << " (L:" << __LINE__ << ")"<< endl

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<string> vs;

template<typename T>
ostream& operator<<(ostream& os, const vector<T>& vec){
    os << "[";
    for(int i = 0; i < vec.size(); i++){
        os << vec[i] << ",";
    }
    os << "]";
    return os;
}

template<typename T>
T input(){
    T t;
    cin >> t;
    return t;
}

template<typename T>
vector<T> input(const int N){
    vector<T> v(N);
    repeat(i,N) cin >> v[i];
    return v;
}

long long gcd(long long a, long long b){
    if(b == 0){
        return a;
    }
    return gcd(b, a % b);
}

long long lcm(long long a, long long b){
    return (a / gcd(a, b)) * b;
}

long long mul(const long long& a, const long long& b, const long long& mod) {
    return ((a % mod) * (b % mod)) % mod;
}

long long power(const long long& x, const long long& y, const long long& mod) {
    if (y == 0) {
        return 1;
    } else if (y == 1) {
        return x % mod;
    } else {
        long long value = power(x, y / 2, mod);
        if (y % 2 == 0) {
            return mul(value, value, mod);
        } else {
            return mul(value, value, mod) * x % mod;
        }
    }
}

long long div(const long long& a, const long long& b, const long long& mod) {
    return mul(a, power(b, mod - 2, mod), mod);
}

map<long long, long long> factorials;
long long factorial(const long long& n, const long long& mod) {
    if (n == 0 || n == 1) {
        return 1;
    }
    if (factorials[n] != 0) {
        return factorials[n];
    }
    factorials[n] = n * factorial(n - 1, mod) % mod;
    return factorials[n] % mod;
}

long long combination(const long long& n, const long long& x, const long long& mod) {
    long long numerator = 1;
    long long denominator = 1;
    repeat(i, x) {
        numerator *= (n - i) % mod;
        numerator %= mod;
        denominator *= (i + 1) % mod;
        denominator %= mod;
    }
    return div(numerator, denominator, mod);
}

bool compare(ll a, ll b) {
    return a <= b;
}

int main(){
    ll N;
    cin >> N;
    vector<ll> high(N);
    vector<ll> middle(N);
    vector<ll> low(N);
    repeat(i, N) {
        cin >> high[i];
    }
    repeat(i, N) {
        cin >> middle[i];
    }
    repeat(i, N) {
        cin >> low[i];
    }

    sort(high.begin(), high.end());
    sort(middle.begin(), middle.end());
    sort(low.begin(), low.end());

    ll combinations = 0;

    map<ll, ll> midMap;
    map<ll, ll> lowMap;

    ll prevHigh = 0;
    ll prevMid = 0;

    for (ll i = 0; i < N; ++i) {
        if (midMap[high[i]] != 0) {
            combinations += midMap[high[i]];
            continue;
        }
        if (high[i] < prevMid) {
            combinations += midMap[prevHigh];
            continue;
        }
        vector<ll>::iterator midItr = lower_bound(middle.begin(), middle.end(), high[i], compare);
        if (midItr == middle.end()) {
            break;
        }
        prevHigh = high[i];
        prevMid = *midItr;

        vector<ll>::iterator itr;
        ll sum = 0;
        ll prevLow = 0;
        ll prevMid2 = 0;
        for (itr = midItr; itr != middle.end(); ++itr) {
            if (lowMap[*itr] != 0) {
                sum += lowMap[*itr];
                continue;
            }
            if (*itr < prevLow) {
                sum += lowMap[prevMid2];
                continue;
            }
            vector<ll>::iterator lowItr = lower_bound(low.begin(), low.end(), *itr, compare);
            if (lowItr == low.end()) {
                break;
            }
            prevMid2 = *itr;
            prevLow = *lowItr;
            ll dist = distance(lowItr, low.end());
            lowMap[*itr] = dist;
            sum += dist;
        }
        midMap[high[i]] = sum;
        combinations += sum;
    }
    cout << combinations << endl;
    return 0;
}