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 sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [int(input_raw[0])]
    input_raw = read_func().strip().split()
    A = []
    for i in range(N):
        A.append(int(input_raw[i]))
    input_raw = read_func().strip().split()
    B = []
    for i in range(N):
        B.append(int(input_raw[i]))
    input_raw = read_func().strip().split()
    C = []
    for i in range(N):
        C.append(int(input_raw[i]))
    A.sort()
    B.sort()
    C.sort()

    A_backet = {}
    C_backet = {}
    A_ptr = 0L
    C_ptr = 0L
    count = 0L
    for i in range(N):
        while A_ptr < N and A[A_ptr] < B[i]:
            A_ptr += 1L
        while C_ptr < N and C[C_ptr] <= B[i]:
            C_ptr += 1L
        count += A_ptr * (N - C_ptr)
    print count




if __name__ == '__main__':
    main()

Solution (Python):
import bisect
N = int(input())
J = list(map(int,input().split()))
K = list(map(int,input().split()))
L = list(map(int,input().split()))
J.sort()
K.sort()
L.sort()
S = 0
for i in range(N):
  k = bisect.bisect_left(J,K[i])
  l = N - bisect.bisect_right(L,K[i])  
  S += k*l
print(S)

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 = sorted(a)
b = sorted(b)
c = sorted(c)
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 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()
    S = [0]

    s = 0
    i = 0
    for b in B:
        while i < N:
            if b <= A[i]:
                break
            i += 1
        s += i
        S.append(s)

    t = 0
    i = 0
    for c in C:
        while i < N:
            if c <= B[i]:
                break
            i += 1
        t += S[i]


    print(t)


if __name__ == '__main__':
    main()

Solution (Python):
import bisect
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 = sorted(A)
B = sorted(B)
C = sorted(C)


def my_func(A, B, C):
    acc_a = [0]*N
    acc_c = [0]*N

    i_a = 0
    i_c = N-1
    for i in range(N):
        b = B[i]
        while i_a < N and A[i_a] < b:
            acc_a[i] += 1
            i_a += 1

    for i in reversed(range(N)):
        b = B[i]
        while i_c >= 0 and C[i_c] > b:
            acc_c[i] += 1
            i_c -= 1

    acc_a = list(accumulate(acc_a))
    acc_c = list(accumulate(acc_c[::-1]))
    acc_c = acc_c[::-1]

    ans = 0
    for a, c in zip(acc_a, acc_c):
        ans += a*c


def bisect_func(A, B, C):
    acc_a = [0]*N
    acc_c = [0]*N
    for i in range(N):
        acc_a[i] = bisect.bisect_left(A, B[i])
        acc_c[i] = N-bisect.bisect_right(C, B[i])

    ans = 0
    for a, c in zip(acc_a, acc_c):
        ans += a*c
    return ans


print(bisect_func(A, B, C))

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 = sorted(A)
B = sorted(B)
C = sorted(C)

acc_a = [0]*N
acc_c = [0]*N

i_a = 0
i_c = N-1
for i in range(N):
    b = B[i]
    while i_a < N and A[i_a] < b:
        acc_a[i] += 1
        i_a += 1

for i in reversed(range(N)):
    b = B[i]
    while i_c >= 0 and C[i_c] > b:
        acc_c[i] += 1
        i_c -= 1

acc_a = list(accumulate(acc_a))
acc_c = list(accumulate(acc_c[::-1]))
acc_c = acc_c[::-1]

ans = 0
for a, c in zip(acc_a, acc_c):
    ans += a*c
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 = sorted(A)
B = sorted(B)
C = sorted(C)

acc_a = [0]*N
acc_c = [0]*N

i_a = 0
i_c = N-1
for i in range(N):
    b = B[i]
    while i_a < N and A[i_a] < b:
        acc_a[i] += 1
        i_a += 1

for i in reversed(range(N)):
    b = B[i]
    while i_c >= 0 and C[i_c] > b:
        acc_c[i] += 1
        i_c -= 1

acc_a = list(accumulate(acc_a))
acc_c = list(accumulate(acc_c[::-1]))
acc_c = acc_c[::-1]

ans = 0
for a, c in zip(acc_a, acc_c):
    ans += a*c
print(ans)

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

BC_type_num = []
pre = 0
for b in reversed(B):
    upper_bound = bisect.bisect_right(C, b)
    BC_type_num.append(pre + N - upper_bound)
    pre += N - upper_bound
BC_type_num.reverse()

res = 0
for a in A:
    upper_bound = bisect.bisect_right(B, a)
    if upper_bound < N:
        res += BC_type_num[upper_bound]

print res

Solution (Python):
def inpl(): return [int(i) for i in input().split()]
import bisect
N = int(input())
A = sorted(inpl())
B = inpl()
C = sorted(inpl())
ans = 0
for b in B:
    ans += bisect.bisect_left(A,b)*(N-bisect.bisect_right(C,b))
print(ans)

Solution (Python):


II = lambda: int(input())
MI = lambda: map(int, input().split())


def main():
    N = II()
    A, B, C = sorted(list(MI())), sorted(list(MI())), sorted(list(MI()))

    s = [i for i in range(1, N + 1)]
    ia = N-1
    ns = [None] * N
    for ib in range(N-1, -1, -1):
        while ia >= 0 and A[ia] >= B[ib]:
            ia -= 1
        if ia >= 0:
            ns[ib] = s[ia]
        else:
            ns[ib] = 0
    for i in range(1, N):
        ns[i] += ns[i-1]

    s = ns
    ib = N-1
    ns = [None] * N
    for ic in range(N-1, -1, -1):
        while ib >= 0 and B[ib] >= C[ic]:
            ib -= 1
        if ib >= 0:
            ns[ic] = s[ib]
        else:
            ns[ic] = 0
    return sum(ns)


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

Solution (Python):
from bisect import bisect_left,bisect
N = int(input())
info = [sorted(list(map(int,input().split()))) for i in range(3)]
ans = 0
for j in range(N):
    small = bisect_left(info[0],info[1][j])
    large = N - bisect(info[2],info[1][j])
    ans += small * large
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(' ')))

sorted_a = sorted(a)
sorted_c = sorted(c)
ans = 0
for each_b in b:
    ai = bisect.bisect_left(sorted_a, each_b)
    ci = bisect.bisect_right(sorted_c, each_b)
    ans += ai * (len(c)-ci)
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(' ')))

sorted_a = sorted(a)
sorted_b = sorted(b)
sorted_c = sorted(c)
ans = 0
before_a = 0
before_c = 0
count_a = 0
count_c_rest = 0
for each_b in sorted_b:
    for ai in range(before_a, len(sorted_a)):
        if each_b > sorted_a[ai]:
            count_a += 1
            before_a += 1
        else:
            before_a = ai
            break

    for ci in range(before_c, len(sorted_c)):
        if sorted_c[ci] <= each_b:
            count_c_rest += 1
            before_c += 1
        else:
            before_c = ci
            break

    ans += count_a * (len(sorted_c)-count_c_rest)
print(ans)

Solution (Python):
def inpl(): return [int(i) for i in input().split()]
import bisect
N = int(input())
A = sorted(inpl())
B = inpl()
C = sorted(inpl())
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, 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 range(N):
    a=bisect_left(A, B[j])
    c=bisect_right(C, B[j])
    ans+=a*(N-c)
print(ans)

Solution (Python):
import bisect
N=int(input().strip())
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(len(b)):
    la=bisect.bisect_left(a, b[i])
    ra=bisect.bisect_right(c, b[i])
    
    ans+=la*(len(c)-ra)





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 = sorted(A)
B = sorted(B)
C = sorted(C)

BB = [0] * N

cidx = 0
for i in range(N):
    while cidx < N and C[cidx] <= B[i]:
        cidx += 1
    BB[i] = N - cidx

BBB = [0] * N
for i in range(N - 1, -1, -1):
    if (i == N - 1):
        BBB[i] = BB[i]
    else:
        BBB[i] = BB[i] + BBB[i+1]

sum = 0
bidx = 0
for i in range(N):
    while bidx < N and B[bidx] <= A[i]:
        bidx += 1
    if bidx != N:
        sum += BBB[bidx]

print(sum)

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

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

Z = []

for a in A:
    Z.append([a,0])
    
for b in B:
    Z.append([b,1])

for c in C:
    Z.append([c,2])
    
Z.sort()

accum_a = 0
a_buf = 0
accum_c = 0
c_buf = 0
b_buf = 0
prev_z = 0
ans = 0
for z in Z:
    if z[0]==prev_z:
        if z[1]==0:
            a_buf+=1
        elif z[1]==1:
            b_buf+=1
        else:
            c_buf+=1
    else:
        prev_z = z[0]
        accum_c+=c_buf
        ans+=b_buf*(N-accum_c)*accum_a
        accum_a+=a_buf
        a_buf=0
        b_buf=0
        c_buf=0
        if z[1]==0:
            a_buf+=1
        elif z[1]==1:
            b_buf+=1
        else:
            c_buf+=1
            
print(ans)

Solution (Python):
import bisect
import math
import sys
n=int(input())
ans=0
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
a.sort()
b.sort()
c.sort()
for i in b:
    aa=bisect.bisect_left(a,i)
    cc=len(c)-bisect.bisect_right(c,i)
    ans+=aa*cc
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()
b.sort()
c.sort()
d=0
for i in b:
    d+=bisect_left(a,i)*(n-bisect_right(c,i))
print(d)

Solution (Python):
n = int(input())
list_a = list(map(int, input().split()))
list_b = list(map(int, input().split()))
list_c = list(map(int, input().split()))
list_a.sort()
list_b.sort()
list_c.sort()
list_a.append(10**10)
list_b.append(10**10)
list_c.append(10**10)

list_memo = []
i = 0
j = 0
k = 0
start = 0
list_ans = []
a_count = 0
while k < n:
    if list_c[k] == min(list_a[i], list_b[j], list_c[k]):
        list_memo.append(0)
        list_memo[-1] = sum(list_memo[start:-1])
        list_ans.append(list_memo[-1])
        start = len(list_memo) - 1
        k += 1
    elif list_b[j] == min(list_a[i], list_b[j], list_c[k]):
        list_memo.append(a_count)
        j += 1
    else:
        list_memo.append(0)
        a_count += 1
        i += 1
print(sum(list_ans))

Solution (Python):


from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18

N = input()
A = [-INF] + sorted(map(int, raw_input().split())) + [INF]
B = sorted(map(int, raw_input().split()))
C = [-INF] + sorted(map(int, raw_input().split())) + [INF]

ans = 0

pos1 = 0
pos2 = 0

for num in B:
    while A[pos1] < num:
        pos1 += 1
    pos1 -= 1
    while C[pos2] <= num:
        pos2 += 1
    pos2 -= 1
    ans += pos1 * (N - pos2)

print ans

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

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

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

cnt = 0
for j in range(n):
    ai = bisect.bisect_left(a,b[j])
    ci = n-bisect.bisect_right(c,b[j])
    cnt += ai*ci
print(cnt)

Solution (Python):
from bisect import *
n,a,b,c=[sorted(map(int,input().split())) for _ in [0]*4]
print(sum(bisect_left(a,x)*(n[0]-bisect_right(c,x)) for x in b))

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

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

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

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

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

d = 0
e = 0
ans = 0
for i in b:
    while i > a[min(d,n-1)] and d <= n-1:
        d += 1
    while i >= c[min(e,n-1)] and e < n:
        e += 1
    ans += d*(n-e)

print(ans)

Solution (Python):
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import *

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

N = read()
A = sorted(reads())
B = sorted(reads())
C = sorted(reads())

numB = [0] * N
for i in range(N):
  numB[i] = N - bisect_left(C, B[i]+1)
print(numB, file=stderr)

anumB = [0] * (N+1)
for i in range(N):
  anumB[i+1] = anumB[i] + numB[i]
print(anumB, file=stderr)

numA = [0] * N
for i in range(N):
  numA[i] = anumB[N] - anumB[bisect_left(B, A[i]+1)]
print(numA, file=stderr)

print(sum(numA))

Solution (Python):
import bisect
n = int(input())
As = list(map(int, input().split()))
Bs = sorted(list(map(int, input().split())))
Cs = sorted(list(map(int, input().split())))

cnt = [0]*(n+1)
v = 0
for idx, b in enumerate(Bs[::-1]):
    v += len(Cs) - bisect.bisect_right(Cs, b)
    cnt[n-1-idx] = v

print(sum([cnt[bisect.bisect_right(Bs, a)] for a in As]))

Solution (Python):
from bisect import bisect_left, bisect_right
n = int(input())
a, b, c = [sorted(list(map(int, input().split()))) for i in range(3)]
ans = 0
for i in range(n):
  j = bisect_left(a, b[i])
  k = bisect_right(c, b[i])
  ans += j*(n-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()))
answer=0


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


Acount=[]

Bcount=[]

Bsum=[]

count=0
for i in range(N):
  while(count!=N and A[i]>=B[count]): 
    count+=1
  Acount.append(N-count)

count=0
for i in range(N):
  while(count!=N and B[i]>=C[count]):
    count+=1
  Bcount.append(N-count) 
  
Bsum.append(Bcount[N-1])



for i in range(1,N):
  Bsum.append(Bcount[N-i-1]+Bsum[i-1])

for i in range(N):
  if Acount[i]==0:
    print(answer)
    exit()
  answer+=Bsum[Acount[i]-1]
print(answer)

Solution (Python):
n = int(input())
a = sorted(map(int, input().split()))
b = sorted(map(int, input().split()))
c = sorted(map(int, input().split()))
p = 0
ans = 0
f = []
g = [0] * n
for i, x in enumerate(b):
    while p < n and x >= c[p]:
        p += 1
    f.append(n - p)
for i in range(n - 1, -1, -1):
    g[i] = f[i] + (g[i + 1] if i < n - 1 else 0)
p = 0
for i, x in enumerate(a):
    while p < n and x >= b[p]:
        p += 1
    ans += g[p] if p < n else 0
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 b in B:
    lenA = bisect.bisect_left(A,b)
    lenC = N - bisect.bisect(C,b)
    ans += lenA*lenC
    
    
print(ans)

Solution (Python):
import bisect

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

count = 0
for i in b:
	count += bisect.bisect_left(a,i)*(n-bisect.bisect_right(c,i))

print count

Solution (Python):
import bisect

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

ans = 0

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

print(ans)

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

count = 0

A.sort()
C.sort()

for i in B:
    t = bisect.bisect_left(A,i)
    s = N - bisect.bisect_right(C,i)
    count += t*s
print(count)

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:
	a2 = bisect.bisect_left(a,i)
	c2 = len(c) - bisect.bisect_right(c,i)
	ans += a2 * c2
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()
total = 0
for i in range(N):
  total += bisect.bisect_left(a,b[i]) * (N - bisect.bisect_right(c,b[i]))
print(total)

Solution (Python):

import bisect
n=int(input())

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

ret=0

for i in range(n):
    numa = bisect.bisect_left(a,b[i])
    numc = len(c) - bisect.bisect_right(c,b[i])
    ret+=numa*numc
print(ret)

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


cnum=[0 for i in range(n)]
for i in range(n):
    cnum[i] = len(c) - bisect.bisect_right(c,b[i])

accum=[0 for i in range(n+1)]
for i in reversed(range(n)):
    accum[i]=accum[i+1]+cnum[i]

ret=0
for i in range(n):
    idx=bisect.bisect_right(b,a[i])
    ret+=accum[idx]
print(ret)

Solution (Python):
import bisect

raw_input = input()
input_number = int(raw_input)
counts = 0

top_lane = input().split()
mid_lane = input().split()
bot_lane = input().split()

for i in range(input_number):
    top_lane[i] = int(top_lane[i])
    mid_lane[i] = int(mid_lane[i])
    bot_lane[i] = int(bot_lane[i])

top_lane.sort()
mid_lane.sort()
bot_lane.sort()

for mid in mid_lane:
    top_index = bisect.bisect_left(top_lane, mid)
    bot_index = input_number - bisect.bisect_right(bot_lane, mid)
    counts += top_index * bot_index

print(counts)

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

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()))
assert len(a) == len(b) == len(c) == n

a = sorted(a)
b = sorted(b)
c = sorted(c)

def num(top, bottoms):
    nu = bisect.bisect(bottoms, top)
    return n - nu

def numb(bottom, tops):
    nu = bisect.bisect_left(tops, bottom)
    return nu

res = 0
for i in range(n):
    res += num(b[i], c) * numb(b[i], a)
print(res)

Solution (Python):
n = int(input())
a = sorted(list(map(int, input().split())), reverse=True)
b = sorted(list(map(int, input().split())), reverse=True)
c = sorted(list(map(int, input().split())), reverse=True)
m1 = [-1 for i in range(n)]
j = 0
for i in range(n):
    while j < n:
        if a[j] < b[i]:
            m1[i] = j
            break
        j += 1
m3 = [-1 for i in range(n)]
j = 0
for i in range(n):
    while j < n:
        if b[j] < c[i]:
            m3[i] = j
            break
        j += 1
temp = 0
m2 = []
for i in m1[::-1]:
    if i == -1:
        m2.append(0)
    else:
        m2.append(temp + (n-i))
        temp = m2[-1]
m2.reverse()
ans = 0
for i in m3:
    if i != -1:
        ans += m2[i]
print(ans)

Solution (Python):
import sys
import bisect
n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))
b = list(map(int, sys.stdin.readline().split()))
c = list(map(int, sys.stdin.readline().split()))
a.sort()
b.sort()
c.sort()
r = 0
for bn in b:
  cn = n - bisect.bisect_right(c, bn)
  an = bisect.bisect_left(a, bn)
  r += an * cn
print(r)

Solution (Python):
import numpy as np

N=int(input())
all_lst=[]
for i in range(3):
    buf = input()
    for e in buf.split():
        all_lst.append((int(e), 3 - i))
        
all_lst = sorted(all_lst, key = lambda x: (x[0], x[1]))
ac, bc, cc = 0, 0, 0
for tpl in all_lst:
    if tpl[1]==3:
        ac += 1
    elif tpl[1]==2:
        bc += ac
    elif tpl[1] == 1:
        cc += bc
print(cc)

Solution (Python):

import bisect
N = int(input())

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

As.sort()
Bs.sort()
Cs.sort()

ans = 0












Bcnt = [0 for i in range(N)]
i = 0
for b in Bs:
    cnt = N - bisect.bisect_right(Cs, b)
    
    Bcnt[i] = cnt
    i += 1

Bsum = [0 for i in range(N)]
Bsum[N-1] = Bcnt[N-1]
for i in range(N-2, -1, -1):
    Bsum[i] = Bsum[i+1] + Bcnt[i]
    

for a in As:
    cnt = bisect.bisect_right(Bs, a)
    if cnt == N:
        ans += 0
    else:
        ans += Bsum[cnt]



print(ans)

Solution (Python):
import bisect

N = int(input())
s = sorted(list(map(int,input().split())))
m = sorted(list(map(int,input().split())))
l = sorted(list(map(int,input().split())))
count = 0
for mitem in m:
  count += bisect.bisect_left(s,mitem)*(N-bisect.bisect_right(l,mitem))
print (count)

Solution (Python):

from bisect import bisect_left, bisect
N = int(input())
A = sorted(tuple(map(int, input().split())))
B = sorted(tuple(map(int, input().split())))
C = sorted(tuple(map(int, input().split())))

AB = []
result = 0
 
for i in range(N):
    AB = bisect_left(A, B[i])
    BC = N - bisect(C, B[i])
    result += AB * BC

print(result)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n = I()
    a = sorted(LI())
    b = sorted(LI())
    c = sorted(LI())
    t = [0] * n
    j = 0
    for i in range(n):
        bi = b[i]
        while j < n and c[j] <= bi:
            j += 1
        t[i] = n - j
    r = 0
    for i in range(n-2,-1,-1):
        t[i] += t[i+1]
    j = 0
    for i in range(n):
        ai = a[i]
        while j < n and b[j] <= ai:
            j += 1
        if j < n:
            r += t[j]

    return r



print(main())

Solution (Python):
import sys
import random

def rd():
        return sys.stdin.readline().rstrip()

def rdi():
        return [ int(x) for x in rd().split() ]

N, = rdi()
A = rdi()
B = rdi()
C = rdi()

a = sorted(A)
b = sorted(B)
c = sorted(C)

s = []
j = 0
for i in range(N):
        while j < N and a[j] < b[i]:
                j += 1
        if i == 0:
                s.append(j)
        else:
                s.append(s[i-1] + j)

t = 0
j = 0
for i in range(N):
        while j < N  and b[j] < c[i]:
                j += 1
        
        if j > 0:
                t += s[j-1]

print t

Solution (Python):

import bisect

def SnukeFestival(N, A, B, C):
    A = sorted(A)
    B = sorted(B)
    C = sorted(C)
    count = 0

    for b in B:
        n_a = bisect.bisect_left(A,b)
        n_c = bisect.bisect_right(C,b)
        count += n_a * (N - n_c)

    print(count)

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

Solution (Python):
import bisect

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

a = sorted(a)
b = sorted(b)
c = sorted(c)

ans = 0

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

print(ans)

Solution (Python):
import bisect

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

a = sorted(a)
b = sorted(b)
c = sorted(c)

ans = 0

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

print(ans)

Solution (Python):


import bisect

def f(n, aVals, bVals, cVals):
    

    bVals = sorted(bVals)
    cVals = sorted(cVals)

    xs = [0]*(n+1)
    x = 0
    for i in range(n-1, -1, -1):
        x += n - bisect.bisect_right(cVals, bVals[i])
        xs[i] = x

    return sum(xs[bisect.bisect_right(bVals, a)]
               for a in aVals)


def test():
    assert f(2, [1, 5], [2, 4], [3, 6]) == 3
    assert f(3, [1, 1, 1], [2, 2, 2], [3, 3, 3]) == 27
    assert f(6, [3, 14, 159, 2, 6, 53],
             [58, 9, 79, 323, 84, 6],
             [2643, 383, 2, 79, 50, 288]) == 87


def main():
    n = int(input())
    aVals = list(map(int, input().split()))
    bVals = list(map(int, input().split()))
    cVals = list(map(int, input().split()))

    print(f(n, aVals, bVals, cVals))


if __name__ == '__main__':
    main()

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

a.sort()
c.sort()


def bisect_left(a, x, lo=0, hi=None):
    """Return the index where to insert item x in list a, assuming a is sorted.
    The return value i is such that all e in a[:i] have e < x, and all e in
    a[i:] have e >= x.  So if x already appears in the list, a.insert(x) will
    insert just before the leftmost x already there.
    Optional args lo (default 0) and hi (default len(a)) bound the
    slice of a to be searched.
    """

    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if a[mid] < x: lo = mid+1
        else: hi = mid
    return lo

def bisect_right(a, x, lo=0, hi=None):
    """Return the index where to insert item x in list a, assuming a is sorted.
    The return value i is such that all e in a[:i] have e <= x, and all e in
    a[i:] have e > x.  So if x already appears in the list, a.insert(x) will
    insert just after the rightmost x already there.
    Optional args lo (default 0) and hi (default len(a)) bound the
    slice of a to be searched.
    """

    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if x < a[mid]: hi = mid
        else: lo = mid+1
    return lo

num = 0
for i in range(N):
    num += (bisect_left(a, b[i])) * (len(c) - bisect_right(c, b[i]))
print(num)

Solution (Python):
length = int(input())
smallest = sorted([int(n) for n in input().split(" ")])
normal = sorted([int(n) for n in input().split(" ")], reverse = True)
biggest = sorted([int(n) for n in input().split(" ")], reverse = True)
norm_vs_s = [0 for n in range(length)]
index = length - 1
for i, n in enumerate(smallest):
    if normal[0] < n:
        index = i
        break
for i in range(length):
    while index >= 0 and normal[i] <= smallest[index]:
        index -= 1
    norm_vs_s[i] = index + 1
answer = 0
sum_normal = sum(norm_vs_s)
index = 0
for i in range(length):
    while index < length and normal[index] >= biggest[i]:
        sum_normal -= norm_vs_s[index]
        index += 1
    answer += sum_normal
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()))
ans = 0
A.sort()
B.sort()
C.sort()

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

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

print(ans)

Solution (Python):
import bisect

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort(); b.sort(); c.sort()
dp = [0]*(n+1)
for i in range(n-1, -1, -1) :
    ins_c = bisect.bisect_right(c, b[i])
    dp[i] = dp[i+1] + (n-ins_c)
ans = 0
for i in range(n) :
    ins_b = bisect.bisect_right(b, a[i])
    ans += dp[ins_b]

print(ans)