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 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<stdio.h>
#include<stdlib.h>


#define SENTINEL 1000000000

int count = 0;

void mergeSort(int A[], int left, int right) {
	int i, mid;
	if (left + 1 < right) {
		mid = (left + right) / 2;
		mergeSort(A, left, mid);
		mergeSort(A, mid, right);
		merge(A, left, mid, right);
	}
}



void merge(int A[], int left, int mid, int right) {
	int n1, n2, i, j, k;
	int *L, *R;
	n1 = mid - left;
	n2 = right - mid;
	L = (int *)malloc(sizeof(int)*(n1 + 1));
	R = (int *)malloc(sizeof(int)*(n2 + 1));
	for (i = 0; i <= n1 - 1; i++) {
		L[i] = A[left + i];
	}
	for (j = 0; j <= n2 - 1; j++) {
		R[j] = A[mid + j];
	}
	L[n1] = SENTINEL;
	R[n2] = SENTINEL;
	i = 0;
	j = 0;
	for (k = left; k <= right - 1; k++) {
		if (L[i] <= R[j]) {
			A[k] = L[i];
			i++;
			count++;
		}
		else {
			A[k] = R[j];
			j++;
			count++;
		}
	}
	free(L);
	free(R);
}


main() {
	int A[500000];
	int B[500000];
	int C[500000];
	long long int result=0;
	long long int num[500000];
	long long int sum[500000];
	long long int sum2[500000];
	long long int num2[500000];
	int n, k=0;

	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &A[i]);
	}
	for (int i = 0; i < n; i++) {
		scanf("%d", &B[i]);
	}
	for (int i = 0; i < n; i++) {
		scanf("%d", &C[i]);
	}
	mergeSort(A, 0, n);
	mergeSort(B, 0, n);
	mergeSort(C, 0, n);
	for (long long int i = 0; i < n; i++) {
		for (long long int j = k; j < n; j++) {
			if (B[i] < C[j]) {
				
					num[i] = n - j ;
					if (i == 0) {
						sum[i] = num[i];
					}
					else {
						sum[i] = sum[i - 1] + num[i];
					}
				break;
			}
          if(j==n-1){
          if (i == 0) {
						sum[i] = num[i];
					}
					else {
						sum[i] = sum[i - 1] + num[i];
					}
          }
			k++;
		}
	}
	k = 0;
	for (long long int i = 0; i < n; i++) {
		for (long long int j = k; j < n; j++) {
			if (A[i] < B[j]) {
				if (j == 0) {
					num2[i] = sum[n - 1];
				}
				else {
					num2[i] = sum[n - 1] - sum[j - 1];
				}
				if (i == 0) {
					sum2[i] = sum[n-1];
                  
				}
				else { sum2[i] = num2[i] + sum2[i - 1]; }
                
				break;
			}
          if(j==n-1){sum2[i] = num2[i] + sum2[i - 1];}
			k++;
		}
	}
	printf("%lld", sum2[n-1]);
	return 0;
}