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

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


def count_a(A, X):
    l = -1
    r = N
    while r - l > 1:
        m = (r + l) // 2
        if A[m] < b:
            l = m
        else:
            r = m
    return l + 1


def count_c(C, X):
    l = -1
    r = N
    while r - l > 1:
        m = (r + l) // 2
        if C[m] > b:
            r = m
        else:
            l = m
    return N - r


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

ans = 0
A_min = A[0]
C_max = C[-1]

for b in B:
    if b <= A_min or b >= C_max:
        continue

    a_cnt = count_a(A, b)
    if a_cnt == 0:
        continue
    c_cnt = count_c(C, b)

    ans += a_cnt * c_cnt

print(ans)

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

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

ans = 0

for b in B:
    l = -1
    r = N
    while r - l > 1:
        m = (r + l) // 2
        if A[m] < b:
            l = m
        else:
            r = m
    a_cnt = l + 1 if l != -1 else 0

    l = -1
    r = N
    while r - l > 1:
        m = (r + l) // 2
        if C[m] > b:
            r = m
        else:
            l = m
    c_cnt = N - r

    ans += a_cnt * c_cnt

print(ans)

Solution (Python):


import bisect

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

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

b_ans_list = [0 for _ in range(n)]
for i in reversed(range(n)):
    
    if i == n-1:
        b_ans_list[i] = n - bisect.bisect_right(c, b[i])
    else:
        b_ans_list[i] = n - bisect.bisect_right(c, b[i]) + b_ans_list[i+1]

ans = 0
for i in range(n):
    b_index = bisect.bisect_right(b, a[i])
    if b_index < n: ans += b_ans_list[b_index]

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()
c.sort()
import bisect as bi
ans=0
for i in b:
    k=1
    k*=bi.bisect_left(a,i)
    k*=n-bi.bisect_right(c,i)
    ans+=k
print(ans)

Solution (Python):
import bisect


n = int(input())
A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
C = sorted(list(map(int, input().split())))
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():
    import sys
    
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    
    from itertools import combinations, permutations
    
    from bisect import bisect_left,bisect_right
    from math import floor, ceil
    

    

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

    res = 0
    for j in B:
        res += bisect_left(A, j) * (n-bisect_right(C, j))
    print(res)

if __name__ == '__main__':
    main()

Solution (Python):
from bisect import bisect_left, bisect_right

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

ans = 0
for bb in b:
    ans += bisect_left(a, bb) * (n - bisect_right(c, bb))

print(ans)

Solution (Python):

n = int(input())
k = [list(map(int,input().split())) for _ in range(3)]
cnt = [[0] * n for _ in range(2)] 

a = sorted(k[0])
b = sorted(k[1])
c = sorted(k[2])
for i in range(n):
    l = -1
    r = n 
    while r-l > 1:
        
        k = (l+r)//2
        if b[i] > a[k]: l = k
        else: r = k
    cnt[0][i] = l+1
    l = -1
    r = n 
    while r-l > 1:
        
        k = (l+r)//2
        if b[i] < c[k]: r = k
        else: l = k
    cnt[1][i] = n-r

""" ACするが、O(n**2)
i = 0
j = 0
while i < n:
    if b[i] < c[j]: 
        cnt[1][i] = n-j
        i +=1
    else: j+=1
    if j >= n:
        j = n-1
        cnt[1][i] = 0
        i += 1
i = 0
j = 0
while i < n:
    if b[i] > a[j]:
        j+= 1
    else:
        cnt[0][i] = j
        i += 1
    if j >= n:
        j = n-1
        cnt[0][i] = n
        i += 1"""
ans = 0
for i in range(n):
    ans += cnt[0][i]*cnt[1][i]
print(ans)

Solution (Python):

n = int(input())
k = [list(map(int,input().split())) for _ in range(3)]
cnt = [[0] * n for _ in range(2)] 

a = sorted(k[0])
b = sorted(k[1])
c = sorted(k[2])
i = 0
j = 0
while i < n:
    if b[i] < c[j]: 
        cnt[1][i] = n-j
        i +=1
    else: j+=1
    if j >= n:
        j = n-1
        cnt[1][i] = 0
        i += 1
i = 0
j = 0
while i < n:
    if b[i] > a[j]:
        j+= 1
    else:
        cnt[0][i] = j
        i += 1
    if j >= n:
        j = n-1
        cnt[0][i] = n
        i += 1
ans = 0
for i in range(n):
    ans += cnt[0][i]*cnt[1][i]
print(ans)

Solution (Python):
from bisect import bisect_left
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()

ba_cnt = [bisect_left(a, e) for e in b]
ba_acm = [0] + list(accumulate(ba_cnt))

ans = 0
for e in c:
    i = bisect_left(b, e)
    ans += ba_acm[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()

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

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

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, b, c = sorted(a), sorted(b), sorted(c)

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

print(ans)

Solution (Python):
def nibu_right(a,A): 
    if a<A[0]:return 0
    elif A[-1]<a:return len(A)
    else:
        mi = 0
        ma = len(A)-1
        while ma-mi>1:
            mid = (ma+mi)//2
            if a < A[mid]:
                ma = mid
            else:
                mi = mid
        if A[ma]==a:
            return ma+1
        else:
            return ma

def nibu_left(a,A): 
    if a<A[0]:return -1
    elif A[-1]<a:return len(A)-1
    else:
        mi = 0
        ma = len(A)-1
        while ma-mi>1:
            mid = (ma+mi+1)//2
            if a > A[mid]:
                mi = mid
            else:
                ma = mid
        if A[mi]==a:
            return mi-1
        else:
            return mi




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

ans = 0
for i in range(n):
    ans += (nibu_left(B[i],A)+1) * (n-nibu_right(B[i],C))
print(ans)

Solution (Python):
import bisect

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

    ans = 0

    a_list.sort()
    c_list.sort()

    for i in range(n):
        toa_insert_index = bisect.bisect_left(a_list, b_list[i])
        toc_insert_index = bisect.bisect_right(c_list, b_list[i])
        ans += toa_insert_index * ( n - toc_insert_index )

    print(ans)

if __name__ == '__main__':
    main()

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

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

Solution (Python):
import bisect

n=int(input())
a=sorted(list(map(int,input().split())))
b=(list(map(int,input().split())))
c=sorted(list(map(int,input().split())))
ans=0
for i in b:
    ta=bisect.bisect_left(a,i)
    tc=n-bisect.bisect_right(c,i)
    
    ans+=ta*tc
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 i in range(n):
    d = bisect.bisect_left(a,b[i])
    e = bisect.bisect_right(c,b[i])
    cnt += d*(n-e)
print(cnt)

Solution (Python):
from bisect import bisect_left,bisect_right

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:  
  ans += bisect_left(A,bi) * (n-bisect_right(C,bi))
  
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()))
c_num = len(c_list)

a_list.sort()
c_list.sort()

ans = 0
for b in b_list:
    a_point = bisect.bisect_left(a_list,b)
    c_point = bisect.bisect(c_list,b)
    
    ans+= a_point*(c_num-c_point)
print(ans)

Solution (Python):

import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
import bisect


def main():
    mod=10**9+7
    
    N=I()
    A=[[] for _ in range(3)]
    for i in range(3):
        A[i]=LI()
        A[i].sort()
        
    BC=[0]*N
    
    for j in range(N):
        BC[j]=N-bisect.bisect_right(A[2], A[1][j])
                                    
    S=[0]*(N+1)
    for i in range(N):
        S[i+1]=S[i]+BC[i]
        
    ans=0
    for i in range(N):
        ans+=S[-1] - S[bisect.bisect_right(A[1],A[0][i])]
        
    print(ans)
    

main()

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

def check_a(a_num, b_num):
    res = False
    if a_num < b_num:
        res = True
    return res

def check_c(c_num, b_num):
    res = False
    if b_num < c_num:
        res = True
    return res

ans = 0
cnt_a = 0
cnt_c = 0
for bi in range(len(b)):
    left = 0
    right = len(a)-1
    if check_a(a[0], b[bi]) is False:
        cnt_a = 0
    elif check_a(a[-1], b[bi]) is True:
        cnt_a = len(a)
    else:
        while right-left > 1:
            mid = (left + right) // 2
            if check_a(a[mid], b[bi]):
                left = mid
            else:
                right = mid
        cnt_a = left + 1

    left = 0
    right = len(c) - 1
    if check_c(c[0], b[bi]) is True:
        cnt_c = len(c)
    elif check_c(c[-1], b[bi]) is False:
        cnt_c = 0
    else:
        while right - left > 1:
            mid = (left + right) // 2
            if check_c(c[mid], b[bi]):
                right = mid
            else:
                left = mid
        cnt_c = len(c) - right
    ans += cnt_a * cnt_c
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())))





a = [0]
b = [0]
for i in range(N):
    ai = bisect.bisect_right(B, A[i])
    a.append(N-ai+a[i])

    bi = bisect.bisect_right(C, B[i])
    b.append(N-bi+b[i])




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

print(ans)

Solution (Python):

import sys
import math
import os
import itertools
import string
import heapq
import bisect
import re
import queue
from collections import Counter
from collections import defaultdict
from functools import lru_cache


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def float():
        return float(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def map_float():
        return [float(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def PI():
        return math.acos(-1)

    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    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

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    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

    @staticmethod
    def divisor(n):
        i = 1
        table = []
        while i * i <= n:
            if n % i == 0:
                table.append(i)
                table.append(n // i)
            i += 1
        table = list(set(table))
        return table


class PriorityQueue:
    def __init__(self, l=[]):
        self.__q = l
        heapq.heapify(self.__q)
        return

    def push(self, n):
        heapq.heappush(self.__q, n)
        return

    def pop(self):
        return heapq.heappop(self.__q)

    def list(self):
        return self.__q

    def length(self):
        return len(self.__q)


sys.setrecursionlimit(1000000)
MOD = int(1e09) + 7
INF = int(1e30)


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):
        ok = -1
        ng = N
        while abs(ok - ng) > 1:
            mid = (ok + ng) // 2
            if A[mid] < B[i]:
                ok = mid
            else:
                ng = mid
        a = ok + 1
        ok = N
        ng = -1
        while abs(ok - ng) > 1:
            mid = (ok + ng) // 2
            if C[mid] > B[i]:
                ok = mid
            else:
                ng = mid
        c = N - ok
        ans += a * c

    print(ans)
    return


if __name__ == "__main__":
    main()

Solution (Python):
import sys
import math
import fractions
import itertools
from collections import deque
import copy
import bisect
import numpy

MOD = 10 ** 9 + 7
INF = 10 ** 18 + 7
input = lambda: sys.stdin.readline().strip()

N = int(input())
C = sorted(deque(map(int, input().split())))
B = sorted(deque(map(int, input().split())))
A = sorted(list(map(int, input().split())))
BB = []
for i in B:
    BB.append(N - bisect.bisect_right(A, i))
BB_sum = list(numpy.cumsum(BB[::-1])[::-1])
BB_sum.append(0)
ans = 0
for i in C:
    ans += BB_sum[bisect.bisect_right(B, i)]
print(ans)

Solution (Python):
def I(): return int(input())
def MI(): return map(int, input().split())
def LMI(): return list(map(int, input().split()))
MOD = 10 ** 9 + 7


def to_cumsum(arr, pad=True):
    """
    1次元配列を累積和の1次元配列に変換する
    pad: 先頭に0の項を作る場合はTrue
    """
    cumsum = 0
    padding = 1 if pad else 0
    new = [0] * (len(arr) + padding)
    for i, e in enumerate(arr):
        cumsum += e
        new[i + padding] = cumsum
    return new


N = I()
A = LMI()
B = LMI()
C = LMI()

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


def bi(A, x):
    l = -1
    r = len(A)
    while l + 1 < r:
        p = (l + r) // 2
        if x < A[p]:
            r = p
        else:
            l = p
    return r


BC = [0] * N
for i, b in enumerate(B):
    BC[i] = N - bi(C, b)
BC = to_cumsum(BC)
ans = 0
for i, a in enumerate(A):
    ans += BC[N] - BC[bi(B, a)]
print(ans)

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

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

ans = 0
for b_i in B:
    a_n = bisect_left(A, b_i)
    c_n = bisect_right(C, b_i)
    
    ans += a_n * (N - c_n)

print(ans)

Solution (Python):
import numpy as np
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()
a=np.array(a)
b=np.array(b)
c=np.array(c)

cnt_a=np.searchsorted(a,b)
cnt_c=n-np.searchsorted(c,b,side='right')
print(np.sum(cnt_a*cnt_c))

Solution (Python):
def main():
    import numpy as np
    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()
    a=np.array(a)
    b=np.array(b)
    c=np.array(c)

    cnt_a=np.searchsorted(a,b)
    cnt_c=n-np.searchsorted(c,b,side='right')
    print(np.sum(cnt_a*cnt_c))
if __name__=='__main__':
    main()

Solution (Python):
def main():
    import numpy as np
    n=int(input())
    c=list(map(int,input().split()))
    b=list(map(int,input().split()))
    a=list(map(int,input().split()))
    a.sort()
    b.sort()
    c.sort()
    a=np.array(a)
    b=np.array(b)
    c=np.array(c)

    cnt_a=np.searchsorted(b,a).tolist()
    cnt_b=np.searchsorted(c,b).tolist()

    ans,j=0,0
    for i in range(n):
        ans+=sum(cnt_b[j:cnt_a[i]])*(n-i)
        j=cnt_a[i]
    print(ans)
if __name__=='__main__':
    main()

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

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

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

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

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

for i in range(n-1, 0, -1):
    bc_count[i-1] += bc_count[i]

ans = 0
bi = 0
for ai in range(n):
    while bi < n and a[ai] >= b[bi]:
        bi += 1
    if bi < n: ans += bc_count[bi]
print(ans)

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

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

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

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


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

for i in range(n-1,0,-1):
    bc_count[i-1] += bc_count[i]
    
ans = 0
bi = 0
for ai in range(n):
    while bi<n and a[ai] >= b[bi]:
        bi += 1
    if bi<n: ans += bc_count[bi]
    
print(ans)

Solution (Python):
import numpy as np
N, *L = map(int, open(0).read().split())
A = np.array(L[:N])
B = np.array(L[N:2*N])
C = np.array(L[2*N:])
A.sort()
B.sort()
C.sort()
binf = np.zeros(N)
j = 0
binf = np.searchsorted(A,B)
bs = np.cumsum(binf)
bs = np.concatenate([[0],bs])
cinf = np.searchsorted(B,C)
ans = np.sum(bs[cinf])
print(ans)

Solution (Python):
from bisect import bisect_left
N = int(input())
A = sorted(list(map(int,input().split())))
B = sorted(list(map(int,input().split())))
C = sorted(list(map(int,input().split())))
B1 = []
for i in range(N):
    B1.append(bisect_left(A,B[i]))
Br = [0]
for i in range(N):
    Br.append(Br[-1]+B1[i])
cnt = 0
for i in range(N):
    ind = bisect_left(B,C[i])
    cnt += Br[ind]
print(cnt)

Solution (Python):
import bisect

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

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


ans = 0
for p in b:
    t = bisect.bisect_left(a,p)
    b = n - bisect.bisect_left(c,p+1)
    
    ans += t * b

print(ans)

Solution (Python):
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7

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

a.sort()
alst=[0]*n
b.sort()
blst=[0]*n
c.sort()





for i in range(n):
    alst[i]=n-bisect.bisect_right(b,a[i])
    blst[i]=n-bisect.bisect_right(c,b[i])


blst.reverse()
acc=ruiseki(blst)

ans=0
for i in range(n):
    ans+=acc[alst[i]]
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()))

ret = 0
for mid in b:
    ret += bisect_left(a, mid) * (n - bisect_left(c, mid + 1))
print(ret)

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


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


def main():
    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):
        b = B[i]
        ia = lower_bound(A, b)
        ic = upper_bound(C, b)

        ans += (N-ic)*ia
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import bisect


def main():
    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):
        b = B[i]
        ia = bisect.bisect_left(A, b)
        ic = bisect.bisect_right(C, b)
        

        ans += (N-ic)*ia
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from bisect import bisect
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()

bc = [0 for _ in range(n)]
for i in range(len(b)):
    if b[i] >= c[n-1]:
        break
    p = bisect(c,b[i])
    bc[i] = (n-p)

for i in range(len(b)-1,0,-1):
    bc[i-1] += bc[i]
ans = 0
for x in a:
    if x < b[n-1]:
        p = bisect(b,x)
        ans += bc[p]

print(ans)

Solution (Python):
import sys
import bisect
def input(): return sys.stdin.readline().rstrip()

def main():
    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.bisect_left(A, b) * (N - bisect.bisect_right(C, b))
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
from bisect import bisect
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()

bc = [0 for _ in range(n)]
for i in range(len(b)):
    if b[i] >= c[n-1]:
        break
    p = bisect(c,b[i])
    bc[i] = (n-p)

for i in range(len(b)-1,0,-1):
    bc[i-1] += bc[i]
ans = 0
for i in range(len(a)):
    if a[i] >= b[n-1]:
        break
    p = bisect(b,a[i])
    ans += bc[p]

print(ans)

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    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:
        sum_v = 1
        l = -1
        r = N
        while r - l > 1:
            m = (l + r) // 2
            if b > A[m]:
                l = m
            else:
                r = m
        sum_v *= l + 1

        l = -1
        r = N
        while r - l > 1:
            m = (l + r) // 2
            if b < C[m]:
                r = m
            else:
                l = m
        sum_v *= N - r
        ans += sum_v
    print(ans)

if __name__ == '__main__':
    main()

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 in range(len(B)):
  apos=bisect.bisect_left(A,B[i])
  cpos=bisect.bisect_right(C,B[i])
  ans+=(apos)*(N-cpos)
print(ans)

Solution (Python):
import bisect

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

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

print(ans)

Solution (Python):
import bisect, itertools

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

c_num = [0]*n
for i in range(n):
    c_num[i] = n - bisect.bisect_right(C, B[i])
c_accum = [0] + list(itertools.accumulate(c_num))

ans = 0
for i in range(n):
    b_index = bisect.bisect_right(B, A[i])
    ans += (c_accum[-1] - c_accum[b_index])
print(ans)

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

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

cnt_A = np.searchsorted(A,B)
cnt_C = N - np.searchsorted(C,B,side='right')
ans = (cnt_A*cnt_C).sum()

print(ans)

Solution (Python):
import bisect

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

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

print(cnt)

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

ret = 0
for mid in b:
    ret += bisect_left(a, mid) * (n - bisect_left(c, mid + 1))
print(ret)

Solution (Python):

import sys
from bisect import bisect_left,bisect_right

def solve(N: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    answer = 0
    A.sort()
    C.sort()
    for i in range(N):
        answer += bisect_left(A,B[i])*(N-bisect_right(C,B[i]))
    print(answer)
    return


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

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

Solution (Python):
n = int(input())
AS = list(map(int,input().split()))
AS.sort()
BS = list(map(int,input().split()))
CS = list(map(int,input().split()))
CS.sort()
import bisect
  
ans = 0
for b in range(n):
  ans += bisect.bisect_left(AS,BS[b])*(n-bisect.bisect_right(CS,BS[b]))

print(ans)