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:
from bisect import bisect_left
n = int(input())
listtop = sorted(list(map(int, input().split())))
listmiddle = sorted(list(map(int, input().split())))
listbottom = sorted(list(map(int, input().split())))
sum = 0

# 最初0があるので注意
# 0~N個目に乗せられるパーツの合計（累積和）
listmiddlesum = [0]
for i in range(n):
    # ride:n個目に乗せられるlisttopのパーツの数
    ride = bisect_left(listtop, listmiddle[i])
    listmiddlesum.append(listmiddlesum[i] + ride)
for i in range(n):
    ridesum = bisect_left(listmiddle, listbottom[i])
    sum += listmiddlesum[ridesum]
print(sum)

Submission status: Accepted

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

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

jsum=0
for j in b:
    ai=bisect(a,j-1)
    ci=lenc-bisect_left(c,j+1)
#    print(j,ai,ci)
    jsum+=ai*ci

print(jsum)

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)
ans = 0

for i in range(n):
    a_M = bisect.bisect_left(a, b[i])
    c_m = bisect.bisect_right(c, b[i])
    ans += a_M * (n - c_m)
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 b:
  d = bisect.bisect_left(a, i)
  e = bisect.bisect_right(c, i)
  if d > 0 and e < len(c):
    ans += d*(n-e)
print(ans)

Submission status: Accepted

Solution code:
# abc077_c.py
'''
多分あとは、Bで重複がある場合は探索しないで個数かけていくとかにしないといけない。
'''
def is_okC(idx,key,target):
########## wirte criteria here ##########
    if target[idx] > key:
        return True
    return False

def meguru_bisectC(key,target):
    left = -1
    length = len(target)
    right = length
    while (abs(right - left) > 1):
        mid = left + (right - left) // 2
        if is_okC(mid,key,target):
            right = mid
        else:
            left = mid
    return length-right


def is_okA(idx,key,target):
########## wirte criteria here ##########
    if target[idx] >= key:
        return True
    return False

def meguru_bisectA(key,target):
    left = -1
    length = len(target)
    right = length
    while (abs(right - left) > 1):
        mid = left + (right - left) // 2
#        print("mid : " , mid)
        if is_okA(mid,key,target):
            right = mid
        else:
            left = mid
    return right

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()
a = 0
c = 0
ans = 0
#print(A)
#print(B)
#print(C)
for i in B:
    a = meguru_bisectA(i,A)
#    print(A[:a] )
#    print("i : ",i)
    c = meguru_bisectC(i,C)
#    print(C[c:])
    ans += a*c
#    print()

print(ans)

Submission status: Accepted

Solution code:
import sys
import bisect

readline = sys.stdin.readline

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 = 0
    for b in B:
        ansA = bisect.bisect_left(A, b)
        ansC = N - bisect.bisect_right(C, b)
        ans += ansA * ansC

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import bisect
n = int(input())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))
cc = list(map(int, input().split()))

aa.sort()
cc.sort()

cnt = 0
cnt = sum([bisect.bisect_left(aa,b) * (n - bisect.bisect_right(cc,b)) for b in bb])
print(cnt)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
aa = np.array(list(map(int, input().split())))
bb = list(map(int, input().split()))
cc = np.array(list(map(int, input().split())))

aa.sort()
cc.sort()

cnt = 0
for b in bb:
    na = np.searchsorted(aa,b, side='left')
    nc = n - np.searchsorted(cc,b, side='right')
    cnt += na*nc
print(cnt)

Submission status: Accepted

Solution code:
import numpy as np
import bisect
n = int(input())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))
cc = list(map(int, input().split()))

aa.sort()
cc.sort()

cnt = 0
for b in bb:
    na = bisect.bisect_left(aa,b)
    nc = n - bisect.bisect_right(cc,b)
    cnt += na*nc
print(cnt)

Submission status: Accepted

Solution code:
import bisect
N=int(input())
As=list(map(int,input().split()))
Bs=list(map(int,input().split()))
Cs=list(map(int,input().split()))
As.sort()
Cs.sort()
pyout=0

for i in range(N):
    num1=bisect.bisect_left(As,Bs[i])
    num2=N-bisect.bisect_right(Cs,Bs[i])
    pyout+=num2*num1
print(pyout)

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

alow = []
for i in range(n):
    key = B[i]
    ok = -1
    ng = n
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if A[mid] < key:
            ok = mid
        else:
            ng = mid
    alow.append(ok+1)
    
cup = []
for i in range(n):
    key = B[i]
    ok = n
    ng = -1
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if C[mid] > key:
            ok = mid
        else:
            ng = mid
    cup.append(n-ok)

ans = 0
for i in range(n):
    ans+=alow[i] * cup[i]
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect

n=int(input())
la=sorted(list(map(int,input().split())))
lb=list(map(int,input().split()))
lc=sorted(list(map(int,input().split())))
ans=0
for i in lb:
    ans+=bisect_left(la,i)*(len(lc)-bisect(lc,i)) #iの挿入点つまりiより小さい値の数×(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.sort(),c.sort()
x=len(c)
ans=0
for bi in b:
  xa=bisect.bisect_right(c,bi)
  xb=bisect.bisect_left(a,bi)
  ans+=xb*(x-xa)
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
cnt = []
for i in b:
    cnt.append(n-bisect.bisect_right(c,i))
cnt += [0]
for i in range(n-1,-1,-1):
    cnt[i] += cnt[i+1]
for i in a:
    ans += cnt[bisect.bisect_right(b,i)]
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left as bl,bisect_right as br
n=int(input())
a,b,c=[sorted(list(map(int,input().split()))) for i in range(3)]
print(sum(bl(a,v)*(n-br(c,v))for v in b))

Submission status: Accepted

Solution code:
from bisect import bisect_left as bl,bisect_right as br
n=int(input())
a,b,c=[sorted(list(map(int,input().split()))) for i in range(3)]
ans=0
for v in b:
  if v<=a[0] or v>=c[-1]:continue
  ans+=bl(a,v)*(n-br(c,v))
print(ans)

Submission status: Accepted

Solution code:
import bisect

def snuke_festival(N,arr):
    n_pair = 0
    for item in arr[1]:
        up_pair = bisect.bisect_left(arr[0],item)
        down_pair = bisect.bisect_right(arr[2],item)
        n_pair += up_pair*(N-down_pair)
    return n_pair


if __name__ == "__main__":
    N  =int(input())
    arr = []
    for i in range(3):
        arr.append(list(map(int,input().split())))
    arr_sorted = []
    for j in range(3):
        arr_sorted.append(arr[j].sort())
    print(snuke_festival(N,arr))

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())))
cnt = 0
for num in b:
    i = bisect.bisect_left(a, num)
    j = bisect.bisect_right(c, num)
    cnt += i * (n - j)
print(cnt)

Submission status: Accepted

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import permutations,combinations
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
# from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint

def myinput():
    return map(int,input().split())

def mylistinput(n):
    return [ list(myinput()) for _ in range(n) ]

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

n = int(input())
a = list(myinput())
b = list(myinput())
c = list(myinput())

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

# print(a)
# print(b)
# print(c)
# print("\n")

ls = [0]*n
for j in range(n):
    ind_a = bisect_left(a,b[j])
    ls[j] = ind_a
# print(ls)

ls_sum = [0]*n
s = 0
for i in range(n):
    s += ls[i]
    ls_sum[i] = s
# print(ls_sum)
     
count = 0
for k in range(n):
    ind_b = bisect_left(b,c[k])
    # print(k,ind_b)
    if ind_b==0:
        count += 0
    elif ind_b==n:
        count += ls_sum[-1]
    else:
        count += ls_sum[ind_b-1]
    # print(count)
print(count)

Submission status: Accepted

Solution code:
from bisect import bisect_left as bl
from bisect import bisect_right as br
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 += bl(a,b[i])*(n-br(c,b[i]))
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left as bs
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()
ab = [0]*(n+1)
for i in range(1,n+1): ab[i] += bs(a,b[i-1]) + ab[i-1]
ans = 0
for i in range(n): ans += ab[bs(b,c[i])]
print(ans)

Submission status: Accepted

Solution code:
def binSearch(array, num):
    n = len(array)
    l = -1
    r = n

    while l + 1 < r:
        m = (l + r) // 2
        if num <= array[m]:
            r = m
        else:
            l = m

    return r


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(len(b)):
    a_idx = binSearch(a, b[i])
    c_idx = binSearch(c, b[i] + 1)
    ans += a_idx * (n - c_idx)
print(ans)

Submission status: Accepted

Solution code:
def is_ok_under(array, mid, target):
    return array[mid] < target


def is_ok_over(array, mid, target):
    return array[mid] > target


# ok is -1. ng is len(array)
def bSearchUnder(array, ok, ng, target):
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if is_ok_under(array, mid, target):
            ok = mid
        else:
            ng = mid
    return ok


# ng is -1. ok is len(array)
def bSearchOver(array, ok, ng, target):
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if is_ok_over(array, mid, target):
            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()
b.sort()
c.sort()

ans = 0

for i in range(len(b)):
    add = 1
    add *= bSearchUnder(a, -1, len(a), b[i]) + 1
    add *= len(c) - bSearchOver(c, len(c), -1, b[i])
    ans += add

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
c_len = len(c)

for i in b:
    a_index = bisect.bisect_left(a , i)
    c_index = bisect.bisect_right(c , i)
    tmp = a_index * (c_len - c_index)
    ans += tmp
    
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 sys, copy, math, heapq, bisect
from itertools import accumulate
from collections import deque, defaultdict, Counter
input = sys.stdin.readline # 文字列の入力のとき'/n'まで受け取るので注意!!!
sys.setrecursionlimit(500000)
ans = 0
for i in range(N):
    a = bisect.bisect_left(A,B[i])
    c = bisect.bisect_right(C,B[i])

    ans += (N-c)*a
print(ans)

Submission status: Accepted

Solution code:
import bisect
def resolve():
    N = int(input())
    A = sorted(list(map(int, input().split())))
    B = sorted(list(map(int, input().split())))
    C = sorted(list(map(int, input().split())))
    
    cnt = 0
    for b in B:
        i = bisect.bisect_left(A, b)
        k = bisect.bisect_right(C, b)
        cnt += i * (N-k)
    print(cnt)

if '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:
import sys
import math
import queue
import copy
import bisect#2分探索

def input():
    return sys.stdin.readline()[:-1]
def inputi():
    return int(input())
def inputl():
    return list(map(int, input().split()))
def printl(li):
    print(*li, sep="\n")
def argsort(s):
    return sorted(range(len(s)), key=lambda k: s[k])

#mod = 10**9+7

N = inputi()
#N, K = inputl()
#L = [int(input()) for i in range(N)]
A = inputl()
B = inputl()
C = inputl()
#S = [inputl() for i in range(H)]
#q = queue.Queue() #q.put(i) #q.get() 
#q = queue.LifoQueue() #q.put(i) #q.get() 

#a= [[0]*3 for i in range(5)] #2次元配列はこう準備、[[0]*3]*5だとだめ
#b=copy.deepcopy(a)  #2次元配列はこうコピーする
#w.sort(key=lambda x:x[1],reverse=True)  #二個目の要素で降順並び替え

#素数リスト
# n = 100
# primes = set(range(2, n+1))
# for i in range(2, int(n**0.5+1)):
#     primes.difference_update(range(i*2, n+1, i))
# primes=list(primes)

#bisect.bisect_left(a, 4)#aはソート済みである必要あり。aの中から最も左の4位置を返す。
#bisect.insort_left(a, 4)#挿入


##TLE
# A.sort(reverse=True)
# B.sort()
# C.sort()
# tot=0
# cut=N
# for a in A:
#     cut=bisect.bisect_right(B[0:cut], a)
#     cut2=N
#     for i in reversed(range(cut,N)):
#         b=B[i]
#         cut2=bisect.bisect_right(C[0:cut2], b)
#         tot+=N-cut2

#right ans
tot=0
A.sort()
C.sort()
for b in B:
    cut1=bisect.bisect_left(A, b)
    cut2=bisect.bisect_right(C, b)
    tot+=cut1*(N-cut2)
print(tot)

Submission status: Accepted

Solution code:
import sys
#import math
#import queue
#import copy
import bisect#2分探索

def input():
    return sys.stdin.readline()[:-1]
def inputi():
    return int(input())
def inputl():
    return list(map(int, input().split()))
def printl(li):
    print(*li, sep="\n")
def argsort(s):
    return sorted(range(len(s)), key=lambda k: s[k])

#mod = 10**9+7

N = inputi()
#N, K = inputl()
#L = [int(input()) for i in range(N)]
A = inputl()
B = inputl()
C = inputl()
#S = [inputl() for i in range(H)]
#q = queue.Queue() #q.put(i) #q.get() 
#q = queue.LifoQueue() #q.put(i) #q.get() 

#a= [[0]*3 for i in range(5)] #2次元配列はこう準備、[[0]*3]*5だとだめ
#b=copy.deepcopy(a)  #2次元配列はこうコピーする
#w.sort(key=lambda x:x[1],reverse=True)  #二個目の要素で降順並び替え

#素数リスト
# n = 100
# primes = set(range(2, n+1))
# for i in range(2, int(n**0.5+1)):
#     primes.difference_update(range(i*2, n+1, i))
# primes=list(primes)

#bisect.bisect_left(a, 4)#aはソート済みである必要あり。aの中から最も左の4位置を返す。
#bisect.insort_left(a, 4)#挿入


##TLE
# A.sort(reverse=True)
# B.sort()
# C.sort()
# tot=0
# cut=N
# for a in A:
#     cut=bisect.bisect_right(B[0:cut], a)
#     cut2=N
#     for i in reversed(range(cut,N)):
#         b=B[i]
#         cut2=bisect.bisect_right(C[0:cut2], b)
#         tot+=N-cut2

#right ans
tot=0
A.sort()
C.sort()
for b in B:
    cut1=bisect.bisect_left(A, b)
    cut2=bisect.bisect_right(C, b)
    tot+=cut1*(N-cut2)
print(tot)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(input())
    a = list(map(int, readline().split()))
    b = list(map(int, readline().split()))
    c = list(map(int, readline().split()))
    a.sort()
    b.sort()
    c.sort()

    count_ab = [0] * n
    count_bc = [0] * n

    import bisect

    for bi, middle in enumerate(b):
        count_ab[bi] = bisect.bisect_left(a, middle)

    for ci, bottom in enumerate(c):
        count_bc[ci] = bisect.bisect_left(b, bottom)

    from itertools import accumulate

    cum_ab = [0] + list(accumulate(count_ab))
    ans = 0

    for i in range(n):
        num = count_bc[i]
        ans += cum_ab[num]

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect
import io
import math
import string

data = 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()
counter = 0

for b in B:
    a_index = bisect.bisect_left(A,b)
    c_index = bisect.bisect_right(C,b)
    counter += a_index*(len(C)-c_index)

print(counter)

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

Submission status: Accepted

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

A.sort()
C.sort()

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

Submission status: Accepted

Solution code:
import copy
import bisect
N=int(input())
A=list(map(int,input().split()))
A.sort()
B=list(map(int,input().split()))
B.sort()
C=list(map(int,input().split()))
C.sort()
l1n=[-1 for _ in range(N)]
l2n=[-1 for _ in range(N)]
idab=0
idbc=0
buf1=[]
flag1=0
buf2=[]
flag2=0
i=0
#print(A)
#print(B)
#print(C)
exc=0
mulsum=[0]*N
"""
while i<N:
    if (idab!=N)and(A[i]>=B[idab]):
        flag1=1
        idab=idab+1
    elif flag1==1:
        l1n[i]=idab-1
        i=i+1
    else:
        i=i+1
    #print(l1n)
    #input()
#print(l1n)
i=0
while i<N:
    if (idbc!=N)and(B[i]>=C[idbc]):
        flag2=1
        idbc=idbc+1
    elif flag2==1:
        l2n[i]=idbc-1
        i=i+1
    else:
        i=i+1
    #print(l2n)
    #input()
#print(l1n,l2n)
#print(sum(l1n)+N,sum(l2n)+N)
mulsum[0]=l2n[0]+1
for i in range(N-1):
    mulsum[i+1]=mulsum[i]+(l2n[i+1]+1)
#print(mulsum)
for i in range(N):
    if l1n[i]!=-1:
        exc=exc+mulsum[l1n[i]]
        #print(exc)
print(pow(N,3)-(sum(l1n)+N)*N-(sum(l2n)+N)*N+exc)
"""
aok=0
cok=0
#Bの要素を固定してA[i]<B[j]かつB[j]<C[k]なるi,j,kの組みを探す
#print(A,B,C)
cnt=0
for i in range(N):
    piv=B[i]
    aok=bisect.bisect_right(A,piv-1)
    cok=bisect.bisect_left(C,piv+1)
    cnt=cnt+aok*(N-cok)
    #print(aok,A,cok,C)
print(cnt)

Submission status: Accepted

Solution code:
import copy
N=int(input())
A=list(map(int,input().split()))
A.sort()
B=list(map(int,input().split()))
B.sort()
C=list(map(int,input().split()))
C.sort()
l1n=[-1 for _ in range(N)]
l2n=[-1 for _ in range(N)]
idab=0
idbc=0
buf1=[]
flag1=0
buf2=[]
flag2=0
i=0
#print(A)
#print(B)
#print(C)
exc=0
mulsum=[0]*N
while i<N:
    if (idab!=N)and(A[i]>=B[idab]):
        flag1=1
        idab=idab+1
    elif flag1==1:
        l1n[i]=idab-1
        i=i+1
    else:
        i=i+1
    #print(l1n)
    #input()
#print(l1n)
i=0
while i<N:
    if (idbc!=N)and(B[i]>=C[idbc]):
        flag2=1
        idbc=idbc+1
    elif flag2==1:
        l2n[i]=idbc-1
        i=i+1
    else:
        i=i+1
    #print(l2n)
    #input()
#print(l1n,l2n)
#print(sum(l1n)+N,sum(l2n)+N)
mulsum[0]=l2n[0]+1
for i in range(N-1):
    mulsum[i+1]=mulsum[i]+(l2n[i+1]+1)
#print(mulsum)
for i in range(N):
    if l1n[i]!=-1:
        exc=exc+mulsum[l1n[i]]
        #print(exc)
print(pow(N,3)-(sum(l1n)+N)*N-(sum(l2n)+N)*N+exc)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
ABC = [sorted(list(map(int, input().split()))) for _ in range(3)]
ans = 0
for b in ABC[1]:
    ans += bisect.bisect_left(ABC[0],b)*(N-bisect.bisect_right(ABC[2],b))
print(ans)

Submission status: Accepted

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

def isOK_over(array, index, key):
    if array[index] > key:
        return True
    else:
        return False

def binary_serch_under(array, key):
    ng = -1
    ok = len(array)

    while (abs(ok - ng) > 1):
        mid = (ok + ng)//2

        if isOK_under(array, mid, key):
            ok = mid
        else:
            ng = mid
    return ok

def binary_serch_over(array, key):
    ng = -1
    ok = len(array)
    while (abs(ok - ng) > 1):
        mid = (ok + ng)//2
        if isOK_over(array, mid, key):
            ok = mid
        else:
            ng = mid
    return ok

n = int(input())
array1 = list(map(int,input().split()))
array2 = list(map(int,input().split()))
array3 = list(map(int,input().split()))

array1 = sorted(array1)
array2 = sorted(array2)
array3 = sorted(array3)

# a = binary_serch(array2, 5)
count=0
# for array1_index in range(n):
#     start_array2_index = binary_serch(array2, array1[array1_index])
#     # print(array1[array1_index])
#     for cor_array2_index in range(start_array2_index, n):
#         # print(array2[cor_array2_index])
#         start_array3_index = binary_serch(array3, array2[cor_array2_index])
#         # for k in range(start_array3_index,n):
#         #     print(array3)
#         count+=abs(start_array3_index - n)
for index_array2 in range(n):
    a = binary_serch_under(array1, array2[index_array2])
    c = binary_serch_over(array3, array2[index_array2])
    count += a*abs(c-n)
print(count)

Submission status: Accepted

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
import sys
inf = 10**9+7
mod = 10**9+7
import bisect
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
b.sort()
c.sort()
b_len = len(b)
c_len = len(c)
count = 0
b_list = [0 for i in range(b_len)]

for i,num in enumerate(b):
    ind_c = bisect.bisect_left(c, num)
    if c_len == ind_c:
        continue
    else:
        if c[ind_c] == num:
            ind_c = bisect.bisect_left(c, num+1)
            if c_len == ind_c:
                continue
            else:
                b_list[i] = c_len - ind_c
        else:
            b_list[i] = c_len-ind_c

# 累積和
a2 = [0]
for i in b_list:a2.append(a2[-1]+i)
def SumArea(l, r):
    return a2[r]-a2[l]


for num in a:
    ind_b = bisect.bisect_left(b, num)
    if b_len == ind_b:
        continue
    else:
        if b[ind_b] == num:
            ind_b = bisect.bisect_left(b, num+1)
            if b_len == ind_b:
                continue
            else:
                count += SumArea(ind_b, b_len)
        else:
            count += SumArea(ind_b, b_len)
print(count)

Submission status: Accepted

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

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:
n=int(input())
a=sorted(list(map(int,input().split())))
b=sorted(list(map(int,input().split())))
c=sorted(list(map(int,input().split())))

def binarySearchA(i,lst):
	#[lb,ub)で持って、条件式を実現する最大のindexを求める
	#indexが小さいほど実現しやすいとき。
	lb=0
	ub=n
	if lst[0]>=i:
		return -1
	while ub-lb>1:
		mid=(lb+ub)//2
		if lst[mid]<i:
			lb=mid
		else:
			ub=mid
	return lb

def binarySearchB(i,lst):
	#(lb,ub]で持って、条件式を実現する最小のindexを求める
	lb=-1
	ub=n-1
	if lst[ub]<=i:
		return n
	while ub-lb>1:
		mid=(lb+ub)//2
		if lst[mid]>i:
			ub=mid
		else:
			lb=mid
	return ub

ans=0
for i in b:
	a_max=binarySearchA(i,a)
	c_min=binarySearchB(i,c)
	ans+=(a_max+1)*(n-c_min)
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
import sys
from collections import Counter
from itertools import permutations, combinations
from fractions import gcd
from math import ceil, floor
import bisect
sys.setrecursionlimit(10 ** 6)

def input():
    return sys.stdin.readline()[:-1]

def main():
    n = int(input())
    la = list(map(int, input().split()))
    lb = list(map(int, input().split()))
    lc = list(map(int, input().split()))
    
    la.sort()
    lb.sort()
    lc.sort()

    ans = 0
    for b in lb:
        idxa = bisect.bisect_left(la, b)
        idxc = bisect.bisect_right(lc, b)
        if idxa > 0 and idxc < n:
            ans += idxa * (n - idxc)
    print(ans) 

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right

n = int(input())
a = sorted(map(int, input().split()))
b = sorted(map(int, input().split()))
c = sorted(map(int, input().split()))
ans = 0
def bin_search(x, n, flag):
    if flag == True:
        return bisect_left(x, n)
    else:
        return bisect_right(x, n)



for i in range(n):
    ans += bin_search(a, b[i], True) * (n - bin_search(c, b[i], False))

print(ans)

Submission status: Accepted

Solution code:
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 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=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 B:
    ans +=bisect_left(A,i)*(n-bisect_right(C,i))
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 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 = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort()
B.sort()
C.sort()

mid_list = [0]*N
for i in range(N):
    index = bisect.bisect_left(A,B[i])
    if i == 0:
        mid_list[i] = index
    else:
        mid_list[i] = mid_list[i-1] + index

ans = 0
for i in range(N):
    index = bisect.bisect_left(B,C[i])
    if index == 0:
        continue
    ans += mid_list[index-1]

print(ans)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
ABC = [sorted(list(map(int,input().split()))) for i in range(3)]

ans = 0
for b in ABC[1]:
  ans += bisect.bisect_left(ABC[0],b)*(N-bisect.bisect_right(ABC[2],b))
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())))


"""
minnum以上で最小の数のindexを返す。無いときはlen(List)を返す
計算量log(len(List))

def bts(List,minnum,under=0):
    if List[0]>=minnum:
        return under
    if List[-1]<minnum:
        return len(List)+under
    i = int(len(List)/2)
    if List[i]>=minnum and List[i-1]<minnum:
        return i + under
    if List[i]<minnum:
        return bts(List[i+1:],minnum,under+i+1)
    return bts(List[:i],minnum,under)

"""

def binary_search(list, item):
    if list[0]>=item:
        return 0
    if list[-1]<item:
        return len(list)

    low = 0
    high = len(list) - 1

    while low <= high:
        mid = (low + high) //2
        guess = list[mid]
        if guess >= item and list[mid-1] < item:
            return mid
        if guess >= item:
            high = mid -1
        else:
            low = mid + 1

    return None

res = 0
for i in range(N):
    b = B[i]
    a = binary_search(A,b)
    c = N - binary_search(C,b+1)
    res += a*c

print(res)

Submission status: Accepted

Solution code:
import bisect,itertools
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort()
b.sort()
c.sort()
ba = []
for i in b:
    ba.append(bisect.bisect_left(a, i))
accba = list(itertools.accumulate(ba))#累積和
ans = 0
for i in c:
    tmp = bisect.bisect_left(b,i)
    if tmp==0: continue
    ans += accba[tmp-1]

print(ans)