Problem:
 Score : 300 points    Problem Statement The season for Snuke Festival has come again this year. First of all, Ringo will perform a ritual to summon Snuke. For the ritual, he needs an altar, which consists of three parts, one in each of the three categories: upper, middle and lower.  He has N parts for each of the three categories. The size of the i -th upper part is A_i , the size of the i -th middle part is B_i , and the size of the i -th lower part is C_i .  To build an altar, the size of the middle part must be strictly greater than that of the upper part, and the size of the lower part must be strictly greater than that of the middle part. On the other hand, any three parts that satisfy these conditions can be combined to form an altar.  How many different altars can Ringo build? Here, two altars are considered different when at least one of the three parts used is different.      Constraints  1 \leq N \leq 10^5  1 \leq A_i \leq 10^9(1\leq i\leq N)  1 \leq B_i \leq 10^9(1\leq i\leq N)  1 \leq C_i \leq 10^9(1\leq i\leq N)  All input values are integers.         Input Input is given from Standard Input in the following format:  N  A_1  ...  A_N  B_1  ...  B_N  C_1  ...  C_N       Output Print the number of different altars that Ringo can build.        Sample Input 1 2
1 5
2 4
3 6      Sample Output 1 3  The following three altars can be built:   Upper: 1 -st part, Middle: 1 -st part, Lower: 1 -st part  Upper: 1 -st part, Middle: 1 -st part, Lower: 2 -nd part  Upper: 1 -st part, Middle: 2 -nd part, Lower: 2 -nd part        Sample Input 2 3
1 1 1
2 2 2
3 3 3      Sample Output 2 27       Sample Input 3 6
3 14 159 2 6 53
58 9 79 323 84 6
2643 383 2 79 50 288      Sample Output 3 87

Solution (Python):
def II(): return int(input())
def LII(): return list(map(int, input().split()))

n=II()
alist=LII()
blist=LII()
clist=LII()

alist.sort()
blist.sort()
clist.sort()


def count_a(b):
    ok,ng = -1,n
    def is_ok(i):
        return alist[i]<b
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if is_ok(mid):
            ok = mid
        else:
            ng = mid
    return ok+1

def count_c(b):
    ok,ng = n,-1
    def is_ok(i):
        return clist[i]>b
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if is_ok(mid):
            ok = mid
        else:
            ng = mid
    return n-ok

result=0
for b in blist:
    result += count_a(b) * count_c(b)

print(result)

Solution (Python):
import bisect

import sys
stdin=sys.stdin

ip=lambda: int(sp())
fp=lambda: float(sp())
lp=lambda:list(map(int,stdin.readline().split()))
sp=lambda:stdin.readline().rstrip()
yp=lambda:print('Yes')
np=lambda:print('No')

n=ip()
a=lp()
b=lp()
c=lp()

a.sort()
b.sort()
c.sort()
ans=0

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

Solution (Python):



import sys
from io import StringIO
import unittest
import bisect


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """2
1 5
2 4
3 6"""
        output = """3"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """3
1 1 1
2 2 2
3 3 3"""
        output = """27"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """6
3 14 159 2 6 53
58 9 79 323 84 6
2643 383 2 79 50 288"""
        output = """87"""
        self.assertIO(input, output)


def resolve():
    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:
        an = bisect.bisect_left(A, b)
        cn = N - bisect.bisect_right(C, b)
        ans += an*cn

    print(ans)


if __name__ == "__main__":
    
    resolve()

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()
su = 0
for b in bl:
    su += bisect.bisect_left(al, b) * (len(cl) - bisect.bisect_right(cl, b))
print(su)

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 j in range(n):
    i = bisect.bisect_left(A, B[j])
    k = n - bisect.bisect_right(C, B[j])
    ans += int(i*k)
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 j in range(n):
    i = bisect.bisect_left(A, B[j])
    k = n - bisect.bisect_right(C, B[j])
    ans += int(i*k)
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 elem in b:
    id_a=bisect.bisect_left(a,elem)
    id_c=bisect.bisect(c,elem)
    ans+=id_a*(n-id_c)

print(ans)

Solution (Python):
import bisect
N = int(input())
top = list(map(int,input().split()))
middle = list(map(int,input().split()))	
bottom = list(map(int,input().split()))	

top.sort()
middle.sort()
bottom.sort()

ans = 0
for i in middle:
  can_put_upstair = bisect.bisect_left(top,i)
  can_put_downstair = N - bisect.bisect_right(bottom,i)
  
  ans += can_put_upstair*can_put_downstair

print(ans)

Solution (Python):
import sys
read = sys.stdin.read

from bisect import bisect, bisect_left
from itertools import accumulate
def main():
    data = list(map(int, read().split()))
    n = data[0]
    a = data[1:n + 1]
    b = data[n + 1: n * 2 + 1]
    c = data[n * 2 + 1:]
    a.sort()
    b.sort()
    c.sort()
    b2 = [bisect_left(a, be) for be in b]
    b2a = list(accumulate(b2))
    b2a = [0] + b2a
    ans = sum([b2a[bisect_left(b, ce)] for ce in c])
    print(ans)

if __name__ == '__main__':
    main()

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

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

print(ans)

Solution (Python):
from bisect import bisect
import random


def binary_search(L, n, i, j):
    
    low = i
    high = j

    while low <= high:
        mid = (low + high) //2
        guess = L[mid]
        
            
        if guess > n:
            high = mid -1
        else:
            low = mid + 1
    
    return low

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
i, j = 0, 0

A_ind = []
B_cum = [0]

for a in A:
    
    i = bisect(B, a)
    A_ind.append(i)

s = 0
for b in B:
    
    j = i = bisect(C, b)
    s += N-j
    B_cum.append(s)

for a in A_ind:
    ans += B_cum[-1] - B_cum[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:
    a = bisect.bisect_left(A, b)
    c = bisect.bisect_right(C, b)
    ans += a*(n-c)
 
print(ans)

Solution (Python):
import bisect
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 b:
    ans+=(n-bisect.bisect_right(c,i))*bisect.bisect_left(a,i)

print(ans)

Solution (Python):
import numpy as np
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())
b1=0
d=0

ans=0
 
A=list(map(int, input().split()))
A=sorted(A)
B=list(map(int, input().split()))
B=sorted(B, reverse=True)
C=list(map(int, input().split()))
C=sorted(C)
D=[]
E=[]

for b in B:
  d=d+len(C)-bisect.bisect_right(C,b)
  D.append(d)

D=sorted(D, reverse= True)  
B=sorted(B)

for a in A:
  if 0 <= bisect.bisect_right(B,a) <= len(D)-1:
    ans += D[bisect.bisect_right(B,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())))
res = 0
for bi in b:
    aUpper = bisect.bisect_left(a, bi)
    cLower = len(c) - bisect.bisect_right(c, bi)
    res += (aUpper * cLower)
print(res)

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_cnt = [1] * N
A_sum = [i+1 for i in range(N)]
A_sum.append(0)
B_cnt = [0] * N
B_sum = [0] * N
C_cnt = [0] * N
C_sum = [0] * N

for i in range(N):
    key = B[i]
    ok = -1
    ng = N
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if A[mid] < key:
            ok = mid
        else:
            ng = mid
    B_cnt[i] = A_sum[ok]

B_sum[0] = B_cnt[0]
for i in range(1, N):
    B_sum[i] = B_sum[i-1] + B_cnt[i]
B_sum.append(0)

for i in range(N):
    key = C[i]
    ok = -1
    ng = N
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if B[mid] < key:
            ok = mid
        else:
            ng = mid
    C_cnt[i] = B_sum[ok]

C_sum[0] = C_cnt[0]
for i in range(1, N):
    C_sum[i] = C_sum[i-1] + C_cnt[i]

print(C_sum[-1])

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

ans = 0
for i in b:
    p1 = bisect_left(a, i)
    p2 = bisect_right(c, i)

    ans += p1*(n-p2)

print(ans)

Solution (Python):

from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def SL(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def R(n): return [sys.stdin.readline().strip() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SLR(n): return [SL() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]

def perm(n, r): return math.factorial(n) // math.factorial(r)
def comb(n, r): return math.factorial(n) // (math.factorial(r) * math.factorial(n-r))

def make_list(n, *args, default=0): return [make_list(*args, default=default) for _ in range(n)] if args else [default for _ in range(n)]

dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
alphabets = "abcdefghijklmnopqrstuvwxyz"
ALPHABETS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
MOD = 1000000007
INF = float("inf")

sys.setrecursionlimit(1000000)

def main():
    N = I()
    A, B, C = sorted(LI()), sorted(LI()), sorted(LI())

    ans = 0
    for bi in B:
        left, right = -1, N
        while right - left > 1:
            mid = (right + left) // 2
            if A[mid] < bi: left = mid
            else: right = mid
        tmp = left
        left, right = -1, N
        while right - left > 1:
            mid = (left + right) // 2
            if C[mid] > bi: right = mid
            else: left = mid
        ans += (tmp + 1) * (N - right)

    print(ans)



if __name__ == '__main__':
    main()

Solution (Python):

from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def SL(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def R(n): return [sys.stdin.readline().strip() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SLR(n): return [SL() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]

def perm(n, r): return math.factorial(n) // math.factorial(r)
def comb(n, r): return math.factorial(n) // (math.factorial(r) * math.factorial(n-r))

def make_list(n, *args, default=0): return [make_list(*args, default=default) for _ in range(n)] if args else [default for _ in range(n)]

dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
alphabets = "abcdefghijklmnopqrstuvwxyz"
ALPHABETS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
MOD = 1000000007
INF = float("inf")

sys.setrecursionlimit(1000000)

def main():
    N = I()
    A, B, C = sorted(LI()), sorted(LI()), sorted(LI())

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



if __name__ == '__main__':
    main()

Solution (Python):
def lb(k, a):
    n = len(a)
    l, u = 0, n
    while l < u:
        m = (l + u) // 2
        if k < a[m]:
            u = m
        else:
            l = m + 1
    return l

def ub(k, a):
    n = len(a)
    l, u = -1, n - 1
    while l < u:
        m = (l + u + 1) // 2
        if a[m] < k:
            l = m
        else:
            u = m - 1
    return l

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

res = 0
for i in range(N):
    j = ub(B[i], A)
    k = lb(B[i], C)
    res += (j + 1)*(N - k)

print(res)

Solution (Python):
import bisect
n = int(input())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))
cl = list(map(int, input().split()))
al.sort()
bl.sort()
cl.sort()
ans = 0

for b in bl:
    ca = bisect.bisect_left(al, b)
    cc = n-bisect.bisect_right(cl, b)
    ans += ca*cc
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):
    aa = bisect.bisect_left(a,b[i])
    ca = n-bisect.bisect_right(c,b[i])
    ans += aa * ca
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()
from bisect import *
ans = 0
for b in B:
  a = bisect_left(A,b)
  c = N-bisect_right(C,b)
  ans += a*c
print(ans)

Solution (Python):
import bisect
n = int(input())
X = sorted(list(map(int,input().split())))
Y = sorted(list(map(int,input().split())))
Z = sorted(list(map(int,input().split())))
print(sum(bisect.bisect_left(X,b) * (n-bisect.bisect_right(Z,b)) for b in Y))

Solution (Python):
import math , sys, 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() ) ) )

Bc = []

for i in range(N):
    X = B[i]

    count = bisect.bisect_left( A , X )
    Bc.append( count )


s = 0
Ec = [0]
for i in range(N):
    s += Bc[i]
    Ec.append(s)

ans = 0
for i in range(N):
    X = C[i]
    ind = bisect.bisect_left( B , X )
    ans += Ec[ind]
    

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())))
cnt = 0
for i in b:
    left = -1
    right = len(a) 
    while right - left > 1:
        mid = (right + left) //2 
        if a[mid] <  i:
            left = mid 
        else :
            right = mid 
    left2 = -1
    right2 = len(c)
    while right2 - left2 > 1:
        mid2 = (right2 + left2) //2
        if c[mid2] > i:
            right2 = mid2
        else :
            left2 = mid2
    cnt += (left+1)*(n-right2)

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

Solution (Python):
import math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]

def main():
    n = i_input()
    a = i_list()
    b = i_list()
    c = i_list()
    a.sort()
    b.sort()
    c.sort()

    al = []
    bl = [0]
    import bisect

    for k,i in enumerate(b):
        x = bisect.bisect(c, i)
        bl.append(bl[k]+(n-x))

    ans = 0

    for i in a:
        x = bisect.bisect(b,i)
        ans += bl[-1] - bl[x]
    print(ans)




if __name__=="__main__":
    main()

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

ans = 0



for i in range(n):
    b = B[i]
    pos_a = bisect_left(A, b)
    pos_c = bisect_right(C, b)
    ans += pos_a * (n - pos_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())))
bl = [0]*n
blli = [0]*n
al = [0]*n
alli = [0]*n
import bisect

for i in range(n):
    bl[i]=n-bisect.bisect(c,b[i])

blli[0] = sum(bl)
for i in range(n-1):
    blli[i+1] = blli[i]-bl[i]
   
for i in range(n):
    al[i]=n-bisect.bisect(b,a[i])

for i in range(n):
    if al[i]!=0:
        alli[i] = blli[-al[i]]

print(sum(alli))

Solution (Python):
def getval():
    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()
    return n,a,b,c 

def main(n,a,b,c):
    def bs(arr,key):
        lo = 0
        hi = len(arr)-1
        while lo<=hi:
            idx = (lo+hi)//2
            if arr[idx]>=key:
                hi = idx-1
            else:
                lo = idx+1
        return hi

    arrb = []
    arrc = []
    for i in b:
        arrb.append(bs(a,i)+1)
    for i in c:
        arrc.append(bs(b,i)+1)
    for i in range(1,n):
        arrb[i] += arrb[i-1]
    ans = 0
    for i in arrc:
        if i!=0:
            ans += arrb[i-1]
    print(ans)

if __name__=="__main__":
    n,a,b,c = getval()
    main(n,a,b,c)

Solution (Python):
import bisect

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

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


if __name__ == '__main__':
    main()

Solution (Python):
def binary_search(lst, t, i):
    left = -1
    right = len(lst)

    while abs(right - left) > 1:
        mid = (left + right) // 2
        if i == 0:
            if lst[mid] < t:
                left = mid
            else:
                right = mid
        elif i == 1:
            if lst[mid] > t:
                right = mid
            else:
                left = mid

    return left


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())))
    ans = 0

    for b in B:
        a = binary_search(A, b, 0)
        c = binary_search(C, b, 1)
        ans += (a + 1) * (n - c - 1)

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def binary_search(lst, t, i):
    left = -1
    right = len(lst)

    while abs(right - left) > 1:
        mid = (left + right) // 2
        if i == 0:
            if lst[mid] < t:
                left = mid
            else:
                right = mid
        elif i == 1:
            if lst[mid] > t:
                right = mid
            else:
                left = mid

    return left


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())))
    ans = 0

    for b in B:
        a = binary_search(A, b, 0)
        c = binary_search(C, b, 1)
        ans += (a + 1) * (n - c - 1)

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

import sys
from math import factorial

MOD = 10 ** 9 + 7
INF = 10000000000

def binary_search_section(list, min, max):
    low = 0
    high = len(list) - 1
    upper = len(list)
    lower = -1
    while low <= high:
        mid = (low + high) // 2
        guess = list[mid]
        if guess >= min:
            high = mid - 1
        else:
            low = mid + 1
            lower = mid
    low = 0
    high = len(list) - 1
    while low <= high:
        mid = (low + high) // 2
        guess = list[mid]
        if guess > max:
            high = mid - 1
            upper = mid
        else:
            low = mid + 1

    return [lower, upper]

def main():
    
    
    n = int(input())    
    aList = list(map(int, input().split()))     
    bList = list(map(int, input().split()))     
    cList = list(map(int, input().split()))     

    
    aListSorted = sorted(aList)
    bListSorted = sorted(bList)
    cListSorted = sorted(cList)

    result = 0
    
    
    
    
    
    
    for b in bListSorted:
        sec = binary_search_section(aListSorted, aListSorted[0], b-1)
        sec2 = binary_search_section(cListSorted, b+1, cListSorted[-1])
        result += (sec[1] - sec[0] - 1)*(sec2[1] - sec2[0] - 1)

    
    print("{}".format(result))
    
    
    
    


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
As = list(map(int,input().split()))
Bs = list(map(int,input().split()))
Cs = list(map(int,input().split()))

As.sort(reverse = True)
Bs.sort(reverse = True)
Cs.sort(reverse = True)

ls = [0]
sum = 0
for i in range(N):
    l = 0
    r = N-1
    while(r-l>1):
        mid = (l+r)//2
        if Cs[mid] <= Bs[i]:
            r = mid
        else:
            l = mid
    if Cs[0] <= Bs[i]:
        r = 0
    if Cs[N-1] > Bs[i]:
        r = N
    sum += r
    ls.append(sum)

ans = 0
for i in range(N):
    l = 0
    r = N-1
    while(r-l>1):
        mid = (l+r)//2
        if Bs[mid] <= As[i]:
            r = mid
        else:
            l = mid
    if Bs[0] <= As[i]:
        r = 0
    if Bs[N-1] > As[i]:
        r = N
    ans += ls[r]

print(ans)

Solution (Python):
import bisect as bs

def main():
    with open(0) as f:
        N = int(f.readline())
        A = sorted(list(map(int, f.readline().split())))
        B = list(map(int, f.readline().split()))
        C = sorted(list(map(int, f.readline().split())))

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

main()

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())))
ans = 0
import bisect
b = []
for i in B:
    x = bisect.bisect_right(C,i)
    b.append(N-x)
y = sum(b)
c = [y]
for i in range(1,N):
    y -= b[i-1]
    c.append(y)
c.append(0)
for i in A:
    a = bisect.bisect_right(B,i)
    ans += c[a]
print(ans)

Solution (Python):
from bisect import bisect_left
from numpy import cumsum

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
ans = 0
start = 0
for i, b in enumerate(B):
    j = bisect_left(A, b, start)
    B_[i] = j
    start = j
B_ = cumsum(B_)
start = 0
for c in C:
    i = bisect_left(B, c, start)
    start = i
    if i != 0:
        ans += B_[i-1]
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 = bisect.bisect_right(C, b)
    ans += a*(n-c)

print(ans)

Solution (Python):
import bisect

n = int(input())
al = sorted(list(map(int, input().split())))
bl = sorted(list(map(int, input().split())))
cl = sorted(list(map(int, input().split())))
ans = 0

for b in bl:
    i = bisect.bisect_left(al, b)
    j = bisect.bisect_right(cl, b)
    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())))
ans=0

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

Solution (Python):
from bisect import bisect_right, bisect_left
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 range(N):
  s=b[i]
  k=bisect_left(a,s)*(N-bisect_right(c,s))
  ans+=k
print(ans)

Solution (Python):


def get_input() -> tuple:
    """
    標準入力を取得する.

    Returns:\n
        tuple: 標準入力
    """
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))

    return N, A, B, C


def get_lb(l: list, key: int) -> int:
    """
    l[index] > keyとなる最小のindexを求める.

    Args:\n
        l (list): リスト
        key (int): 整数

    Returns:\n
        int: l[index] > keyとなる最小のindex
    """
    low = -1
    high = len(l)
    while 1 < high - low:
        mid = (low + high) // 2
        guess = l[mid]
        if guess > key:
            high = mid
        else:
            low = mid
    return high


def get_ub(l: list, key: int) -> int:
    """
    l[index] < keyとなる最大のindexを求める.

    Args:\n
        l (list): リスト
        key (int): 整数

    Returns:\n
        int: l[index] < keyとなる最大のindex
    """
    low = -1
    high = len(l)
    while 1 < high - low:
        mid = (low + high) // 2
        guess = l[mid]
        if guess < key:
            low = mid
        else:
            high = mid
    return low


def main(N: int, A: list, B: list, C: list) -> None:
    """
    メイン処理.

    Args:\n
        N (int): パーツの数(1 <= N <= 10**5)
        A (list): 上部のパーツのサイズ(1 <= A_i <= 10**9)
        B (list): 中部のパーツのサイズ(1 <= B_i <= 10**9)
        C (list): 下部のパーツのサイズ(1 <= C_i <= 10**9)
    """
    
    A = sorted(A)
    B = sorted(B)
    C = sorted(C)

    
    ans = 0
    for i in range(N):
        B_i = B[i]
        ans += (get_ub(A, B_i) + 1) * (N - get_lb(C, B_i))

    
    print(ans)


if __name__ == "__main__":
    
    N, A, B, C = get_input()

    
    main(N, A, B, C)

Solution (Python):
def solve():
    def bin_l(arr, key):
        l = 0
        r = len(arr) - 1
        while l <= r:
            mid = (l + r)//2
            if arr[mid] > key:
                r = mid - 1
            else:
                l = mid + 1
            
        if l < len(arr):
            return l
        else:
            return None
    
    def bin_s(arr, key):
      l = 0
      r = len(arr) - 1
      while l <= r:
        mid = (l + r) // 2
        if arr[mid] >= key:
          r = mid - 1
        else:
          l = mid + 1
      
      if r >= 0 :
        return r
      else:
        return None
        
    n = int(input())
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))
    C = list(map(int,input().split()))
    A = sorted(A)
    C = sorted(C)
    
    sum = 0
    for b in B:
        a = bin_s(A, b)
        c = bin_l(C, b)
        a = a + 1 if a is not None else None
        c = n - c if c is not None else None
        if a is not None and c is not None:
            sum += a*c
    
    print(sum)

if __name__ == "__main__":
    solve()

Solution (Python):
def bin_l(arr, key):
    l = 0
    r = len(arr) - 1
    while l <= r:
        mid = (l + r)//2
        if arr[mid] > key:
            r = mid - 1
        else:
            l = mid + 1
        
    if l < len(arr):
        return l
    else:
        return None

def bin_s(arr, key):
  l = 0
  r = len(arr) - 1
  while l <= r:
    mid = (l + r) // 2
    if arr[mid] >= key:
      r = mid - 1
    else:
      l = mid + 1
  
  if r >= 0 :
    return r
  else:
    return None
    
n = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
A = sorted(A)
C = sorted(C)

sum = 0
for b in B:
    a = bin_s(A, b)
    c = bin_l(C, b)
    a = a + 1 if a is not None else None
    c = n - c if c is not None else None
    if a is not None and c is not None:
        sum += a*c

print(sum)

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.append(float("INF"))
AB = [0] * (N+1)
j = 0
for i in range(N):
    while A[j] < B[i]:
        j += 1
        if j == N:
            break
    AB[i+1] = AB[i] + j

ans = 0
B.append(float("INF"))
j = 0
for i in range(N):
    while B[j] < C[i]:
        j += 1
        if j == N:
            break
    ans += AB[j]

print(ans)

Solution (Python):
from bisect import bisect_left
from itertools import accumulate
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()
bs = [0]*N
for i in range(N):
    b = B[i]
    ai = bisect_left(A, b)
    bs[i] = ai
bs = [0]+list(accumulate(bs, lambda x,y:x+y))
ans = 0
for i in range(N):
    c = C[i]
    bi = bisect_left(B, c)
    ans += bs[bi]
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=sorted(a)
b=sorted(b)
c=sorted(c)
c.insert(0,-1)
c.append(10**10)
a.insert(0,-1)
a.append(10**10)
ans=0
for i in range(n):
    x=b[i]
    l=0
    r=n+1
    while r-l>1:
        m=(l+r)//2
        if a[m]<x:
            l=m
        else:
            r=m
    L=0
    R=n+1
    while R-L>1:
        M=(L+R)//2
        if c[M]<=x:
            L=M
        else:
            R=M
    ans+=l*(n+1-R)
print(ans)