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
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]
import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
from itertools import accumulate #list(accumulate(A))

N = ii()
S = [sorted(li()) for _ in range(3)]
rec = [[0]*N for i in range(3)]

for i in range(N):
    #for j in range(2):
    #rec[0][i] = bisect.bisect_right(S[1], S[0][i])
    rec[1][i] = N - bisect.bisect_right(S[2], S[1][i])

acc = list(accumulate(rec[1]))
#acc_1 = [0] * N
#for i in range(N):
    #ind = bisect.bisect_right(S[2], acc_

ans = 0
for i in range(N):
    #for j in range(rec[0][i], N):
    ind = bisect.bisect_right(S[1], S[0][i])
    if ind == 0:
        ans += acc[-1]
    else:
        ans += acc[-1] - acc[ind-1]

print(ans)

Submission status: Accepted

Solution code:
def isOK(index, key, a):
    if a[index] >= key:
        return True
    else:
        return False

def binary_search(key, a, N):
    ng = -1
    ok = N

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

    return ok

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

# C > B > Aの組み合わせを見つける
# C > B と　B > A の組み合わせの数をカウントしてその積をたす
ans = 0
for i in range(N):
    key = B[i]
    A_index = binary_search(key, A, N)
    C_index = binary_search(key + 1, C, N)
    # print('key: ',key)
    # print('A: ', A[A_index],'   ',A_index)
    # print('C: ', C[C_index],'   ',C_index)

    count_A = A_index
    count_C = N - C_index
    
    ans += count_A * abs(N-C_index)


print(ans)

Submission status: Accepted

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

"""
前処理で、中部の祭壇でBi以下のサイズを選べるときの、中段と上段のパーツの組み合わせのパターン数を計算しておく。
"""
BPattern = []
for i in range(N):
    b = B[i]
    idx = bisect(A,b-1)
    if i == 0:
        BPattern.append(idx)
    else:
        BPattern.append(BPattern[-1]+idx)

ans = 0
for i in range(N):
    c = C[i]
    idx = bisect(B,c-1)
    if idx:
        ans+=BPattern[idx-1]

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect,bisect_left
from collections import deque
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()
tmp=0
for i in b:
    dd=bisect_left(a,i)*(n-bisect(c,i))
    tmp+=dd
print(tmp)

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)
C = sorted(C)
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:
import sys
from bisect import bisect_left, bisect_right

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


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

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

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

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from sys import stdin
from bisect import bisect_right, bisect_left


def input():
    return stdin.readline().strip()


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

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def main():
    N, *ABC = map(int, read().split())
    A = sorted(ABC[:N])
    B = sorted(ABC[N : 2 * N])
    C = sorted(ABC[2 * N : 3 * N])

    ans = 0
    for b in B:
        ok = N
        ng = -1
        while ok - ng > 1:
            mid = (ok + ng) // 2
            if A[mid] >= b:
                ok = mid
            else:
                ng = mid
        a_num = ok

        ok = N
        ng = -1
        while ok - ng > 1:
            mid = (ok + ng) // 2
            if C[mid] > b:
                ok = mid
            else:
                ng = mid
        c_num = N - ok

        ans += a_num * c_num

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_left, bisect_right

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


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

    A.sort()
    C.sort()

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

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_left, bisect_right

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def main():
    N, *ABC = map(int, read().split())
    A = sorted(ABC[:N])
    B = ABC[N : 2 * N]
    C = sorted(ABC[2 * N : 3 * N])

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

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def main():
    N, *ABC = map(int, read().split())
    A = sorted(ABC[:N])
    B = sorted(ABC[N : 2 * N])
    C = sorted(ABC[2 * N : 3 * N])

    ans = 0
    for b in B:
        ok = -1
        ng = N
        while ng - ok > 1:
            mid = (ok + ng) // 2
            if A[mid] < b:
                ok = mid
            else:
                ng = mid
        a_num = ok + 1

        ok = N
        ng = -1
        while ok - ng > 1:
            mid = (ok + ng) // 2
            if C[mid] > b:
                ok = mid
            else:
                ng = mid
        c_num = N - ok

        ans += a_num * c_num

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = [0] + sorted([int(i) for i in input().split()]) + [10**9+1]
B = sorted([int(i) for i in input().split()])
C = [10**9+1] + sorted([int(i) for i in input().split()], reverse=True) + [0]

def A_count(n, ls):
    left = 0
    right = N + 1

    while right - left > 1:
        mid = (left + right) // 2

        if n <= ls[mid]: right = mid
        if n > ls[mid]: left = mid

    return left

def C_count(n, ls):
    left = 0
    right = N + 1

    while right - left > 1:
        mid = (left + right) // 2

        if n >= ls[mid]: right = mid
        if n < ls[mid]: left = mid

    return left

ans = 0
for i in range(N):
    n = B[i]
    ans += A_count(n, A) * C_count(n, C)

print(ans)

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

N = I()
A,B,C = ILL(3)
A.sort()
B.sort()
C.sort()

B2 = []

for b in B:
  p = bisect.bisect_left(A,b)
  if B2 == []:
    B2.append(p)
  else:
    B2.append(B2[-1]+p)

C2 = []
for i in range(N):
  c = C[i]
  p = bisect.bisect_left(B,c)
  if C2 == []:
    if p == 0:
      C2.append(0)
    else:
      C2.append(B2[p-1])
  else:
    if p == 0:
      C2.append(C2[-1])
    else:
      C2.append(C2[-1]+B2[p-1])
print(C2[-1])

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

def isOk(index, key, a):
    if a[index] >= key:
        return True
    else:
        return False

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

    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if isOk(mid, key, a):
            ok = mid
        else:
            ng = mid

    return ok

ans = 0
a.sort()
c.sort()
for i in range(n):
    upper = binary_search(b[i], a)
    lower = n - binary_search(b[i] + 1, c)
    ans += upper * lower
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()
a.append(10**10)
c.append(10**10)
a = [-1] + a
c = [-1] + c
ans = 0
for i in range(len(b)):
    al = 0
    ar = n+1
    while True:
        am = (al+ar)//2
        if a[am] < b[i]:
            al = am
        else:
            ar = am
        an = al
        if al+1 == ar:
            break
    cl = 0
    cr = n+1
    while True:
        cm = (cl+cr)//2
        if c[cm] <= b[i]:
            cl = cm
        else:
            cr = cm
        cn = n - (cr-1)
        if cl+1 == cr:
            break
    ans += an*cn
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())))
count = 0

A.insert(0,-1)
A.append(10e+10)
C.insert(0,-1)
C.append(10e+10)

for bi in B:
    left = 0
    right = n+1
    
    while right-left>1:
        middle = (left+right) //2
        if A[middle]>=bi:
            right = middle
        else:
            left = middle
    a_count = left

    left = 0
    right = n+1
    while right-left>1:
        middle = (left+right) //2
        if C[middle]>bi:
            right = middle
        else:
            left = middle
    c_count = n+1-right

    count += a_count*c_count

print(count)

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

A.insert(0, -100000000000)
A.append(1000000000000)
C.insert(0, -100000000000)
C.append(1000000000000)


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

    a_count = l

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

    c_count = N + 1 - r

    total += a_count * c_count

print(total)

Submission status: Accepted

Solution code:
import bisect

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

cnt = 0

for b in B:
    insert_index1 = bisect.bisect_left(A, b)
    insert_index2 = bisect.bisect_right(C, b)

    cnt += insert_index1 * (N - insert_index2)

print(cnt)

Submission status: Accepted

Solution code:
import bisect


def main():
    N = int(input())
    AArr = list(map(int, input().split()))
    BArr = list(map(int, input().split()))
    CArr = list(map(int, input().split()))

    AArr.sort()
    CArr.sort()
    ans = 0
    for i in range(N):
        a = bisect.bisect_left(AArr, BArr[i])
        c = bisect.bisect_right(CArr, BArr[i])
        c = N-c
        ans += a*c

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
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:
    point_A = bisect.bisect_left(A, b)
    possible_A = point_A
    point_C = bisect.bisect_right(C, b)
    possible_C = N - point_C
    ans += possible_A * possible_C

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
def calc_possible_alter(upper, middle, lower):
  cnt = 0
  for m in middle:
    # choose a part that is smaller than m from upper
    # choose a part that is larger than m from lower
    cnt += bisect_left(upper, m) * (len(lower) - bisect_right(lower, m))
  return cnt


N = int(input())
upper_part = sorted(list(map(int, input().split())))
middle_part = sorted(list(map(int, input().split())))
lower_part = sorted(list(map(int, input().split())))

print(calc_possible_alter(upper_part, middle_part, lower_part))

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

Submission status: Accepted

Solution code:
#ABC077C
def main():
    import sys, 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()
    #print(A, B, C)
    ans = 0
    #Bを起点
    for i in range(N):
        idxA = bisect.bisect_left(A, B[i])
        idxC = bisect.bisect_right(C, B[i])
        #print(idxA, idxC)
        ans +=idxA*(N-idxC)
    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()
B.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)

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

Submission status: Accepted

Solution code:
n = int(input())
aaa = list(map(int, input().split()))
bbb = list(map(int, input().split()))
ccc = list(map(int, input().split()))

aaa.sort()
ccc.sort()

def is_ok_a(mid, b):
    if mid < 0:
        return True
    elif mid >= n:
        return False
    else:
        return aaa[mid] < b

def m_bisect_a(ng, ok, b):
    while abs(ng - ok) > 1:
        mid = (ng + ok) // 2
        if is_ok_a(mid, b):
            ok = mid
        else:
            ng = mid
    return ok

def is_ok_c(mid, b):
    if mid < 0:
        return False
    elif mid >= n:
        return True
    else:
        return ccc[mid] > b

def m_bisect_c(ng, ok, b):
    while abs(ng - ok) > 1:
        mid = (ng + ok) // 2
        if is_ok_c(mid, b):
            ok = mid
        else:
            ng = mid
    return ok

ans = 0
for i in range(n):
    b = bbb[i]
    ng, ok = n, -1
    cnt_a = m_bisect_a(ng, ok, b) + 1
    ng, ok = -1, n
    cnt_c = n - m_bisect_c(ng, ok, b)
    ans += (cnt_a * cnt_c)

print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc077/tasks/arc084_a
import sys
from bisect import bisect_left, bisect_right


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


def I(): return int(sys.stdin.readline().rstrip())


def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))


def LS(): return list(sys.stdin.readline().rstrip().split())


N = I()
A = LI()
B = LI()
C = LI()
A.sort()
B.sort()
C.sort()
ans = 0
for b in B:
    ok_a = bisect_left(A, b)
    ok_c = bisect_right(C,b)
    ans += ok_a * (N-ok_c)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = sorted(tuple(map(int, input().split())))
B = tuple(map(int, input().split()))
C = sorted(tuple(map(int, input().split())))

import bisect

count = 0
for b in B:
    # Aの候補
    la = bisect.bisect_left(A, b)
    # Cの候補
    rc = bisect.bisect_right(C, b)
    
    count += la*(n-rc)

print(count)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import bisect
    
def snuke_festival(a,b,c):
    count = 0
    a_sorted = sorted(a)
    b_sorted = sorted(b)
    c_sorted = sorted(c)
    maxlen=len(b)

    for target in b_sorted:
        #mid_a = binary_search_left(a_sorted,target)
        #mid_c = binary_search_right(c_sorted,target)
        mid_a = bisect.bisect_left(a_sorted,target)
        mid_c = bisect.bisect_right(c_sorted,target)
        count += mid_a * (maxlen - mid_c)
        #print("mid_a %d, mid_c %d, count %d" %(mid_a,maxlen - mid_c,count))
    return count

if __name__ == "__main__":
    n = int(input())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    c = list(map(int,input().split()))

    print(snuke_festival(a,b,c))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from bisect import bisect_right, bisect_left
import numpy as np

n = int(input())

top = map(int, input().split())
mid = map(int, input().split())
bot = map(int, input().split())
top = np.array(sorted(top))
mid = np.array(sorted(mid))
bot = np.array(sorted(bot))

ans = 0
for i in mid:
    top_num = len(top[:bisect_left(top,i)])
    bot_num = len(bot[bisect_right(bot,i):])
    ans += top_num * bot_num

print(ans)

Submission status: Accepted

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

"""
まず、前処理として、「Bの祭壇から大きさb以下のパーツを使うとき、Aの祭壇のパーツとの組み合わせはいくつあるか」というのを調べてメモしておく。
次に、Cの祭壇の大きさがcのとき、という風に場合わけして、選べるBの祭壇の大きさを絞って、A,B,Cの組み合わせの数を加算していく。
"""


tmpSum = 0
dicB = {}
for i in range(N):
    b = B[i]
    if b not in dicB:
        dicB[b] = tmpSum
    idx = bisect_left(A,b)
    dicB[b] += idx
    tmpSum += idx

ans = 0
for i in range(N):
    c = C[i]
    idx = bisect_left(B,c)
    if idx != 0:
        b = B[idx-1]
        ans += dicB[b]

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

def n_smaller_a(num):
    if num>a[n-1]:
        return n
    if num<=a[0]:
        return 0
    left=0
    right=n-1
    mid=(left+right)//2
    while right-left>1:
        if a[mid]>=num:
            right=mid
        else:
            left=mid
        mid=(left+right)//2

    if a[left]==num:
        return left
    else:
        return left+1

def n_bigger_c(num):
    if num >= c[n-1]:
        return 0
    if num < c[0]:
        return n
    left=0
    right=n-1
    mid=(left+right)//2
    while right-left>1:
        if c[mid]>num:
            right=mid
        else:
            left=mid
        mid=(left+right)//2
    if c[right]==num:
        return n-right-1
    else:
        return n-right

ans = 0

for i in range(n):
    ans+=n_bigger_c(b[i])*n_smaller_a(b[i])
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
A=[int(x) for x in input().rstrip().split()]
B=[int(x) for x in input().rstrip().split()]
C=[int(x) for x in input().rstrip().split()]
A.sort()
C.sort()
ans=0

for i in B:
  left=0
  right=n
  while(left<right):
    now=(left+right)//2
    if A[now]<i:
      left=now+1
    else:
      right=now

  left2=0
  right2=n
  while(left2<right2):
    now=(left2+right2)//2
    if C[now]<=i:
      left2=now+1
    else:
      right2=now

  ans+=right*(n-right2)
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
 
n,ans=int(input()),0
 
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
 
A.sort()
C.sort()
 
for b in B:
  j = bisect_left(A,b)
  k = bisect_right(C,b)
  ans += j*(n-k)
  
print(ans)

Submission status: Accepted

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

    for i in range(len(b)):
        a_index = bisect.bisect_left(a, b[i])
        c_index = bisect.bisect_right(c, b[i])
        a_num = 0
        c_num = 0
        if a_index == len(a):
            a_num = len(a)
        else:
            if a[a_index] == b[i]:
                a_num = a_index 
            else:
                a_num = a_index 
        
        if c_index == len(c):
            c_num = 0
        else:
            c_num = len(c) - c_index
        ans += a_num * c_num
    print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right

n,ans=int(input()),0

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

A.sort()
C.sort()

for b in B:
  j = bisect_left(A,b)
  k = bisect_right(C,b)
  ans += j*(n-k)
  
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy
import bisect
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()
    count = 0
    for j in range(N):
        b = B[j]
        a = bisect.bisect_left(A, b)
        c = bisect.bisect_right(C, b)
        count += a * (N - c)
    print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000000)
import numpy as np
from heapq import heapify, heappop, heappush
import copy
from collections import deque
# queueとして使う場合 push, popleftを使う
from bisect import bisect_left, bisect_right


def main():
    N = int(input())
    A = [int(c) for c in input().split()]
    B = [int(c) for c in input().split()]
    C = [int(c) for c in input().split()]

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

    num = 0
    for j in range(N):
        bj = B[j]
        c = bisect_right(C, bj)
        a = bisect_left(A, bj)
        num += a * (N - c)
    
    print(num)


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

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce
from fractions import gcd
import sys
def input(): return sys.stdin.readline().rstrip()
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(input())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = 10**18
# ----------------------------------------------------------- #

n = I()
A = sorted(LI())
B = sorted(LI())
C = sorted(LI())

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

from bisect import bisect_left, bisect_right
from pprint import pprint
from collections import deque, defaultdict
import itertools
import math
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.buffer.readline
INF = float('inf')

n_blocks = int(input())
blocks_top = sorted(list(map(int, input().split())))
blocks_middle = sorted(list(map(int, input().split())))
blocks_bottom = sorted(list(map(int, input().split())))
# pprint(blocks_top)
# pprint(blocks_middle)
# pprint(blocks_bottom)

res = 0
for middle in blocks_middle:
    index_top = bisect_left(blocks_top, middle)
    index_bottom = bisect_right(blocks_bottom, middle)
    # print(f"middle = {middle}")
    # print(f"index_top = {index_top}")
    # print(f"index_bottom = {index_bottom}\n")
    res += index_top * (n_blocks - index_bottom)

print(res)

Submission status: Accepted

Solution code:
# import math
# import itertools
# from collections import deque
# from collections import defaultdict
# import heapq
# import sys
# import numpy as np
# from scipy.special import comb
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()

    ans = 0
    for i in range(N):
        a_i = bisect.bisect_left(A, B[i])
        b_i = (N - bisect.bisect_right(C, B[i]))
        #print("a:{} b:{}".format(a_i, b_i))
        ans += a_i * b_i

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

sum = 0
for j in range(n):
  atemp = bisect.bisect_left(a, b[j])
  ctemp = n - bisect.bisect_right(c, b[j])
  sum += atemp * ctemp

print(sum)