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):
from collections import defaultdict
N = int(input())
A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
C = sorted(list(map(int, input().split())))
ad = defaultdict(lambda: -1)
i, j = 0, 0
while i < N and j < N:
    if A[i] < B[j]:
        ad[i] = j
        i += 1
    else:
        j += 1
bd = defaultdict(lambda: -1)
i, j = 0, 0
while i < N and j < N:
    if B[i] < C[j]:
        bd[i] = j
        i += 1
    else:
        j += 1
csum = [0] * N
for i in range(N):
    if bd[i] >= 0:
        csum[i] = N-bd[i]
bsum = [0]* N
cnt = 0
for i in range(N):
    cnt += csum[N-1-i]
    bsum[N-1-i] = cnt
ans = 0
for i in range(N):
    if ad[i] >= 0:
        ans += bsum[ad[i]]
print(ans)

Solution (Python):
import bisect
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
C=[-int(i) for i in input().split()]
A.sort()
B.sort()
C.sort()
ans = 0
for b in B:
    a = bisect.bisect_left(A,b)
    c = bisect.bisect_left(C,-b)
    
    ans += a*c
print(ans)

Solution (Python):
import bisect

N = int(input())
As = sorted(list(map(int, input().split())))
Bs = sorted(list(map(int, input().split())))
Cs = sorted(list(map(int, input().split())))

cnt = 0
for b in Bs:
    a_b = bisect.bisect_left(As,b)
    b_c = N - bisect.bisect_right(Cs, b)
    
    cnt += a_b * b_c
print(cnt)

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


def main():
    n = int(readline())
    a = list(map(int,readline().rsplit()))
    b = list(map(int,readline().rsplit()))
    c = list(map(int,readline().rsplit()))
    a_s = sorted(a)
    c_s = sorted(c)
    ans = 0
    for i in b:
        d = bisect.bisect_left(a_s, i)
        e = n - bisect.bisect_right(c_s, i)
        ans += d * e
    print(ans)

if __name__ == '__main__':
    main()

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


def main():
    n = int(readline())
    a = list(map(int,readline().rsplit()))
    b = list(map(int,readline().rsplit()))
    c = list(map(int,readline().rsplit()))
    a.sort()
    c.sort()
    ans = 0
    for i in b:
        d = bisect.bisect_left(a, i)
        e = n - bisect.bisect_right(c, i)
        ans += d * e
    print(ans)

if __name__ == '__main__':
    main()

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


def main():
    n = int(readline())
    a = list(map(int,readline().rsplit()))
    b = list(map(int,readline().rsplit()))
    c = list(map(int,readline().rsplit()))
    a.sort()
    c.sort()
    ans = 0
    for i in b:
        d = bisect.bisect_left(a, i)
        e = n - bisect.bisect_right(c, i)
        ans += d * e
    print(ans)

if __name__ == '__main__':
    main()

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


def main():
    n = int(readline())
    a = list(map(int,readline().rsplit()))
    b = list(map(int,readline().rsplit()))
    c = list(map(int,readline().rsplit()))
    a.sort()
    c.sort()
    ans = 0
    for i in b:
        d = bisect.bisect_left(a, i)
        e = len(c) - bisect.bisect_right(c, i)
        ans += d * e
    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()
c.sort()
ans=0

for i in range(n):
    a_idx=bisect_left(a,b[i])
    c_idx=bisect_right(c,b[i])
    ans+=a_idx*(n-c_idx)
print(ans)

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

k = [0] * N
j = 0
for i in range(N):
    while j != N and B[i] >= C[j]:
        j += 1
    k[i] = N - j

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

ans = 0
j = 0
for i in range(N):
    while j != N and A[i] >= B[j]:
        j += 1
    ans += z[j]

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

Solution (Python):
N=int(input())
la=list(map(lambda x:[int(x),0],input().split()))
lb=list(map(lambda x:[int(x),1],input().split()))
lc=list(map(lambda x:[int(x),2],input().split()))
l=la+lb+lc
l.sort(key=lambda x:(x[0],-x[1]))
na=0
sumb=0
ans=0
for i in range(3*N):
    if l[i][1]==0:
        na+=1
    elif l[i][1]==1:
        sumb+=na
    else:
        ans+=sumb
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
ans=0
for mid in b:
    ind_t = bisect.bisect_left(a,mid)
    ind_b = bisect.bisect_right(c,mid)
    ans+=ind_t*(n-ind_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()

import bisect
rui=[0]
sm=0
for mid in b:
    ind = bisect.bisect_left(c,mid+1)
    sm+= (n-ind)
    rui.append(sm)

ans=0
for top in a:
    ind = bisect.bisect_left(b,top+1)
    tmp = rui[-1] - rui[ind]
    ans+=tmp
print(ans)

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

Solution (Python):
import bisect

N = int(input())
[A, B, C] = [sorted([int(i) for i in input().split()]) for j in range(3)]
ans = 0
for j in B:
	ans += bisect.bisect_left(A, j) * (N-bisect.bisect_right(C, j))
print(ans)

Solution (Python):
def sup(A, j):
	if A[0] >= j:
		return 0
	l = 0
	r = len(A) - 1
	while True:
		i = (l+r) // 2
		if i == len(A) - 1 and A[i] < j:
			return len(A)
		elif A[i]<j and A[i+1]>=j:
			return i+1
		else:
			if A[i] >= j:
				r = i-1
			else: 
				l = i+1		

def inf(A, j): 
    if A[-1] <= j:
        return 0
    l = 0
    r = len(A) - 1
    while True:
        i = (l+r) // 2
        if i == 0 and A[i]>j:
            return len(A)
        elif A[i]>j and A[i-1]<=j:
            return len(A)-i
        else:
            if A[i] <= j:
                l = i+1
            else: 
                r = i-1		

N = int(input())
[A, B, C] = [sorted([int(i) for i in input().split()]) for j in range(3)]
ans = 0
for j in B:
	ans += sup(A, j) * inf(C, j)
print(ans)

Solution (Python):
import bisect

N = int(input())
parts = [[] for i in range(3)]
con = [0] * N

ans = 0

for i in range(3):
  t = sorted(list(map(int,input().split())))
  parts[i] = t

under =parts[2]
middle = parts[1]
upper = parts[0]

for i in range(N):
  p = middle[i]
  ans += bisect.bisect_left(upper,p) * (N - bisect.bisect_right(under,p))
    
print(ans)

Solution (Python):

import sys
import math
import os
import itertools
import string
import heapq
from _collections import defaultdict
from functools import lru_cache
import bisect

class Scanner():
    def int():
        return int(sys.stdin.readline().rstrip())
    def string():
        return sys.stdin.readline().rstrip()
    def map_int():
        return [int(x) for x in Scanner.string().split()]
    def string_list(n):
        return [input() for i in range(n)]
    def int_list_list(n):
        return [Scanner.mapInt() for i in range(n)]
    def int_cols_list(n):
        return [int(input()) for i in range(n)]
        
class Math():
    def gcd(a,b):
        if b == 0:
            return a
        return Math.gcd(b,a % b)
    def lcm(a,b):
        return (a * b) // Math.gcd(a,b)
    def roundUp(a,b):
        return -(-a // b)
    def toUpperMultiple(a,x):
        return Math.roundUp(a,x) * x
    def toLowerMultiple(a,x):
        return (a // x) * x
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3,d + 1,2):
            if n % i == 0:
                return False
        return True

MOD = int(1e09) + 7

def main():
    N = Scanner.int()
    A = Scanner.map_int()
    B = Scanner.map_int()
    C = Scanner.map_int()
    A.sort()
    B.sort()
    C.sort()
    ans = 0
    for i in range(N):
        n,m = 0,0
        n = bisect.bisect_left(A,B[i])
        m = N - bisect.bisect_right(C,B[i])
        ans += n * m
    print(ans)
    return

if __name__ == "__main__":
    main()

Solution (Python):
from bisect import bisect

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

ans = 0
bb = [n-bisect(c, b[i])  for i in range(n)]
bbb = []
tmp = 0
for i in range(n)[::-1]:
    tmp += bb[i]
    bbb.append(tmp)
bbb = bbb[::-1]
j = 0
while i < n:
    j = bisect(b, a[i], j,n-1)
    if b[j] <= a[i]:
        break
    ans += bbb[j]
    i += 1

print(ans)

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A, B, C):
    A.sort()
    B.sort()
    C.sort()

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


def main():
    N = read_int()
    A = read_int_n()
    B = read_int_n()
    C = read_int_n()
    print(slv(N, A, B, C))


if __name__ == '__main__':
    main()

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A, B, C):
    A.sort()
    B.sort()
    C.sort()

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

    ans = 0
    for i in range(N-1, -1, -1):
        j = bisect.bisect_right(B, A[i])
        ans += D[j]

    return ans


def main():
    N = read_int()
    A = read_int_n()
    B = read_int_n()
    C = read_int_n()
    print(slv(N, A, B, C))


if __name__ == '__main__':
    main()

Solution (Python):
import bisect

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

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

print(ans)

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

N=int(input())

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

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

ans=0
for i in range(N):
    idxa=bisect_left(A,B[i])
    idxc=bisect_right(C,B[i])
    ans+=idxa*(N-idxc)

print(ans)

Solution (Python):
from _collections import deque
from _ast import Num


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor


"""
need to consider the case that ticket is not enough to lower everything
"""

N = gi()
A = [0] * N
B = [0] * N
C = [0] * N
sB = [0] * N
sC = [0] * N

for i in range(N):
    A[i] = gi()
A.sort()

for i in range(N):
    B[i] = gi()
B.sort()

nai = 0

for i in range(N):
    while nai < N and A[nai] < B[i]:
        nai += 1
    sB[i] = nai
    if i > 0:
        sB[i] += sB[i - 1]

for i in range(N):
    C[i] = gi()
C.sort()

nbi = 0
for i in range(N):
    while nbi < N and B[nbi] < C[i]:
        nbi += 1
    if nbi:
        sC[i] = sB[nbi - 1]

    if i > 0:
        sC[i] += sC[i - 1]

print(sC[-1])

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 j in range(N):
    c = N-bisect.bisect_right(C,B[j])
    b = bisect.bisect_right(A,B[j]-1)
    count += b*c
print(count)

Solution (Python):
import bisect
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
a.sort()
b.sort()
c.sort()
ans=0
for i in range(n):
  x=b[i]
  y=bisect.bisect_left(a,x)
  z=bisect.bisect_right(c,x)
  ans+=(n-z)*y
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
b = []
for i in range(N):
    b.append(N - bisect.bisect_left(C,B[i]+1))
b.append(0)
for i in range(N-1,-1,-1):
    b[i] += b[i+1]
ans = 0
for i in range(N):
    ans += b[bisect.bisect_left(B, A[i]+1)]
print(ans)

Solution (Python):
N=6



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

D=[0]*N
E=[0]*N

A=list(sorted(A,reverse=True))
B=list(sorted(B,reverse=True))
C=list(sorted(C,reverse=True))

u=N-1
d=N-1
c=0
while(True):
    if A[u] < B[d]:
        c+=1
        u-=1
    else:
        D[d]=c
        d-=1
    if(-1==u or -1==d):
        for i in range(d,-1,-1):
            D[i]=c
        break
u=0
d=0
c=0
while(True):
    if B[u] < C[d]:
        c+=1
        d+=1
    else:
        E[u]=c
        u+=1
    if(N==u or N==d):
        for i in range(u,N):
            E[i]=c
        break

s=0
for i in range(N):
    s+=D[i]*E[i]


print(s)

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

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

ue = []
for i in range(n):
    ue.append(bisect.bisect_left(a,b[i]))

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

ans = 0
for i in range(n):
    ans += ue[i]*sita[i]

print(ans)

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

def isOK(target,mid):
  return target < mid
def isOK_dep(target,mid):
  return target <= mid

def bin_search(l,r,li,target):
  left = l
  right = r
  while right-left>1:
    mid = (left + right)//2
    if isOK(target,li[mid]):
      right = mid
    else:
      left = mid
  return left

def bin_search_dep(l,r,li,target):
  left = l
  right = r
  while right-left>1:
    mid = (left + right)//2
    if isOK_dep(target,li[mid]):
      right = mid
    else:
      left = mid
  return left

ans = 0
for i in range(n):
  top,bottom = 0,0
  top += bin_search_dep(-1,n,A,B[i]) + 1
  bottom += n-bin_search(-1,n,C,B[i])-1
  ans += top * bottom
print(ans)

Solution (Python):
import bisect

N = int(input())
A = tuple(sorted(map(int, input().split())))
B = tuple(map(int, input().split()))
C = tuple(sorted(map(int, input().split())))
res = 0
for i in range(N):
	res += bisect.bisect_left(A, B[i])*(N - bisect.bisect_right(C, B[i]))
print(res)

Solution (Python):
import sys
from bisect import bisect_right as br
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
b.sort()
c.sort()
res = 0
bt = [0] * N
for j in range(N):
  y = b[j]
  k = br(c, y)
  bt[j] = N - k
csbt = [0] * (N + 1)
for i in range(N):
  csbt[i + 1] = csbt[i] + bt[i]
for i in range(N):
  x = a[i]
  j = br(b, x)
  if j < N:
    res += csbt[-1] - csbt[j]
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 = sorted(a)
b = sorted(b)
c = sorted(c)

ans = 0

for i in range(n):
    ans1 = 0
    ans2 = 0
    ok = -1
    ng = n
    while abs(ng - ok) > 1:
        mid = (ok + ng) // 2
        if a[mid] < b[i] :
            ok = mid
        else:
            ng = mid
        if abs(ng - ok) == 1:
            ans1 += ok + 1
    ok = n
    ng = -1
    while abs(ng - ok) > 1:
        mid = (ok + ng) // 2
        if b[i] < c[mid]:
            ok = mid
        else:
            ng = mid
        if abs(ng - ok) == 1:
            ans2 += n - ok
    ans += ans1 * ans2
print(ans)

Solution (Python):

import sys

def fill(D, A, c):
    for a in A:
        if a not in D:
            D[a] = {'A': 0, 'B': 0, 'C': 0}
        D[a][c] += 1


def solve(N: int, A, B, C):
    D = {}
    fill(D, A, 'A')
    fill(D, B, 'B')
    fill(D, C, 'C')
    D = sorted(D.items(), key=lambda x: x[0], reverse=True)

    a = 0
    b = 0
    c = 0
    for k, v in D:
        a += b * v['A']
        b += c * v['B']
        c += v['C']

    print(a)


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

if __name__ == '__main__':
    main()

Solution (Python):
import bisect
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
A.sort()
B.sort()
C.sort()
ans=0
BC=[0]*N
for i in range(N):
  id = bisect.bisect_right(C,B[i])
  BC[i]=N-id
sBC=[0]*(N+1)
for i in range(1,N+1):
  sBC[i]=sBC[i-1]+BC[i-1]
for i in range(N):
  id=bisect.bisect_right(B,A[i])
  ans+=sBC[N]-sBC[id]
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()
lenC = len(C)


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

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

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

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()
    c.sort()
    ans=0
    for x in b:
        ans += bisect.bisect_left(a, x) * (n - bisect.bisect_right(c, x))
    print(ans)
if __name__ == "__main__":
    main()

Solution (Python):
import sys
import bisect
n = int(input())
a_ls = [int(i) for i in sys.stdin.readline().split()]
b_ls = [int(i) for i in sys.stdin.readline().split()]
c_ls = [int(i) for i in sys.stdin.readline().split()]
a_ls.sort()
b_ls.sort()
c_ls.sort()
comb_a = []
comb_b = []
len_b = len(b_ls)
len_c = len(c_ls)
for a in a_ls:
    ind = len_b - bisect.bisect_right(b_ls, a)
    comb_a.append(ind)

prod_b = []
res = 0
for b in b_ls[::-1]:
    ind = len_c - bisect.bisect_right(c_ls, b)
    comb_b.append(ind)
    res += ind
    prod_b.append(res)
comb_b = comb_b[::-1]
_sum = 0
for a in comb_a:
    if a <= 0:
        continue
    _sum += prod_b[a-1]
print(_sum)

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

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

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

bcb_cum = [0] * (n+1)
for i in range(n):
    bcb_cum[i+1] = bcb_cum[i] + n - bisect_right(c, b[i])
    
for x in a:
    l = bisect_right(b, x)
    ans += bcb_cum[n] - bcb_cum[l]

print(ans)

Solution (Python):

import sys
def input():
	return sys.stdin.readline().strip()

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

B_num = []
a = 0
for i in range(N):
	while a < N and A[a] < B[i]:
		a += 1
	if a == N:
		B_num.append(N)
	else:
		B_num.append(a)

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


C_num = []
b = 0
for i in range(N):
	while b < N and B[b] < C[i]:
		b += 1
	if b == N:
		C_num.append(B_num[-1])
	elif b > 0:
		C_num.append(B_num[b - 1])
print(sum(C_num))

Solution (Python):
import sys

input = sys.stdin.readline

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

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

ans = 0


BB = [0] * N
a = 0
for b in range(N):
    while A[a] < B[b] and a <= N-2:
        a += 1
    if a == N-1 and A[a] < B[b]:
        BB[b] = N
    else:
        BB[b] = a


Bsum = [0] * N
Bsum[0] = BB[0]
for bb in range(1,N):
    Bsum[bb] = Bsum[bb-1] + BB[bb]


ans = 0
b_ = 0
for c in range(N):
    while B[b_] < C[c] and b_ <= N-2:
        b_ += 1
    if b_ == N-1 and B[b_] < C[c]:
        b_ += 1
    b_ -= 1
    if b_ == -1:
        b_ = 0
        continue
    
    ans += Bsum[b_]

print(ans)

Solution (Python):
def slove():
    import sys
    import bisect
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    b = list(map(int, input().rstrip('\n').split()))
    c = list(map(int, input().rstrip('\n').split()))
    a.sort()
    b.sort()
    c.sort()
    t = 0
    for v in b:
        an = bisect.bisect_left(a, v)
        cn = n - bisect.bisect_right(c, v)
        t += (an * cn)
    print(t)


if __name__ == '__main__':
    slove()

Solution (Python):
n = int(input())
an = sorted([int(i) for i in input().split()])
bn = sorted([0]+[int(i) for i in input().split()])
cn = sorted([int(i) for i in input().split()])
dpb = [0]*(n+1)
dpc = [0]*n
cnt = 0
for i in range(n+1):
  while cnt < n and bn[i] > an[cnt]:
    cnt += 1
  dpb[i] = cnt
cnt = 0
temp = 0
for i in range(n):
  while cnt < n+1 and cn[i] > bn[cnt]:
    temp += dpb[cnt]
    cnt += 1
  dpc[i] = temp
print(sum(dpc))

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

Solution (Python):
import bisect
import numpy as np
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_bisect = [bisect.bisect_left(a,one) for one in b]
b_cumsum = np.cumsum(b_bisect)
c_bisect = [bisect.bisect_left(b,one) for one in c]

ans = 0
for one in c_bisect:
    if one == 0:
        continue
    else:
        ans += b_cumsum[one-1]
print(ans)

Solution (Python):
import bisect

n = int(input())
a_list = [int(x) for x in input().split()]
b_list = [int(x) for x in input().split()]
c_list = [int(x) for x in input().split()]

a_list = sorted(a_list)
b_list = sorted(b_list)
c_list = sorted(c_list)

ans = 0

for b_i in b_list:
    a_count_i = bisect.bisect_left(a_list,b_i)
    c_count_i = len(c_list) - bisect.bisect_right(c_list,b_i)
    ans += a_count_i * c_count_i

print(ans)

Solution (Python):
import bisect

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

BC = {}
for i in range(N):
  rank = N - bisect.bisect_right(C, B[i])
  BC[B[i]] = rank

BCS = [0 for _ in range(N)]
BCS[N-1] = BC[B[N-1]]
for i in range(N-2,-1,-1):
  BCS[i] =  BCS[i+1] + BC[B[i]]


ans = 0
for i in range(N):
  rank = bisect.bisect_right(B, A[i])
  if rank < N:
    ans += BCS[rank]
print(ans)

Solution (Python):
from bisect import bisect_right


def main():
    n = int(input())
    a = sorted(list(map(int, input().split())))
    b = sorted(list(map(int, input().split())))
    c = sorted(list(map(int, input().split())))
    l = [0] * n
    for index, i in enumerate(b):
        l[index] = n - bisect_right(c, i)
    s = [l[-1]] * n
    for i in range(n - 2, -1, -1):
        s[i] = s[i + 1] + l[i]
    ans = 0
    for i in a:
        j = bisect_right(b, i)
        if j < n:
            ans += s[j]
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import bisect as bi

N=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))

a.sort()
c.sort()



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

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

Solution (Python):
import bisect

N = int(input())

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

A.sort()
C.sort()
LC = len(C)
ans = 0
for b in B:
    na = bisect.bisect_left(A, b)
    nc = LC - bisect.bisect_right(C, b)
    ans += na * nc

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

A = sorted(A)
B = sorted(B)
C = sorted(C)

ia = -1
ic = -1

for b in B:
  while True:
    if ia == N-1 or A[ia+1]>=b: 
      break
    ia += 1
  while True:
    if ic == N-1 or C[ic+1]>b:
      break
    ic += 1
  s += (ia+1)*(N-ic-1)
    
print(s)

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

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 B:
    ia = min(bisect.bisect_left(A,b),N-1)
    ic = min(bisect.bisect_right(C,b),N-1)
    a = ia if A[ia]>=b else ia+1
    c = N-ic-1 if C[ic]<=b else N-ic
    ans += a*c
print(ans)

Solution (Python):
from bisect import bisect_left, bisect_right

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 += bisect_left(a, i) * (n - bisect_right(c, i))

print(ans)

Solution (Python):
import bisect

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

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

for i in range(n):
    count1 = bisect.bisect_left(a, b[i])
    count2 = n-bisect.bisect_left(c, b[i]+1)
    ans+=count1*count2
    count1=0
    count2=0
print(ans)

Solution (Python):
from bisect import bisect_right, bisect_left

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

print(ans)

Solution (Python):
import bisect
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
ad=sorted(a)
bd=sorted(b)
cd=sorted(c)



count=0
for i in range(n):
  al=bisect.bisect_left(ad,bd[i])
  cl=bisect.bisect_right(cd,bd[i])
  sum=al * ( n - cl )
  
  count=count+( al * ( n - cl ) )
print(count)