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=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)
B=[0]*n
ans=0
for i in range(n):
    x=b[i]
    l=0
    r=n+1
    while r-l>1:
        m=(l+r)//2
        if c[m]>x:
            r=m
        elif c[m]<=x:
            l=m
    B[i]=n-l
g=[0]*n


g[n-1]=B[n-1]
for i in range(n-1):
    g[-2-i]=g[-1-i]+B[-2-i]
g.append(0)

b.insert(0,-1)
b.append(10**10)
for i in range(n):
    x=a[i]
    l=0
    r=n+1
    while r-l>1:
        m=(l+r)//2
        if b[m]>x:
            r=m
        elif b[m]<=x:
            l=m
    ans+=g[l]
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)
B=[0]*n
ans=0
for i in range(n):
    x=b[i]
    l=0
    r=n+1
    while r-l>1:
        m=(l+r)//2
        if c[m]>x:
            r=m
        elif c[m]<=x:
            l=m
    B[i]=n-l
g=[0]*n


g[n-1]=B[n-1]
for i in range(n-1):
    g[-2-i]=g[-1-i]+B[-2-i]
g.append(0)

b.insert(0,-1)
b.append(10**10)
for i in range(n):
    x=a[i]
    l=0
    r=n+1
    while r-l>1:
        m=(l+r)//2
        if b[m]>x:
            r=m
        elif b[m]<=x:
            l=m
    ans+=g[l]
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)
B=[0]*n
ans=0
for i in range(n):
    x=b[i]
    l=0
    r=n+1
    while r-l>1:
        m=(l+r)//2
        if c[m]>x:
            r=m
        elif c[m]<=x:
            l=m
    B[i]=n-l
g=[0]*n


g[n-1]=B[n-1]
for i in range(n-1):
    g[-2-i]=g[-1-i]+B[-2-i]
g.append(0)

b.insert(0,-1)
b.append(10**10)
for i in range(n):
    x=a[i]
    l=0
    r=n+1
    while r-l>1:
        m=(l+r)//2
        if b[m]>x:
            r=m
        elif b[m]<=x:
            l=m
    ans+=g[l]
print(ans)

Solution (Python):
from bisect import *
n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
A.sort()
B.sort()
C.sort()
ans=0
for b in B:
    ans+=(n-bisect(C,b))*bisect_left(A,b)
print(ans)

Solution (Python):
from bisect import *
n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
A.sort()
B.sort()
C.sort()
ans=0
for b in B:
    ans+=(n-bisect(C,b))*bisect_left(A,b)
print(ans)

Solution (Python):
INF = 10**13
N = int(input())
A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
C = sorted(list(map(int, input().split())))
cusumB = [0 for i in range(N+1)]
C += [INF]
left = 0
for i, bi in enumerate(B):
    while C[left]<=bi:
        left += 1
    cusumB[i] = N - left
for i in range(N, 0, -1):
    cusumB[i-1] += cusumB[i]
B += [INF]
left = 0
ans = 0
for i, ai in enumerate(A):
    while B[left]<=ai:
        left += 1
    ans += cusumB[left]
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()

import bisect
As = list(range(N+1))

Bs = [0] * N
s = 0
for i,b in enumerate(B):
    ind = bisect.bisect_left(A,b)
    s += As[ind]
    Bs[i] = s
Bs = [0] + Bs
Cs = [0] * N
ans = 0
for i,c in enumerate(C):
    ind = bisect.bisect_left(B,c)
    ans += Bs[ind]

print(ans)

Solution (Python):
def bin_src_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_src_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_src_s(A, b)
  c = bin_src_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 = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

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

if a[0] > b[-1] or b[0] > c[-1]:
    print(0)
    exit()


bi = 0
lb = []
for ai in range(n):
    while bi < n and a[ai] >= b[bi]:
        lb.append(ai)
        bi += 1
while bi < n:
    lb.append(n)
    bi += 1

ans = 0
ci = n - 1
for bi in range(n-1, -1, -1):
    while ci >= 0 and b[bi] < c[ci]:
        ci -= 1
    ans += lb[bi] * (n - ci - 1)

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

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

print(cnt)

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

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

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

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

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
B = list(map(int, input().split()))
B.sort(reverse=True)
C = list(map(int, input().split()))
C.sort(reverse=True)
count = [0]*N
answer = 0
z = 0
for i in range(N):
    if B[N-1] >= C[i]:
        break
    z += 1
count[N-1] = z
for i in range(1, N):
    k=z
    for j in range(k):
        if B[N-1-i] < C[k-1-j]:
            break
        else:
            z -= 1      
    count[N-1-i] = z
sum_matrix = [0]*(N+1)
y = 0
for i in range(N):
    sum_matrix[i+1] = sum_matrix[i] + count[i]
for i in range(N):
    if A[N-1] >= B[i]:
        break
    answer += count[i]
    y += 1
for i in range(1, N):
    x = y
    for j in range(x):
        if A[N-1-i] < B[x-1-j]:
            break        
        y -= 1
    answer += sum_matrix[y]
print(answer)

Solution (Python):

def main():
    from bisect import bisect, bisect_left

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

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


if __name__ == '__main__':
    main()

Solution (Python):

def main():
    from bisect import bisect, bisect_left

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

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


if __name__ == '__main__':
    main()

Solution (Python):

def main():
    from bisect import bisect

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

    lst = [N - bisect(C, b) for b in B]
    lst = lst[::-1]
    tmp_lst = [lst[0]]
    for i in lst[1:]:
        tmp_lst.append(i + tmp_lst[-1])
    tmp_lst = tmp_lst[::-1]

    ans = 0
    for a in A:
        res = bisect(B, a)
        ans += tmp_lst[res] if res < N else 0
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from bisect import bisect_right as br 
from bisect import bisect_left as bl

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


B2=[]
C2=[]
for b in B:
    B2.append(bl(A,b))
for c in C:
    C2.append(bl(B,c))
    
B3=[]
for b in B2:
    if B3:
        B3.append(B3[-1]+b)
    else:
        B3.append(b)
    
res=sum([B3[C2[i]-1] if C2[i]!=0 else 0 for i in range(N)])
print(res)

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

A.sort()
B.sort()
C.sort()
ans=0
for b in range(N):
    a=bisect.bisect_left(A,B[b])
    c=N-bisect.bisect_right(C,B[b])
    ans+=a*c
print(ans)

Solution (Python):
import bisect
N = int(input())
lsA = list(map(int,input().split()))
lsB = list(map(int,input().split()))
lsC = list(map(int,input().split()))
lsA.sort()
lsB.sort()
lsC.sort()
ans = 0
for i in range(N):
    B = lsB[i]
    indexA = bisect.bisect(lsA,B-1)
    indexC = N - bisect.bisect(lsC,B)
    ans += indexA*indexC
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_right(A, b-1)
    c = len(C)-bisect.bisect_right(C, b)
    ans += a*c
print(ans)

Solution (Python):
N = int(input())
INF = 10**10
A = sorted(list(map(int,input().split())))
B = sorted(list(map(int,input().split())))+[INF]
C = sorted(list(map(int,input().split())))+[INF]
AB = [0]*N
BC = [0]*N
j = 0
for i in range(N):
    while A[i] >= B[j]:
        j += 1
    if j < N:
        AB[j] += 1
for i in range(N-1):
    AB[i+1] += AB[i]
j = 0
cur = 0
ans = 0
for i in range(N):
    while C[i] > B[j]:
        cur += AB[j]
        j += 1
    ans += cur
print(ans)

Solution (Python):
import bisect
n=int(input())
a,b,c=map(list,[sorted(list(map(int, input().split()))) for i in range(3)])
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, b, c = map(list, [sorted(list(map(int, input().split()))) for i in range(3)])
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
import itertools

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


b_counts = [0] * N
for i in range(N):
    b_count = bisect.bisect_left(A, B[i])
    b_counts[i] = b_count

cumsum_b_counts = list(itertools.accumulate(b_counts))
cumsum_b_counts = [0] + cumsum_b_counts


total = 0
for c in C:
    count = bisect.bisect_left(B, c)
    total += cumsum_b_counts[count]

print(total)

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

A_ind = 0
C_ind = 0
ans = 0
for B in Bs:
    while A_ind < N and As[A_ind] < B:
        A_ind += 1
    while C_ind < N and Cs[C_ind] <= B:
        C_ind += 1
    ans += A_ind * (N - C_ind)

print(ans)

Solution (Python):
import sys
import math
import itertools
import collections
from collections import deque
from collections import defaultdict

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
MOD2 = 998244353
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()

def main():
    N = NI()
    A = NLI()
    B = NLI()
    C = NLI()
    
    A = sorted(A)
    C = sorted(C)
    
    ans = 0
    
    for b in B:
        

        A_ng = -1       
        A_ok = len(A)  

        while (abs(A_ok - A_ng) > 1):
            A_mid = (A_ok + A_ng) // 2

            if A[A_mid] >= b:
                A_ok = A_mid
            else:
                A_ng = A_mid
                
        C_ng = -1      
        C_ok = len(C)  

        while (abs(C_ok - C_ng) > 1):
            C_mid = (C_ok + C_ng) // 2

            if C[C_mid] > b:
                C_ok = C_mid
            else:
                C_ng = C_mid
                
                
        len_C = len(C)
        
        ans += A_ok * (len_C-C_ok)
        
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
def abc077c_snuke_festival():
    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())))
    total = 0
    for i in range(n):
        b_size = b[i]
        j = bisect.bisect_left(a, b_size)
        j = n - j
        k = bisect.bisect_right(c, b_size)

        total += (n - j) * (n - k)
    print(total)


abc077c_snuke_festival()

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



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

ct_AB_sum = [0 for _ in range(N)] 
ct_AB_sum[0] = ct_AB[0]
for i in range(1,N):
  ct_AB_sum[i] = ct_AB[i] + ct_AB_sum[i-1]
  
ans = 0
for i in range(N):
  j = bisect.bisect_left(B, C[i]) - 1
  
  if j >= 0:
    ans += ct_AB_sum[j]
print(ans)

Solution (Python):
def main():
    import sys
    input = sys.stdin.buffer.readline
    from bisect import bisect, bisect_left
    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 b in B:
        a = bisect_left(A, b)
        c = N - bisect(C, b)
        ans += a*c
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy


from collections import Counter,defaultdict,deque





def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplt(n): return [tuple(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n = inp()
A = sorted(inpl())
B = sorted(inpl())
C = sorted(inpl())

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

print(ans)

Solution (Python):
from bisect import *
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort()
C.sort()
pat_b = [0]*N
for i in range(N):
    b = B[i]
    pat_b[i] = bisect_left(A, b)
    pat_b[i] *= (N - bisect_right(C, b))

print(sum(pat_b))

Solution (Python):
def upper_bound(A, x):
    s, t = -1, len(A)
    while t - s > 1:
        m = (s+t)//2
        if A[m] > x:
            t = m
        else:
            s = m
    
    return t

def lower_bound(A, x):
    s, t = -1, len(A)
    while t - s > 1:
        m = (s+t)//2
        if A[m] >= x:
            t = m
        else:
            s = m
    
    return t


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 += lower_bound(A, b) * (N - upper_bound(C, b))

print(ans)

Solution (Python):
def upper_bound(A, x):
    s, t = -1, len(A)
    while t - s > 1:
        m = (s+t)//2
        if A[m] > x:
            t = m
        else:
            s = m
    
    return t

def lower_bound(A, x):
    s, t = -1, len(A)
    while t - s > 1:
        m = (s+t)//2
        if A[m] >= x:
            t = m
        else:
            s = m
    
    return t


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 += lower_bound(A, b) * (N - upper_bound(C, b))

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

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

import bisect as bs
import numpy as np
A.sort()
C.sort()

B_a = np.array([bs.bisect_left(A,b) for b in B]) 
B_c = np.array([N-bs.bisect_right(C,b) for b in B])
ans = np.dot(B_a, B_c)
print(ans)

Solution (Python):



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

"""
with open('generator.txt', 'r') as f:
    read = f.read().split('\n')
    N = int(read[0])
    A = list(map(int, read[1].split()))
    B = list(map(int, read[2].split()))
    C = list(map(int, read[3].split()))
"""
A.sort()
C.sort()


def binary_search(lst, value, high):
    l = 0
    r = N - 1
    while l <= r:
        mid = (l + r) // 2
        if lst[mid] < value:
            l = mid + 1
        elif lst[mid] > value:
            r = mid - 1
        else:
            
            if high:
                
                return binary_search(lst, value + 0.5, high)
            else:
                
                return binary_search(lst, value - 0.5, high)
    if high:
        return l
    else:
        return r


def main():
    ans = 0
    
    for middle in B:
        lower = binary_search(A, middle, False) + 1 
        higher = N - binary_search(C, middle, True)
        ans += lower * higher
    print(ans)

    

def main2():
    ans = 0
    for a in A:
        for b in B:
            for c in C:
                if a < b < c:
                    ans += 1
    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()
ans = 0
for i in b:
  p = bisect.bisect_left(a, i)
  q = bisect.bisect_right(c, i)
  ans += (p*(n-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()))

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

def bin_serch(array,num):
    head = 0
    tail = len(array)
    while head < tail:
        center = (head + tail) // 2
        if num < array[center]:
            tail = center
        else:
            head = center + 1
    return head

BC = [0 for _ in range(len(B))]
for i in range(len(B)):
    BC[i] = len(C) - bin_serch(C,B[i])
    
s = 0
for i in range(len(BC)-1, -1, -1):
    s += BC[i]
    BC[i] = s

ans = 0
for a in A:
    b = bin_serch(B,a)
    if b < len(B):
        ans += BC[b]
    
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()

def bin_serch(array,num):
    head = 0
    tail = len(array)
    while head < tail:
        center = (head + tail) // 2
        if num < array[center]:
            tail = center
        else:
            head = center + 1
    return head

BC = [0 for _ in range(len(B))]
for i in range(len(B)):
    BC[i] = len(C) - bin_serch(C,B[i])
    
s = 0
for i in range(len(BC)-1, -1, -1):
    s += BC[i]
    BC[i] = s

ans = 0
for a in A:
    b = bin_serch(B,a)
    if b < len(B):
        ans += BC[b]
    
print(ans)

Solution (Python):
from bisect import *

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:
    index_A = bisect_left(A, b)
    index_C = bisect_right(C, b)
    ans += index_A * (n - index_C)
print(ans)

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

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

ans = 0
i,k = 0,0
for j in range(n):
    bj = b[j]
    while(i<n):
        if(a[i] >= bj):
            break
        i += 1
    while(k<n):
        if(c[k] > bj):
            break
        k += 1
    ans += i * (n-k)

print(ans)

Solution (Python):
N=int(input())
import math
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=A+[math.inf]
B=B+[math.inf]
C=C+[math.inf]
d=[0 for i in range(N)]
ac=0
for i in range(N):
  while C[ac]<=B[i]:
    ac+=1
  if ac==N:
    break
  d[i]=N-ac
ab=0
ans=0
for i in range(1,N):
  d[i]+=d[i-1]
def f(x):
  if x!=0:
    return d[N-1]-d[x-1]
  else:
    return d[N-1]
for i in range(N):
  while B[ab]<=A[i]:
    ab+=1
    if ab==N:
      break
  ans+=f(ab)
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.sort()
B.sort()
C.sort()

combiBC = [0] * len(B)


ic = 0
lc = len(C)
for ib in range(len(B)):
	b = B[ib]
	while ic < lc:
		c = C[ic]
		if b >= c:
			ic += 1
		else:
			combiBC[ib] = lc - ic
			break

sumCombiBC = []
for i in range(len(combiBC)):
	if i == 0:
		sumCombiBC.append(combiBC[-1])
	else:
		sumCombiBC.insert(0, combiBC[-i-1] + sumCombiBC[0])

cnt = 0
ib = 0
for ia in range(len(A)):
	a = A[ia]
	while ib < len(B):
		b = B[ib]
		if a >= b:
			ib += 1
		else:
			cnt += sumCombiBC[ib]
			break

print(cnt)

Solution (Python):
n=int(input())
alist=list(map(int, input().split()))
blist=list(map(int, input().split()))
clist=list(map(int, input().split()))
alist.sort()
blist.sort()
clist.sort()
import bisect
ans=0
for b in blist:
  num1=n-bisect.bisect(clist,b)
  num2=bisect.bisect_left(alist,b)
  ans+=num1*num2
print(ans)

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

cnt = []

for b in B:
    cnt.append(bisect_left(A, b) * (N - bisect_right(C, b)))
print(sum(cnt))

Solution (Python):
from bisect import bisect, bisect_left


def bin_search(n):

    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):
        upper_b = bisect_left(a, b[i])
        under_b = n - bisect(c, b[i])
        ans += upper_b * under_b
    return ans


if __name__ == '__main__':
    n = int(input())
    print(bin_search(n))

Solution (Python):
from bisect import *

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 = []

for b in B:
    cnt.append(bisect_left(A,b) * (N - bisect_right(C,b)))
print(sum(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())))
ans = 0
for i in b:
    an = bisect.bisect_left(a, i)
    cn = n - bisect.bisect_right(c, i)
    ans += an * cn

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

Solution (Python):
import sys
import bisect

def main():
    input = sys.stdin.buffer.readline
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    B = list(map(int, input().split()))
    B.sort()
    C = list(map(int, input().split()))
    C.sort()
    ans = 0
    for i in B:
        a = bisect.bisect_left(A, i)
        c = bisect.bisect_right(C, i)
        ans += a*(N-c)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def isOK(index, key, in_ls):
    if key<in_ls[index]: return True
    else: return False

def bs(in_ls, key):
    ng = -1
    ok = len(in_ls)
    while 1<abs(ok - ng):
        mid = (ng + ok)//2
        if isOK(mid, key, in_ls): ok = mid
        else: ng = mid
    return ok

def main():
    n = int(input())
    a_list = list(map(int, input().split(" ")))
    b_list = list(map(int, input().split(" ")))
    c_list = list(map(int, input().split(" ")))
    

    a_sorted = list(sorted(a_list))
    b_sorted = list(sorted(b_list))
    c_sorted = list(sorted(c_list))

    bc_dict = {}
    score = 0
    score_list = [0 for i in range(n)]
    for i, b in enumerate(b_sorted[::-1]):
        ok = bs(c_sorted, b)
        score+=n - ok
        score_list[n-i-1]+=score

    total = 0
    for a in a_sorted:
        ok = bs(b_sorted, a)
        if ok==n:
            continue
        total+=score_list[ok]

    print(total)

if __name__=="__main__":
    main()

Solution (Python):
def bs_left(list, target):
    low = 0
    high = len(list)

    while low < high:
        mid = (low + high) // 2
        if target > list[mid]:
            low = mid + 1
        else:
            high = mid
    return low

def bs_right(list, target):
    low = 0
    high = len(list)
    while low < high:
        mid = (low + high) // 2
        if target < list[mid]:
            high = mid
        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()
C.sort()

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

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

    B_accum = []
    B_accum_inv = [0] * N

    for b in B:
        c_idx = binary_search(C, b)
        B_accum.append(N-c_idx-1)

    tmp_sum = 0
    for i in range(len(B_accum)):
        tmp_sum += B_accum[N-i-1]
        B_accum_inv[N-i-1] = tmp_sum

    sum_ans = 0
    for a in A:
        b_idx = binary_search(B, a)
        if b_idx+1 < N:
            sum_ans += B_accum_inv[b_idx+1]

    print(sum_ans)


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


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

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

import bisect

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

Solution (Python):
from bisect import bisect_left, bisect_right
N = int(input())
A, B, C = [sorted(map(int, input().split())) for _ in range(3)]
print(sum(bisect_left(A,b)*(N-bisect_right(C,b)) for b in B))

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

count=0
for i in range(N):
    a_cand = bisect.bisect_right(A,B[i]-1)
    c_cand = N-bisect.bisect_right(C,B[i])
    count += a_cand*c_cand
print(count)

Solution (Python):
def main():
    import bisect

    N = int(input())
    upper = list(map(int, input().split()))
    middle = list(map(int, input().split()))
    lower = list(map(int, input().split()))

    upper.sort()
    middle.sort()
    lower.sort()

    cnt = 0

    for i in range(N):
        mid = middle[i]
        index_up = bisect.bisect_left(upper, mid)
        index_low = bisect.bisect_right(lower, mid)
        
        cnt += index_up * (N - index_low)
    
    print(cnt)


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

Solution (Python):
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
a = [0]+a
c = c+[10**10]
ai, ci = 0, 0
for i in range(n):
    while ai < n and a[ai+1] < b[i]:
        ai += 1
    while ci < n and c[ci] <= b[i]:
        ci += 1
    ans += ai*(n-ci)
print(ans)