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: Runtime Error

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())))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
B=[]
C=[]
x=0
f=0
import bisect
for i in range(n):
    if a[x]>=b[i] and f==0:
        for j in range(n-x):
            x+=1
            if x>=n:
                x=n
                f=1
                break
            if a[x]<b[i]:
                break
    B.append([b[i],n-x])
S=0
for i in range(n):
    S+=B[i][1]
b1=[S]
for i in range(n-1):
    b1.append(b1[i]-B[i][1])
y=0
ans=0
f1=0
for i in range(n):
    if b[y]>=c[i] and f==0:
        for j in range(n-y):
            y+=1
            if y>=n:
                f==1
                break
            if b[y]<c[i]:
                break
    if y<n:
        ans+=b1[y]
    #print(ans,y)
#print(a)
#print(b)
#print(c)
#print(B)
#print(b1)
print(ans)

Submission status: Runtime Error

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())))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
B=[]
C=[]
x=0
for i in range(n):
    if a[x]>=b[i]:
        for j in range(n-x):
            x+=1
            if x>=n:
                x=n
                break
            if a[x]<b[i]:
                break
    B.append([b[i],n-x])
S=0
for i in range(n):
    S+=B[i][1]
b1=[S]
for i in range(n-1):
    b1.append(b1[i]-B[i][1])
y=0
ans=0
for i in range(n):
    if b[y]>=c[i]:
        for j in range(n-y):
            y+=1
            if y>=n:
                break
            if b[y]<c[i]:
                break
    if y<n:
        ans+=b1[y]
    #print(ans,y)
#print(a)
#print(b)
#print(c)
#print(B)
#print(b1)
print(ans)

Submission status: Runtime Error

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())))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
B=[]
C=[]
x=0
for i in range(n):
    if a[x]>=b[i]:
        for j in range(n-x):
            x+=1
            if a[x]<b[i]:
                break
    B.append([b[i],n-x])
S=0
for i in range(n):
    S+=B[i][1]
b1=[S]
for i in range(n-1):
    b1.append(b1[i]-B[i][1])
y=0
ans=0
for i in range(n):
    if b[y]>=c[i]:
        for j in range(n-y):
            y+=1
            if y>=n:
                break
            if b[y]<c[i]:
                break
    if y<n:
        ans+=b1[y]
    #print(ans,y)
#print(a)
#print(b)
#print(c)
#print(B)
#print(b1)
print(ans)

Submission status: Runtime Error

Solution code:
import bisect

n=int(input())

list_A=sorted(list(input().split('')))
list_B=sorted(list(input().split('')))
list_C=sorted(list(input().split('')))
ans=0
for i in range(len(list_B)):
	a=bisect.bisect_left(list_A, list_B[i])
	b=bisect.bisect_right(list_C, list_B[i])
	ans += a*(n-b)
print(ans)

Submission status: Runtime Error

Solution code:
import bisect

n=int(input())

list_A=sorted(list(input().split('')))
list_B=sorted(list(input().split('')))
list_C=sorted(list(input().split('')))
ans=0
for i in list_B:
	a=bisect.bisect_left(list_A, i)
	b=bisect.bisect_right(list_C, i)
	ans += a*(n-b)
print(ans)

Submission status: Runtime Error

Solution code:
import bisect

n=int(input())

list_A=sorted(list(input().split(' ')))
list_B=sorted(list(input().split(' ')))
list_C=sorted(list(input().split(' ')))

for i in range(len(list_B)):
	ans=0
	a=bisect.bisect_left(list_A, list[i])
	b=bisect.bisect_right(list_C, list[i])
	ans += a*b
print(ans)

Submission status: Runtime Error

Solution code:
import bisect

n=int(input())

list_A=sorted(list(input().split(' ')))
list_B=sorted(list(input().split(' ')))
list_C=sorted(list(input().split(' ')))
ans=0
for i in range(len(list_B)):
	a=bisect.bisect_left(list_A, list[i])
	b=bisect.bisect_right(list_C, list[i])
print(a*b)

Submission status: Runtime Error

Solution code:
import bisect

N=int(input())
a1=0
a2=0
b1=0
b2=0
c1=0
c2=0
c3=0

ans=0

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)

for i in range(len(A)):
  a1=A[i]
  b2=bisect.bisect_left(B,a1)

  if a1==B[b2]:
      for j in range(b2+1,len(B)):
        b1=B[j]
        c2=bisect.bisect_left(C,b1)
        c3=len(C)-c2
        if b1==C[c2]:
          c3 -=1
          ans += c3
        else:
          ans += c3

  else:
    for j in range(b2,len(B)):
      b1=B[j]
      c2=bisect.bisect_left(C,b1)
      c3=len(C)-c2
      if b1==C[c2]:
        c3 -=1
        ans += c3
      else:
        ans += c3

print(ans)

Submission status: Runtime Error

Solution code:
def abis(l, key):
    le = 0
    ri = n
    while le < ri:
        mid = (le + ri) // 2
        if mid == n:
            return mid
        elif l[mid] == key:
            while mid<n and l[mid-1]==l[mid]:
                mid-=1
            return mid
        elif l[mid] > key:
            if mid != 0 and l[mid - 1] < key:
                return mid
            ri = mid
        else:
            le = mid + 1
    return mid + 1

def cbis(l, key):
    le = 0
    ri = n
    while le < ri:
        mid = (le + ri) // 2
        if mid == n:
            return 0
        elif l[mid] == key:
            while mid<n and l[mid+1]==l[mid]:
                mid+=1
            return n-mid-1
        elif l[mid] > key:
            if mid != 0 and l[mid - 1] < key:
                return n-mid
            ri = mid
        else:
            le = mid + 1
    return n-mid



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):
    aa = abis(a, b[i])
    ca = cbis(c, b[i])
    ans += aa * ca
print(ans)

Submission status: Runtime Error

Solution code:
import bisect
n = int(input())
x = sorted(list(map(int,input().split())), reverse=True)
y = sorted(list(map(int,input().split())), reverse=True)
z = sorted(list(map(int,input().split())), reverse=True)
count = 0
X =[]
for b in range(n):
  k = bisect.bisect_right(x,y[b])
  X[b] =(n-k)
for a in range(n):
  j = bisect.bisect_right(y,z[a])
  count+=sum(X[j:])
print(count)

Submission status: Runtime Error

Solution code:
import bisect
n = int(input())
x = sorted(list(map(int,input().split())), reverse=True)
y = sorted(list(map(int,input().split())), reverse=True)
z = sorted(list(map(int,input().split())), reverse=True)
count = 0
X =[]
for b in range(n):
  k = bisect.bisect_right(x,y[b])
  X[b] =(n-k)
for a in range(n):
  j = bisect.bisect_right(y,z[a])
    count+=sum(X[j:])
print(count)

Submission status: Runtime Error

Solution code:
n = int(input())
x = sorted(list(map(int,input().split())))[::-1]
y = sorted(list(map(int,input().split())))[::-1]
z = sorted(list(map(int,input().split())))[::-1]
count = 0
for a in range(n):
  j = bisect.bisect_right(y,z[a])
  for b in range(j,n-1):
    k = bisect.bisect_right(x,y[b])
    count+=(n-k)
print(count)

Submission status: Runtime Error

Solution code:
import collections

def nibutansaku(lis,lower,upper,key):
    while abs(upper - lower) > 1:
        mid = int((upper + lower) / 2)
        if lis[mid] >= key:
            upper = mid
        else:
            lower = mid
    return upper

n = int(input())

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

a = sorted(a)
a.reverse()
b = sorted(b)
b = collections.deque(b)
c = sorted(c)

ans = 0

while len(b) > 0:
    key = b.popleft()
    upper = n + 1
    lower = -1
    aNum = nibutansaku(a,upper,lower,key-1)
    cNum = nibutansaku(c,lower,upper,key+1)
    ans += (n - 1 - aNum) * (n - cNum)

print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
B = list(map(int, input().split()))
B.sort(reverse=True)
C = list(map(int, input().split()))
C.sort(reverse=True)
count = [0]*N
answer = 0
z = 0
for i in range(N):
    if B[N-1] >= C[i]:
        break
    z += 1
count[N-1] = z
for i in range(1, N):
    for j in range(z):
        if B[N-1-i] < C[z-1-j]:
            break
        else:

            z -= 1
    count[N-1-i] = z
sum_matrix = [0]*(N+1)
y = 0
for i in range(N):
    sum_matrix[i+1] = sum_matrix[i] + count[i]
for i in range(N):
    if A[N-1] >= B[i]:
        break
    answer += count[i]
    y += 1

for i in range(1, N):
    for j in range(y):
        if A[N-1-i] < B[y-1-j]:
            break        
    y -= j
    answer += sum_matrix[y]
    #print(y)
    #print(answer)
#print(count)
#print(sum_matrix)
print(answer)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(1000000)

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


def binary_search_A_max_index(b, begin, end):
  if A[begin] >= b: return begin-1
  elif A[end] <  b: return end
  elif begin == end - 1: return begin
  
  # if end - begin > 1:
  mid_index = (begin + end) // 2
  if A[mid_index] < b:
    if A[mid_index+1] >= b:
      return mid_index
    else:
      return binary_search_A_max_index(b, mid_index+1, end)
  
  else:
    if A[mid_index-1] < b:
      return mid_index-1
    else:
      return binary_seach_A_max_index(b, begin, mid_index)
    
    
def binary_search_C_min_index(b, begin, end):
  if C[begin] <= b: return begin-1
  elif C[end] >  b: return end
  elif end == begin + 1: return begin
  
  # if end - begin > 1:
  mid_index = (begin + end) // 2
  if C[mid_index] > b:
    if C[mid_index+1] <= b:
      return mid_index
    else:
      return binary_search_C_min_index(b, mid_index+1, end)
    
  else:
    if C[mid_index-1] > b:
      return mid_index-1
    else:
      return binary_search_C_min_index(b, begin, mid_index)
    
    
combination = 0
for b in B:
  a_count = binary_search_A_max_index(b, 0, N-1) + 1
  c_count = binary_search_C_min_index(b, 0, N-1) + 1
  combination += a_count * c_count
print(combination)

Submission status: Runtime Error

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


def binary_search_A_max_index(b, begin, end):
  if A[begin] >= b: return begin-1
  elif A[end] <  b: return end
  elif begin == end - 1: return begin
  
  # if end - begin > 1:
  mid_index = (begin + end) // 2
  if A[mid_index] < b:
    if A[mid_index+1] >= b:
      return mid_index
    else:
      return binary_search_A_max_index(b, mid_index+1, end)
  
  else:
    if A[mid_index-1] < b:
      return mid_index-1
    else:
      return binary_seach_A_max_index(b, begin, mid_index)
    

def binary_search_C_min_index(b, begin, end):
  if C[begin] <= b: return begin-1
  elif C[end] >  b: return end
  elif end == begin + 1: return begin
  
  # if end - begin > 1:
  mid_index = (begin + end) // 2
  if C[mid_index] > b:
    if C[mid_index+1] <= b:
      return mid_index
    else:
      return binary_search_C_min_index(b, mid_index+1, end)
    
  else:
    if C[mid_index-1] > b:
      return mid_index-1
    else:
      return binary_search_C_min_index(b, begin, mid_index)
    

combination = 0
for b in B:
  a_count = binary_search_A_max_index(b, 0, N-1) + 1
  c_count = binary_search_C_min_index(b, 0, N-1) + 1
  combination += a_count * c_count
print(combination)

Submission status: Runtime Error

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
# from collections import Counter,defaultdict,deque
# from itertools import permutations, combinations
# from heapq import heappop, heappush
# # input = sys.stdin.readline.rstrip()
# sys.setrecursionlimit(10**8)
# mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplt(n): return [tuple(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n = inp()
A = sorted(inpl())
B = sorted(inpl())
C = sorted(inpl())

ans = 0
d = defaultdict(int)
for b in B:
    d[b] = len(C[bisect.bisect_right(C,b):])
for a in A:
    for b in B[bisect.bisect_right(B,a):]:
        ans += d[b]

print(ans)

Submission status: Runtime Error

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


def binary_search(lst, value, high):
  	if high:
      	value += 0.5
    else:
      	value -= 0.5
    l = 0
    r = N - 1
    while l <= r:
        mid = (l + r) // 2
        if lst[mid] < value:
            l = mid + 1
        elif lst[mid] > value:
            r = mid - 1
    if high:
        return l
    else:
        return r


def main():
    ans = 0
    for middle in B:
        lower = binary_search(A, middle, False) + 1
        higher = N - binary_search(C, middle, True)
        ans += lower * higher
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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()
d=[0 for i in range(N)]
ac=0
for i in range(N):
  while C[ac]<=B[i]:
    ac+=1
  d[i]=N-ac
ab=0
ans=0
for i in range(1,N):
  d[i]+=d[i-1]
def f(x):
  if x!=0:
    return d[N-1]-d[x-1]
  else:
    return d[N-1]
for i in range(N):
  while B[ab]<=A[i]:
    ab+=1
    if ab==N:
      break
  ans+=f(ab)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import bisect

def main():
    input = sys.stdin.buffer.readline
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    B = list(map(int, input().split()))
    B.sort()
    C = list(map(int, input().split()))
    ans = 0
    for i in B:
        a = bisect.bisect_left(A, b)
        c = bisect.bisect_right(C, b)
        ans += a*(N-c)
    print(sum(A_count))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A_count = [0] * len(A)
B_count = [0] * len(B)
for i in range(len(B)):
    B_count[i] = len(C) - bisect(C, B[i])
for i in range(len(A)):
    A_count[i] = sum(B_count[bisect(B, A[i]):])
print(sum(A_count))

Submission status: Runtime Error

Solution code:
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 a:
    j_idx = bisect_left(b, i)
    if j_idx == n:
        continue
    for j in b[j_idx:]:
        if i == j:
            continue
        k_idx = bisect_left(c, j)
        if j == c[k_idx]:
            k_idx += 1
        if k_idx == n:
            continue
        ans += (n-k_idx)
print(ans)

Submission status: Runtime Error

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

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

from bisect import bisect_right

d=[]
for i in range(n):
    index = bisect_right(c,b[i])
    if b[i]==c[index]:
        index-=1
    
    if index==-1:
        index=1

    d.append(n-index)

import numpy as np
s=sum(d)
cum=np.cumsum(d)

ans=0
for j in range(n):
    dex = bisect_right(b,a[j])
    if dex==0:
        ans+=s
    elif dex==n:
        continue
    else:
        ans+=s-cum[dex-1]


    

print(ans)

Submission status: Runtime Error

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)

#二分探索(x以上の最小値index) !同じ値の時はindex+1をする
def lower_bound(arr,x):
    l=0
    r=len(arr)-1#数列arrの最後尾のインデックス
    while l<=r:
        mid=(l+r)//2#中央値検索
        if x==arr[mid]:#ちょうど中央値に存在したとき
            return mid+1
        elif x<arr[mid]:#xが左側の配列に存在するとき
            r=mid-1
        else:#xが右側の配列に存在するとき
            l=mid+1
        #print(mid,r,l)
    if x>arr[-1]:
        return -1
    else:
        return r+1

#二分探索(x以下の最大値) !同じ値の時はindex-1をする
def upper_bound(arr,x):
    l=0
    r=len(arr)-1#数列arrの最後尾のインデックス
    while l<=r:
        mid=(l+r)//2#中央値検索
        if x==arr[mid]:#ちょうど中央値に存在したとき
            return mid-1
        elif x<arr[mid]:#xが左側の配列に存在するとき
            r=mid-1
        else:#xが右側の配列に存在するとき
            l=mid+1
        #print(mid,r,l)
    if x<arr[0]:
        return -1
    else:
        return r

#個数をカウント
count=0
for i in range(n):
    a_count=upper_bound(a,b[i])+1#b要素固定でのa要素の個数
    c_count=lower_bound(c,b[i])+1#b要素固定でのc要素の個数
    if a_count=-1 or c_count=-1:
        count=0
    else:
        count+=a_count*(len(c)-c_count+1)
        #print(upper_bound(a,b[i]),lower_bound(c,b[i]))
        #print(a_count,len(c),c_count,len(c)-c_count+1)
        #print(count)

print(count)

Submission status: Runtime Error

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)

#二分探索(x以上の最小値index) !同じ物は一個下げる
def lower_bound(arr,x):
    l=0
    r=len(arr)-1#数列arrの最後尾のインデックス
    while l<=r:
        mid=(l+r)//2#中央値検索
        if x==arr[mid]:#ちょうど中央値に存在したとき
            return mid+1
        elif x<arr[mid]:#xが左側の配列に存在するとき
            r=mid-1
        else:#xが右側の配列に存在するとき
            l=mid+1
        #print(mid,r,l)
    return r+1

#二分探索(x以下の最大値)
def upper_bound(arr,x):
    l=0
    r=len(arr)-1#数列arrの最後尾のインデックス
    while l<=r:
        mid=(l+r)//2#中央値検索
        if x==arr[mid]:#ちょうど中央値に存在したとき
            return mid-1
        elif x<arr[mid]:#xが左側の配列に存在するとき
            r=mid-1
        else:#xが右側の配列に存在するとき
            l=mid+1
        #print(mid,r,l)
    return r

#個数をカウント
count=0
for i in range(n):
    if min(a)>=b[i] or max(c)<=b[i]:
        count=0
    elif:
        a_count=upper_bound(a,b[i])+1#b要素固定でのa要素の個数
        c_count=lower_bound(c,b[i])+1#b要素固定でのc要素の個数
        count+=a_count*(len(c)-c_count+1)
        #print(upper_bound(a,b[i]),lower_bound(c,b[i]))
        #print(a_count,len(c),c_count,len(c)-c_count+1)
        #print(count)

print(count)

Submission status: Runtime Error

Solution code:
def binary_search(sorted_list, item):
    """
    sorted_listのうち，itemより大きくなる最初のindexを求める．
    """
    low = 0
    high = len(sorted_list)

    while low <= high:
        mid = (low + high) // 2
        if(mid >= len(sorted_list) or mid < 0):
            return None
        if sorted_list[mid] > item and mid == 0:
            return mid
        elif sorted_list[mid] > item and sorted_list[mid - 1] <= item:
            return mid
        elif sorted_list[mid] > item and sorted_list[mid - 1] > item:
            high = mid - 1
        else:
            low = mid + 1
    return None


def solve():
    # 標準入力部分
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))
    # A, B, Cをsort
    A.sort()
    B.sort()
    C.sort()
    ans = 0
    for i, a in enumerate(A):
        if(i > 0):
            if(A[i] == A[i-1]):
                ans += ans_tmp
                continue
        index_b_bigger_a = binary_search(B, a)
        if(index_b_bigger_a is None):
            continue
        for j in range(index_b_bigger_a, N):

            k = binary_search(C, B[j])
            if(k is None):
                continue
            ans_tmp = N - k
            ans += ans_tmp
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
import io
import sys
sys.path.append('./')
from snippet import util

_INPUT = """\
6
3 14 159 2 6 53
58 9 79 323 84 6
2643 383 2 79 50 288
"""
sys.stdin = io.StringIO(_INPUT)


def binary_search(sorted_list, item):
    """
    sorted_listのうち，itemより大きくなる最初のindexを求める．
    """
    low = 0
    high = len(sorted_list)

    while low <= high:
        mid = (low + high) // 2
        if(mid >= len(sorted_list) or mid < 0):
            return None
        if sorted_list[mid] > item and mid == 0:
            return mid
        elif sorted_list[mid] > item and sorted_list[mid - 1] <= item:
            return mid
        elif sorted_list[mid] > item and sorted_list[mid - 1] > item:
            high = mid - 1
        else:
            low = mid + 1
    return None


@util.measure_time
def solve():
    # 標準入力部分
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))
    # A, B, Cをsort
    A.sort()
    B.sort()
    C.sort()
    ans = 0
    for i, a in enumerate(A):
        if(i > 0):
            if(A[i] == A[i-1]):
                ans += ans_tmp
                continue
        index_b_bigger_a = binary_search(B, a)
        if(index_b_bigger_a is None):
            continue
        for j in range(index_b_bigger_a, N):

            k = binary_search(C, B[j])
            if(k is None):
                continue
            ans_tmp = N - k
            ans += ans_tmp
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
#関数リスト
import sys
from bisect import bisect
input = sys.stdin.readline
def RD(): return input().rstrip()
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]

def main():
    N = I()
    A = LI()
    A.sort()
    B = LI()
    B.sort()
    B_len = len(B)
    C = LI()
    C.sort()
    C_len = len(C)
    result = 0
    A_init = A[0]
    for i in A:
        index = bisect(B,i)
        if index == B_len:
            break
        for j in range(index, B_len):
            index2 = bisect(C,B[j]
            if index2 == C_len:
                break
            result += C_len - index2
    print(result)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
(defun lower-bound (predicate &optional (ok 0) (ng (expt 10 10)))
  (if (<= (abs (- ng ok)) 1)
      ok
      (let ((mid (floor (+ ng ok) 2)))
        (if (funcall predicate mid)
            (lower-bound predicate mid ng)
            (lower-bound predicate ok mid)))))


(defun smaller-p (xs index b-size)
  (< (aref xs index) b-size))


(defun bigger-p (xs index b-size)
  (> (aref xs index) b-size))


(defun solve (a b c &optional (res 0))
  (declare (optimize speed))
  (declare (type (array fixnum) a b c))
  (declare (type integer res))
  (the integer
       (dotimes (j (length b) res)
         (incf res (* (lower-bound #'(lambda (x)
                                       (smaller-p a x (aref b j)))
                                   0
                                   (length a))
                      (lower-bound #'(lambda (x)
                                       (bigger-p c x (aref b j)))
                                   0
                                   (length c)))))))


(defparameter *inf* (1+ (expt 10 9)))



(locally
  (declare (inline sort))
  (let* ((n (read))
         (a (make-array (1+ n) :element-type 'fixnum))
         (b (make-array n      :element-type 'fixnum))
         (c (make-array (1+ n) :element-type 'fixnum)))
    (declare (type fixnum n))
    (setf (aref a (1- (length a))) 0)
    (setf (aref c (1- (length c))) *inf*)
    (dotimes (i n)
      (setf (aref a i) (read)))
    (dotimes (i n)
      (setf (aref b i) (read)))
    (dotimes (i n)
      (setf (aref c i) (read)))
    (sort a #'<)
    (sort c #'>)
    (princ (solve a b c))
    (fresh-line)))

Submission status: Runtime Error

Solution code:
(defun lower-bound (predicate &optional (ok 0) (ng (expt 10 10)))
  (if (<= (abs (- ng ok)) 1)
      ok
      (let ((mid (floor (+ ng ok) 2)))
        (if (funcall predicate mid)
            (lower-bound predicate mid ng)
            (lower-bound predicate ok mid)))))


(defun smaller-p (xs index b-size)
  (< (aref xs index) b-size))


(defun bigger-p (xs index b-size)
  (> (aref xs index) b-size))


(defun solve (a b c &optional (res 0))
  (declare (optimize (speed 3) (safety 0)))
  (declare (type (array fixnum) a b c)
           (type integer res))
  (the integer
       (dotimes (j (length b) res)
         (incf res (* (lower-bound #'(lambda (x)
                                       (smaller-p a x (aref b j)))
                                   0
                                   (length a))
                      (lower-bound #'(lambda (x)
                                       (bigger-p c x (aref b j)))
                                   0
                                   (length c)))))))


(defparameter *inf* (1+ (expt 10 9)))



(locally
  (declare (inline sort))
  (let* ((n (read))
         (a (make-array (1+ n) :element-type 'fixnum))
         (b (make-array n      :element-type 'fixnum))
         (c (make-array (1+ n) :element-type 'fixnum)))
    (declare (type fixnum n))
    (setf (aref a (1- (length a))) 0)
    (setf (aref c (1- (length c))) *inf*)
    (dotimes (i n)
      (setf (aref a i) (read)))
    (dotimes (i n)
      (setf (aref b i) (read)))
    (dotimes (i n)
      (setf (aref c i) (read)))
    (sort a #'<)
    (sort c #'>)
    (princ (solve a b c))
    (fresh-line)))

Submission status: Runtime Error

Solution code:
(defun lower-bound (predicate &optional (ok 0) (ng (expt 10 10)))
  (if (<= (abs (- ng ok)) 1)
      ok
      (let ((mid (floor (+ ng ok) 2)))
        (if (funcall predicate mid)
            (lower-bound predicate mid ng)
            (lower-bound predicate ok mid)))))


(defun smaller-p (xs index b-size)
  (< (aref xs index) b-size))


(defun bigger-p (xs index b-size)
  (> (aref xs index) b-size))


(defun solve (a b c &optional (res 0))
  (declare (type (array fixnum) a b c)
           (type integer res))
  (declare (optimize (speed 3) (safety 0)))
  (the integer
       (dotimes (j (length b) res)
         (incf res (* (lower-bound #'(lambda (x)
                                       (smaller-p a x (aref b j)))
                                   0
                                   (length a))
                      (lower-bound #'(lambda (x)
                                       (bigger-p c x (aref b j)))
                                   0
                                   (length c)))))))


(defparameter *inf* (1+ (expt 10 9)))



(locally
  (declare (inline sort))
  (let* ((n (read))
         (a (make-array (1+ n) :element-type 'fixnum))
         (b (make-array n      :element-type 'fixnum))
         (c (make-array (1+ n) :element-type 'fixnum)))
    (declare (type fixnum n))
    (setf (aref a (1- (length a))) 0)
    (setf (aref c (1- (length c))) *inf*)
    (dotimes (i n)
      (setf (aref a i) (read)))
    (dotimes (i n)
      (setf (aref b i) (read)))
    (dotimes (i n)
      (setf (aref c i) (read)))
    (sort a #'<)
    (sort c #'>)
    (princ (solve a b c))
    (fresh-line)))

Submission status: Runtime Error

Solution code:
(defun lower-bound (predicate &optional (ok 0) (ng (expt 10 10)))
  (if (<= (abs (- ng ok)) 1)
      ok
      (let ((mid (floor (+ ng ok) 2)))
        (if (funcall predicate mid)
            (lower-bound predicate mid ng)
            (lower-bound predicate ok mid)))))


(defun smaller-p (xs index b-size)
  (< (aref xs index) b-size))


(defun bigger-p (xs index b-size)
  (> (aref xs index) b-size))


(defun solve (a b c &optional (res 0))
  (declare (type (array fixnum) a b c)
           (type (integer res))
           (optimize (speed 3) (safety 0)))
  (the integer
       (dotimes (j (length b) res)
         (incf res (* (lower-bound #'(lambda (x)
                                       (smaller-p a x (aref b j)))
                                   0
                                   (length a))
                      (lower-bound #'(lambda (x)
                                       (bigger-p c x (aref b j)))
                                   0
                                   (length c)))))))


(defparameter *inf* (1+ (expt 10 9)))



(locally
  (declare (inline sort))
  (let* ((n (read))
         (a (make-array (1+ n) :element-type 'fixnum))
         (b (make-array n      :element-type 'fixnum))
         (c (make-array (1+ n) :element-type 'fixnum)))
    (declare (type fixnum n))
    (setf (aref a (1- (length a))) 0)
    (setf (aref c (1- (length c))) *inf*)
    (dotimes (i n)
      (setf (aref a i) (read)))
    (dotimes (i n)
      (setf (aref b i) (read)))
    (dotimes (i n)
      (setf (aref c i) (read)))
    (sort a #'<)
    (sort c #'>)
    (princ (solve a b c))
    (fresh-line)))