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()
c.sort()
ans = 0
def find_lt(a, x):
    'Find rightmost value less than x'
    i = bisect.bisect_left(a, x)
    if i:
        return i
    return 0
def find_gt(a, x):
    'Find leftmost value greater than x'
    i = bisect.bisect_right(a, x)
    if i != len(a):
        return len(a)-i
    return 0
for bs in b:
  ans += find_lt(a, bs)*find_gt(c,bs)
print(ans)

Solution (Python):
import bisect

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

ans = 0
for j in range(N):
    x = bisect.bisect_left(A, B[j])
    y = N - bisect.bisect_right(C, B[j])
    ans += x * y
print(ans)

Solution (Python):
import bisect as bi

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

A.sort()
B.sort()
C.sort()
ans = 0

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

print(ans)

Solution (Python):



import bisect

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_list.sort()
C_list.sort()

ans = 0
for i in range(N):
    
    ab = bisect.bisect_left(A_list, B_list[i])
    bc = N - bisect.bisect_right(C_list, B_list[i])
    
    ans += ab * bc
print(ans)

Solution (Python):
N=int(input().strip())
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,reverse=True)
def binsearch_int(func, min_x, max_x):
    """func(x) == Trueとなる最大のxを返す
    """
    if func(max_x):
        return max_x
    if not func(min_x):
        return -1
    left,right=min_x,max_x
    while left+1<right:
        mid=(left+right)//2
        if func(mid):
            left=mid
        else:
            right=mid
    return left
smaller=[binsearch_int(lambda x: A[x]<i,0,N-1)+1 for i in B]
bigger=[binsearch_int(lambda x: C[x]>i,0,N-1)+1 for i in B]
print(sum(a*c for a,c in zip(smaller, bigger)))

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

Solution (Python):
from bisect import bisect_left, bisect_right

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

result = 0

for i in B:
    a = bisect_left(A,i)
    c = bisect_right(C,i)
    result += a * (N - c)

print(result)

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 hoge():
    count = 0
    Ai = -1
    Ci = -1
    for i in range(N):
        if Ai < N - 1:
            while A[Ai + 1] < B[i]:
                Ai += 1
                if Ai == N - 1:
                    break
        if Ci < N - 1:
            while C[Ci + 1] <= B[i]:
                Ci += 1
                if Ci == N - 1:
                    break
        count += (Ai + 1) * (N - (Ci + 1))
    return count


print(hoge())

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 hoge():
    count = 0
    Ai = -1
    Ci = -1
    for i in range(N):
        if Ai < N - 1:
            while A[Ai + 1] < B[i]:
                Ai += 1
                if Ai == N - 1:
                    break
        if Ci < N - 1:
            while C[Ci + 1] <= B[i]:
                Ci += 1
                if Ci == N - 1:
                    break
        count += (Ai + 1) * (N - (Ci + 1))
    return count


print(hoge())

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

    count = 0
    Ai = -1
    Ci = -1
    for i in range(N):
        if Ai<N-1:
            while A[Ai+1]<B[i]:
                Ai += 1
                if Ai == N-1:
                    break
        if Ci<N-1:
            while C[Ci+1]<=B[i]:
                Ci += 1
                if Ci==N-1:
                    break
        count += (Ai+1) * (N-(Ci+1))

    return count

if __name__ == '__main__':
    print(main())

Solution (Python):



def higher_search(list, key):
    l = -1
    r = len(list)

    while r - l > 1:
        mid = l + (r - l) // 2
        if list[mid] > key:
            r = mid
        else:
            l = mid

    if r == len(list):
        return -1
    else:
        return r


def lower_search(list, key):
    l = -1
    r = len(list)

    while r - l > 1:
        mid = r - (r - l) // 2
        if list[mid] < key:
            l = mid
        else:
            r = mid

    if l == -1:
        return -1
    else:
        return l


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_index = lower_search(A, b)
    c_index = higher_search(C, b)
    if c_index == -1:
        c_index = n
    ans += (a_index + 1) * (n - c_index)

print(ans)

Solution (Python):
import bisect as bi


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

count = 0

for b in B:
    i = bi.bisect_left(A, b) 
    k = bi.bisect_right(C, b) 
    count += i * (N - k)

print(count)

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

count = 0

for b in B:
    i = bisect_left(A, b)
    k = bisect_right(C, b)
    count += i * (N-k)

print(count)

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())))
import bisect as bi
ans=0
for j in range(n):
    i=bi.bisect_left(a, b[j])
    k=n-bi.bisect_right(c, b[j])
    ans+=i*k
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)
num = 0
p, q = 0, 0
for i in range(n):
    while p < n and a[p] < b[i]:
        p += 1
    while q < n and c[q] <= b[i]:
        q += 1
    num += p * (n - q)

print(num)

Solution (Python):
import bisect

N = int(input())
one = list(map(int, input().split()))
second = list(map(int, input().split()))
third = list(map(int, input().split()))

one.sort()
second.sort()
third.sort()

c = 0
for second_v in second:
    one_n = bisect.bisect_left(one, second_v)
    third_n = len(third) - bisect.bisect_right(third, second_v)
    c += one_n * third_n
print(c)

Solution (Python):
import bisect

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

total = 0

for middle in b:

    total += bisect.bisect_left(a,middle) * (n-bisect.bisect_right(c,middle))

print(total)

Solution (Python):
import bisect
def inpl(): return list(map(int, input().split()))
N = int(input())
a, b, c = sorted(inpl()), sorted(inpl()), sorted(inpl())

ans = 0
for j in b:
    ans += bisect.bisect_left(a, j)*(N-bisect.bisect_right(c, j))
print(ans)

Solution (Python):
N = int(input())
s = [sorted(list(map(int, input().split()))) for _ in range(3)]
ans = [[1]*(N) for _ in range(3)]

for i in range(2):

    for j in range(1,N):
        ans[i][j] = ans[i][j]+ans[i][j-1]

    last = -1
    for j in range(N):
        target = s[i+1][j]
        lo = last
        hi = N-1
        while hi - lo > 1:
            m = (hi+lo)//2
            if s[i][m] < target:
                lo = m
            else:
                hi = m
        if s[i][hi] < target:
            last = hi
        elif s[i][lo] < target:
            last = lo
        if last >= 0:
            ans[i+1][j] = ans[i][last]
        else:
            ans[i+1][j] = 0

print(sum(ans[2]))

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
A.sort();C.sort()
M = 0
for i in range(N):
    low = 0
    high = N - 1
    t = (low + high) // 2
    if A[N-1] < B[i]:
        t = N-1
    elif A[0] >= B[i]:
        t = -1
    else:
        while (low <= high):
            if A[t] < B[i] <= A[t+1]:
                break
            elif B[i] <= A[t]:
                high = t
            elif A[t+1] < B[i]:
                low = t+1
            t = (low + high) // 2
    low = 0
    high = N - 1
    s = (low + high) // 2
    if C[N-1] <= B[i]:
        s = N-1
    elif B[i] < C[0]:
        s = -1
    else:
        while (low <= high):
            if C[s] <= B[i] < C[s+1]:
                break
            elif B[i] < C[s]:
                high = s
            elif C[s+1] <= B[i]:
                low = s + 1
            s = (low + high) // 2
    M += (t+1)*(N-s-1)
print(M)

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

count=0
for i in range (N):
    count_A=bisect.bisect_left(A,B[i])
    count_B=len(C)-bisect.bisect_right(C,B[i])
    count+=count_A*count_B
print(count)

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

i,j,k = 0,0,0
ans = 0
while j < N:
  while i < N and a[i]<b[j] :
    i += 1
  while k < N and c[k]<=b[j]:
    k += 1
  ans += i * (N - k)
  j += 1
 

print(ans)

Solution (Python):
import bisect

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

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

ans = 0
for i in B:
    ins_a = bisect.bisect_left(A,i)
    ins_c = bisect.bisect_right(C,i)
    ans+=ins_a*(N-ins_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())))

bmul = [0]*(n+1)
ci = 0

for bi in range(n):
    while ci != n and c[ci] <= b[bi]:
        ci += 1
    bmul[bi] = n-ci
for i in range(n):
    bmul[n-i-1] += bmul[n-i]
bi = 0
ai = 0
ret = 0
for ai in range(n):
    while bi != n and b[bi] <= a[ai]:
        bi += 1
    ret += bmul[bi]
print(ret)

Solution (Python):
from bisect import 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()
B.sort()
C.sort()

B2 = [0 for i in range(N)]
for i,b in enumerate(B):
	idx = bisect_left(C,b+1)
	B2[i] = N - idx

B2sum = [0 for i in range(N+1)]
for i in range(N)[::-1]:
	B2sum[i] = B2sum[i+1] + B2[i]


ans = 0
for a in A:
	idx = bisect_left(B,a+1)
	ans += B2sum[idx]

print(ans)

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

res = 0
for bb in b:
    b_res = bisect.bisect_left(a, bb)
    c_res = len(c) - bisect.bisect_right(c, bb)
    res += (b_res*c_res)

print(res)

Solution (Python):
import bisect

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

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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
import bisect
a.sort()
b.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):
def someone_ans():
    def binary_search(L, key, mode):
        low, high = 0, len(L)
        mid = len(L) // 2
        while low <= high - 1:
            if L[mid] < key and mode == 0 or L[mid] <= key and mode == 1:
                low = mid + 1
            else:
                high = mid
            mid = (low + high) // 2
        return high

    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:
        Ai = binary_search(A, i, 0)
        Ci = len(C) - binary_search(C, i, 1)
        ans += Ai*Ci
    return ans



print(someone_ans())

Solution (Python):
def BinarySearch(L, key, mode):
    left, right = 0, len(L)
    mid = len(L)//2
    while  left <= right-1:
        if L[mid] < key and mode == 0 or L[mid] <= key and mode == 1:
            left = mid+1
        else:
            right = mid
        mid =  (left+right)//2
    return right

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 B:
    Ai = BinarySearch(A, i, 0)
    Ci = len(C)-BinarySearch(C, i, 1)
    ans += Ai*Ci
print(ans)

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

lis = [0] * n
j = 0
for i in range(n):
    while j < n and a[j] < b[i]:
        j += 1
    lis[i] = j

li = [0] * n
m = 0
j = 0
for i in range(n):
    while j < n and b[j] < c[i]:
        m += lis[j]
        j += 1
    li[i] = m
print(sum(li))

Solution (Python):
def binary_search(lst, e, mode):
    l, u = 0, len(lst)
    mid = len(lst) // 2

    while l <= u-1:
        if lst[mid] < e and mode == "l" or lst[mid] <= e and mode == "u":
            l = mid + 1
        else:
            u = mid
        mid = (l+u) // 2

    return u


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

com = 0

for i in b:
    j = binary_search(a, i, "l")
    k = len(c) - binary_search(c, i, "u")
    com += j * k

print(com)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
import bisect
a.sort()
c.sort()
cl=len(c)
cnt=0
for i in b:
    x=bisect.bisect_left(a,i)
    y=bisect.bisect_right(c,i)
    cnt+=x*(cl-y)
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())))
Ans = 0
for b in B:
    Ans += bisect.bisect_left(A,b) * (N - bisect.bisect_right(C,b))
print(Ans)

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

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

print(ans)

Solution (Python):





if __name__ == '__main__':
    n = int(input())
    a = sorted(list(map(int, input().split())))
    b = list(map(int, input().split()))
    c = sorted(list(map(int, input().split())))
    count = 0

    
    
    
    
    from bisect import bisect_left
    from bisect import bisect_right

    for number in b:
        a_count = bisect_left(a, number)
        c_count = n - bisect_right(c, number)
        count += a_count * c_count

    print(count)

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

Solution (Python):
def altars(n, a, b, c):
    up = sorted(a)
    mid = sorted(b)
    down = sorted(c, reverse=True)
    i = j = 0
    up_count = []
    down_count = []
    for midpart in mid:
        while i < n and up[i] < midpart:
            i += 1
        up_count.append(i)
    for midpart in mid[::-1]:
        while j < n and midpart < down[j]:
            j += 1
        down_count.append(j)
    down_count.reverse()
    return sum(map(lambda x, y: x * y, up_count, down_count))


if __name__ == '__main__':
    n = int(input())
    a = map(int, input().split())
    b = map(int, input().split())
    c = map(int, input().split())
    print(altars(n, a, b, c))

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:
    aa = bisect.bisect_left(a, i)
    cc = n-bisect.bisect_right(c, i)
    ans += aa*cc
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:
    aa=bisect.bisect_left(a,i)
    cc=N-bisect.bisect_right(c,i)
    ans+=aa*cc
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()]
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
d = [0] * (n + 1)
j = 0
for i in range(n):
    while j < n and b[i] <= a[j]: j += 1
    d[i] = (n - j)
for i in range(n - 1, 0, -1):
    d[i - 1] += d[i]
j = 0
s = 0
for i in range(n):
    while j < n and c[i] <= b[j]: j += 1
    s += d[j]
print(s)

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

lis = [0] * n
j = 0
for i in range(n):
    while j < n and a[j] < b[i]:
        j += 1
    lis[i] = j

li = [0] * n
m = 0
j = 0
for i in range(n):
    while j < n and b[j] < c[i]:
        m += lis[j]
        j += 1
    li[i] = m
print(sum(li))

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

B = sorted(B)
C = sorted(C)
b_list = {N:0}
for i in range(N-1,-1,-1):
    j = bisect_right(C,B[i])
    b_list[i] = N - j + b_list[i+1]
m = 0
for i in range(N):
    j = bisect_right(B,A[i])
    m += b_list[j]
print(m)

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

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()
s=0
for i in b:
    sa = bisect.bisect_left(a,i)
    sc = n-bisect.bisect_right(c,i)
    s += sa*sc
print(s)

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

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
A.sort();C.sort()
M = 0
for i in range(N):
    low = 0
    high = N - 1
    t = (low + high) // 2
    if A[N-1] < B[i]:
        t = N-1
    elif A[0] >= B[i]:
        t = -1
    else:
        while (low <= high):
            if A[t] < B[i] <= A[t+1]:
                break
            elif B[i] <= A[t]:
                high = t
            elif A[t+1] < B[i]:
                low = t+1
            t = (low + high) // 2
    low = 0
    high = N - 1
    s = (low + high) // 2
    if C[N-1] <= B[i]:
        s = N-1
    elif B[i] < C[0]:
        s = -1
    else:
        while (low <= high):
            if C[s] <= B[i] < C[s+1]:
                break
            elif B[i] < C[s]:
                high = s
            elif C[s+1] <= B[i]:
                low = s + 1
            s = (low + high) // 2
    M += (t+1)*(N-s-1)
print(M)

Solution (Python):
import bisect

n = int(input())
tops = list(map(int, input().split()))
mids = list(map(int, input().split()))
bottoms = list(map(int, input().split()))

tops.sort()
bottoms.sort()

nos_sets = 0

for m in mids:
    t = bisect.bisect_left(tops, m)
    b = n - bisect.bisect_right(bottoms, m)
    nos_sets += t * b

print(nos_sets)

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



pat=0
ikeep=-1
for i in range(n):
  x=b[i]
  if(x!=ikeep):
    ikeep=x
  else:
    pat+=paty*patz
    continue
  paty=bisect.bisect_left(a,x)
  patz=n-bisect.bisect_right(c,x)
  
  pat+=paty*patz
print(pat)

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

Solution (Python):
import sys
import math
import collections
import itertools
import array
import inspect
import bisect


sys.setrecursionlimit(10000)


def chkprint(*args):
    names = {id(v):k for k,v in inspect.currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))


def to_bin(x):
    return bin(x)[2:]


def dim2input(N):
    li = []
    for _ in range(N):
        li.append(list(map(int, input())))
    return li



dp = None

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_idx = bisect.bisect_left(A, b)
        c_idx = bisect.bisect_right(C, b)

        ans += a_idx * (N - c_idx)

    print(ans)




main()

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

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

BiggerB=[0 for i in range(N)]
j=0
for i in range(N):
     while j<N:
        if C[j] > B[i]:
            BiggerB[i]=N-j
            break
        else:
            j+=1
for i in range(N-2, -1, -1):
    BiggerB[i]+=BiggerB[i+1]

ans=0
j=0
for i in range(N):
    while j<N:
        if B[j] > A[i]:
            break
        else:
            j+=1
    if j!= N:
        ans+=BiggerB[j]
print(ans)

Solution (Python):

def solve(N, A, B, C):
    A.sort()
    B.sort()
    C.sort()
    def calc(xs, ys, ts):
        rs = [0] * N
        t = 0
        j = N - 1
        for i in range(N - 1, -1, -1):
            x = xs[i]
            while x < ys[j] and j >= 0:
                t += ts[j]
                j -= 1
            rs[i] = t
        return rs
    rs = [1] * N
    rs = calc(B, C, rs)
    rs = calc(A, B, rs)
    return sum(rs)

N = int(input())
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]
C = [int(_) for _ in input().split()]
print(solve(N, A, B, C))

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 k in b:
    i=bisect.bisect_left(a,k)
    j=bisect.bisect_right(c,k)
    if 0<i and j!=len(c):
        ans+=i*(n-j)
print(ans)

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()))
    a.sort()
    b.sort()
    c.sort()
    ans = 0
    for y in b:
        i = bisect.bisect_left(a, y)
        j = bisect.bisect_right(c, y)
        if i > 0 and j != len(c):
            ans += i * (n - j)
 
    print(ans)
 
main()

Solution (Python):
N = int(input())
A = [0 for i in range(N)]
B = [0 for i in range(N)]
C = [0 for i in range(N)]
st = input().split()
for i in range(N):
    A[i] = int(st[i])
st = input().split()
for i in range(N):
    B[i] = int(st[i])
st = input().split()
for i in range(N):
    C[i] = int(st[i])
A.sort();C.sort()
M = 0
for i in range(N):
    low = 0
    high = N - 1
    t = (low + high) // 2
    if A[N-1] < B[i]:
        t = N-1
    elif A[0] >= B[i]:
        t = -1
    else:
        while (low <= high):
            if A[t] < B[i] <= A[t+1]:
                break
            elif B[i] <= A[t]:
                high = t
            elif A[t+1] < B[i]:
                low = t+1
            t = (low + high) // 2
    low = 0
    high = N - 1
    s = (low + high) // 2
    if C[N-1] <= B[i]:
        s = N-1
    elif B[i] < C[0]:
        s = -1
    else:
        while (low <= high):
            if C[s] <= B[i] < C[s+1]:
                break
            elif B[i] < C[s]:
                high = s
            elif C[s+1] <= B[i]:
                low = s + 1
            s = (low + high) // 2
    M += (t+1)*(N-s-1)
print(M)

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

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

i = 0

ans = 0
li = []

i = 0

for Bi in B:
    while i < N and Bi >= C[i]:
        i += 1
    li += [N - i]

i = N - 1

o_ans = 0
for Ai in A[::-1]:
    while i >= 0 and Ai < B[i]:
        o_ans += li[i]
        i -= 1
    ans += o_ans

print(ans)

Solution (Python):
n=int(input())
A = list(map(int,input().split(' ')))
A.sort()
B = list(map(int,input().split(' ')))
B.sort()
C = list(map(int,input().split(' ')))
C.sort()
ia=0
ib=0
Lb = [0]*n
while ia<n and ib<n:
    if A[ia]<B[ib]:
        Lb[ib]+=1
        ia+=1
    else:
        ib+=1
jb = 0
LB=[]
bb=0
while jb<n:
    bb+=Lb[jb]
    LB.append(bb)
    jb+=1

kb=0
kc=0
Lc=[0]*n
while kb<n and kc<n:
    if B[kb]<C[kc]:
        Lc[kc]+=LB[kb]
        kb+=1
    else:
        kc+=1

jc=0
LC=[]
cc=0
while jc<n:
    cc+=Lc[jc]
    LC.append(cc)
    jc+=1
x=int(sum(LC))
print(x)

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

print(counter)

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

sa = sorted(a)
sc = sorted(c)

for x in b:
  ans += bisect.bisect_left(sa, x) * (n - bisect.bisect_right(sc, x))
print(ans)

Solution (Python):

n = int(input())

xs = sorted(map(int, input().split()))
ys = sorted(map(int, input().split()))
zs = sorted(map(int, input().split()))

bs = [0] * n
j = 0
for i in range(n):
    while j < n and xs[j] < ys[i]:
        j += 1
    bs[i] = j

cs = [0] * n
j = 0
k = 0
for i in range(n):
    while j < n and ys[j] < zs[i]:
        k += bs[j]
        j += 1
    cs[i] = k

print(sum(cs))

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()]
ans = 0
sa = sorted(a)
sc = sorted(c)

for x in b:
   ans += bisect.bisect_left(sa,x)*(n-bisect.bisect_right(sc,x)) 
    
print(ans)

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

ans = 0
for b in B:
    AB = A.searchsorted(b, side='left')
    CB = len(B) - C.searchsorted(b, side='right')
    ans += AB * CB

print(ans)

Solution (Python):


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

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

res = 0

for y in b:
    i = bisect.bisect_left(a, y)
    j = bisect.bisect_right(c, y)
    res += i * (n - j)

print(res)

Solution (Python):
import bisect
N = int(input())
Alist = list(map(int,input().split()))
Blist = list(map(int,input().split()))
Clist = list(map(int,input().split()))
Alist.sort()
Clist.sort()
retnum = 0 
for i in Blist:
    Anum = bisect.bisect_left(Alist,i)
    Cnum =  N - bisect.bisect_right(Clist,i)
    retnum +=(Anum * Cnum)
print(retnum)

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()
lc=len(c)
print(sum([(lc-bisect.bisect_right(c,j))*(bisect.bisect_left(a,j)) for j in b]))

Solution (Python):
import bisect
n=int(input())
a,b,c = [sorted(map(int,input().split())) for _ in "abc"]
print(sum([bisect.bisect_left(a,x)*(n-bisect.bisect_right(c,x)) for x 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())))

N_B = len(B)
N_C = len(C)
T = [0] * N_B

for i, b in enumerate(B):
    T[i] = N_C - bisect.bisect_right(C, b)
TT = [0] * N_B
TT[-1] = T[-1]
for i in range(N_B - 2, -1, -1):
    TT[i] = T[i] + TT[i + 1]




ans = 0
for a in A:
    i = bisect.bisect_right(B, a)
    if i >= N_B:
        continue
    ans += TT[i]
print(ans)