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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <cstdio>

int a[120000], b[120000], c[120000];

int n;
unsigned long long ans;

void sort(int *a, int n) {
	for(int i = n - 1; i > 0; --i) {
		int k = 0, tmp = a[i];
		a[i] = a[0];
		a[0] = tmp;
		for(;;)
			if(k * 2 + 2 < i)
				if(tmp < a[k * 2 + 1])
					if(a[k * 2 + 1] > a[k * 2 + 2]) { SWAP_1: a[k] = a[k * 2 + 1]; a[k = k * 2 + 1] = tmp; }
					else { SWAP_2: a[k] = a[k * 2 + 2]; a[k = k * 2 + 2] = tmp; }
				else if(tmp < a[k * 2 + 2]) goto SWAP_2;
				else break;
			else if(k * 2 + 2 == i)
				if(tmp < a[k * 2 + 1]) goto SWAP_1;
				else break;
			else break;
	}
}

void bs(int l = 0, int r = n, int a_left = 0, int a_right = n, int c_left = 0, int c_right = n) {
	if(l < r) {
		int al = a_left, ar = a_right;
		while(al < ar)
			if(a[(al + ar - 1) / 2] < b[(l + r - 1) / 2]) al = (al + ar + 1) / 2;
			else ar = (al + ar - 1) / 2;
		int cl = c_left, cr = c_right;
		while(cl < cr)
			if(c[(cl + cr) / 2] > b[(l + r - 1) / 2]) cr = (cl + cr) / 2;
			else cl = (cl + cr + 2) / 2;
		ans += (unsigned long long)ar * (n - cr);
		bs(l, (l + r - 1) / 2, a_left, ar, c_left, cr);
		bs((l + r + 1) / 2, r, ar, a_right, cr, c_right);
	}
}

int main() {
	scanf("%d", &n);
	for(int i = 0; i < n; ++i) {
		scanf("%d", a + i);
		for(int j = i, ia = a[i]; ia > a[(j - 1) / 2]; a[j = (j - 1) / 2] = ia) a[j] = a[(j - 1) / 2];
	}
	for(int i = 0; i < n; ++i) {
		scanf("%d", b + i);
		for(int j = i, ib = b[i]; ib > b[(j - 1) / 2]; b[j = (j - 1) / 2] = ib) b[j] = b[(j - 1) / 2];
	}
	for(int i = 0; i < n; ++i) {
		scanf("%d", c + i);
		for(int j = i, ic = c[i]; ic > c[(j - 1) / 2]; c[j = (j - 1) / 2] = ic) c[j] = c[(j - 1) / 2];
	}
	sort(a, n);
	sort(b, n);
	sort(c, n);
	bs();
	printf("%llu\n", ans);
}