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 (Python):
def C_SnukeFestival(N, A, B, C):
    a = sorted(A)
    b = sorted(B)
    c = sorted(C)

    import bisect
    ans = 0
    for j in range(N):
        
        i = bisect.bisect_left(a, b[j])
        
        k = N - bisect.bisect_right(c, b[j])
        ans += i * k
    return ans

N = int(input())
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]
C = [int(_) for _ in input().split()]
print(C_SnukeFestival(N, A, B, C))

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]	
B = [int(i) for i in input().split()]	
C = [int(i) for i in input().split()]	

A.sort()
B.sort()
C.sort()






ab=[0]*N
bc=[0]*N

b=N-1
c=N-1
count=0
while (0<=b):
	while (0<=c):
		if B[b]<C[c]:
			count += 1
			c -= 1
		else:
			break
	bc[b] = count
	if b<N-1:
		bc[b] += bc[b+1]
	b -= 1

a=N-1
b=N-1
count=0
while (0<=a):
	while (0<=b):
		if A[a]<B[b]:
			count += 1
			b -= 1
		else:
			break
	ab[a] = count
	a -= 1





ans=0

for i in ab:


	if i>0:
		ans += bc[N-i]

print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort()
B.sort()
C.sort()

Bcon = [0] * (N + 1)
res = 0
p = 0
for n in range(N):
    while p <= N - 1 and B[n] > A[p]:
        p += 1
    Bcon[n + 1] = p + Bcon[n]
    
p = 0
for n in range(N):
    while p <= N - 1 and C[n] > B[p]:
        p += 1
    res += Bcon[p]
    
print(res)

Solution (Python):
N = int(input())
A = [int(x) for x in input().split(' ')]
B = [int(x) for x in input().split(' ')]
C = [int(x) for x in input().split(' ')]
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

M = [0] * N
index = -1
for i in range(N):
    if i != 0:
        M[i] += M[i - 1]

    inc = 0
    for j in range(index, N - 1):
        if C[j + 1] <= B[i]:
            break
        inc += 1
    M[i] += inc
    index += inc

T = [0] * N
index = -1
for i in range(N):
    if i != 0:
        T[i] += T[i - 1]
    inc = 0
    for j in range(index, N - 1):
        if B[j + 1] <= A[i]:
            break
        inc += 1
    T[i] += sum(M[index + 1: index + inc + 1])
    index += inc
print(sum(T))

Solution (Python):

from sys import stdin, stdout
from bisect import bisect
def rint():
   return map(int, stdin.readline().split())

n = int(input())

a = list(rint())
b = list(rint())
c = list(rint())
a.sort()
b.sort()
c.sort()
bc = [0 for _ in range(n)]
cc = [0 for _ in range(n)]

ci = 0
for i in range(n):
   ii = bisect(b, a[i])
   if ii >= n:
      for iii in range(ci+1, ii):
         bc[iii] += bc[ci]
      ci = n
      break
   bc[ii] += 1
   for iii in range(ci+1, ii+1):
      bc[iii] += bc[ci]
   ci = ii
if ci < n-1:
   for iii in range(ci+1, n):
      bc[iii] += bc[ci]
ci = 0
for i in range(n):
   ii = bisect(c, b[i])
   if ii >= n:
      for iii in range(ci+1, ii):
         cc[iii] += cc[ci]
      ci = n
      break
   cc[ii] += bc[i]
   for iii in range(ci+1, ii+1):
      cc[iii] += cc[ci]
   ci = ii

if ci < n-1:
   for iii in range(ci+1, n):
      cc[iii] += cc[ci]

print(sum(cc))

Solution (Python):
inf=10**9+1

n=int(input())
array_a=sorted([a for a in map(int, input().split())])+[inf]
array_b=sorted([a for a in map(int, input().split())])+[inf]
array_c=sorted([a for a in map(int, input().split())])

nums_b=[0 for _ in range(n)]
cur=0
for (idx,b) in enumerate(array_b[:-1]):
    while(array_a[cur]<b):
        cur+=1
    nums_b[idx]=cur


cur=0
res=0
for (idx,c) in enumerate(array_c):
    while(array_b[cur]<c):
        res+=nums_b[cur]*(n-idx)
        cur+=1
print(res)

Solution (Python):
import math
from math import modf

n = int(input())
A = input()
B = input()
C = input()
a = [(int(i) + 0.1) for i in A.split()]
b = [int(i) for i in B.split()]
c = [(int(i) - 0.1) for i in C.split()]
al = sorted(a + b + c)
ans = 0
a_count = 0
c_count = n
for i in al:
    decimal, integer = modf(i)
    if decimal > 0.8:
        c_count -= 1
    elif decimal > 0.05:
        a_count += 1
    else:
        ans += a_count * c_count
print(ans)

Solution (Python):
def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))
    
    A.sort()
    B.sort()
    C.sort()
    
    A_cand = [0 for i in range(N)]
    B_cand = [0 for i in range(N)]
    
    skip_loop = 0
    for i in range(N):
        cnt = skip_loop
        for j in range(N-skip_loop):
            if B[N-i-1] < C[N-skip_loop-j-1]:
                cnt += 1
            else:
                break
        B_cand[N-i-1] = cnt
        skip_loop = cnt
    
    skip_loop = 0
    ans = 0
    pre_loop_ans = 0
    for i in range(N):
        cnt = skip_loop
        ans += pre_loop_ans
        for j in range(N-skip_loop):
            if A[N-i-1] < B[N-skip_loop-j-1]:
                ans += B_cand[N-skip_loop-j-1]
                pre_loop_ans += B_cand[N-skip_loop-j-1]
                cnt += 1
            else:
                break
        A_cand[N-i-1] = cnt
        skip_loop = cnt
    print(ans)
    

solve()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

a.sort()
b.sort()
c.sort()
b_ceil = [n]*len(b)
b_ceil.insert(0, 0)
b_floor = [n]*len(b)
b_floor.insert(0, 0)
count = 0
for i in range(len(b)):
    for j in range(b_ceil[i], len(a)):
        if a[j] >= b[i]:
            b_ceil[i+1] = j
            break
    for k in range(b_floor[i], len(c)):
        if b[i] < c[k]:
            b_floor[i+1] = k
            break
    count += b_ceil[i+1] * (n - b_floor[i+1])

print(count)

Solution (Python):
N = int(input())
A = sorted(map(int, input().split()))
B = sorted(map(int, input().split()))
C = sorted(map(int, input().split()))
import bisect
ans = 0
for b in B:
    i = bisect.bisect_left(A,b)
    j = bisect.bisect_right(C,b)
    ans += i * (N-j)

print(ans)

Solution (Python):
N = int(input())
A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
C = sorted(list(map(int, input().split())))
BA = []
BC = []
x = 0
for i in range(N):
	while True:
		if A[x] < B[i]:
			if x == N - 1:
				BA.append(N)
				break
			else:
				x += 1
		else:
			BA.append(x)
			break
x = 0
for i in range(N):
	while True:
		if C[N - 1 - x] > B[N - 1 - i]:
			if x == N - 1:
				BC.append(N)
				break
			else:
				x += 1
		else:
			BC.append(x)
			break
BC = BC[::-1]

S = 0
for i in range(N):
	S += BA[i] * BC[i]

print(S)

Solution (Python):





from sys import stdin

def solve(As, Bs, Cs):
    As.sort()
    Bs.sort()
    Cs.sort()
    
    
    j = 0
    for i in range(len(Bs)):
        while j < len(Cs) and Bs[i] >= Cs[j]: j += 1
        Bs[i] = (Bs[i], len(Cs) - j)
    
    for i in range(len(Bs) - 1, 0, -1):
        Bs[i-1] = (Bs[i-1][0], Bs[i-1][1] + Bs[i][1])
    
    
    j = 0
    for i in range(len(As)):
        while j < len(Bs) and As[i] >= Bs[j][0]: j += 1
        As[i] = Bs[j][1] if j < len(Bs) else 0
    return sum(As)

N = int(stdin.readline())  
As = [int(w) for w in stdin.readline().split()]
Bs = [int(w) for w in stdin.readline().split()]
Cs = [int(w) for w in stdin.readline().split()]
if len(As) != N or len(Bs) != N or len(Cs) != N: raise
print(solve(As, Bs, Cs))

Solution (Python):
import bisect

if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))

    A.sort()
    C.sort()
    ans = 0
    for b in B:
        a_num = bisect.bisect_left(A, b)
        c_num = N - bisect.bisect_right(C, b)
        ans += a_num * c_num

    print(ans)

Solution (Python):
import sys
import math

N = int(input())
A = list(map(int, input().split(' ')))
B = list(map(int, input().split(' ')))
C = list(map(int, input().split(' ')))

A.sort()
B.sort()
C.sort()

array1 = list()
count = 0
f = 0
for j in range(N):
    for i in range(f, N):
        if B[N-1-j] < C[N-1-i]:
            count += 1
            if i == N-1:
                f = N
        else:
            f = i
            break
    array1.append(count)
ans = 0
f = 0
count = 0
for i in range(N):
    for j in range(f, N):
        if B[i] > A[j]:
            count += 1
            if j == N-1:
                f = N
        else:
            f = j
            break
    ans += count * array1[N-1-i]
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
c = 0
Acount = [0] * len(A)
Bcount = [0] * len(B)
count = 0
A = sorted(A, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)

for b in range(len(B)):
    Bcount[b] = count
    if c < len(C):
        while B[b] < C[c]:
            c += 1
            Bcount[b] += 1
            if c == len(C):
                break
        count = Bcount[b]
b = 0
count = 0
for a in range(len(A)):
    Acount[a] = count
    if b < len(B):
        while A[a] < B[b]:
            Acount[a] += Bcount[b]
            b += 1
            if b == len(B):
                break
        count = Acount[a]

print(sum(Acount))

Solution (Python):
x = int(raw_input())
a = map(int, raw_input().split())
b = map(int, raw_input().split())
c = map(int, raw_input().split())

a = sorted(a)
b = sorted(b)
c = sorted(c)

def get_res(arr_u, arr_l, arr_res):
    ind_u, ind_l = 0, 0
    acc = 0
    res = []

    while ind_l < len(arr_l):
        while ind_u < len(arr_u) and arr_u[ind_u] < arr_l[ind_l]:
            acc += arr_res[ind_u]
            ind_u += 1

        while ind_l < len(arr_u) and (ind_u == len(arr_u) or arr_u[ind_u] >= arr_l[ind_l]):
            res.append(acc)
            ind_l += 1

    return res

res_b = get_res(a, b, [1,]*len(b))
res_c = get_res(b, c, res_b)
print sum(res_c)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

A.sort()
B.sort()
C.sort()
C.reverse()

ans = 0
ai = 0
ci = N-1
for b in B:
    while ai < N and A[ai] < b:
        ai += 1
    while ci >= 0 and C[ci] <= b:
        ci -= 1
    ans += ai * (ci+1)

print(ans)

Solution (Python):
import bisect

def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    c = list(map(int, input().split()))
    a.sort()
    b.sort()
    c.sort()
    ans = 0
    for y in b:
        i = bisect.bisect_left(a, y)
        j = bisect.bisect_right(c, y)
        if i > 0 and j != len(c):
            ans += i * (n - j)

    print(ans)

main()