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

Solution (Python):


import bisect


N = int(input())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
C_list = list(map(int, input().split()))


A_list = sorted(A_list)
B_list = sorted(B_list)
C_list = sorted(C_list)
ans = 0


for i in range(N):
    b = B_list[i]
    a_nums = bisect.bisect_left(A_list, b)
    c_nums = N - bisect.bisect_right(C_list, b)
    ans += a_nums * c_nums


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 j in range(n):
    bind = j
    top = n
    bot = -1
    while top-bot > 1:
        mid = (top+bot)//2
        if b[bind] > a[mid]:bot = mid
        else:top = mid
    
    aind = bot
    l = -1
    r = n
   
    while r-l> 1:
   
        cen = (l+r)//2
        if b[bind] < c[cen]:r = cen
        else:l = cen
    cind = r
    ans +=(aind+1)*(n-cind)
    
print(ans)

Solution (Python):
from itertools import accumulate
import bisect
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()
ai = []
bi = []
for i in range(n):
    ai.append(n - bisect.bisect_right(b, a[i]))
    bi.append(n - bisect.bisect_right(c, b[i]))
ans = 0
c = [0]+list(accumulate(bi[::-1]))
for j in ai:
    ans += c[j]
print(ans)

Solution (Python):
def 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()
    Bdic = [0]*N
    from bisect import bisect
    from itertools import accumulate
    for i, b in enumerate(B):
        idx = bisect(C, b)
        Bdic[i] = N - idx
    Bacc = list(accumulate([b for b in Bdic + [0]][::-1]))[::-1]
    ans = 0
    for i, a in enumerate(A):
        idx = bisect(B, a)
        ans += (Bacc[idx])
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    from bisect import 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())))
    b_comb_accum = [0] * (N+1)
    for i in range(N-1, -1, -1):
        c_min_ind = bisect_right(C, B[i])
        b_comb_accum[i] = b_comb_accum[i+1] + N - c_min_ind
    ans = 0
    for a in A:
        b_min_ind = bisect_right(B, a)
        ans += b_comb_accum[b_min_ind]
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from bisect import 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())))
F = [0 for _ in range(N+1)]
for i in range(N-1,-1,-1):
    b = B[i]
    indC = bisect_right(C,b)
    if i<N-1:
        indC1 = bisect_right(C,B[i+1])
    else:
        indC1 = N
    F[i] = F[i+1]+indC1-indC
G = [0 for _ in range(N+1)]
for i in range(N-1,-1,-1):
    G[i] = G[i+1]+F[i]
cnt = 0
for i in range(N):
    a = A[i]
    indB = bisect_right(B,a)
    cnt += G[indB]
print(cnt)

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




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

X = []
j = 0
for i in range(n):
    while j < n and B[i] >= C[j]:
        j += 1
    X.append(n-j)

S = [0]
for i in range(n):
    S.append(S[-1]+X[i])

j = 0
ans = 0
for i in range(n):
    while j < n and A[i] >= B[j]:
        j += 1
    ans += S[n]-S[j]
print(ans)

Solution (Python):
from heapq import heappush, heappop
from itertools import permutations, accumulate, combinations
import math
import bisect
import numpy as np
from collections import defaultdict, deque
from operator import itemgetter
from bisect import bisect_left, bisect_right
import sys
input = sys.stdin.readline





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:
        i = bisect_left(A, b)
        j = n - bisect_right(C, b)
        if i == 0 or j == 0:
            continue
        ans += i * j
    print(ans)



if __name__ == '__main__':
    main()

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from heapq import heapify, heappop, heappush
from bisect import bisect, bisect_left, bisect_right

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()
B = LIST()
C = LIST()
A.sort()
B.sort()
C.sort()




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

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

Solution (Python):
import bisect


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

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

print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from bisect import bisect_left
from itertools import accumulate

n = int(readline())
a = sorted(list(map(int, readline().split())))
b = sorted(list(map(int, readline().split())))
c = sorted(list(map(int, readline().split())))
check = [0] * n
ans = 0
for i in range(n):
    index = bisect_left(c, b[i])
    if index != n:
        if c[index] == b[i]:
            index = bisect_left(c, b[i] + 1)
    check[i] = n - index
cumsum = [0] + list(accumulate(check[::-1]))
for aa in a:
    index = bisect_left(b, aa)
    if index != n:
        if b[index] == aa:
            index = bisect_left(b, aa + 1)
    ans += cumsum[n - index]
print(ans)

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

ans = 0

for bi in range(N):
    ai_max = bisect_left(A, B[bi])
    ci_min = bisect_right(C, B[bi])
    ans += (ai_max) * (N - ci_min)

print(ans)

Solution (Python):
def resolve():
    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):
        btest = b[i]
        aind = bisect.bisect_left(a,btest)
        cind = bisect.bisect_right(c, btest)
        ans += aind * (len(c)-cind)
    print(ans)
resolve()

Solution (Python):
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) 
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    


MOD = 10**9 + 7
INF = 2**31  

from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  





N = read_a_int()
A = read_ints()
B = read_ints()
C = read_ints()

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

ans = 0
for b in B:
    i = bisect_left(A, b)  
    k = bisect_right(C, b)
    ans += i * (N - k)
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 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 = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
A.sort()
C.sort()

ans = 0
from bisect import bisect, bisect_left
for b in B:
    i = bisect_left(A, b)
    j = bisect(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())))

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

Solution (Python):
from bisect import bisect
from collections import deque

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

BC_ruiseki = deque([0])
for i, b in enumerate(B):
  BC_ruiseki.append(BC_ruiseki[i] + N - bisect(C, b))

ans = 0
for a in A:
  idx = bisect(B, a)
  ans += BC_ruiseki[-1] - BC_ruiseki[idx]
print(ans)

Solution (Python):
from bisect import*;(N,),A,B,C=[sorted(map(int,input().split()))for _ in[0]*4];print(sum(bisect_left(A,b)*(N-bisect(C,b))for b 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,b)*(N-bisect(C,b))for b in B))

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

Solution (Python):
from bisect import*
r=lambda:sorted(map(int,input().split()))
N=r()[0]
A,B,C,w,i=r(),r(),r(),[0]*(N+1),N
while i:i-=1;w[i]=w[i+1]+N-bisect(C,B[i])
print(sum(w[bisect(B,A[i])]for i in range(N)))

Solution (Python):
from bisect import*
r=lambda:sorted(map(int,input().split()))
N=r()[0]
A,B,C,w,i=r(),r(),r(),[0]*(N+1),N
while i:w[i-1]=w[i]+N-bisect(C,B[i-1]);i-=1
print(sum(w[bisect(B,A[i])]for i in range(N)))

Solution (Python):
from bisect import*
N=int(input())
r=lambda:sorted(map(int, input().split()))
A,B,C,w=r(),r(),r(),[0]*(N+1)
for i in range(N-1,-1,-1):w[i]=w[i+1]+N-bisect(C,B[i])
print(sum(w[bisect(B,A[i])]for i in range(N)))

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

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

Solution (Python):
import bisect

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

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

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

print(count)

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
A=sorted(A)
B=list(map(int,input().split()))
B=sorted(B)
C=list(map(int,input().split()))
C=sorted(C)
def ais_ok(i,b):
    if i<-1: 
        return True
    if n<=i: 
        return False
    return A[i]<b 
def cis_ok(i,b):
    if i<-1: 
        return False
    if n<=i: 
        return True
    return b<C[i]
ans=0
for i in range(n):
    b=B[i]
    ok_a,ng_a=-1,n
    ok_c,ng_c=n,-1
    while (ng_a-ok_a)>1:
        mid=(ok_a+ng_a)//2
        if ais_ok(mid,b):
            ok_a=mid
        else:
            ng_a=mid
    while (ok_c-ng_c)>1:
        mid=(ok_c+ng_c)//2
        if cis_ok(mid,b):
            ok_c=mid
        else:
            ng_c=mid
    ans+=(ok_a+1)*(n-(ng_c+1))
print(ans)

Solution (Python):

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


class Scanner():
    @staticmethod
    def int():
        return int(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 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 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 divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

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

    @staticmethod
    def is_prime(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


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def solve():
    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:
            m = (ok + ng) // 2
            if A[m] < B[i]:
                ok = m
            else:
                ng = m
        n = ok + 1
        ok = N
        ng = -1
        while abs(ok - ng) > 1:
            m = (ok + ng) // 2
            if C[m] > B[i]:
                ok = m
            else:
                ng = m
        m = N - ok
        ans += n * m
    print(ans)


def main():
    
    solve()


if __name__ == "__main__":
    main()

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

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
A = sorted(inpl())
B = sorted(inpl())
C = sorted(inpl())
cnt = [0] * n
res = 0
for i,b in enumerate(B):
    c = bisect.bisect_left(C,b+1)
    cnt[i] = n-c

acc = [0]
for x in cnt:
    acc += [acc[-1] + x]
su = sum(cnt) 
for i,a in enumerate(A):
    c = bisect.bisect_left(B,a+1)
    res += su - acc[c]
print(res)

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

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


for i in range(N):
    b = B_list[i]
    a_nums = bisect.bisect_left(A_list, b)
    c_nums = N - bisect.bisect_right(C_list, b)
    ans += a_nums * c_nums


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

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

print(res)

Solution (Python):
from bisect import bisect_right, bisect_left

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

A.sort()
C.sort()

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

print(ans)

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

def bin_a(list,bi):
    ok=-1
    ng=n
    while abs(ok-ng)>1:
        mid=(ok+ng)//2
        if bi>list[mid]:
            ok=mid
        else:
            ng=mid
    return ok+1
def bin_c(list,bi):
    ok=n
    ng=-1
    while abs(ok-ng)>1:
        mid=(ok+ng)//2
        if bi<list[mid]:
            ok=mid
        else:
            ng=mid
    return n-ok

cnt=0
for i in range(n):
    cnt+=bin_a(a,b[i])*bin_c(c,b[i])
    
print(cnt)

Solution (Python):
import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    A = [int(a) for a in input().split()]
    B = [int(b) for b in input().split()]
    C = [int(c) for c in input().split()]
    C.append(10 ** 10)
    A.sort()
    B.sort()
    C.sort()
    B_choise = [0] * N
    C_i = 0
    for i, b in enumerate(B):
        while C[C_i] <= b: C_i += 1
        B_choise[i] = N - C_i
    B_choise_sum = [0] * (N + 1)
    for i in reversed(range(N)):
        B_choise_sum[i] = B_choise_sum[i+1] + B_choise[i]
    
    B.append(10 ** 10)
    B_i = 0
    Ans = 0
    for i, a in enumerate(A):
        while B[B_i] <= a: B_i += 1
        Ans += B_choise_sum[B_i]
    print(Ans)
  
    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
def 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()
    
    from bisect import bisect
    ans = []
    ans2 = []
    for a in A:
        b = bisect(B, a)
        ans.append(N-b)
    for b in B:
        c = bisect(C, b)
        ans2.append(N-c)
    from itertools import accumulate
    ans2 = list(accumulate([0] + ans2[::-1]))
    answer = 0
    for a in ans:
        answer += ans2[a]
    print(answer)
    
    


if __name__ == '__main__':
    main()

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

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

ab = np.searchsorted(A, B, side="left")
bc = N - np.searchsorted(C, B, side="right")
ans = (ab*bc).sum()
print(ans)

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

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

import bisect
a.sort()
c.sort()
ans = 0

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

print(ans)

Solution (Python):
import bisect

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

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

print(ans)

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

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

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

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

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


from bisect import bisect_right

Bcnt = [0] * N
for i,b in enumerate(B):
    r = bisect_right(C, b)
    Bcnt[i] = N - r

from itertools import accumulate

Bcnt = list(accumulate([0] + Bcnt))

Acnt = [0] * N
for i,a in enumerate(A):
    r = bisect_right(B, a)
    Acnt[i] = Bcnt[N] - Bcnt[r]

print(sum(Acnt))

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
import bisect
for b in B:
    ia = bisect.bisect_left(A, b)
    ic = n-bisect.bisect_right(C, b)
    ans += ia*ic
print(ans)

Solution (Python):
import bisect








import sys
sys.setrecursionlimit(10 ** 5 + 10)


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

    print(cnt)





resolve()

Solution (Python):
from collections import *
from heapq import *
from itertools import *
from fractions import gcd
import sys
from decimal import *
import copy
from bisect import *
input=lambda :sys.stdin.readline().rstrip()

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

lstB=[0 for i in range(N)]
a=0
c=N-1
count=0




for b in range(N)[::-1]:
    while c>=0 and C[c]>B[b]:
        c-=1
    lstB[b]=N-(c+1)
b=N-1
lstA=[0 for i in range(N+1)]
for a in range(N)[::-1]:
    lstA[a]+=lstA[a+1]
    while b>=0 and B[b]>A[a]:
        lstA[a]+=lstB[b]
        b-=1




print(sum(lstA))

Solution (Python):
from bisect import bisect_left
from bisect import bisect_right
N = int(input())
As = sorted(list(map(int, input().split())))
Bs = sorted(list(map(int, input().split())))
Cs = sorted(list(map(int, input().split())))


cnt = 0
for b in Bs:
    ind_c = bisect_left(Cs,b+1)
    ind_a = bisect_right(As,b-1)
    if ind_c == N:continue
    
    cnt += (N - ind_c) * (ind_a)
print(cnt)

Solution (Python):
import sys
input=sys.stdin.readline
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
def ab_check(list_count,i):
    ok=-1
    ng=n
    B=b[i]
    while ng-ok>1:
        mid=(ok+ng)//2
        if list_count[mid]<B:
            ok=mid
        else:
            ng=mid
    
    return ok+1

def bc_check(list_count,i):
    ok=n
    ng=-1
    B=b[i]
    while ok-ng>1:
        mid=(ok+ng)//2
        if list_count[mid]>B:
            ok=mid
        else:
            ng=mid
    
    return n-ok
    

for i in range(n):
    ans+=ab_check(a,i)*bc_check(c,i)
print(ans)

Solution (Python):
import bisect

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

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

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

Solution (Python):
from bisect import bisect_right

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

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

Bcnt = [0]*(N+1)
idx = N
for i in range(N-1,-1,-1):
    idx = bisect_right(C,B[i],hi=idx)
    Bcnt[i] = N-idx+Bcnt[i+1]

ans = 0
idx = 0
for i in range(N):
    idx = bisect_right(B,A[i],lo=idx)
    ans += Bcnt[idx]

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

ret = 0
for b in B:
    i = bisect.bisect_left(A, b)
    j = bisect.bisect_right(C, b)

    ret += i * (N - j)
    

print(ret)

Solution (Python):



def main():
  n = int(input())
  la = list(map(int, input().split()))
  lb = list(map(int, input().split()))
  lc = list(map(int, input().split()))
  la.sort(), lb.sort(), lc.sort()
  
  i = 0
  ab = []
  for b in lb:
    while i < n and la[i] < b:
      i += 1
    ab.append(i)
      
  i = 0
  bc = []
  for c in lc:
    while i < n and lb[i] < c:
      i += 1
    bc.append(i)
    
  ans = 0
  cs = [0]
  for x in ab:
    cs.append(cs[-1] + x)
  for y in bc:
    ans += cs[y]
  print(ans)
  

if __name__=='__main__':
  main()

Solution (Python):
import sys
import os
import re
import datetime
import bisect



N = int(input())

partsNumA = sorted(map(int, input().split()))
partsNumB = sorted(map(int, input().split()))
partsNumC = sorted(map(int, input().split()))


partsNumSumBa = []
sumA = 0
for curPartsB in partsNumB:
    indexA = bisect.bisect_left(partsNumA, curPartsB)
    numA = indexA
    sumA += numA
    partsNumSumBa.append(sumA)


sumBAll = 0
for curPartsC in partsNumC:
    indexB = bisect.bisect_left(partsNumB, curPartsC)
    if indexB == 0:
        continue
    numB = partsNumSumBa[indexB - 1]
    sumBAll += numB

print(sumBAll)

Solution (Python):
import sys
import os
import re
import datetime
import bisect

def Test(value):
    a = [1,3,5]
    

    insert_index = bisect.bisect_left(a, value)
    
    print("insert_index = ", insert_index)
    
if False:   
    while(True): 
        Test(int(input('? ')));
    sys.exit()
 
N = int(input())

partsNumA = sorted(map(int, input().split()))
partsNumB = sorted(map(int, input().split()))
partsNumC = sorted(map(int, input().split()))


partsNumSumBa = []
sumA = 0

for curPartsB in partsNumB:
    indexA = bisect.bisect_left(partsNumA, curPartsB)
    numA = indexA
    sumA += numA
    partsNumSumBa.append(sumA)

if False:
    print("Sorted A" , partsNumA)
    print("Sorted B" , partsNumB)
    print("Sorted C" , partsNumC)

    print("B loop" , partsNumSumBa)


sumBAll = 0
for curPartsC in partsNumC:
    indexB = bisect.bisect_left(partsNumB, curPartsC)
    if indexB == 0:
        continue
    numB = partsNumSumBa[indexB - 1]
    sumBAll += numB
        
print(sumBAll)

Solution (Python):
import sys
import os
import re
import datetime
import bisect



N = int(input())

partsNumA = sorted(map(int, input().split()))
partsNumB = sorted(map(int, input().split()))
partsNumC = sorted(map(int, input().split()))


partsNumSumBa = []
sumA = 0
for curPartsB in partsNumB:
    indexA = bisect.bisect_left(partsNumA, curPartsB)
    numA = indexA
    sumA += numA
    partsNumSumBa.append(sumA)


sumBAll = 0
for curPartsC in partsNumC:
    indexB = bisect.bisect_left(partsNumB, curPartsC)
    if indexB == 0:
        continue
    numB = partsNumSumBa[indexB - 1]
    sumBAll += numB

print(sumBAll)

Solution (Python):
from bisect import bisect_right,bisect_left
n=int(input())
A=sorted(list(map(int,input().split())))
B=list(map(int,input().split()))
C=sorted(list(map(int,input().split())))

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

print(ans)

Solution (Python):
from bisect import bisect_left,bisect_right
n=int(input())
A=sorted(list(map(int,input().split())))
B=list(map(int,input().split()))
C=sorted(list(map(int,input().split())))

ans=0
for b in B:
    index_a=bisect_left(A,b)
    index_c=bisect_right(C,b)
    ans +=index_a*(n-index_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()

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

print(total)

Solution (Python):
from bisect import bisect_right

if __name__ == "__main__":
    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

    
    clist = [0] * n
    clist[n-1] = n - bisect_right(c, b[n-1])
    for i in range(n-2, -1, -1):
        clist[i] = n - bisect_right(c, b[i]) + clist[i+1]

    for aa in a:
        b_pos = bisect_right(b, aa)
        if b_pos != n:
            ans += clist[b_pos]

    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())))
ali = [0] * (N+1)
bli = [N - bisect.bisect_left(C,B[i]+1) for i in range(N)]
for i in range(N):
    ali[i+1] = ali[i] + bli[i]
print(sum([ali[N]-ali[bisect.bisect_left(B,A[i]+1)] for i in range(N)]))