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

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()
bans = []
for bi in range(n):
  bnow = b[bi]
  here = bisect.bisect_right(c,bnow)
  bans.append([bnow,n-here])
bsun = 0
for bi in range(n):
  bsun += bans[n-1-bi][1]
  bans[n-1-bi].append(bsun)
ans = 0
for ai in range(n):
  anow = a[ai]
  here = bisect.bisect_right(b,anow)
  if here != n:
    ans += bans[here][2]

print(ans)

Solution (Python):
from bisect import bisect
from itertools import accumulate
n,*l = map(int,open(0).read().split())
s = sorted
a,b,c = s(l[:n]),s(l[n:2*n]),s(l[2*n:])
ref=[]
for i in b:
	ref.append(n-bisect(c,i))
ref = list(accumulate(ref[::-1]))[::-1] + [0]
ans=0
for i in a:
	ans += ref[bisect(b,i)]
print(ans)

Solution (Python):
import bisect

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 = sorted(a)
b = sorted(b)
c = sorted(c)

ans = 0

for x in b:
    aa = bisect.bisect_left(a,x)
    cc = len(c) - bisect.bisect_right(c,x)
    ans += aa * cc

print(ans)

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

ans = 0
for i in range(N):
    cnta = bisect.bisect_left(A, B[i])
    cntc = bisect.bisect_right(C, B[i])
    ans += cnta * (N - cntc)

print(ans)

Solution (Python):
import numpy as np
n=int(input())

lista=np.array(list(map(int,input().rstrip().split())))
listb=np.array(list(map(int,input().rstrip().split())))
listc=np.array(list(map(int,input().rstrip().split())))
lista=np.sort(lista)
listc=np.sort(listc)

cont=0

for naka in listb:
	ueikeruyatsu=np.searchsorted(lista,naka,side='left')
	shitaikeruyatsu=n - np.searchsorted(listc,naka,side='right')
	cont += ueikeruyatsu * shitaikeruyatsu


print(cont)

Solution (Python):

import sys, math, fractions, itertools, bisect


def solve(N: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    A = sorted(A)
    B = sorted(B)
    C = sorted(C)

    counter = 0
    for j in range(N):
        b = B[j]
        ba = bisect.bisect_left(A, b)
        cb = N - bisect.bisect_right(C, b)
        counter += ba*cb
    
    print(counter)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [ int(next(tokens)) for _ in range(N) ]  
    B = [ int(next(tokens)) for _ in range(N) ]  
    C = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, A, B, C)

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

lenC = len(C)

cnt = 0

for b in B:
    ind_a = bisect.bisect_left(A, b)
    ind_c = bisect.bisect_right(C, b)
    cnt += ind_a * (lenC - ind_c)

print(cnt)

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

    sort_A = sorted(A)
    sort_B = sorted(B)
    sort_C = sorted(C)
    cnt = 0
    for b in sort_B:
        a = bisect.bisect_left(sort_A, b)
        c = N - bisect.bisect_right(sort_C, b)
        cnt += a * c

    print(cnt)

main()

Solution (Python):

def main():
    from bisect import bisect_right
    N = int(input())
    A = sorted(int(x) for x in input().split())
    B = sorted(int(x) for x in input().split())
    C = sorted(int(x) for x in input().split())

    b2c = [0] * (N+1)
    for i in range(N):
        b = B[i]
        j = bisect_right(C, b)
        b2c[i] = N - j
        

    for i in range(N-1)[::-1]:
        b2c[i] += b2c[i+1]
        pass
    

    total = 0
    for i in range(N):
        a = A[i]
        a2b = bisect_right(B, a)
        t = b2c[a2b]
        
        total+=t

    print(total)
        
main()

Solution (Python):
from bisect import bisect_right

N = int(input())
A = sorted(int(x) for x in input().split())
B = sorted(int(x) for x in input().split())
C = sorted(int(x) for x in input().split())

b2c = [0] * (N+1)
for i in range(N):
    b = B[i]
    j = bisect_right(C, b)
    b2c[i] = N - j
    

for i in range(N-1)[::-1]:
    b2c[i] += b2c[i+1]
    pass


total = 0
for i in range(N):
    a = A[i]
    a2b = bisect_right(B, a)
    t = b2c[a2b]
    
    total+=t

print(total)

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:
  num_a = bisect.bisect_left(A, b)
  num_c = n - bisect.bisect_right(C, b)
  ans += num_a * num_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()

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

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

print(ans)

Solution (Python):

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

A.sort()
C.sort()

def bin_search_withR(vlist, targ):
    lb = -1
    ub = len(vlist)
    while ub - lb > 1:
        mid = (lb + ub) // 2
        if vlist[mid] >= targ:
            ub = mid
        else:
            lb = mid
    return ub, lb

def bin_search_withL(vlist, targ):
    lb = -1
    ub = len(vlist)
    while ub - lb > 1:
        mid = (lb + ub) // 2
        if vlist[mid] > targ:
            ub = mid
        else:
            lb = mid
    return ub, lb

total = 0
for b in B:
    a_ub, a_lb = bin_search_withR(A, b)
    c_ub, c_lb = bin_search_withL(C, b)
    total += a_ub * (N - c_ub)

print(total)

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

import bisect
upper_condition = [bisect.bisect_left(A, b) for b in B]
lower_condition = [N - bisect.bisect_right(C, b) for b in B]

print(sum(map(lambda x: x[0]*x[1], zip(upper_condition, lower_condition))))

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:
    lower_a = bisect.bisect_left(A, b)
    upper_c = N - bisect.bisect_right(C, b)
    ans += lower_a*upper_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 i in range(N):
    ans += bisect.bisect_left(A, B[i]) * (N - bisect.bisect_right(C, B[i]))
print(ans)

Solution (Python):
from bisect import bisect_right, bisect_left

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

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

ans = 0
for i in range(N):
    p = bisect_left(A, B[i])
    q = N - bisect_right(C, B[i])

    ans += p * q

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

nums = []
for idx in range(N):
  nums.append((1, A[idx]))
  nums.append((2, B[idx]))
  nums.append((3, C[idx]))
nums.sort(key=lambda x:(x[1], -x[0]))
sum_a = 0
sum_b = 0
sum_c = 0
for idx in range(3*N):
  if nums[idx][0] == 1:
    sum_a += 1
  elif nums[idx][0] == 2:
    sum_b += sum_a
  elif nums[idx][0] == 3:
    sum_c += sum_b
print(sum_c)

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())))
B=[0]*n
j=0
for i in range(n):
    while b[i]>=c[j] and j<n-1:j+=1
    if b[i]<c[j]:B[i]=n-j
ans=0
s=sum(B)
j=0
for i in range(n):
    while a[i]>=b[j] and j<n-1:s-=B[j];j+=1
    if a[i]<b[j]:ans+=s
print(ans)

Solution (Python):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)

from math import factorial
import bisect



def main():
    num = int(input())
    data_a = list(map(int, input().split()))
    data_a.sort()
    data_b = list(map(int, input().split()))
    data_b.sort()
    data_c = list(map(int, input().split()))
    data_c.sort()

    ans = 0
    for ele in data_b:
        aaa = bisect.bisect_left(data_a, ele)
        bbb = bisect.bisect_right(data_c, ele)
        bbb = num - bbb
        ans += aaa * bbb
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
A,B,C=[sorted(map(int,input().split())) for i in range(3)]
ans=a=c=0
for i in B:
    while a<N and A[a]<i:
        a+=1
    while c<N and C[c]<=i:
        c+=1
    if c==N:break
    ans+=a*(N-c)
print(ans)

Solution (Python):

import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math






def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    N = inputInt()
    A = inputList()
    B = inputList()
    C = inputList()
    
    A.sort()
    B.sort()
    C.sort()
    
    tmplist = []
    for i,val in enumerate(B):
        target1 = bisect_left(C, val+1)
        tmp = N - target1
        tmplist.append(tmp)
            
    for i in range(N-2, -1, -1):
        tmplist[i] = tmplist[i] + tmplist[i+1]
        
    
    
    ans = 0
    for i,val in enumerate(A):
        target1 = bisect_left(B, val+1)
        if target1 == N:
            continue
        ans += tmplist[target1]
        
    print(ans)
    
    
            	
if __name__ == "__main__":
	main()

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

Solution (Python):

import sys
from bisect import bisect_left
INF = float("inf")


def solve(N: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    A.sort()
    B.sort()
    C.sort()
    count = 0
    for b in B:
        countA = bisect_left(A, b)
        countC = N-bisect_left(C, b+1)
        count += countA*countC
    print(count)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    B = [int(next(tokens)) for _ in range(N)]  
    C = [int(next(tokens)) for _ in range(N)]  
    solve(N, A, B, C)


if __name__ == '__main__':
    main()

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

CI = []
for b in B:
    ci = bisect_right(C, b)
    CI.append(N-ci)

CII = CI[::-1]
D = []
d = 0
for cii in CII:
    d += cii
    D.append(d)

D = D[::-1]

ans = 0
for a in A:
    bi = bisect_right(B, a)
    if bi == N:
        continue
    ans += D[bi]
print(ans)

Solution (Python):
import bisect
n = int(input())
k = []
for _ in range(3):
    a = [int(i) for i in input().split()]
    a.sort()
    k.append(a)
count = 0
for i in k[1]:
    b = bisect.bisect_left(k[0],i)
    c = bisect.bisect_right(k[2],i)
    count += b*(n-c)
print(count)

Solution (Python):
import bisect

N=int(input())
A=[]
for _ in range(3):
    lst=list(map(int,input().split()))
    lst.sort()
    A.append(lst)
cnt=0
for i in range(N):
    numl=bisect.bisect_left(A[0],A[1][i])
    numr=(N-bisect.bisect_right(A[2],A[1][i]))
    cnt+=numl*numr
print(cnt)

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:
	ans += (bisect.bisect_left(a,i)) * (len(c) - bisect.bisect_right(c,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()
c.sort()

ans = 0

for i in b:
	ans += (bisect.bisect_left(a,i)) * (len(c) - bisect.bisect_right(c,i))

print(ans)

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

import bisect
for i in range(N):
  b = B[i]
  end_A = bisect.bisect_left(A, b)
  start_C = bisect.bisect_right(C, b)
  ans += end_A * (N - start_C)

print(ans)

Solution (Python):
import bisect
N = int(input())
ABC = [list(map(int,input().split())) for i in range(3)]
ABC[0].sort()
ABC[2].sort()

ans = 0
for i in range(N):
    B_hi = bisect.bisect_left(ABC[0],ABC[1][i])
    B_lo =N - bisect.bisect_right(ABC[2],ABC[1][i])
    ans += B_hi * B_lo

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:
    countA = bisect.bisect_left(A, b)
    countC = len(C) - bisect.bisect(C, b)
    ans += countA * countC

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
import bisect
import numpy as np
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_b=np.zeros(n,dtype=np.int64)
def lower_elements(A, b):
    ''' リストAの要素のうち，b未満のものの個数を返す関数 '''
    lower_bound = -1
    upper_bound = len(A)
    while upper_bound - lower_bound > 1:
        middle = (lower_bound + upper_bound) // 2
        if A[middle] < b:
            lower_bound = middle
        else:
            upper_bound = middle
    return lower_bound + 1
 
 
def upper_elements(C, b):
    ''' リストCの要素のうち，cより大きいものの個数を返す関数 '''
    i = -1
    j = len(C)
    while j - i > 1:
        m = (i + j) // 2
        if C[m] > b:
            j = m
        else:
            i = m
    return len(C) - j

for i in range(n):
    count_b[n-1-i]=upper_elements(c,b[i])
count=np.cumsum(count_b)
ans=0
for i in range(n):
    x=upper_elements(b,a[i])
    if x!=0:
        ans+=count[x-1]
print(ans)

Solution (Python):
import bisect


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

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

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

Solution (Python):
n=int(input())
a,b,c=[list(map(int,input().split())) for _ in [0]*3]
from itertools import accumulate
a.sort()
b.sort()
c.sort()
a+=[10**20]
b+=[10**20]
 
p,q=0,0
cnt=0
ans=[0]*n
for i in range(n):
    for j in range(p,n+1):
        if b[i]>a[j]:
            pass
        else:
            break
    ans[i]=j
    p=j
ans=[0]+list(accumulate(ans))

for i in range(n):
    for j in range(q,n+1):
        if c[i]>b[j]:
            pass
        else:
            break
    q=j
    cnt+=ans[j]
print(cnt)

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):
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 = input()
a = sorted(map(int,input().split()))
b = sorted(map(int,input().split()))
c = sorted(map(int,input().split()))

a_big_b = [0] * len(a)
for i,p in enumerate(a):
    j = bisect.bisect_right(b, p)
    a_big_b[i] = j

b_big_c = [0] * len(b)
for i in range(len(b)-1,-1,-1):
    k = len(c) - bisect.bisect_right(c, b[i])
    if i==len(b)-1:b_big_c[i] = k
    else:b_big_c[i] = k + b_big_c[i+1]

g=0
for aa in a_big_b:
    if aa == len(b_big_c):pass
    else:g += b_big_c[aa]
print(g)

Solution (Python):


from bisect import bisect_left, bisect_right


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

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

    print(res)


if __name__ == '__main__':
    main()

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

ret = 0
i, j, k = 0, 0, 0

while j < N:
    while i < N and A[i] < B[j]:
        i += 1
    while k < N and C[k] <= B[j]:
        k += 1

    ret += i * (N - k)
    j += 1

print(ret)

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(len(b)):
    ai = bisect_left(a, b[i])
    ci = bisect_right(c, b[i])
    
    ans += ai * (n - ci)

print(ans)

Solution (Python):
from bisect import bisect_left, bisect_right
N = int(input())
A = sorted([int(_) for _ in input().split()])
B = sorted([int(_) for _ in input().split()])
C = sorted([int(_) for _ in input().split()])

ans = 0
for item in B:
    A_pos = bisect_left(A, item)
    C_pos = N - bisect_right(C, item)
    ans += A_pos * C_pos
print(ans)

Solution (Python):
from bisect import bisect_left, bisect_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())))

cnt_a = [0] * N
for i in range(N):
    cnt_a[i] = bisect_left(A, B[i])
    
cnt_b = [0] * N
for i in range(N):
    cnt_b[i] = bisect_right(C, B[i])
    
ans = sum(v * (N - w) for v, w in zip(cnt_a, cnt_b))
print(ans)

Solution (Python):

import sys
input = sys.stdin.readline

from bisect import bisect_left, bisect_right

def main():
    n = int(input())
    A, B, C = [sorted(map(int, input().rstrip().split())) for _ in range(3)]
    l, ans = len(C), 0
    for i in B:
        ans += bisect_left(A, i) * (l - bisect_right(C, i))
    print(ans)


if __name__ == "__main__":
    main()

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()
l=0
for j in b:
    aa=bisect.bisect_left(a,j)
    cc=bisect.bisect_right(c,j)
    if cc!=0:
        l+=(aa*(len(c)-cc))
    else:
        l+=(aa*len(c))
print(l)

Solution (Python):
def lower_elements(A, b):
    lower_bound = -1
    upper_bound = len(A)
    while upper_bound - lower_bound > 1:  
        middle = (lower_bound + upper_bound) // 2
        if A[middle] < b:  
            lower_bound = middle
        else:
            upper_bound = middle
    return lower_bound + 1  


def upper_elements(C, b):
    lower_bound = -1
    upper_bound = len(C)
    while upper_bound - lower_bound > 1:  
        middle = (lower_bound + upper_bound) // 2
        if C[middle] <= b:
            lower_bound = middle
        else:
            upper_bound = middle
    return len(C) - upper_bound


def answer(N: int, A: list, B: list, C: list):
    assert N == len(A) == len(B) == len(C)
    A.sort()
    B.sort()
    C.sort()
    count = 0
    for b in B:
        count += lower_elements(A, b) * upper_elements(C, b)
    return count


def snuke_festival():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))
    print(answer(N, A, B, C))


if __name__ == '__main__':
  snuke_festival()

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
acount=[0]*n
bcount=[0]*n
ruiseki=[0]*(n+1)
for i in range(n):
    acount[i]=n-bisect.bisect(b,a[i])
    bcount[i]=n-bisect.bisect(c,b[i])
    ruiseki[i+1]=ruiseki[i]+bcount[i]




for i in range(n):
    
    ans+=ruiseki[n]-ruiseki[n-acount[i]]
print(ans)

Solution (Python):
import bisect

n = int(input());
a = input().split(" ");
b = input().split(" ");
c = input().split(" ");
num_b = [0]*n;
for i in range(n) :
	a[i] = int(a[i]);
	b[i] = int(b[i]);
	c[i] = int(c[i]);
a.sort();
b.sort();
c.sort();
num_b = [0]*(n+1);
for i in range(n) :
	num_b[n-1-i] = n - bisect.bisect_right(c,b[n-1-i]) + num_b[n-i];
ans = 0;
for i in range(n) :
	ans += num_b[bisect.bisect_right(b,a[i])];
print(ans);

Solution (Python):
def lower_elements(A, b):
    ''' リストAの要素のうち，b未満のものの個数を返す関数 '''
    lower_bound = -1
    upper_bound = len(A)
    while upper_bound - lower_bound > 1:
        middle = (lower_bound + upper_bound) // 2
        if A[middle] < b:
            lower_bound = middle
        else:
            upper_bound = middle
    return lower_bound + 1


def upper_elements(C, b):
    ''' リストCの要素のうち，cより大きいものの個数を返す関数 '''
    i = -1
    j = len(C)
    while j - i > 1:
        m = (i + j) // 2
        if C[m] > b:
            j = m
        else:
            i = m
    return len(C) - j


def answer(N, A, B, C):
    A.sort()
    B.sort()
    C.sort()
    count = 0
    for b in B:
        count += lower_elements(A, b) * upper_elements(C, b)
    return count


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

Solution (Python):
def lower(A, b):
    i = -1
    j = len(A)
    while j - i > 1:
        m = (i + j) // 2
        if A[m] < b:
            i = m
        else:
            j = m
    return i + 1


def upper(C, b):
    i = -1
    j = len(C)
    
    while j - i > 1:
        m = (i + j) // 2
        
        if C[m] > b:
            j = m
        else:
            i = m
    return len(C) - j


def answer(N, A, B, C):
    A.sort()
    B.sort()
    C.sort()
    
    
    
    count = 0
    for b in B:
        
        count += lower(A, b) * upper(C, b)
    return count


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

Solution (Python):
n = int(raw_input())
A = map(int, raw_input().split())
B = map(int, raw_input().split())
C = map(int, raw_input().split())
A.sort()
B.sort()
C.sort()
abc = 0
i = 0
j = 0
k = 0
for j in range(n):
    while i < n-1 and A[i+1] < B[j]:
        i += 1
    while k < n-1 and B[j] >= C[k]:
        k += 1
    if A[i] < B[j] and B[j] < C[k]:
        
        abc += (i + 1) * (n - k)
print abc

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

print(ans)

Solution (Python):
import bisect

def solve(n, a, b, c):
    a = sorted(a)
    b = sorted(b)
    c = sorted(c)
    res = 0
    for x in b:
        i = bisect.bisect_left(a, x)
        k = bisect.bisect_right(c, x)
        res += i * (n-k)
    return res

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

Solution (Python):
from bisect import bisect_left, bisect_right

N = int(input())
*A, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())
A.sort(); B.sort(); C.sort()
ans = 0
for b in B:
    smaller_than_b = bisect_left(A, b)
    larger_than_b = N - bisect_right(C, b)
    ans += smaller_than_b * larger_than_b
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 bj in b:
    ai = bisect.bisect_left(a, bj);
    ck = n - bisect.bisect_right(c, bj);
    ans += ai * ck;
print(ans)

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

N = int(input())
A = np.array(input().split(), dtype=np.int32)
B = np.array(input().split(), dtype=np.int32)
C = - np.array(input().split(), dtype=np.int32)

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

cnt_A = np.searchsorted(A,B,side='left')
cnt_C = np.searchsorted(C,-B,side='left')

answer = (cnt_A * cnt_C).sum()
print(answer)

Solution (Python):
import bisect
N=int(input())
LA=list(map(int,input().split()))
LB=list(map(int,input().split()))
LC=list(map(int,input().split()))
LA.sort()
LB.sort()
LC.sort()
ans=0
for b in LB:
    a,c=bisect.bisect_left(LA,b),bisect.bisect_right(LC,b)
    ans+=a*(N-c)
print(ans)

Solution (Python):
import bisect
n=int(input())
abc=[sorted(list(int(i) for i in input().split())) for i in range(3)]
print(sum([bisect.bisect_left(abc[0],b)*(n-bisect.bisect_right(abc[2],b)) for b in abc[1]]))

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

N = int(input())
A = np.array(input().split(), dtype=np.int64)
B = np.array(input().split(), dtype=np.int64)
C = - np.array(input().split(), dtype=np.int64)

A.sort()

C.sort()

cnt_A = np.searchsorted(A,B,side='left')
cnt_C = np.searchsorted(C,-B,side='left')

answer = (cnt_A * cnt_C).sum()
print(answer)

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

N = int(input())
A = np.array(input().split(), dtype=np.int64)
B = np.array(input().split(), dtype=np.int64)
C = - np.array(input().split(), dtype=np.int64)

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

cnt_A = np.searchsorted(A,B,side='left')
cnt_C = np.searchsorted(C,-B,side='left')

answer = (cnt_A * cnt_C).sum()
print(answer)

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

N = int(input())
A = np.array(input().split(), dtype=np.int64)
B = np.array(input().split(), dtype=np.int64)
C = np.array(input().split(), dtype=np.int64)

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


cnt_A = np.searchsorted(A,B,side='left')

cnt_C = np.searchsorted(C,B,side='right')

answer = (cnt_A * (N - cnt_C)).sum()
print(answer)

Solution (Python):
import numpy as np
N = int(input())
A = np.array(input().split(), dtype=np.int64)
B = np.array(input().split(), dtype=np.int64)
C = np.array(input().split(), dtype=np.int64)

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


cnt_A = np.searchsorted(A,B,side='left')

cnt_C = np.searchsorted(C,B,side='right')

cnt_C = N - cnt_C

answer = (cnt_A * cnt_C).sum()
print(answer)

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

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

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

ans = 0
for v in B:
    l = bisect.bisect_left(A, v)
    r = bisect.bisect_right(C, v)
    ans += l * (N - r)
    
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:
    
    
    ans += bisect.bisect_left(A, i-0.1) * (N-bisect.bisect_right(C, i+0.1))
print (ans)

Solution (Python):
import sys
input=sys.stdin.readline
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:
    ans+=bisect.bisect_left(A,b)*(N-bisect.bisect_right(C,b))
print(ans)