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

Solution (Python):
def binary_search_left(n, key, in_list):
    left = 0
    right = n
    while left < right:
        mid = int((left+right)/2)
        if in_list[mid] < key:
            left = mid + 1
        else:
            right = mid
    return left


def binary_search_right(n, key, in_list):
    left = 0
    right = n
    while left < right:
        mid = int((left+right)/2)
        if in_list[mid] <= key:
            left = mid + 1
        else:
            right = mid
    return left


if __name__ == "__main__":
    n = int(input())
    a_list = list(map(int, input().split()))
    b_list = list(map(int, input().split()))
    c_list = list(map(int, input().split()))
    a_list.sort()
    c_list.sort()
    ans = 0
    for b in b_list:
        a = binary_search_left(n, b, a_list)
        c = binary_search_right(n, b, c_list)
        ans += a*(n-c)
    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 b in b_list:
    a = bisect.bisect_left(a_list, b)
    c = bisect.bisect_right(c_list, b)
    ans += a*(n-c)
print(ans)

Solution (Python):
import itertools as ite
import math
import random

INF = 10 ** 18
MOD = 10 ** 9 + 7

N = input()
A = map(int, raw_input().split())
B = map(int, raw_input().split())
C = map(int, raw_input().split())
ls = []
for num in A:
    ls += [(num, 2)]
for num in B:
    ls += [(num, 1)]
for num in C:
    ls += [(num, 0)]
ls.sort()

sumA = sumB = 0
ans = 0
for num, tp in ls:
    if tp == 0:
        ans += sumB
    elif tp == 1:
        sumB += sumA
    else:
        sumA += 1
print ans

Solution (Python):
import itertools as ite
import math
import random

INF = 10 ** 18
MOD = 10 ** 9 + 7

N = input()
A = map(int, raw_input().split())
B = map(int, raw_input().split())
C = map(int, raw_input().split())
ls = []
for num in A:
    ls += [(num, 2)]
for num in B:
    ls += [(num, 1)]
for num in C:
    ls += [(num, 0)]
ls.sort()

sumA = sumB = 0
ans = 0
for num, tp in ls:
    if tp == 0:
        ans += sumB
    elif tp == 1:
        sumB += sumA
    else:
        sumA += 1
print ans

Solution (Python):

def isOK(lis, index, key):
    if lis[index] > key:
        return True
    else:
        return False
def binary_search(lis, key):
    ng = -1
    ok = len(lis)
    while abs(ok - ng) > 1:
        mid = int((ok + ng)/2)
        if isOK(lis, mid, key):
            ok = mid
        else:
            ng = mid
    return ok

N = int(input())
A = sorted([int(a) for a in input().split()])
B = sorted([int(b) for b in input().split()])
C = sorted([int(c) for c in input().split()])
COUNT = [0]
total = 0
for b in B[::-1]:
    index = binary_search(C, b)
    total += (N - index)
    COUNT.append(total)
COUNT = COUNT[::-1]
counter = 0
for a in A:
    index = binary_search(B, a)
    counter += COUNT[index]
print(counter)

Solution (Python):
N = int(input())
A = [int(s) for s in input().split()]
B = [int(s) for s in input().split()]
C = [int(s) for s in input().split()]
A.sort()
B.sort()
C.sort()
def isOK(lis,index,key):
    if lis[index] > key:
        return True
    else:
        return False

def binary_search(lis,key):
    ng = -1
    ok = len(lis)

    while(abs(ok-ng)>1):
        mid = int((ok+ng)/2)
        if isOK(lis,mid,key):
            ok = mid
        else:
            ng = mid
    return ok

count = 0
count_lis = []
for b in B[::-1]:
    b_index = binary_search(C,b)
    if b_index != N:
        count += N - b_index
    count_lis.append(count)

count_lis = count_lis[::-1]
ans = 0
for a in A:
    a_index = binary_search(B,a)
    if a_index != N:
        ans += count_lis[a_index]
print(ans)

Solution (Python):
def binary_serch(target, num_list):
    ok = len(num_list)
    ng = -1

    while(abs(ok - ng) > 1):
        mid = int((ok + ng) / 2)
        mid_num = num_list[mid]
        if target < mid_num:
            ok = mid
        else:
            ng = mid

    return ok


if __name__ == '__main__':
    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()

    
    A2B = [binary_serch(a, B) for a in A]

    
    B2C = []
    tmp = 0
    for b in B[::-1]:
        tmp = tmp + (N - binary_serch(b, C))
        B2C.append(tmp)

    B2C = B2C[::-1]

    ans = 0

    for i in range(N):
        if not A2B[i] == N:
            ans += B2C[A2B[i]]

    print(ans)

Solution (Python):

def isOK(lis, index, key):
    if lis[index] > key:
        return True
    else:
        return False
def binary_search(lis, key):
    ng = -1
    ok = len(lis)
    while abs(ok - ng) > 1:
        mid = int((ok + ng)/2)
        if isOK(lis, mid, key):
            ok = mid
        else:
            ng = mid
    return ok

N = int(input())
A = sorted([int(a) for a in input().split()])
B = sorted([int(b) for b in input().split()])
C = sorted([int(c) for c in input().split()])
COUNT = []
total = 0
for b in B[::-1]:
    index = binary_search(C, b)
    if index == N:
        total += 0
    else:
        total += (N - index)
    COUNT.append(total)
COUNT = COUNT[::-1]
counter = 0
for a in A:
    index = binary_search(B, a)
    if index != N:
        counter += COUNT[index]
print(counter)

Solution (Python):

import string
import sys
from itertools import chain, dropwhile, takewhile


def read(
    *shape, f=int, it=chain.from_iterable(sys.stdin), whitespaces=set(string.whitespace)
):
    def read_word():
        w = lambda c: c in whitespaces
        nw = lambda c: c not in whitespaces
        return f("".join(takewhile(nw, dropwhile(w, it))))

    if not shape:
        return read_word()
    elif len(shape) == 1:
        return [read_word() for _ in range(shape[0])]
    elif len(shape) == 2:
        return [[read_word() for _ in range(shape[1])] for _ in range(shape[0])]


def readi(*shape):
    return read(*shape)


def readi1(*shape):
    return [i - 1 for i in readi(*shape)]


def readf(*shape):
    return read(*shape, f=float)


def reads(*shape):
    return read(*shape, f=str)


def arr(*shape, fill_value=0):
    if len(shape) == 1:
        return [fill_value] * shape[fill_value]
    elif len(shape) == 2:
        return [[fill_value] * shape[1] for _ in range(shape[0])]


def dbg(**kwargs):
    print(
        ", ".join("{} = {}".format(k, repr(v)) for k, v in kwargs.items()),
        file=sys.stderr,
    )


def bs(a, v):
    ok = len(a)
    ng = -1
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if a[mid] > v:
            ok = mid
        else:
            ng = mid
    return ok


def main():
    N = readi()
    A = sorted(readi(N))
    B = sorted(readi(N))
    C = sorted(readi(N))
    ab = [bs(B, a) for a in A]
    bc = [bs(C, b) for b in B]
    bcount = [N - bc[i] for i in range(N)]
    bcum = arr(N + 1)
    for i in range(N - 1, -1, -1):
        bcum[i] = bcum[i + 1] + bcount[i]
    ans = 0
    for i in range(N):
        ans += bcum[ab[i]]
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import bisect

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

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

Solution (Python):
import bisect

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

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

Solution (Python):
import bisect
n = int(input())
abc = [sorted(list(map(int, input().split()))) for i in range(3)]
print(sum([bisect.bisect_left(abc[0], b)*(n-bisect.bisect_right(abc[2], b)) for b in abc[1]]))

Solution (Python):
import bisect
n = int(input())
abc = [sorted(list(map(int, input().split()))) for i in range(3)]
print(sum([bisect.bisect_left(abc[0], b)*(n-bisect.bisect_right(abc[2], b)) for b in abc[1]]))

Solution (Python):
import bisect
n = int(input())
abc = [sorted(list(map(int, input().split()))) for i in range(3)]
print(sum([bisect.bisect_left(abc[0], b)*(n-bisect.bisect_right(abc[2], b)) for b in abc[1]]))

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

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

cnt = 0
for j in range(N):
  target = B[j]
  insert_indexA = bisect.bisect_left(A, target)
  insert_indexC = bisect.bisect_right(C, target)
  cnt = cnt + insert_indexA * (len(C)-insert_indexC)

print(cnt)

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()
idxa = 0
idxc = 0
ans = 0
for b in range(N):
    while idxa < N and A[idxa] < B[b]:
        idxa += 1
    while idxc < N and B[b] >= C[idxc]:
        idxc += 1
    ans += idxa * (N - idxc)
print(ans)

Solution (Python):


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


def lower_bound(arr, target):
    
    if target > arr[-1]:
        return None
    left, right = 0, len(arr)
    while left < right:
        mid = (left + right) // 2
        if arr[mid] > target:
            right = mid
        else:
            left = mid + 1
    return right


def upper_bound(arr, target):
    
    if target < arr[0]:
        return None
    left, right = 0, len(arr)
    while left < right:
        mid = (left + right) // 2
        if arr[mid] >= target:
            right = mid
        else:
            left = mid + 1
    return right


count = 0

for b in B:
    ai = upper_bound(A, b)
    ci = lower_bound(C, b)
    if ai is not None and ci is not None:
        count += ai * (N - ci)

print(count)

Solution (Python):

def main():
    num = int(input())
    data_a = list(map(int, input().split()))
    data_a.sort()
    data_b = list(map(int, input().split()))
    data_b.sort()
    data_c = list(map(int, input().split()))
    data_c.sort()

    shita_b = [0 for i in range(num)]

    ind = 0
    for i in range(num):
        now_b = data_b[i]
        while data_c[ind] <= now_b:
            ind += 1
            if ind >= num:
                ind = num
                break
        if ind == num:
            break
        shita_b[i] = num - ind


    ans = 0
    tasu_now = sum(shita_b)
    ind = 0
    for i in range(num):
        now_a = data_a[i]
        while now_a >= data_b[ind]:
            tasu_now -= shita_b[ind]
            ind += 1
            if ind >= num:
                ind = num
                break
        if ind == num:
            break
        ans += tasu_now

    print(ans)



if __name__ == '__main__':
    main()

Solution (Python):


def main():
    num = int(input())
    data_a = list(map(int, input().split()))
    data_a.sort()
    data_b = list(map(int, input().split()))
    data_b.sort()
    data_c = list(map(int, input().split()))
    data_c.sort()

    shita_b = [0 for i in range(num)]

    ind = 0
    for i in range(num):
        now_b = data_b[i]
        while data_c[ind] <= now_b:
            ind += 1
            if ind >= num:
                ind = num
                break
        if ind == num:
            break
        shita_b[i] = num - ind


    ans = 0
    tasu_now = sum(shita_b)
    ind = 0
    for i in range(num):
        now_a = data_a[i]
        while now_a >= data_b[ind]:
            tasu_now -= shita_b[ind]
            ind += 1
            if ind >= num:
                ind = num
                break
        if ind == num:
            break
        ans += tasu_now

    print(ans)



if __name__ == '__main__':
    main()

Solution (Python):
import bisect


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

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


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

Solution (Python):
from bisect import bisect_left, bisect_right

n = int(input())
aa = sorted(map(int, input().split()))
bb = map(int, input().split())
cc = sorted(map(int, input().split()))

sum = 0
for b in bb:
  sum += bisect_right(aa, b-0.1) * (n - bisect_left(cc, b+0.1))
print(sum)

Solution (Python):
N = int(input())
As = list(map(int,input().split(" ")))
As.sort(reverse=True)
Bs = list(map(int,input().split(" ")))
Bs.sort(reverse=True)
Cs = list(map(int,input().split(" ")))
Cs.sort(reverse=True)
bcruiseki = [0]
abruiseki = [0]
j = 0
i = 0
while True:
    temp= bcruiseki[j]
    while i < N and Bs[j] <Cs[i]  :
        temp += 1
        i+= 1
    bcruiseki.append(temp)
    j += 1
    if j == N:
        break
j = 0
i = 0
while True:
    temp= abruiseki[j]
    while i < N and As[j] <Bs[i]  :
        temp += bcruiseki[i+1]
        i+= 1
    abruiseki.append(temp)
    j += 1
    if j == N:
        break

print(sum(abruiseki))

Solution (Python):











import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial
from heapq import heappop, heappush, heappushpop
from bisect import bisect_left,bisect_right
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N = I()
    A = LI()
    B = LI()
    C = LI()
    A.sort()
    B.sort()
    C.sort()
    ans=0
    for b in B:
        ans += bisect_left(A, b) * (N-bisect_right(C, b))
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
A,B,C=[sorted(list(map(int,input().split()))) for _ in range(3)]
cnt=0
a,c=0,0
for b in range(n):
	while a<n and A[a]<B[b]:
		a+=1
	while c<n and B[b]>=C[c]:
		c+=1
	if c==n:break
	cnt+=a*(n-c)
print(cnt)

Solution (Python):
def bs_left(A, target):
    low, high = 0, len(A)
    while low<high:
        mid = (low+high)//2
        if A[mid]<target:
            low = mid+1
        else:
            high = mid
    return low

def bs_right(C, target):
    low, high = 0, len(C)
    while low<high:
        mid = (low+high)//2
        if target<C[mid]:
            high = mid
        else:
            low = mid+1
    return low
if __name__ == '__main__':
    N = int(input())

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

    count = 0
    for i in range(N):
        b = B[i]
        count = count + bs_left(A,b) * (N - bs_right(C,b))
    print(count)

Solution (Python):
import bisect

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

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

print(ans)

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





jj = 0
jF = False
for _j in range(N):
  if (A[0] < B[_j]):
    jj = _j
    break
  if (_j == N-1):
    jF = True
if ( not jF ):
  i,k=0,0
  s = 0
  Flag = True
  for j in range(N):

    if (i<N):
      for _ in range(N*N):
        if ( A[i] >= B[j] ):
          break
        i += 1
        if (i==N):
          break
    for _ in range(N*N):
      if ( B[j] < C[k] ):
        break
      k += 1
      if ( k == N ):
        Flag = False
        break
    if ( not Flag ):
      break
    
    s += i*( N-k )
  print (s)
else:
  print (0)

Solution (Python):
def binary_search_a(n, a, k):
    

    lb = 0
    ub = n - 1

    if a[ub] < k:
        return n
    elif a[lb] >= k:
        return 0

    while (ub - lb > 1):
        mid = int((ub + lb) / 2)
        if a[mid] < k:
            lb = mid
        elif a[mid] >= k:
            ub = mid
    
    return lb + 1

def binary_search_c(n, c, k):
    

    lb = 0
    ub = n - 1

    if c[ub] <= k:
        return 0
    elif c[lb] > k:
        return n

    while (ub - lb > 1):
        mid = int((ub + lb) / 2)
        if c[mid] > k:
            ub = mid
        elif c[mid] <= k:
            lb = mid
    
    return n - ub
        

def answer(n, a, b, c):
    ans = 0

    a.sort()
    c.sort()
    

    for k in b:
        ans += binary_search_a(n, a, k) * binary_search_c(n, c, k)
        
    return ans


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

print(str(answer(n, a, b, c)))

Solution (Python):
from bisect import*
n,*t=map(int,open(0).read().split())
s=sorted
a,b,c=s(t[:n]),s(t[n:n+n]),s(t[n+n:])
x=[n-bisect(c,i)for i in b]+[0]
for i in range(n):x[-i-2]+=x[~i]
print(sum(x[bisect(b,i)]for i in a))

Solution (Python):
from bisect import*
n,*t=map(int,open(0).read().split())
s=sorted
a,b,c=s(t[:n]),s(t[n:n+n]),s(t[n+n:])
x=[n-bisect(c,i)for i in b]+[0]
for i in range(n):x[-i-2]+=x[~i]
print(sum(x[bisect(b,i)]for i in a))

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()
num_b = []
stim = [0]
cnt = 0
for i in B:
    ind = bisect.bisect_left(A,i)
    num_b.append(ind)

for i in num_b:
    stim.append(stim[-1]+i)

for i in C:
    ind = bisect.bisect_left(B,i)
    cnt += stim[ind]

print(cnt)

Solution (Python):
import bisect
from operator import mul

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

A_arr = [bisect.bisect_left(A, b) for b in B]

C_arr = [N - bisect.bisect_right(C, b) for b in B]

p = map(mul, A_arr, C_arr)
print(sum(p))

Solution (Python):
from bisect import bisect_left, bisect_right


n = int(input())
lst_A = sorted(map(int, input().split()))
lst_B = sorted(map(int, input().split()))
lst_C = sorted(map(int, input().split()))


ans = 0
for b in lst_B:
    na = bisect_left(lst_A, b)
    nc = n - bisect_right(lst_C, b)
    ans += na * nc


print(ans)

Solution (Python):
import bisect


n = int(input())
lst_A = list(map(int, input().split()))
lst_B = list(map(int, input().split()))
lst_C = list(map(int, input().split()))
lst_A.sort()
lst_B.sort()
lst_C.sort()


ans = 0
for b in lst_B:
    na = bisect.bisect_left(lst_A, b)
    nc = n - bisect.bisect_right(lst_C, b)
    ans += na * nc


print(ans)

Solution (Python):



def main():
    from bisect 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:
        a_count = bisect(a, bi - 1)
        c_count = n - bisect(c, bi)
        ans += a_count * c_count

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import bisect

n = int(input())

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

parts[0] = sorted(parts[0])
parts[2] = sorted(parts[2])

c = 0

for i in parts[1]:
    low = 0
    high = 0
    
    low = bisect.bisect_left(parts[0],i)
    high = n-bisect.bisect_right(parts[2],i)
    
    c += (low*high)

print(c)

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()
print(sum(bisect_left(A, i) * (N - bisect_right(C, i)) for i in B))

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()
s = 0
for i in range(N):
    ii = bisect.bisect_left(A, B[i])
    jj = bisect.bisect_right(C, B[i])
    s += ii * (N - jj)

print(s)

Solution (Python):
from bisect import bisect_left, bisect_right

n = int(input())

an = list(map(int, input().split()))
bn = list(map(int, input().split()))
cn = list(map(int, input().split()))

an.sort()
bn.sort()
cn.sort()

cnt = 0
for b in bn:
    a = bisect_left(an, b)
    c = bisect_right(cn, b)
    cnt += a * (n - c)

print(cnt)

Solution (Python):
from sys import stdin
input = stdin.readline
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()


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

print(ans)

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

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  N=II()
  l1=LI()
  l2=LI()
  l3=LI()

  l=[]
  for x in l1:
    l.append([x,3])

  for x in l2:
    l.append([x,2])

  for x in l3:
    l.append([x,1])

  l=sorted(l,key=lambda x:[x[0],x[1]])

  a=0
  b=c=N
  cnt=0
  for x in l:
    y=x[1]
    if y==3:
      a+=1
    elif y==1:
      c-=1
    else:
      cnt+=a*c
      b-=1

  return cnt


print(main())

Solution (Python):

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()
ai = 0
ci = 0
for bi in b:
    while ai < N and a[ai] < bi:
        ai += 1
    while ci < N and c[ci] <= bi:
        ci += 1
    ans += ai * (N -ci)
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:
    i = bisect.bisect_left(A, b)
    j = N - bisect.bisect_right(C, b)
    ans += i * j
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()

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

print(s)

Solution (Python):
import bisect
num = int(input())
up = sorted(list(map(int,input().split())))
mid = sorted(list(map(int,input().split())))
dw = sorted(list(map(int,input().split())))
SUM = 0

for i in mid:
  head = bisect.bisect_left(up,i)
  foot = len(dw) - bisect.bisect_right(dw,i)
  SUM += head * foot

print(SUM)

Solution (Python):
n=int(input())
inf=[10**10]
a=sorted([int(j) for j in input().split()+inf])
b=sorted([int(j) for j in input().split()])
c=sorted([int(j) for j in input().split()+inf])
ans,pa,pc=0,0,0
for j in range(n):
    while a[pa]<b[j]:
        pa+=1
    while c[pc]<=b[j]:
        pc+=1
    ans+=pa*(n-pc)
print(ans)

Solution (Python):
n=int(input())
a=[10**10]+[int(j) for j in input().split()]
b=[int(j) for j in input().split()]
c=[10**10]+[int(j) for j in input().split()]

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

ans=0
pa=0
pc=0
for j in range(n):
    while a[pa]<b[j]:
        pa+=1
    while c[pc]<=b[j]:
        pc+=1
    ans+=pa*(n-pc)
    
print(ans)

Solution (Python):
import bisect

    
def main():
    answer=0
    n = int(input())
    
    A = input().split( )
    B = input().split( )
    C = input().split( )
    
    A = [int(a) for a in A]
    B = [int(b) for b in B]
    C = [int(c) for c in C]
    
    A.sort()
    B.sort()
    C.sort()
    
    for b in B:
        
        num_A=bisect.bisect_left(A,b)
        
        num_C=len(C)-bisect.bisect_right(C,b)
        
        num=num_A*num_C
        
        answer+=num
    print(answer)


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

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

print(ans)

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

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

print(ans)

Solution (Python):
from bisect import bisect_left, bisect_right

N = int(input())
A, B, C = (sorted([int(i) for i in input().split()]) for _ in range(3))

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

Solution (Python):
import bisect
N=int(input())
A,B,C=[sorted(map(int,input().split())) for _ in range(3)]

p,q=0,0
xB=[None]*N

for i in range(N):
	while p < N and A[p] < B[i]:
		p += 1
	xB[i] = p

p,q=0,0
xC=[None]*N

for i in range(N):
	while p < N and B[p] < C[i]:
		q += xB[p]
		p += 1
	xC[i] = q

print(sum(xC))

Solution (Python):
N = int(input())
A = sorted(map(int,input().split())) + [10**10]
B = sorted(map(int,input().split())) + [10**10+1]
C = sorted(map(int,input().split())) + [10**10+2]

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

S = [0]*(N+1)
for i in range(N-1,-1,-1):
    S[i] = S[i+1] + nB[i]
ans = 0
j = 0
for i in range(N):
    while A[i] >= B[j]:
        j += 1
    ans += S[j]
print(ans)

Solution (Python):

def read_input():
    n = int(input())
    alist = list(map(int, input().split()))
    blist = list(map(int, input().split()))
    clist = list(map(int, input().split()))

    return n, alist, blist, clist


def submit():
    n, alist, blist, clist = read_input()

    alist.sort(reverse=True)
    blist.sort(reverse=True)
    clist.sort(reverse=True)

    b_a_n = [0] * n
    i = j = 0

    while j < n:
        while i < n and alist[i] >= blist[j]:
            i += 1
        b_a_n[j] = n - i
        j += 1

    b_a_sum = b_a_n.copy()
    for i in range(2, n + 1):
        b_a_sum[n - i] = b_a_sum[n - i + 1] + b_a_n[n - i]

    count = 0
    i = j = 0
    while i < n:
        while blist[j] >= clist[i]:
            j += 1
            if j == n:
                print(count)
                return
        count += b_a_sum[j]
        i += 1

    print(count)

if __name__ == '__main__':
    submit()

Solution (Python):
import bisect
import sys
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()))
A.sort()
B.sort()
C.sort()
cnt = 0
for j in B:
    a_index = bisect.bisect_left(A, j)
    c_index = bisect.bisect_right(C, j)
    cnt += a_index*(N-c_index)
print(cnt)

Solution (Python):
def c_snuke_festival(N, A, B, C):
    from bisect import bisect_left, bisect_right
    a, b, c = sorted(A), sorted(B), sorted(C)
    
    return sum(bisect_left(a, _b) * (N - bisect_right(c, _b)) for _b in b)

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()]
print(c_snuke_festival(N, A, B, C))

Solution (Python):
import numpy as np
import bisect

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
index_b=0
index_c=0
count_b=[0]*(n+1)
for i in range(n):
        count_b[i+1]=bisect.bisect_left(a,b[i])+count_b[i]
for i in range(n):
        count+=count_b[bisect.bisect_left(b,c[i])]
print(count)

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

o = []
p = []

i = 0
k = 0
for j in range(N):
    while i < N and A[i] < B[j]:
        i = i + 1
    o.append(i)

    while k < N and B[j] >= C[k]:
        k = k + 1
    p.append(N-k)

sum = 0
for x in range(N):
    sum = sum + o[x] * p[x]

print(sum)