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):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007


def A():
    s = S()
    t = S()
    if s+t == t[::-1]+s[::-1]:
        print("YES")
    else:
        print("NO")


def B():
    n = I()
    for i in range(int(n**0.5)+2)[::-1]:
        if i*i <= n:
            print(i*i)
            quit()

def C():
    n = I()
    a = LI()
    b = LI()
    c = LI()
    q = [0 for i in range(n)]
    ans = 0
    a.sort()
    b.sort()
    c.sort()
    for i in range(n):
        j = bisect.bisect_left(a,b[i])
        q[i] = j
    for i in range(n-1):
        q[i+1] += q[i]
    q.insert(0,0)
    for i in range(n):
        j = bisect.bisect_left(b,c[i])
        ans += q[j]
    print(ans)

def D():
    return


def E():
    return


def F():
    return


def G():
    return


def H():
    return


if __name__ == "__main__":
    C()

Solution (Python):
import bisect as bs
N = int(input())
A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
C = sorted(list(map(int, input().split())))
ans = 0
for b in B:
    a = bs.bisect_left(A, b)
    c = N - bs.bisect_right(C, b)
    ans += a * c
print(ans)

Solution (Python):
import bisect

def main():
    n = int(input())
    a = sorted([int(s) for s in input().split()])
    b = sorted([int(s) for s in input().split()])
    c = sorted([int(s) for s in input().split()])
    print(solve(n, a, b, c))

def solve(n, a, b, c):
    d = [c, b, a]
    dp = []
    dp.append([n - i for i in range(n)])
    dp.append([0 for _ in range(n)])
    dp.append([0 for _ in range(n)])

    for i in range(1, 3):
        for j in reversed(range(n)):
            k = bisect.bisect(d[i-1], d[i][j])
            if k == n:
                continue

            dp[i][j] = dp[i-1][k]
            if j != n - 1:
                dp[i][j] += dp[i][j+1]

    return dp[2][0]


def ilen(it):
    c = 0
    for _ in it:
        c += 1
    return c

main()

Solution (Python):
import bisect
n = int(input())
p = [list(map(int, input().split())) for _ in range(3)]
for i in range(3):
    p[i].sort()
num = [[0 for _ in range(n)] for _ in range(2)]
ret = 0
for i in range(n):
    s = bisect.bisect_left(p[0], p[1][i])
    b = n - bisect.bisect_right(p[2], p[1][i])
    ret += s*b
print(ret)

Solution (Python):
import numpy as np
import bisect
import sys
input = sys.stdin.readline

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
len_C = len(C)
for b in B:
    a = bisect.bisect_left(A,b)
    c = len_C - bisect.bisect_right(C,b)
    ans += a*c

print(int(ans))

Solution (Python):
from bisect import *
n = int(input())
a = sorted(list(map(int,input().split())))
b = list(map(int,input().split()))
c = sorted(list(map(int,input().split())))
ans = 0
for i in range(n):
    l = bisect_left(a, b[i])
    r = n - bisect_right(c,b[i])
    ans += l*r
print(ans)

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

Solution (Python):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))
    X = []
    for i in range(N):
        X += [(A[i], 2), (B[i], 1), (C[i], 0)]
    X.sort()
    cnt = [0] * 4
    cnt[3] = 1
    for x in X:
        size, part = x
        cnt[part] += cnt[part+1]
    print(cnt[0])


if __name__ == "__main__":
    main()

Solution (Python):
import bisect

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

print(sum(bisect.bisect_left(A, B[j]) * (N - bisect.bisect(C, B[j])) for j in range(N)))

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())))
a, c, cnt = 0, 0, 0
for b in B:
  while a < N and A[a] < b:
    a += 1
  while c < N and C[c] <= b:
    c += 1
  cnt += a * (N-c)
print(cnt)

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()

ans = 0

a = 0
b = 0
c = 0

BB = []
j = 0
for i in range(n) :
    while j < n and A[j] < B[i] :
        j += 1
    BB.append(j)

for i in range(n-1) :
    BB[i+1] += BB[i]

j = 0
for i in range(n) :
    while j < n and B[j] < C[i] :
        j += 1
    if 0 <= j-1 < n :
        ans += BB[j-1]
    
print(ans)

Solution (Python):
import bisect
n = int(input())

a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))

ppp = [[0,0] for i in range(n)]

a.sort()
b.sort()
c.sort()

su = 0
for i in range(n):
    ppp[i][0] = bisect.bisect_left(a,b[i])
for i in range(n):
    ppp[i][1] = n-bisect.bisect_right(c,b[i])
for i in range(n):
    su += ppp[i][0]*ppp[i][1]
print(su)

Solution (Python):
from bisect import bisect_right
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()
b2c = [0 for _ in range(len(b))]
for i, bn in enumerate(b):
    b2c[i] = len(c) - bisect_right(c, bn)
sm = sum(b2c)
smb2c = [0 for _ in range(len(b))]
smb2c[0] = b2c[0]
for i, bn in enumerate(b2c):
    if i > 0:
        smb2c[i] += smb2c[i - 1] + bn
ans = 0
for i, an in enumerate(a):
    tmp = bisect_right(b, an)
    if(tmp > 0):
        ans += sm - smb2c[tmp - 1]
    else:
        ans += sm
print(ans)

Solution (Python):
import bisect

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 x in b:
    ans+=bisect.bisect_left(a,x)*(n-bisect.bisect_right(c,x))
print(ans)

Solution (Python):
import bisect
n = int(input()) 
A = sorted([int(i) for i in input().split()] )
B = sorted([int(i) for i in input().split()] )
C = sorted([int(i) for i in input().split()] )

b_count = []
pre_b = 0
for b in B:
    current_count = bisect.bisect_left(A,b)
    b_count.append(pre_b + current_count)
    pre_b += current_count

result = 0
for c in C:
    index = bisect.bisect_left(B,c)
    if index == 0:
        continue
    result += b_count[index-1]
    
print(result)

Solution (Python):
import bisect
N = int(input())
Alist = [int(x) for x in input().split()]
Blist = [int(x) for x in input().split()]
Clist = [int(x) for x in input().split()]

Alist.sort()
Blist.sort()
Clist.sort()

result = 0
for b in Blist:
    a = bisect.bisect_left(Alist, b)
    c = N - bisect.bisect_right(Clist, b)
    result += a*c

print(result)

Solution (Python):
import bisect

def main():
    N = int(input())
    A = sorted([int(i) for i in input().split()])
    B = sorted([int(i) for i in input().split()])
    C = sorted([int(i) for i in input().split()])

    counter = 0
    for b in B:
        a_count = bisect.bisect_left(A, b)
        c_count = N - bisect.bisect_right(C, b)
        counter += a_count*c_count
            

    print(counter)

if __name__ == "__main__":
    main()

Solution (Python):

def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(input())
def S(): return list(input())
n=I()
a=IL()
b=IL()
c=IL()
a.sort()
b.sort()
c.sort()
bc=[0]*(n+1)
j=0
for i in range(n):
    while j<n:
        if c[j]>b[i]:
            break
        else:
            j+=1
    bc[i]=n-j

ans=0
for i in range(2,n+1):
    bc[-i-1]+=bc[-i]

j=0
for i in range(n):
    while j<n:
        if b[j]>a[i]:
            break
        else:   
            j+=1
    ans+=bc[j]

print(ans)

Solution (Python):
import bisect

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 i in b:
  smaller = bisect.bisect_left(a, i)
  bigger = n - bisect.bisect(c, i)
  ans += smaller * bigger
print(ans)

Solution (Python):
import bisect
n=int(input())
a=sorted([int(i) for i in input().split()])
b=sorted([int(i) for i in input().split()])
c=sorted([int(i) for i in input().split()])
ans=0
for bi in b:
    ans+=bisect.bisect_left(a, bi)*(n-bisect.bisect_right(c, bi))
print(ans)

Solution (Python):

N=int(input())
A=list(map(int,input().split()))
A.sort()
B=list(map(int,input().split()))
B.sort()
C=list(map(int,input().split()))
C.sort()
ans=0
a=0
c=0
for b in B:
	if a!=N:
		while A[a]<b:
			a+=1
			if a==N:
				break
	if c!=N:
		while C[c]<=b:
			c+=1
			if c==N:
				break
	ans+=a*(N-c)
print(ans)

Solution (Python):
import bisect

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

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 bisect

n = int(input())

a = list(map(int , input().split()))
b=list(map(int , input().split()))
c=list(map(int , input().split()))

b.sort()
c.sort()

bsum=[0 for i in range(n)]

for i in reversed(range(n)):
    bsum[i] += (n - bisect.bisect_right(c, b[i]))
    if(i != (n-1)) :bsum[i] += bsum[i+1]

ans = 0
for aa in a:
    pos = bisect.bisect_right(b, aa)
    if (pos < n) :ans += bsum[pos]

print(ans)

Solution (Python):
import bisect
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
bsort = sorted(b)
csort = sorted(c)
res = 0
asum = [0] * n
bsum = [0] * n
for i in range(n):
    asum[i] = n - bisect.bisect_right(bsort, a[i])
for i in range(n):
    bsum[i] = n - bisect.bisect_right(csort, b[i])
bsum.sort()
total = 0
for i in range(n):
    total += bsum[i]
    bsum[i] = total
res = 0
for i in range(n):
    if asum[i] != 0:
        res += bsum[asum[i] -1]
print(res)

Solution (Python):
import bisect

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

cnt = 0
for b in B:
    cnt += bisect.bisect_left(A, b) * (N - bisect.bisect_right(C, b))
    
print(cnt)

Solution (Python):
import bisect as bi
n = int(input())
kariA = list(map(int,input().split()))
kariB = list(map(int,input().split()))
kariC = list(map(int,input().split()))
A = sorted(kariA)
B = sorted(kariB)
C = sorted(kariC)
count = 0
Clength = len(C)
for b in B:
    count += bi.bisect_left(A,b)*(Clength - bi.bisect_right(C,b))
print(count)

Solution (Python):
import bisect

n=int(input())
a=[int(i) for i in input().split()]
a.sort()
b=[int(i) for i in input().split()]
c=[int(i) for i in input().split()]
c.sort()


ans=0
for j in range(n):
    aa=bisect.bisect_left(a, b[j])
    bb=bisect.bisect_right(c, b[j])
    ans+=aa*(n-bb)

print(ans)

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

Solution (Python):
import bisect

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()

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

print(res)

Solution (Python):
import bisect as bi
n = int(input())
a = sorted(list(map(int,input().split())))
b = sorted(list(map(int,input().split())))
c = sorted(list(map(int,input().split())))
ans = 0
for i in range(len(b)):
        ans += bi.bisect_left(a,b[i]) *  (len(c) - bi.bisect_right(c,b[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()
a_cut=0
c_cut=0
ans=0
for b_cut in B:
    while a_cut<N and A[a_cut]<b_cut:
        a_cut+=1
    while c_cut<N and C[c_cut]<=b_cut:
        c_cut+=1
    ans+=a_cut*(N-c_cut)
print(ans)

Solution (Python):
from bisect import *
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()
print(sum([bisect_left(a,i)*(n-bisect_right(c,i)) for i in b]))

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()
 
 
from bisect import bisect_left,bisect_right
ans = 0
for i in b:
    count_a = bisect_left(a,i)
    count_c = n - bisect_right(c,i)
    ans += count_a * count_c
print(ans)

Solution (Python):
def comp(i,j):
    
    
    
    if len(i) < len(j):
        return 1
    elif len(i) > len(j):
        return -1
    for x in range(len(i)):
        if i[x] == j[x]:
            continue
        elif i[x] > j[x]:
            return -1
        else:
            return 1
    return 0



def bisect_left(array, i):
    __bisect_array = array
    def _bisect_left(i, left, right):
        n = right - left + 1
        mid = (left+right) // 2
        if n == 1:
            if comp(__bisect_array[mid],i) == 1:
                return 1
            else:
                return 0
        if comp(i, __bisect_array[mid]) >= 0:
            return _bisect_left(i,left,mid)
        else:
            return (mid-left+1) + _bisect_left(i,mid+1,right)
    return _bisect_left(i, 0, len(array)-1)

def bisect_right(array, i):
    __bisect_array = array
    def _bisect_right(i, left, right):
        n = right - left + 1
        mid = (right+left) // 2
        if n == 1:
            if comp(i, __bisect_array[mid]) == 1:
                return 0
            else:
                return 1
        if comp(__bisect_array[mid] ,i) >= 0:
            return  (mid-left+1) + _bisect_right(i,mid+1,right)
        else:
           return _bisect_right(i,left,mid)
    return _bisect_right(i, 0, len(array)-1)

N = int(input())
L = []
for _ in range(3):
    l = list(map(int,input().split()))
    l = list(map(str,sorted(l)))
    L.append(l)
ans = 0
for x in L[1]:
    n = bisect_left(L[0],x)
    m = bisect_right(L[2],x)
    m = N - m
    ans += n*m
print(ans)

Solution (Python):
def bisect_left(array, i):
    __bisect_array = array
    def _bisect_left(i, left, right):
        n = right - left + 1
        mid = (left+right) // 2
        if n == 1:
            if __bisect_array[mid] < i:
                return 1
            else:
                return 0
        if i <= __bisect_array[mid]:
            return _bisect_left(i,left,mid)
        else:
            return (mid-left+1) + _bisect_left(i,mid+1,right)
    return _bisect_left(i, 0, len(array)-1)

def bisect_right(array, i):
    __bisect_array = array
    def _bisect_right(i, left, right):
        n = right - left + 1
        mid = (right+left) // 2
        if n == 1:
            if i < __bisect_array[mid] :
                return 0
            else:
                return 1
        if __bisect_array[mid] <= i:
            return  (mid-left+1) + _bisect_right(i,mid+1,right)
        else:
           return _bisect_right(i,left,mid)
    return _bisect_right(i, 0, len(array)-1)

N = int(input())
L = []
for _ in range(3):
    l = list(map(int,input().split()))
    L.append(sorted(l))
ans = 0
for x in L[1]:
    n = bisect_left(L[0],x)
    m = bisect_right(L[2],x)
    m = N - m
    ans += n*m
print(ans)

Solution (Python):
def bisect_left(array, i, left, right):
    n = right - left + 1
    mid = (left+right) // 2
    if n == 1:
        if array[mid] < i:
            return 1
        else:
            return 0
    if i <= array[mid]:
        return bisect_left(array,i,left,mid)
    else:
        return (mid-left+1) + bisect_left(array,i,mid+1,right)

def bisect_right(array, i, left, right):
    n = right - left + 1
    mid = (right+left) // 2
    if n == 1:
        if i < array[mid] :
            return 0
        else:
            return 1
    if array[mid] <= i:
        return  (mid-left+1) + bisect_right(array,i,mid+1,right)
    else:
        return bisect_right(array,i,left,mid)

N = int(input())
L = []
for _ in range(3):
    l = list(map(int,input().split()))
    L.append(sorted(l))
ans = 0
for x in L[1]:
    n = bisect_left(L[0],x,0,N-1)
    m = bisect_right(L[2],x,0,N-1)
    m = N - m
    ans += n*m
print(ans)

Solution (Python):
N = int(input())
L = []
for _ in range(3):
    l = list(map(int,input().split()))
    L.append(sorted(l))
import bisect
ans = 0
for x in L[1]:
    n = bisect.bisect_left(L[0],x)
    m = bisect.bisect_right(L[2],x)
    m = N - m
    ans += n*m
print(ans)

Solution (Python):
N = int(input())
L = []
for _ in range(3):
    l = list(map(int,input().split()))
    L.append(sorted(l))
import bisect
ans = 0
for x in L[1]:
    n = bisect.bisect_left(L[0],x)
    m = bisect.bisect_right(L[2],x)
    m = N - m
    ans += n*m
print(ans)

Solution (Python):
import bisect
import numpy as np
import sys
input = sys.stdin.readline
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()


def nibutan(list,n):
	left = -1
	right = len(list)
	while left+1 != right:
		middle = (left+right)//2
		if list[middle] < n:
			left = middle
		else:
			right = middle
	return left+1

N = I()
A = sorted(IL())
B = sorted(IL())
C = sorted(IL())

dp = [0]*(N+1)

for i in range(N):
	dp[i] = nibutan(A,B[i])

num = 0
for i in range(N):
	num += dp[i]
	dp[i] = num

ans = 0
for i in range(N):
	ans += dp[nibutan(B,C[i])-1]

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())))
a, c, cnt = 0, 0, 0
for b in B:
  while a < N and A[a] < b:
    a += 1
  while c < N and C[c] <= b:
    c += 1
  cnt += a * (N - c)
print(cnt)

Solution (Python):
n = int(input())
a = [0] + list(map(int, input().split())) 
b = [0] + list(map(int, input().split()))
c = [0] + list(map(int, input().split()))
a.sort()
b.sort()
c.sort()
bp, cp = 1, 1
cana, canb = [0], [0] 
                      
sumb = [0] 
ans = 0
for i in range(1,n+1): 
    if bp != n+1:
        while b[bp] <= a[i] and bp != n+1:
            bp += 1
            if bp == n+1:
                break
    appending = n+1-bp if bp != n+1 else 0
    cana.append(appending)
for i in range(1,n+1):
    if cp != n+1:
        while c[cp] <= b[i] and cp != n+1:
            cp += 1
            if cp == n+1:
                break
    appending = n+1-cp if cp != n+1 else 0
    canb.append(appending)
for i in range(1,n+1):
    sumb.append(sumb[-1]+canb[-i])
for i in range(1,n+1):
    ans += sumb[cana[i]]
print(ans)

Solution (Python):


















from collections import defaultdict, Counter
from math import pi, sqrt
from bisect import bisect_left, bisect_right


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

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

cnt = 0
for bi in b:
    alen = bisect_left(a, bi)
    clen = bisect_right(c, bi)
    if alen > 0 and clen < len(c):
        cnt += alen * (n - clen)

print(cnt)

Solution (Python):
from bisect import bisect_left, bisect_right

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()
B.sort()
C.sort()

ans = 0

for b in B:
    i = bisect_left(A, b)
    j = bisect_right(C, b)
    if i - 1 >= 0 and N - j:
        ans += i * (N - j)

print(ans)

Solution (Python):
import bisect

n = int(input())
a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())))
c = sorted(list(map(int, input().split())))
a_n = [0] * n
b_n = [0] * n
for i in range(n):
    a_n[i] = bisect.bisect_right(b, a[i])
for i in range(n):
    b_n[i] = n - bisect.bisect_right(c, b[i])
ans = 0
b_n_t = [0]
for i in range(n):
    b_n_t.append(b_n[i] + b_n_t[i])
for i in a_n:
    ans += b_n_t[-1] - b_n_t[i]
print(ans)

Solution (Python):
def func1():
    i = 0
    j = 0
    p = [0] * N
    while True:
        if B[i] > A[j]:
            j += 1
            if j == N:
                while i < N:
                    p[i] = N
                    i += 1
                return p
        else:
            while B[i] <= A[j]:
                p[i] = j
                i += 1
                if i == N:
                    return p


def func2(p):
    i = 0
    j = 0
    point = 0
    q = [0] * N
    while True:
        if C[i] > B[j]:
            point += p[j]
            j += 1
            if j == N:
                while i < N:
                    q[i] = point
                    i += 1
                return q
        else:
            while C[i] <= B[j]:
                q[i] = point
                i += 1
                if i == N:
                    return q


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

A = sorted(A)
B = sorted(B)
C = sorted(C)

p = func1()
q = func2(p)
print(sum(q), '\n')

Solution (Python):
n = int(input())
a = sorted([int(i) for i in input().split()])
b = sorted([int(i) for i in input().split()])
c = sorted([int(i) for i in input().split()], reverse=True)

a.append(10**9)

check_a = 0
check_c = n - 1
ans = 0
for i in b:
    while a[check_a] < i and check_a < n :
        check_a += 1
    while c[check_c] <= i and check_c >= 0:
        check_c -= 1
    ans += check_a * (check_c + 1)
print(ans)

Solution (Python):
from bisect import bisect_left,bisect_right
n=int(input())
A=sorted(map(int,input().split()))
B=list(map(int,input().split()))
C=sorted(map(int,input().split()))
ans=0
for i in range(n):
    ans+=(bisect_left(A,B[i])*(n-bisect_right(C,B[i])))
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())))

a = 0
c = 0
ans = 0
for b in B:
  while a < N and A[a] < b:
    a += 1
  while c < N and C[c] <= b:
    c += 1
  ans += a * (N - c)
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())),reverse=True)
ab=[0 for _ in range(N)]
bc=[0 for _ in range(N)]

aind=0
cind=0
for i in range(N):
    b1=B[i]
    while aind<N and A[aind]<b1:
        aind+=1
    ab[i]=aind

    b2=B[-i-1]
    while cind<N and b2 < C[cind]:
        cind+=1
    bc[-i-1]=cind
print(sum([ab[i]*bc[i] for i in range(N)]))

Solution (Python):
from bisect import bisect_left, bisect_right
n = int(input())
*a, = sorted(map(int, input().split()))
*b, = sorted(map(int, input().split()))
*c, = sorted(map(int, input().split()))

ans = 0
for i in range(n):
    ans += (bisect_left(a, b[i])*(n-bisect_right(c, b[i])))
print(ans)

Solution (Python):
from bisect import bisect_left, bisect_right
n = int(input())
*a, = sorted(map(int, input().split()))
*b, = sorted(map(int, input().split()))
*c, = sorted(map(int, input().split()))

ans = 0
for i in range(n):
    ans += (bisect_left(a, b[i])*(n-bisect_right(c, b[i])))
print(ans)

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()

i = 0
j = 0
ans = 0

for b in B:
    while i < N and A[i] < b:
        i += 1
    while j < N and C[j] <= b:
        j += 1
    ans += i * (N - j)

print(ans)

Solution (Python):



N=int(raw_input())
A=sorted(map(int,raw_input().split()))
B=sorted(map(int,raw_input().split()))
C=sorted(map(int,raw_input().split()))

x=0
z=0
res=0

for y in B:
    while x<N and A[x]<y:
        x+=1
    while z<N and C[z]<=y:   
        z+=1
    res+=x*(N-z)   
print res

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

ans = 0
for i in range(N):
    a = bisect.bisect_left(A,B[i])
    c = bisect.bisect_right(C,B[i])
    ans += a*(len(C)-c)
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(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
c_cnt = [1 for i in range(N)]

def count(A, B, b_cnt):
    B.append(-1)
    a_cnt = [0 for i in range(N)]
    i, j = 0, 0
    while i < N:
        if A[i] < B[j]:
            a_cnt[i] += b_cnt[j]
            j += 1
        else:
            i += 1
            if 1 <= i < N:
                a_cnt[i] = a_cnt[i-1]
    return a_cnt

b_cnt = count(B, C, c_cnt)
a_cnt = count(A, B, b_cnt)
print(sum(a_cnt))

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(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
c_cnt = [1 for i in range(N)]

def count(A, B, b_cnt):
    B.append(-1)
    a_cnt = [0 for i in range(N)]
    i, j = 0, 0
    while i < N:
        if A[i] < B[j]:
            a_cnt[i] += b_cnt[j]
            j += 1
        else:
            i += 1
            if 1 <= i < N:
                a_cnt[i] = a_cnt[i-1]
    return a_cnt

b_cnt = count(B, C, c_cnt)
a_cnt = count(A, B, b_cnt)
print(sum(a_cnt))

Solution (Python):
n = int(input())
u = list(map(int,input().split()))
m = list(map(int,input().split()))
d = list(map(int,input().split()))
u.sort()
m.sort()
d.sort()

ucnt = 0
dcnt = 0
ans = 0
for i in range(n):
    for j in range(ucnt,n):
        if u[j] < m[i]:
            ucnt += 1
        else:
            break
        
    for j in range(dcnt,n):
        if d[j] <= m[i]:
            dcnt += 1
        else:
            break

    ans += ucnt * (n-dcnt)
    
print(ans)

Solution (Python):
import bisect

n = int(input())
u = list(map(int,input().split()))
m = list(map(int,input().split()))
d = list(map(int,input().split()))
u.sort()
m.sort()
d.sort()

ans = 0
for a in m:
    ans += bisect.bisect_left(u,a) * (n - bisect.bisect_right(d,a))
print(ans)

Solution (Python):
import bisect

n = int(input())
A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
C = sorted(list(map(int, input().split())))

ans = 0
for b in B:
    ans += (bisect.bisect_left(A, b)) * (n - bisect.bisect_right(C, b))
    
print(ans)

Solution (Python):
import bisect

n = int(input())
A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
C = sorted(list(map(int, input().split())))

ans = 0
for b in B:
    x = bisect.bisect_left(A, b)
    y = bisect.bisect_right(C, b)
    ans += x * (n - y)
    
print(ans)

Solution (Python):
import bisect

n = int(input())
A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
C = sorted(list(map(int, input().split())))

ans = 0
for b in B:
    ans += bisect.bisect_left(A, b) * (n - bisect.bisect_right(C, b))
    
print(ans)

Solution (Python):
import bisect
n = int(input())
al = [int(a) for a in input().split()]
bl = [int(b) for b in input().split()]
cl = [int(c) for c in input().split()]

al.sort()
bl.sort()
cl.sort()

k = 0
for b in bl:
    k += bisect.bisect_left(al, b) * (n - bisect.bisect_right(cl, b))
print(k)

Solution (Python):

 
import bisect
 
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()
 
Ac, Cc = [0]*n, [0]*n

 
for i in range(n):
    Ac[i] = bisect.bisect_left(A, B[i])
    
    Cc[i] = n - bisect.bisect_right(C, B[i])
    
 
 
count = 0
for i in range(n):
    count += Ac[i]*Cc[i]
 
print(count)

Solution (Python):
import bisect
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()

ba = [None]*N
bc = [None]*N

for i in range(N):
    ba[i] = bisect.bisect_left(A, B[i])
for i in range(N):
    bc[i] = bisect.bisect_right(C, B[i])

ans = 0
for i in range(N):
    ans += ba[i]*(N-bc[i])
print(ans)

Solution (Python):
def lma():return list(map(int, input().split()))
a = int(input())
k=[]
s=[]
c=0
s= [lma()]
x2= lma()
x3 = lma()
s.append(x2)
s.append(x3)
res =0
r =0
for j in range(3):
    for i in range(a):
        k.append([j,s[j][i]])
k.sort(key=lambda x: x[1], reverse=True)
for i in range(a*3):
    if k[i][0] == 2:
       c+=1
    elif k[i][0] == 1:
       r+=c 
    else:
       
        res+=r
print(res)

Solution (Python):
import bisect
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 item in B:
    ans += bisect.bisect_left(A,item) * (n - bisect.bisect_right(C,item))

print(ans)

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

def bis(x,y):
    low = 0
    high = N
    while low < high:
        mid = (low + high) // 2
        if x[mid] < y: low = mid + 1
        else: high = mid
    return low

total = 0
for b in B:
    total += bis(A,b) * (N - bis(C,b))

print(total)