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):
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(reverse=1)
B.sort(reverse=1)
C.sort(reverse=1)

ans = 0
before_b = 0
before_c = 0
mem = 0

for a in range(N):
    ans += mem
    for b in range(before_b, N):
        if A[a] < B[b]:
            for c in range(before_c, N):
                if B[b] >= C[c]:
                    before_c = c
                    c -= 1
                    break
                before_c = N

            ans += c + 1
            mem += c + 1
        else:
            before_b = b
            break
        before_b = b + 1


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

Solution (Python):
from bisect import bisect_right as b_r,bisect_left as b_l

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

sum = 0
for b in B:
    sum += b_l(A,b) * (N - b_r(C,b))
print(sum)

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())))
print(sum(bisect.bisect_left(A,b)*(n-bisect.bisect_right(C,b)) for b in B))

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


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

Solution (Python):
n,a=int(input()),[sorted(list(map(int,input().split())))for i in range(3)]
b=[[],[]]
x=0
for i in range(n):
  while a[2][i]>a[1][x]:
    b[1].append(n-i)
    x+=1
    if x==n:break
  if x==n:break
for i in range(n-len(b[1])):b[1].append(0)
for i in range(1,n):b[1][n-i-1]+=b[1][n-i]
x=0
for i in range(n):
  while a[1][i]>a[0][x]:
    b[0].append(b[1][i])
    x+=1
    if x==n:break
  if x==n:break
print(sum(b[0]))

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

for i in range(N):
    count_A = bisect.bisect_left(A,B[i])
    count_C = bisect.bisect_right(C,B[i])
    ans += count_A * (N-count_C)
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()

a2=[]
for i1 in range(N):
    a2.append(bisect.bisect_left(C,B[i1]+1))

a2c=[0]*(N+1)
for i1 in range(N-1,-1,-1):
    a2c[i1] = a2c[i1+1] + N - a2[i1]

ans=0
for i0 in range(N):
    a1 = bisect.bisect_left(B,A[i0]+1)
    ans += a2c[a1]
print(ans)

Solution (Python):
import bisect
from itertools import accumulate

n = int(input())
a_s = sorted([int(item) for item in input().split()])
b_s = sorted([int(item) for item in input().split()])
c_s = sorted([int(item) for item in input().split()])

count = [0] * n
for i, b in enumerate(b_s):
    count[i] = bisect.bisect_left(a_s, b)
cumsum = list(accumulate(count))

ans = 0
for i, c in enumerate(c_s):
    index = (bisect.bisect_left(b_s, c)-1)
    if index >= 0:
        ans += cumsum[index]

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

count=0
for b in B:
    a=bisect.bisect_left(A,b)
    c_=bisect.bisect_right(C,b)
    c=N-c_
    count+=a*c
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()

ans = 0
for b in B:
    i_a = bisect.bisect_left(A, b)
    i_c = bisect.bisect_right(C, b)
    ans += i_a * (N - i_c)
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()
C.sort()

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

print(ans)

Solution (Python):
from sys import exit
import bisect

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



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

for j in b:
   x=bisect.bisect_left(a,j)
   y=bisect.bisect_right(c,j)
   count+=x*(N-y)
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()
c.sort()

ans = 0
for i in b:
    k = bisect.bisect_left(a, i)
    l = bisect.bisect_right(c, i)
    ans += k * (n - l)
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()

dpA = [0]*n
dpB = [0]*n

for i in range(n):
    dpB[i] = n-bisect.bisect_right(C,B[i])
for i in range(n-2,-1,-1):
    dpB[i] += dpB[i+1] 
for i in range(n):
    x = bisect.bisect_right(B,A[i])   
    if x == n:
        dpA[i] = 0
    else:
        dpA[i] = dpB[x]

print(sum(dpA))

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()
sum=0
for i in range(n):
    ans_a=bisect.bisect_left(a,b[i])
    ans_c=n-bisect.bisect_left(c,b[i]+1)
    sum+=ans_a*ans_c
print(sum)

Solution (Python):
def solve(string):
    n, *abc = map(int, string.split())
    a, b, c = map(sorted, [abc[i*n:(i+1)*n] for i in range(3)])
    ai = ci = ans = 0
    for _b in b:
        while ai < n and a[ai] < _b:
            ai += 1
        while ci < n and c[ci] <= _b:
            ci += 1
        ans += ai * (n - ci)
    return str(ans)


if __name__ == '__main__':
    n = int(input())
    print(solve('{}\n'.format(n) + '\n'.join([input() for _ in range(3)])))

Solution (Python):
def solve(string):
    n, *abc = map(int, string.split())
    a, b, c = sorted(abc[:n]), sorted(abc[n:2 * n]), sorted(abc[2 * n:])
    ai = ci = ans = 0
    for _b in b:
        while ai < n and a[ai] < _b:
            ai += 1
        while ci < n and c[ci] <= _b:
            ci += 1
        ans += ai * (n - ci)
    return str(ans)


if __name__ == '__main__':
    n = int(input())
    print(solve('{}\n'.format(n) + '\n'.join([input() for _ in range(3)])))

Solution (Python):
def solve(string):
    n, *abc = map(int, string.split())
    a = sorted(abc[:n], reverse=True)
    b = sorted(abc[n:2 * n], reverse=True)
    c = sorted(abc[2 * n:], reverse=True)
    ab = [0] * n
    j = 0
    for i, _a in enumerate(a):
        while j <= n - 1 and _a < b[j]:
            j += 1
        ab[i] = j
    bc = [0] * (n + 1)
    j = 0
    for i, _b in enumerate(b, start=1):
        while j <= n - 1 and _b < c[j]:
            j += 1
        bc[i] = j

    for i in range(1, len(bc)):
        bc[i] += bc[i - 1]
    return str(sum([bc[_ab] for _ab in ab]))


if __name__ == '__main__':
    n = int(input())
    print(solve('{}\n'.format(n) + '\n'.join([input() for _ in range(3)])))

Solution (Python):
import bisect
N = int(input())
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
C = [int(c) for c in input().split()]

A_sorted = sorted(A)
B_sorted = sorted(B)
C_sorted = sorted(C)

ans = 0
for b in B_sorted:
    
    a_count = bisect.bisect_left(A_sorted, b)
    
    
    c_count = N - bisect.bisect_right(C_sorted, b)
    
    ans += a_count * c_count

print(ans)

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

A_sorted = sorted(A)
B_sorted = sorted(B)
C_sorted = sorted(C)

ans = 0
for b in B_sorted:
    
    left = -1
    right = N 
    while right - left > 1:
        mid = left + (right - left) // 2
        if A_sorted[mid] < b:
            left = mid
        else:
            right = mid
    a_count = right
    
    left = -1
    right = N 
    while right - left > 1:
        mid = left + (right - left) // 2
        if C_sorted[mid] > b:
            right = mid
        else:
            left = mid
    c_count = N - right
    
    ans += a_count * c_count

print(ans)

Solution (Python):

import copy
import numpy as np
import random
import time
import bisect

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

sum = 0

for i in range(N):
    a = bisect.bisect_left(A,B[i])

    c = N-bisect.bisect_right(C,B[i])
    

    sum += a*c

print(sum)

Solution (Python):
N = int(input())
An = sorted(list(map(int, input().split())))
Bn = sorted(list(map(int, input().split())))
Cn = sorted(list(map(int, input().split())))

import bisect
total = 0
for B in Bn:
    ai = bisect.bisect_left(An, B)  
    ci = bisect.bisect_right(Cn, B) 
    total += ai*(N-ci)
print(total)

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()
Bpatan=[0]*N
Bpatan[0]=bisect.bisect_left(A,B[0])*(N-bisect.bisect_right(C,B[0]))
for i in range(1,N):
    Bpatan[i]=Bpatan[i-1]+bisect.bisect_left(A,B[i])*(N-bisect.bisect_right(C,B[i]))
print(Bpatan[N-1])

Solution (Python):
N = int(input())
A = sorted(list(map(int, input().split())))+[1e9+1]
B = sorted(list(map(int, input().split())))+[1e9+1]
C = sorted(list(map(int, input().split())))

i=0
j=0
k=0

NA=list(range(1,N+1))+[0]
NB=[0]*N+[0]
NC=[0]*N+[0]

while j<N:
  while A[i]<B[j]:
    i+=1
  NB[j]=NA[i-1]+NB[j-1]
  j+=1

j=0
while k<N:
  while B[j]<C[k]:
    j+=1
  NC[k]=NB[j-1]+NC[k-1]
  k+=1


    
print(NC[-2])

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()
def binary_search1(x,sortedlist):
  left=-1
  right=len(sortedlist)
  while abs(right-left)>1:
    mid=(left+right)//2
    if sortedlist[mid]<=x:
      left=mid
    else:
      right=mid
  return right
def binary_search2(x,sortedlist):
  left=-1
  right=len(sortedlist)
  while abs(right-left)>1:
    mid=(left+right)//2
    if sortedlist[mid]>=x:
      right=mid
    else:
      left=mid
  return left
ans=0
for i in range(len(b)):
  ans+=(len(c)-binary_search1(b[i],c))*(binary_search2(b[i],a)+1)
print(ans)

Solution (Python):
import sys, bisect
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))

N = read_int()[0]
A = sorted(read_int())
B = sorted(read_int())
C = sorted(read_int())

def solve():
    ans = 0
    
    memo = [0 for _ in range(N + 1)]
    for i in range(N - 1, -1, -1):
        lbc = bisect.bisect_right(C,B[i])
        memo[i] += N - lbc
        if i > 0:
            memo[i - 1] = memo[i]
    for i in range(N):
        lbb = bisect.bisect_right(B,A[i])
        ans += memo[lbb]
    return ans


if __name__ == "__main__":
    print(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()
import bisect
ans = 0
for j in range(len(b)):
    i = bisect.bisect_left(a,b[j])
    k = bisect.bisect_right(c,b[j])
    ans += i*(n-k)
print(ans)

Solution (Python):
import itertools
import math
from collections import deque
from collections import defaultdict
from itertools import permutations
import heapq
import bisect
INF = float("inf")

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 range(N):
    l = bisect.bisect_left(A,B[i])
    r = bisect.bisect_right(C,B[i])
    ans += l*(N-r)

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 = bisect.bisect_left(A, b)
    c = N-bisect.bisect_right(C, b)
    ans += a * c
    
print(ans)

Solution (Python):

import bisect

def solve(N,A,B,C):

    
    
    
    A,B,C=list(map(int, A)),list(map(int, B)),list(map(int, C))
    A=sorted(A)
    B=sorted(B)
    C=sorted(C)
    
    
    
    D = [0]*N
    
    
    
    
    
    
    
    

    for i in range(N):
        b = B[i]
        D[i]=bisect.bisect_left(A,b)*(N-bisect.bisect_right(C,b))
        
        
    print(sum(D))
    
    
    
    
    

    

    
 
 
def main():
    N = int(input())
    
    A=input().rstrip().split()
    B=input().rstrip().split()
    C=input().rstrip().split()
    solve(N,A,B,C)

 
 
if __name__ == '__main__':
    main()

Solution (Python):


import itertools
import collections
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 b in B:
        i = bisect.bisect_left(A, b)
        j = bisect.bisect_right(C, b)

        ans += i * (N - j)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def biserch_min(l,key):
    left = 0; right = len(l)
    while left < right:
        mid = (left+right)//2
        if l[mid] < key:
            left = mid+1
        else:
            right = mid
    return left

def biserch_max(l,key):
    left = 0; right = len(l)
    while left < right:
        mid = (left+right)//2
        if key < l[mid]:
            right = mid
        else:
            left = mid+1
    return right


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:
    top = biserch_min(A,b)
    bottom = N - biserch_max(C,b)
    ans += top*bottom
print(ans)

Solution (Python):
def biserch(l,key,flag=True):
    if flag is True:
        func = judge_1
    else:
        func = judge_2
    left = -1; right = len(l)
    while right - left > 1:
        middle = (left+right) // 2
        if func(l,key,middle) is True:
            right = middle
        else:
            left = middle
    return right

def judge_1(l,key,middle):
    if key <= l[middle]:
        return True
    else:
        return False

def judge_2(l,key,middle):
    if key < l[middle]:
        return True
    else:
        return False

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:
    top = biserch(A,b)
    bottom = N - biserch(C,b,flag=False)
    ans += top*bottom
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 =sorted(a)
b =sorted(b)
c =sorted(c)

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


print(ans)

Solution (Python):
from bisect import bisect,bisect_left
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:
    top = bisect_left(A,b)
    bottom = N - bisect(C,b)
    ans += top*bottom
print(ans)

Solution (Python):
n=int(input())
import bisect
a=[]
b=[]
c=[]
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
a.sort()
b.sort()
c.sort()
sum=0
for i in range(n):
    s=b[i]
    kc=n-bisect.bisect_right(c,s)
    ka=bisect.bisect_left(a,s)
    sum+=kc*ka
print(sum)

Solution (Python):
N = int(input())

An = sorted(map(int, input().split()), reverse=True)
Bn = sorted(map(int, input().split()), reverse=True)
Cn = sorted(map(int, input().split()), reverse=True)


def f(xn, yn, zn):
    tn = [0] * N
    num = 0
    j = 0

    for i in range(N):
        while j <= N - 1 and xn[i] < yn[j]:
            num += zn[j]
            j += 1
        tn[i] = num

    return tn


def main():
    Zn = [1] * N

    Zn = f(Bn, Cn, Zn)
    Zn = f(An, Bn, Zn)

    print(sum(Zn))
    return


main()

Solution (Python):
N = int(input())

An = sorted(map(int, input().split()))
Bn = sorted(map(int, input().split()))
Cn = sorted(map(int, input().split()))


def f(xn, yn, zn):
    tn = [0] * N
    num = 0
    j = N - 1

    for i in range(N - 1, -1, - 1):
        while 0 <= j and xn[i] < yn[j]:
            num += zn[j]
            j -= 1
        tn[i] = num

    return tn


def main():
    Zn = [1] * N

    Zn = f(Bn, Cn, Zn)
    Zn = f(An, Bn, Zn)

    print(sum(Zn))
    return


main()

Solution (Python):
def main():
    from bisect import bisect_left
    from bisect import bisect_right

    N = int(input())

    An = sorted(map(int, input().split()))
    Bn = sorted(map(int, input().split()))
    Cn = sorted(map(int, input().split()))

    ans = 0
    lx = 0
    ux = 0

    for Bi in Bn:
        
        lx = bisect_left(An, Bi, lx, N)

        
        ux = bisect_right(Cn, Bi, ux, N)

        ans += lx * (N - ux)

    print(ans)
    return


main()

Solution (Python):
def main():
    N = int(input())

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

    def lowerBound(a, v):
        
        return lb(a, 0, len(a), v)

    def upperBound(a, v):
        
        
        return lb(a, 0, len(a), v + 1)

    def lb(a, l, r, v):
        low = l - 1
        high = r

        while high - low > 1:
            mid = low + high >> 1
            if a[mid] >= v:
                high = mid
            else:
                low = mid

        return high

    print(sum([lowerBound(A, Bi) * (N - upperBound(C, Bi)) for Bi in B]))

    return


main()

Solution (Python):
def main():
    from bisect import bisect_left
    from bisect import bisect_right

    N = int(input())

    An = map(int, input().split())
    Bn = map(int, input().split())
    Cn = map(int, input().split())

    A = sorted(An)
    B = sorted(Bn)
    C = sorted(Cn)

    print(sum([bisect_left(A, Bi) * (N - bisect_right(C, Bi)) for Bi in B]))
    return


main()

Solution (Python):
def main():
    from bisect import bisect_left
    from bisect import bisect_right

    N = int(input())

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

    print(sum([bisect_left(A, Bi) * (N - bisect_right(C, Bi)) for Bi in B]))
    return


main()

Solution (Python):
def main():
    from bisect import bisect_left
    from bisect import bisect_right

    N = int(input())

    An = map(int, input().split())
    Bn = map(int, input().split())
    Cn = map(int, input().split())

    A = sorted(An)
    B = sorted(Bn)
    C = sorted(Cn)

    print(sum([bisect_left(A, Bi) * (N - bisect_right(C, Bi)) for Bi in B]))
    return


main()

Solution (Python):
def main():
    from bisect import bisect_left
    from bisect import bisect_right

    N = int(input())

    An = map(int, input().split())
    Bn = map(int, input().split())
    Cn = map(int, input().split())

    A = sorted(An)
    C = sorted(Cn)

    print(sum(bisect_left(A, Bi) * (N - bisect_right(C, Bi)) for Bi in Bn))
    return


main()

Solution (Python):
N = int(input())

An = sorted([int(i) for i in input().split()])
Bn = map(int, input().split())
Cn = sorted([int(i) for i in input().split()])


def lowerBound(a, v):
    
    return lb(a, 0, len(a), v)


def upperBound(a, v):
    
    
    return lb(a, 0, len(a), v + 1)


def lb(a, l, r, v):
    low = l - 1
    high = r

    while high - low > 1:
        mid = low + high >> 1
        if a[mid] >= v:
            high = mid
        else:
            low = mid

    return high


def main():
    print(sum([lowerBound(An, Bi)
               * (N - upperBound(Cn, Bi)) for Bi in Bn]))

    return


main()

Solution (Python):
import bisect

N = int(input())

An = sorted(map(int, input().split()))
Bn = map(int, input().split())
Cn = sorted(map(int, input().split()))


def main():
    print(sum([bisect.bisect_left(An, Bi)
               * (N - bisect.bisect(Cn, Bi)) for Bi in Bn]))

    return


main()

Solution (Python):
import bisect

N = int(input())

An = sorted(map(int, input().split()))
Bn = map(int, input().split())
Cn = sorted(map(int, input().split()))


def main():
    print(sum(bisect.bisect_left(An, Bi)
              * (N - bisect.bisect(Cn, Bi)) for Bi in Bn))

    return


main()

Solution (Python):
import bisect

N = int(input())

An = map(int, input().split())
Bn = map(int, input().split())
Cn = map(int, input().split())


def main():
    A = sorted(An)
    C = sorted(Cn)

    ans = 0

    for Bi in Bn:
        
        lx = bisect.bisect_left(A, Bi)

        
        ux = bisect.bisect(C, Bi)

        ans += lx * (N - ux)

    print(ans)
    return


main()

Solution (Python):
import bisect
from sys import stdin

N = int(input())

lines = stdin.readlines

l = [[int(i) for i in line.split()] for line in lines()]

An = l[0]
Bn = l[1]
Cn = l[2]


def main():
    An.sort()
    Cn.sort()

    ans = 0

    for Bi in Bn:
        
        lx = bisect.bisect_left(An, Bi)

        
        ux = bisect.bisect(Cn, Bi)

        ans += lx * (N - ux)

    print(ans)
    return


main()

Solution (Python):

def lower_bound(a, key):
    ng = -1
    ok = len(a)
    
    while abs(ok-ng)>1:
        mid = (ok + ng) // 2
        
        if a[mid]>key: ok=mid 
        else: ng = mid
    return ok

def upper_bound(a, key):
    ng = len(a)
    ok = -1
    
    while abs(ok-ng)>1:
        mid = (ok + ng) // 2
        
        if a[mid]<key: ok=mid 
        else: ng = mid
    return ok

N = int(input())
A, B, C = [sorted(list(map(int, input().split()))) for _ in range(3)]
ans = 0
for b in B:
    a_i = upper_bound(A, b)
    c_i = lower_bound(C, b)
    ans += (a_i+1)*(N-c_i)
print(ans)

Solution (Python):


"""
Created on 2019/3/22
Solved on 2019/3/
@author: shinjisu
"""



import collections


def getInt(): return int(input())

def getIntList(): return [int(x) for x in input().split()]


def zeros(n): return [0]*n


def getIntLines(n): return [int(input()) for i in range(n)]
"""
def getIntLines(n):
    data = zeros(n)
    for i in range(n):
        data[i] = getInt()
    return data
"""

def zeros2(n, m): return [zeros(m) for i in range(n)] 

def getIntMat(n, m):  
    
    mat = zeros2(n, m)
    for i in range(n):
        mat[i] = getIntList()
    return mat

ALPHABET = [chr(i+ord('a')) for i in range(26)]
DIGIT = [chr(i+ord('0')) for i in range(10)]
N1097 = 10**9 + 7
INF = 10**18

class Debug():
    def __init__(self):
        self.debug = True
 
    def off(self):
        self.debug = False
 
    def dmp(self, x, cmt=''):
        if self.debug:
            if cmt != '':
                print(cmt, ':  ', end='')
            print(x)
        return x


def prob():
    d = Debug()
    d.off()
    N = getInt()
    d.dmp((N), 'N')
    aParts = getIntList()
    bParts = getIntList()
    cParts = getIntList()
    aParts.sort()
    bParts.sort()
    cParts .sort()
    d.dmp((aParts), 'A')
    d.dmp((bParts), 'B')
    d.dmp((cParts), 'C')
    combAB = zeros(N)  
    combBC = zeros(N)
    j = 0
    for i in range(N):
        while j < N and bParts[j] <= aParts[i]:
            j += 1
            
        combAB[i] = N-j
        
    d.dmp((combAB), 'combAB')
    j = 0
    for i in range(N):
        while j < N and cParts[j] <= bParts[i]:
            j += 1
        combBC[i] = N-j
    d.dmp((combBC), 'combBC')
    sm = zeros(N+1)
    for i in range(N):
        sm[-2-i] = combBC[-1-i]+sm[-1-i]
    d.dmp((sm), 'sm')
    count = 0
    for i in range(N):

        count += sm[N-combAB[i]]
    return count


ans = prob()
if ans is None:
    pass
elif type(ans) == tuple and ans[0] == 1:  
    for elm in ans[1]:
        print(elm)
else:
    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 i in range(n):
    small=bisect.bisect_left(a,b[i])
    big=n-bisect.bisect_right(c,b[i])
    ans+=small*big
print(ans)

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

Solution (Python):
def snuke_festival(N: int, A: list, B: list, C: list) -> int:
    '''
    '''
    A.sort(), B.sort(), C.sort()
    sa, sb = [0] * (N + 1), [0] * (N + 1)

    a_idx = 0
    for i, b in enumerate(B):
        sa[i + 1] += sa[i]
        while a_idx < N and A[a_idx] < b:
            sa[i + 1] += 1
            a_idx += 1

    b_idx = 0
    for i, c in enumerate(C):
        sb[i + 1] += sb[i]
        while b_idx < N and B[b_idx] < c:
            sb[i + 1] += sa[b_idx+1]
            b_idx += 1

    return sum(sb)


if __name__ == "__main__":
    N = int(input())
    A = [int(s) for s in input().split()]
    B = [int(s) for s in input().split()]
    C = [int(s) for s in input().split()]
    ans = snuke_festival(N, A, B, C)
    print(ans)

Solution (Python):
import sys
from bisect import bisect
from itertools import accumulate

def main():
    input = sys.stdin.readline
    N = int(input())
    A = sorted(list(map(int, input().split())))
    B = sorted(list(map(int, input().split())))
    C = sorted(list(map(int, input().split())))

    Bn = [0] * N
    for i in range(N):
        b = B[i]
        m = bisect(C, b)
        Bn[i] = (N - m)

    Bn = list(accumulate(Bn[::-1]))[::-1]
    Bn += [0]

    ans = 0
    for i in range(N):
        a = A[i]
        m = bisect(B, a)
        ans += Bn[m]

    print(ans)


if __name__ == '__main__':
    main()

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()
BtoC = [0 for i in range(N+1)]
for i in range(N-1, -1, -1):
    c_idx = bisect.bisect_right(C, B[i])
    BtoC[i] = BtoC[i+1] + (N - c_idx)
count = 0
for i in range(N):
    b_idx = bisect.bisect_right(B, A[i])
    count += BtoC[b_idx]


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()
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 = 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 b in B:
    count = 1
    count *= bisect.bisect_left(A,b)
    count *= n-bisect.bisect_right(C,b)
    ans += count

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()
ans = 0
def bs_left(l, target):
    low, high = 0, len(l)
    while low < high:
        mid = (low+high)//2
        if l[mid] < target:
            low = mid+1
        else:
            high = mid
    return low

def bs_right(l, target):
    low, high = 0, len(l)
    while low < high:
        mid = (low+high)//2
        if target < l[mid]:
            high = mid
        else:
            low = mid+1
    return low
for j in range(N):
    i = bs_left(A,B[j]) 
    k = bs_right(C,B[j])
    if i != 0 and k != -1:
        ans += i*(N-k)
print(ans)

Solution (Python):
n = int(input())
A, B, C = [list(sorted(map(int, input().split())))  for _ in range(3)]

cnt = 0

def bs_left(lis, target):
    low, high = 0, len(lis)
    while low<high:
        mid = (low+high)//2
        if lis[mid]<target:
            low = mid+1
        else:
            high = mid
    return low

def bs_right(lis, target):
    low, high = 0, len(lis)
    while low<high:
        mid = (low+high)//2
        if target<lis[mid]:
            high = mid
        else:
            low = mid+1
    return low

for b in B:
    cnt += (bs_left(A, b) * (n-bs_right(C, b)))
print(cnt)

Solution (Python):

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


"""
a = input()
b = input()
if a[2] == b[0] and a[1] == b[1] and a[0] == b[2]:
    print("YES")
else:
    print("NO")
"""


"""
n = I()
n = int(math.sqrt(n))
print(n**2)
"""


n = I()
a = LI()
a.sort()
b = LI()
b.sort()
c = LI()
c.sort()
ans = 0
for i in b:
    x = bisect.bisect_left(a, i)
    x = x * (n - bisect.bisect_right(c, i))
    ans += x
print(ans)