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



Sample input:
2
1 5
2 4
3 6


Sample output:
3

Submission status: Accepted

Solution code:
import sys
from operator import mul
def 解():
    input = sys.stdin.readline
    iN = int(input())
    aA = sorted([int(_) for _ in input().split()])
    aB = sorted([int(_) for _ in input().split()])
    aC = sorted([int(_) for _ in input().split()])

    aBA =[0]*iN
    aBC = [0]*iN

    iIndex = iN-1
    for i in range(iN-1,-1,-1):
        iB = aB[i]
        while aA[iIndex] >= iB:
            iIndex -= 1
            if iIndex < 0:
                break
        if iIndex >= 0 :
            aBA[i] = iIndex + 1
        else:
            break
    iBstart = 0
    if aC[0] > aB[0]:
        for iBstart in range(iN):
            if aB[iBstart] >= aC[0]:
                break
            else:
                aBC[iBstart] = iN
    iIndex = 0
    for i in range(iBstart,iN):
        iB = aB[i]
        while aC[iIndex] <= iB:
            iIndex += 1
            if iN <= iIndex :
                break
        if iIndex < iN  :
            aBC[i] = iN - iIndex
        else:
            break
    print(sum(map(mul,aBA,aBC)))
解()

Submission status: Accepted

Solution code:
import sys
def 解():
    input = sys.stdin.readline
    iN = int(input())
    aA = sorted([int(_) for _ in input().split()])
    aB = sorted([int(_) for _ in input().split()])
    aC = sorted([int(_) for _ in input().split()])

    aBA =[0]*iN
    iIndex = iN-1
    for i in range(iN-1,-1,-1):
        iB = aB[i]
        while aA[iIndex] >= iB:
            iIndex -= 1
            if iIndex < 0:
                break
        if iIndex >= 0 :
            aBA[i] = iIndex + 1
        else:
            break
    aAcBA=[0]*iN
    aAcBA[0]=aBA[0]
    for i in range(1,iN):
        aAcBA[i]=aAcBA[i-1]+aBA[i]
    iAns = 0
    iIndex = iN-1
    for i in range(iN-1,-1,-1):
        iC = aC[i]
        while aB[iIndex] >= iC:
            iIndex -= 1
            if iIndex < 0:
                break
        if iIndex >= 0 :
            iAns += aAcBA[iIndex]
        else:
            break
    print(iAns)
解()

Submission status: Accepted

Solution code:
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

b_map = {}
c_index = 0
all = 0
for i in range(N):
    b = B[i]
    c = C[c_index]
    countable = True
    while(b >= c):
        if c_index == N-1:
            countable = False
            break
        c_index += 1
        c = C[c_index]
    if countable:
        b_map[i] = N-c_index
        all += b_map[i]
    else:
        b_map[i] = 0
m = {}
for i in range(N):
    if i > 0:
        m[i] = m[i-1] - b_map[i-1]
    else:
        m[i] = all

b_index = 0
for i in range(N):
    a = A[i]
    b = B[b_index]
    countable = True
    while(a >= b):
        if b_index == N-1:
            countable = False
            break
        b_index += 1
        b = B[b_index]
    if countable:
        cnt += m[b_index]

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
lis = list(map(int,input().split()))
li = list(map(int,input().split()))
l = list(map(int,input().split()))
cou = 0
co = 0
num = 0
nu = 0
ans = []
an = []
lis.sort(reverse = True)
li.sort(reverse = True)
for i in range(n):
  while True:
    if num == n:
      ans.append(0)
      break
    if lis[num] >= li[i]:
      num += 1
    else:
      ans.append(n-num)
      break
l.sort()
for i in range(n):
  while True:
    if nu == n:
      an.append(0)
      break
    if li[-1-i] >= l[nu]:
      nu += 1
    else:
      an.append(n-nu)
      break
for i in range(n):
  cou += ans[i]*an[-1-i]
print(cou)

Submission status: Accepted

Solution code:
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()

ans=0

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

print(ans)

Submission status: Accepted

Solution code:
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()

ans=0

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

print(ans)

Submission status: Accepted

Solution code:
import sys
fin = sys.stdin.readline
from bisect import bisect, bisect_left
from collections import defaultdict


N = int(fin())
A_list = [int(elem) for elem in fin().split()]
B_list = sorted([int(elem) for elem in fin().split()])
C_list = sorted([int(elem) for elem in fin().split()])

# # create mapping
# map_B_to_num_C = dict()
# for B in B_list:
#     idx = bisect(C_list, B)
#     map_B_to_num_C[B] = N - idx

# # create cuml_sum
# cuml_combinations = [0] * N
# current_combinations = 0
# for i, B in enumerate(reversed(B_list)):
#     current_combinations += map_B_to_num_C[B]
#     cuml_combinations[N - i - 1] = current_combinations

cuml_combinations = [0] * N
current_combinations = 0
for i, B in enumerate(reversed(B_list)):
    idx = bisect(C_list, B)
    current_combinations += N - idx
    cuml_combinations[N - i - 1] = current_combinations

num_combinations = 0
for A in A_list:
    B_idx = bisect(B_list, A)
    try:
        num_combinations += cuml_combinations[B_idx]
    except IndexError:
        pass

print(num_combinations)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
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()
i=0
j=0
k=0
answer = 0
left = 0
right = N
for j in range(N):
    while i<N and a[i] < b[j]:
        left += 1
        i += 1
    while k<N and c[k] <= b[j]:
        right -= 1
        k += 1
    answer += left*right
print(answer)

Submission status: Accepted

Solution code:
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 b in B:
    ans += bisect_left(A, b) * (N - bisect_right(C,  b))

print(ans)

Submission status: Accepted

Solution code:
import bisect
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()))

# O(NlogN)
AI = sorted(_A)
BI = sorted(_B)
CI = sorted(_C)

BJ = []

# O(NlogN)
for bi in BI:
    bj = bisect.bisect_left(AI,bi)
    BJ.append(bj)
    
# O(N)
BJ = np.cumsum(BJ)

ans = 0

# O(NlogN)
for ci in CI:
    cj = bisect.bisect_left(BI, ci)
    if cj != 0:
        ans += BJ[cj-1] 

print(ans)

Submission status: Accepted

Solution code:
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()

anum = [1 for i in range(n)]
bnum = [0 for i in range(n)]
cnum = [0 for i in range(n)]
i = 0
j = 0
count = 0
while j < n:
    while i < n :
        if a[i] >= b[j]:
            break
        count += anum[i]
        i += 1
    bnum[j] = count
    j += 1
i = 0
j = 0
count = 0
while j < n:
    while i < n  :
        if b[i] >= c[j]:
            break
        count += bnum[i]
        i += 1
    cnum[j] = count
    j += 1
print(sum(cnum))

Submission status: Accepted

Solution code:
from bisect import *

n = int(input())
a,b,c = [sorted(map(int,input().split())) for i in range(3)]

print(sum([bisect_left(a,i)*(n-bisect_right(c,i)) for i in b]))

Submission status: Accepted

Solution code:
def BvsA(i):
    min, max = 0, N
    while max-min>1:
        mid = (min+max)//2
        if B[i] > A[mid]:
            min = mid
        else:
            max = mid
    return min+1

def BvsC(i):
    min, max = -1, N-1
    while max-min>1:
        mid = (min+max)//2
        if B[i] < C[mid]:
            max = mid
        else:
            min = mid
    return N-max


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

Ans = 0
for i in range(N):
    if A[0] < B[i] < C[N-1]:
        Ans += BvsA(i) * BvsC(i)
print(Ans)

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(list(map(int,input().split())))
b = sorted(list(map(int,input().split())))
c = sorted(list(map(int,input().split())))
p = [0]*n
q = [0]*n
k = 0
for i in range(n):
  while k<n:
    if a[k]<b[i]:
      k += 1
    else:
      break
  p[i] = k
k = 0
s = 0
ans = 0
for i in range(n):
  while k<n:
    if b[k]<c[i]:
      s += p[k]
      k += 1
    else:
      break
  ans += s
print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import bisect

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

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

num_b = [0 for _ in range(n)]
j = 0
for i in range(n):
    if j == n:
        num_b[i] = n
        continue
    while a[j] < b[i]:
        j += 1
        if j == n:
            break
    num_b[i] = j

ans = 0
j = 0
temp_sum = 0
for i in range(n):
    while b[j] < c[i]:
        temp_sum += num_b[j]
        j += 1
        if j == n:
            ans += (n - i) * sum(num_b)
            break
    if j == n:
        break
    ans += temp_sum

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

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

# print(An)
# print(Bn)
# print(Cn)

# B
C_cnt = []
C_sum = 0
j = 0
for i in range(N):
    while j < N:
        if Bn[i] < Cn[j]:
            C_cnt.append(N - j)
            C_sum += N - j
            break
        else:
            j += 1


B_cnt = []
temp_sum = 0
for cnt in C_cnt:
    B_cnt.append(C_sum - temp_sum)
    temp_sum += cnt

A_cnt = 0
j = 0
for i in range(N):
    while j < N:
        if An[i] < Bn[j]:
            if j < len(B_cnt):
                A_cnt += B_cnt[j]
            break
        else:
            j += 1

# print(C_cnt)
# print(B_cnt)
print(A_cnt)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort()
B.sort()
C.sort()
ans = 0
for i in range(N):
    a = bisect.bisect_left(A, B[i])
    c = bisect.bisect_right(C, B[i])
    ans += a*(N-c)

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 20 10:21:00 2018
ABC077C
@author: maezawa
"""
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()

bcnt = [0]*n
for i, bi in enumerate(b):
    j = bisect.bisect_left(a, bi)
    if i == 0:
        bcnt[i] = j
    else:
        bcnt[i] = bcnt[i-1] + j
cnt = 0
for i, ci in enumerate(c):
    j = bisect.bisect_left(b, ci)
    if j == 0:
        continue
    else:
        cnt += bcnt[j-1]
    #print(ci, j, bcnt[j-1], cnt)
    
print(cnt)

Submission status: Accepted

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

a_max = 0
c_min = 0
ans = 0

for j in range(N):
    b = B[j]

    a_max = bisect.bisect_left(A,b,a_max,N)
    c_min = bisect.bisect_right(C,b,c_min,N)
    ans += (a_max) * (N-c_min)

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    from bisect import bisect_left
    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())))
    ans = 0

    for bi in b:
        less = bisect_left(a, bi)
        greater = n - bisect_right(c, bi)
        ans += less * greater

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    from bisect import bisect_left
    from bisect import 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 bi in b:
        less = bisect_left(a, bi)
        greater = n - bisect_right(c, bi)
        ans += less * greater

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from bisect import bisect_left, bisect_right

n = ni()
a = sorted(list(li()))
b = sorted(list(li()))
c = sorted(list(li()))

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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:
    i_a = bisect.bisect_left(A, b)
    i_c = bisect.bisect_right(C, b)
    ans += i_a * (N-i_c)
print(ans)

Submission status: Accepted

Solution code:
# coding:utf-8

INF = float('inf')


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


N = int(input())
A = sorted(inpl())
B = sorted(inpl())
C = sorted(inpl())

ans = 0
i_a = 0
i_c = 0
for i_b in range(N):
    while i_a < N and A[i_a] < B[i_b]:
        i_a += 1
    while i_c < N and C[i_c] <= B[i_b]:
        i_c += 1

    ans += i_a * (N - i_c)

print(ans)

Submission status: Accepted

Solution code:
import bisect
def main() :
    N = int(input())
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))
    C = list(map(int,input().split()))
    A.sort()
    B.sort()
    C.sort()
    answer = 0
    for b in B :
        i = bisect.bisect_left(A,b)
        j = bisect.bisect_right(C,b)
        if i > 0 and j != len(C) :
            answer += i * (N-j)
    print(answer)
main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def main():
    import bisect
    N = int(input())
    As = sorted(list(map(int, input().split())))
    Bs = sorted(list(map(int, input().split())))
    Cs = sorted(list(map(int, input().split())))

    c_under_b = [0]*N #b(i)の下に置けるcの数
    a_on_b = [0]*N #b(i)の上に置けるaの数

    for i in range(len(Bs)):
        c_under_b[i] = N - bisect.bisect_right(Cs, Bs[i])

    for i in range(len(Bs)):
        a_on_b[i] = bisect.bisect_left(As, Bs[i])

    count = 0
    for i in range(len(Bs)):
        count += c_under_b[i] * a_on_b[i]
    print(count)

def main5():
    """ AC """
    import bisect
    N = int(input())
    As = sorted(list(map(int, input().split())))
    Bs = sorted(list(map(int, input().split())))
    Cs = sorted(list(map(int, input().split())))

    c_under_b = [] #b(i)の下に置けるcの数
    a_on_b = [] #b(i)の上に置けるaの数

    for b in Bs:
        i = bisect.bisect_right(Cs, b)
        c_under_b.append(N - i)

    for b in Bs:
        i = bisect.bisect_left(As, b)
        a_on_b.append(i)

    count = 0
    for i in range(len(Bs)):
        count += c_under_b[i] * a_on_b[i]
    print(count)

def main4():
    """ TLE """
    import bisect
    N = int(input())
    As = sorted(list(map(int, input().split())))
    Bs = sorted(list(map(int, input().split())))
    Cs = sorted(list(map(int, input().split())))

    c_under_b = [] #b(i)の下に置けるcの数
    a_on_b = [] #b(i)の上に置けるaの数

    for b in Bs:
        i = bisect.bisect_right(Cs, b)
        c_under_b.append(len(Cs[i:]))

    for b in Bs:
        i = bisect.bisect_left(As, b)
        a_on_b.append(len(As[:i]))

    count = 0
    for i in range(len(Bs)):
        count += c_under_b[i] * a_on_b[i]
    print(count)

def main3():
    """ TLE """
    import itertools
    N = int(input())
    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))
    Cs = list(map(int, input().split()))

    As.sort()
    Bs.sort()
    Cs.sort()
    count = 0
    for c in Cs:
        for b in Bs:
            if c <= b:
                break
            for a in As:
                if b <= a:
                    break
                if c > b > a:
                    count += 1
    print(count)

def main2():
    """ TLE """
    import itertools
    N = int(input())
    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))
    Cs = list(map(int, input().split()))

    count = 0
    for c in Cs:
        for b in Bs:
            for a in As:
                if a < b < c:
                    count += 1
    print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def main():
    import bisect
    N = int(input())
    As = sorted(list(map(int, input().split())))
    Bs = sorted(list(map(int, input().split())))
    Cs = sorted(list(map(int, input().split())))

    c_under_b = [0]*N #b(i)の下に置けるcの数
    a_on_b = [0]*N #b(i)の上に置けるaの数

    for i in range(len(Bs)):
        c_under_b[i] = bisect.bisect_right(Cs, Bs[i])

    for i in range(len(Bs)):
        a_on_b[i] = bisect.bisect_left(As, Bs[i])

    count = 0
    for i in range(len(Bs)):
        count += c_under_b[i] * a_on_b[i]
    print(count)

def main5():
    import bisect
    N = int(input())
    As = sorted(list(map(int, input().split())))
    Bs = sorted(list(map(int, input().split())))
    Cs = sorted(list(map(int, input().split())))

    c_under_b = [] #b(i)の下に置けるcの数
    a_on_b = [] #b(i)の上に置けるaの数

    for b in Bs:
        i = bisect.bisect_right(Cs, b)
        c_under_b.append(N - i)

    for b in Bs:
        i = bisect.bisect_left(As, b)
        a_on_b.append(i)

    count = 0
    for i in range(len(Bs)):
        count += c_under_b[i] * a_on_b[i]
    print(count)

def main4():
    """ TLE """
    import bisect
    N = int(input())
    As = sorted(list(map(int, input().split())))
    Bs = sorted(list(map(int, input().split())))
    Cs = sorted(list(map(int, input().split())))

    c_under_b = [] #b(i)の下に置けるcの数
    a_on_b = [] #b(i)の上に置けるaの数

    for b in Bs:
        i = bisect.bisect_right(Cs, b)
        c_under_b.append(len(Cs[i:]))

    for b in Bs:
        i = bisect.bisect_left(As, b)
        a_on_b.append(len(As[:i]))

    count = 0
    for i in range(len(Bs)):
        count += c_under_b[i] * a_on_b[i]
    print(count)

def main3():
    """ TLE """
    import itertools
    N = int(input())
    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))
    Cs = list(map(int, input().split()))

    As.sort()
    Bs.sort()
    Cs.sort()
    count = 0
    for c in Cs:
        for b in Bs:
            if c <= b:
                break
            for a in As:
                if b <= a:
                    break
                if c > b > a:
                    count += 1
    print(count)

def main2():
    """ TLE """
    import itertools
    N = int(input())
    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))
    Cs = list(map(int, input().split()))

    count = 0
    for c in Cs:
        for b in Bs:
            for a in As:
                if a < b < c:
                    count += 1
    print(count)


if __name__ == '__main__':
    main5()

Submission status: Accepted

Solution code:
#ABC077C 解説を見て自作
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())))
#あるBの下にいくつCが入るか数える
C_under_B=[0]*N     #i番目のBの下に入るCの数
for i in range(N):
    C_under_B[i]=N-bisect.bisect_right(C,B[i])
#あるBの上にいくつAが乗るか数える
A_on_B=[0]*N     #i番目のBの上に乗るAの数
for i in range(N):
    A_on_B[i]=bisect.bisect_left(A,B[i])
#あるBの上下におけるものを数えて出力
ans=0
for i in range(N):
    ans+=C_under_B[i]*A_on_B[i]
print(ans)

Submission status: Accepted

Solution code:
from sys import stderr
from functools import reduce
from operator import add
import bisect
def f(): return [int(i) for i in input().split()]
def yes(n): print("Yes" if n else "No")
def debug(*x, sep=" ", end="\n"):
    for item in x:
        stderr.write(str(item))
        stderr.write(sep)
    stderr.write(end)

n = int(input())
a = sorted(f())
b = sorted(f())
c = sorted(f())
debug(a,b,c,sep='\n')
ans = 0

# 各bに対して、上に乗せられるaの数を予め求めておく(後のために累積和とする)
canRideOnB = [bisect.bisect_left(a, b[0])]
for middle in b[1:]:
    top = bisect.bisect_left(a, middle)
    canRideOnB.append(top+canRideOnB[-1])
debug(canRideOnB)

# 各cに対して、上に乗せられるbの範囲を求める
canRideOnC = []
for bottom in c:
    mid = bisect.bisect_left(b, bottom)
    canRideOnC.append(mid)
debug(canRideOnC)

# 以上の計算から答えを求める
for bot in canRideOnC:
    if bot == 0:
        pass
    else:
        ans += canRideOnB[bot-1]

print(ans)

Submission status: Accepted

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

a = sorted(a)[::-1]
b = sorted(b)[::-1]
c = sorted(c)[::-1]

a_smaller_b = [0]*n
b_smaller_c = [0]*n
n_a, n_b = 0, 0
j, k = 0, 0
for i in range(n):
    a_tar = a[i]
    b_tar = b[i]
    while True:
        if j == n:
            a_smaller_b[i] = n_a
            break
        if b[j] > a_tar:
            n_a += 1
            j += 1
        else:
            a_smaller_b[i] = n_a
            break

    while True:
        if k == n:
            b_smaller_c[i] = n_b
            break
        if c[k] > b_tar:
            n_b += 1
            k += 1
        else:
            b_smaller_c[i] = n_b
            break
# print(a_smaller_b)
# print(b_smaller_c)

bsc_cumsum = [0]*(n+1)
for i in range(n):
    bsc_cumsum[i+1] = bsc_cumsum[i]+b_smaller_c[i]

ans = 0
for i in range(n):
    ans += bsc_cumsum[a_smaller_b[i]]
print(ans)

Submission status: Accepted

Solution code:
import bisect

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

on_b_nums = []
num = 0
for v in b:
    num += bisect.bisect_left(a, v)
    on_b_nums.append(num)

on_c_nums = []
for v in c:
    i = bisect.bisect_left(b, v)
    if i > 0:
        on_c_nums.append(on_b_nums[i-1])
    else:
        on_c_nums.append(0)

print(sum(on_c_nums))

Submission status: Accepted

Solution code:
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()

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

Submission status: Accepted

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

T = []
for j, D in enumerate([C, B, A]):
    for Di in D:
        T.append(Di * 10 + j)

Ai = Ci = cnt = 0

for t in sorted(T):
    c = chr(ord('C') - (t % 10))
    if c == 'A':
        Ai += 1
    if c == 'C':
        Ci += 1
    if c == 'B':
        cnt += Ai * (N - Ci)

print(cnt)

Submission status: Accepted

Solution code:
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())))

count = 0

for b in B:
    a_num = bisect_left(A, b)
    c_num = n - bisect_right(C, b)
    count += a_num * c_num
print(count)

Submission status: Accepted

Solution code:
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()
result = 0

for b in b_list:
    a_count = bisect.bisect_left(a_list, b)
    c_count = bisect.bisect_right(c_list, b)
    result += a_count*(len(c_list)-c_count)

print(result)

Submission status: Accepted

Solution code:
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:
    ans+=bisect.bisect_left(A,b)*(len(C)-bisect.bisect_right(C,b))
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def main():
    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()))))

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from bisect import bisect_left,bisect_right
N=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
C=[int(i) for i in input().split()]
A.sort()
C.sort()
ans=0
for i in range(N):
    ans+=bisect_left(A,B[i])*(N-bisect_right(C,B[i]))
print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = sorted([int(x) for x in input().split()])
B = sorted([int(x) for x in input().split()])
C = sorted([int(x) for x in input().split()])
a = 0
c = 0
cnt = 0
for i in range(N):
  while a < N:
    if A[a] < B[i]:
      a += 1
    else:
      break
  while c < N:
    if C[c] <= B[i]:
      c += 1
    else:
      break
  cnt += a * (N - c)
print(cnt)