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 bisect
from itertools import accumulate
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
b_to_c = [0]*n
for i in range(n):
  b_to_c[i] = n - bisect.bisect_right(C, B[i])
Q = list(accumulate(b_to_c[::-1]))[::-1]
  
for i in A:
  t = bisect.bisect_right(B, i)
  if t < n:
    ans += Q[t]
print(ans)

Submission status: Accepted

Solution code:
import bisect
n = int(input())

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

ans = 0
for i in b:
    x = bisect.bisect(c,i)
    y = bisect.bisect_left(a,i)
    ans += (n-x) * y
    
print(ans)

Submission status: Accepted

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

answer = 0
for b_i in range(len(B)):
    value = B[b_i]
    """Aについて"""
    right = len(A)
    left = -1
    while right - left > 1:
        center = (right + left) // 2
        if A[center] >= value:
            right = center
        else:
            left = center
    a_num = left + 1
    
    """Cについて"""
    right = len(C)
    left = -1
    while right - left > 1:
        center = (right + left) // 2
        if C[center] <= value:
            left = center
        else:
            right = center
    c_num = len(C) - right

    answer += a_num*c_num

print(answer)

Submission status: Accepted

Solution code:
import bisect

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

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

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.buffer.readline


def binary_search(A, n, p):
    if p == "+":
        n += 0.5
    else:
        n -= 0.5

    left = 0
    right = len(A) - 1

    if n < A[0]:
        return 0
    else:
        while left <= right:
            mid = (left + right) // 2
            if A[mid] < n:
                left = mid + 1
            else:
                right = mid - 1

        return left


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

    A, B, C = sorted(A), sorted(B), sorted(C)

    cnt = 0
    for b in B:
        ab = binary_search(A, b, "-")
        bc = N - binary_search(C, b, "+")
        cnt += ab * bc

    print(cnt)


main()

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.buffer.readline


def binary_search(A, n, p):
    if p == "+":
        n += 0.5
    else:
        n -= 0.5

    left = 0
    right = len(A) - 1

    if n < A[0]:
        return 0
    else:
        while left <= right:
            mid = (left + right) // 2
            if A[mid] < n:
                left = mid + 1
            else:
                right = mid - 1

        return left


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

    A, B, C = sorted(A), sorted(B), sorted(C)

    cnt = 0
    for i, b in enumerate(B):
        ab = binary_search(A, b, "-")
        bc = N - binary_search(C, b, "+")
        cnt += ab * bc

    print(cnt)


main()

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import floor, ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy,copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
import bisect

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

def main():
	n = INT()
	a = sorted(LIST())
	b = sorted(LIST())
	c = sorted(LIST())

	ans = 0

	for i in range(n):
		j = bisect.bisect_left(a, b[i])
		k = bisect.bisect_right(c, b[i])
		ans += j * (n-k)
	print(ans)

if  __name__=='__main__':
	main()

Submission status: Accepted

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

A.sort()
B.sort()
C.sort()
ans = 0
# ary[i]:B[i]より小さいAの要素の数
ary = [0]*len(B)

for i, b in enumerate(B):
    if A[0] >= b:
        continue
    left = 0
    right = len(A)
    while right-left > 1:
        middle = (left+right)//2
        if A[middle] >= b:
            right = middle
        else:
            left = middle
    ary[i] = left+1
    if i != 0:
        ary[i] += ary[i-1]
for i, c in enumerate(C):
    if B[0] >= c:
        continue
    left = 0
    right = len(B)
    while right-left > 1:
        middle = (left+right)//2
        if B[middle] >= c:
            right = middle
        else:
            left = middle
    ans += ary[left]

print(ans)

Submission status: Accepted

Solution code:
import bisect
n=int(input())
a,b,c=[sorted(map(int,input().split())) for i in range(3)]
ans=0
for i in b:
      x=bisect.bisect_left(a,i)
      y=n-bisect.bisect_right(c,i)
      ans+=x*y
print(ans)

Submission status: Accepted

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

ans=0
for i in b:
      idx1=bisect.bisect_left(a,i)
      idx2=n-bisect.bisect_right(c,i)
      ans+=idx1*idx2
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left as bl
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    n=int(input())
    aa=LI()
    bb = LI()
    cc = LI()
    aa.sort()
    bb.sort()
    #print(aa)
    #print(bb)
    ans=0
    bi_to_a_cnt=[0]
    for b in bb:
        bi_to_a_cnt.append(bl(aa,b)+bi_to_a_cnt[-1])
    for c in cc:
        bi=bl(bb,c)
        ans+=bi_to_a_cnt[bi]
    print(ans)

main()

Submission status: Accepted

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

    def is_ok1(mid, S, K):
        if K <= S[mid]:
            return True
        else:
            return False

    def is_ok2(mid, S, K):
        if K < S[mid]:
            return True
        else:
            return False

    def binary_search_meguru(S, K, is_ok):
        left = -1
        right = N
        while right - left > 1:
            mid = left + ((right - left) // 2)
            if is_ok(mid, S, K):
                right = mid
            else:
                left = mid
        return right

    ans = 0
    for b in B:
        a = binary_search_meguru(A, b, is_ok1)
        c = binary_search_meguru(C, b, is_ok2)
        ans += a * (N-c)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ind0=0
LIST=[0]*N
for i in range(N):
    while ind0<N and B[i]<C[ind0]:
        ind0+=1
    LIST[i]=ind0
    
ind0=0
ALIST=[0]*N
for i in range(N):
    while ind0<N and A[i]<B[ind0]:
        ind0+=1
    ALIST[i]=ind0

from itertools import accumulate
S=[0]+list(accumulate(LIST))

ANS=0
for i in range(N):
    ANS+=S[ALIST[i]]
    
print(ANS)

Submission status: Accepted

Solution code:
import bisect

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A= [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]
C = [int(_) for _ in input().split()]

A.sort()
B.sort()
C.sort(reverse=True)

ans = 0
ai = 0
ci = n-1

for b in B:
  while ai <n and  A[ai] <b:
    ai +=1
  while ci >=0 and C[ci] <=b:
    ci -= 1
  ans += ai *(ci +1)
print(ans)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
A_arr = list(map(int,input().split()))
A_arr.sort()
B_arr = list(map(int,input().split()))
B_arr.sort()
C_arr = list(map(int,input().split()))
ans = 0
check_array = [0] * N
for i in range(N):
    check_array[i] = check_array[i-1]+bisect.bisect_left(A_arr,B_arr[i])
for i in C_arr:
    B_index = bisect.bisect_left(B_arr,i)
    if B_index == 0:
        continue
    ans += check_array[B_index-1]
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()))
ans = 0
A.sort()
C.sort()
for i in B:
    x = bisect.bisect_left(A,i)
    y = N-bisect.bisect_right(C,i)
    ans += x*y
print(ans)

Submission status: Accepted

Solution code:
import bisect

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

A_B = []
B_C = []
total = 0

for b in B:
    k = bisect.bisect_left(A,b)
    l = bisect.bisect_right(C,b)
    total += k * (N-l)

print(total)

Submission status: Accepted

Solution code:
import bisect

N=int(input())
As=sorted(list(map(int,input().split())))
Bs=sorted(list(map(int,input().split()))) #Bsはソートしなくてもよい
Cs=sorted(list(map(int,input().split())))

Ans=0

for B in Bs:
    a=bisect.bisect_left(As, B)
    c=len(Cs)-bisect.bisect_right(Cs,B)
    Ans+=a*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()

ans=0
for i in b:
  x=bisect.bisect_left(a,i)
  y=n-bisect.bisect_right(c,i)
  ans+=x*y
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()))

bika=[0]
cika=[0]
j=0
k=0

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

for i in range(n):
  if j<n:
    while a[j]<b[i]:
      bika[i]+=1
      j+=1
      if j>=n:
        break
  if i!=n-1:
    bika.append(bika[i])

for i in range(n):
  if k<n:
    while b[k]<c[i]:
      cika[i]+=bika[k]
      k+=1
      if k>=n:
        break
  if i!=n-1:
    cika.append(cika[i])
print(sum(cika))

Submission status: Accepted

Solution code:
from sys import stdin
import math
import bisect
import heapq
import numpy as np
from math import factorial

n = int(stdin.readline().rstrip())
a = list(map(int,stdin.readline().rstrip().split()))
b = list(map(int,stdin.readline().rstrip().split()))
c = list(map(int,stdin.readline().rstrip().split()))

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

total = 0
for i in range(n):
    l = bisect.bisect(a,b[i]-1)
    r = n -bisect.bisect_left(c,b[i]+1)
    total += l*r

print(total)

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):
    low = bisect.bisect_left(a,b[i])
    upp = bisect.bisect_right(c,b[i])
    ans += low*(n-upp)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
As, Bs, Cs = [
  sorted(map(int, input().split()))
  for _ in range(3)
]

from bisect import *

def sums():
  for b in Bs:
  	yield bisect_left(As, b) * (N - bisect_right(Cs, b))

print(sum(sums()))

Submission status: Accepted

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

bi = [0 for i in range(N)]
for i in range(N):
    b = B[i]
    c = bisect.bisect_right(C,b)
    bi[i]+=(N-c)

S = sum(bi)
rui = [0]
for bii in bi:
    rui.append(rui[-1]+bii)

rui = rui[::-1]
ans = 0
for i in range(N):
    a = A[i]
    ind = bisect.bisect_right(B,a)
    #print(ind,rui[N-ind])
    ans+=(S-rui[N-ind])

    
    
print(ans)
#print(rui)

Submission status: Accepted

Solution code:
def examC():
    N = I()
    A = LI(); B = LI(); C = LI()
    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)


import sys,copy,bisect,itertools
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examC()

Submission status: Accepted

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

dp_b = [0] * n
j = 0
for i in range(n):
    while j < n:
        if B[i] >= C[j]:
            dp_b[i] = j
            break
        j += 1
    if j == n:
        dp_b[i] = n

for i in range(1, n):
    dp_b[i] = dp_b[i] + dp_b[i - 1]

ans = 0
j = 0
for i in range(n):
    while j < n:
        if A[i] >= B[j]:
            if j == 0:
                break
            else:
                ans += dp_b[j - 1]
                break
        j += 1
    if j == n:
        ans += dp_b[n - 1]

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left as bl, bisect_right as br
N = int(input())
A = sorted([int(a) for a in input().split()])
B = sorted([int(a) for a in input().split()])
C = sorted([int(a) for a in input().split()])

ans = 0
for i in range(N):
    ans += bl(A, B[i]) * (N - br(C, B[i]))
print(ans)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
Alist = sorted(list(map(int, input().split())))
Blist = sorted(list(map(int, input().split())))
Clist = sorted(list(map(int, input().split())))

num = 0

for B in Blist:
    small = bisect.bisect_left(Alist, B)
    left = N - bisect.bisect_right(Clist, B)
    num += small * left
print(num)

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()))
ctr= [0 for _ in range(N)]

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

ans = 0

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    A,B,C = [sorted(list(map(int,input().split()))) for i in range(3)]
    ans = 0
    for b in B:
        ans += low(A,b)*(n-high(C,b))
    print(ans)


def low(A,key):
    left,right = -1,len(A)
    while right - left > 1:
        mid = (left+right)//2
        if A[mid] >= key:
            right = mid
        else:
            left = mid
    return left+1


def high(C,key):
    left,right = -1,len(C)
    while right - left > 1:
        mid = (left+right)//2
        if C[mid] <= key:
            left = mid
        else:
            right = mid
    return right


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import bisect

n = int(input())
al, bl, cl = list(sorted(map(int, input().split())) for i in range(3))
ans = 0

for key in bl:
    top = bisect.bisect_left(al, key)
    btm = len(cl) - bisect.bisect_right(cl, key)
    ans += top * btm

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
n = int(input())
al = sorted(list(int(i) for i in input().split()))
bl = sorted(list(int(i) for i in input().split()))
cl = sorted(list(int(i) for i in input().split()))
ans = 0

def low_left(a, key):
    left = -1
    right = len(a)
    while right - left > 1:
        center = (left + right) // 2
        if a[center] >= key:
            right = center
        else:
            left = center
    return left+1 
    
def high_right(a, key):
    left = -1
    right = len(a)
    while right - left > 1:
        center = (left + right) // 2
        if a[center] <= key:
            left = center
        else:
            right = center
    return n-right
    
for i in bl:
    ans += low_left(al, i) * high_right(cl, i)

print(ans)

Submission status: Accepted

Solution code:
import bisect
n=int(input())
la=sorted(map(int,input().split()))
lb=sorted(map(int,input().split()))
lc=sorted(map(int,input().split()))
ans=0
for i in range(n):
  index1=bisect.bisect_left(la,lb[i])
  index2=bisect.bisect_right(lc,lb[i])
  ans+=index1*(n-index2)
print(ans)

Submission status: Accepted

Solution code:
import bisect
n=int(input())
la=sorted(map(int,input().split()))
lb=sorted(map(int,input().split()))
lc=sorted(map(int,input().split()))
countb=[]
count1=0
for i in range(n):
  index=bisect.bisect_left(la,lb[i])
  countb.append(index)
countb2=[]
sum=0
for i in range(n):
  sum+=countb[i]
  countb2.append(sum)
ans=0
for i in range(n):
  index=bisect.bisect_left(lb,lc[i])
  if index==0:
    pass
  else:
    ans+=countb2[index-1]
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()
b_i = [0]*n
c_i = [0]*n
for i in range(n):
    b1 = bisect.bisect_left(a,b[i])
    if i==0:
        b_i[i]=b1
    else:
        b_i[i] = b_i[i-1]+b1
    c1 = bisect.bisect_left(b,c[i])
    c_i[i]= c1

ans = 0
for i in range(n):
    if c_i[i]<1:
        continue
    else:
        ans = ans + b_i[c_i[i]-1]
print(ans)

Submission status: Accepted

Solution code:
#77c
from bisect import bisect_left, bisect_right

N = int(input())

A_list = list((int(e) for e in input().split()))
B_list = list((int(e) for e in input().split()))
C_list = list((int(e) for e in input().split()))

A_list.sort(reverse=False)
B_list.sort(reverse=False)
C_list.sort(reverse=False)

ans = 0

#上から考える場合
#for i in range(N):
#    #print(A_list[i])
#    index_right = bisect_right(B_list, A_list[i])
#    for j in range(index_right,N):
#        index_right = bisect_right(C_list, B_list[j])
#        #print(":",B_list[j],N - index_right)
#        ans += N - index_right

#中段から考える場合
for i in range(N):
    middle_size = B_list[i]
    top_count = bisect_left(A_list, middle_size)
    lower_count = N - bisect_right(C_list, middle_size)
    
    #print(B_list[i],top_count,lower_count)
    
    ans += top_count * lower_count

print(ans)

Submission status: Accepted

Solution code:
import bisect as bi
 
n = int(input())
lia = list(map(int,input().split()))
lib = list(map(int,input().split()))
lic = list(map(int,input().split()))
lia = sorted(lia)
lib = sorted(lib)
lic = sorted(lic)
 
ans = 0
for i in range(n):
  tmpa = bi.bisect_left(lia,lib[i])
  tmpc = n - bi.bisect_right(lic,lib[i])
  ans += tmpa*tmpc
print(ans)

Submission status: Accepted

Solution code:
import bisect

n = int(input())
a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())))
c = sorted(list(map(int, input().split())))
cnt = 0
info1 = []


for i in range(n):
    b_p = bisect.bisect_left(a, b[i])
    info1.append(b_p) # bがそれぞれ何種類のaを載せれるか

info2 = [info1[0]]
for l in range(1, n):
    info2.append(info2[l-1] + info1[l])

for k in range(n):
    a_p = bisect.bisect_left(b, c[k])
    if a_p != 0:
        cnt += info2[a_p - 1]  # cがそれぞれ何種類のbを載せれるか
print(cnt)

Submission status: Accepted

Solution code:
import bisect
point=0
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()
for i in range(N):
  m=bisect.bisect_left(A,B[i])
  n=bisect.bisect_right(C,B[i])
  point+=m*(N-n)
print(point)

Submission status: Accepted

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

def BinaryIndexSearch_floor(p, SL):
	"""
	search the minimum number among the larger numbers than the ginven nymber "n". 
	return the index of it under the list sorted.
	"""
	#SL = sorted(L)
	l = 0
	r = len(SL) - 1

	while True:
		if l == r:
			if SL[l] > p:
				return l
			else:
				return -1
		elif r-l == 1:
			if SL[l] > p:
				return l
			elif SL[r] > p:
				return r
			else:
				return -1

		center = l + ((r-l) // 2)

		if SL[center] <= p:
			l = center + 1
			continue

		else:
			r = center
			continue

def BinaryIndexSearch_ceil(p, SL):
	#SL = sorted(L)
	l = 0
	r = len(SL) - 1

	while True:
		if l == r:
			if SL[l] < p:
				return l
			else:
				return -1
		elif r-l == 1:
			if SL[r] < p:
				return r
			elif SL[l] < p:
				return l
			else:
				return -1
		center = l + ((r-l) // 2)

		if SL[center] < p:
			l = center 
			continue

		else:
			r = center - 1
			continue

min_A = A[0]
max_C = C[-1]
ans = 0
for b in B:
	if not (min_A < b < max_C):
		continue

	a = BinaryIndexSearch_ceil(b,A)
	c = BinaryIndexSearch_floor(b,C)

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

print(ans)

Submission status: Accepted

Solution code:
import bisect

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

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

Submission status: Accepted

Solution code:
n=int(input())
a=sorted(list(map(int,input().split())))
b=list(map(int,input().split()))
c=sorted(list(map(int,input().split())))
import bisect as bi
t=0
for i in b:
  x=n-bi.bisect_right(c,i)
  y=bi.bisect_left(a,i)
  t+=x*y
print(t)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
C = [int(c) for c in input().split()]

A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

L = [0]*N
j = 0
for i in range(N):
    while j < N and C[j] > B[i]:
        j += 1
    L[i] = j
    
for i in range(N-1):
    L[i+1] += L[i]

ans = 0
j = 0
for i in range(N):
    while j < N and B[j] > A[i]:
        j += 1
    if j > 0:
        ans += L[j-1]
    
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 bi in B:
    #biより小さいものをAから絞り込む
    ai = bisect.bisect_left(A,bi)
    #biより大きいものをCから絞り込む
    ci = len(C) - bisect.bisect_right(C,bi)

    ans += ai * ci

print(ans)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
a,b,c = [sorted(map(int,input().split())) for _ in "abc"]
print(sum([bisect.bisect_left(a,x)*(n-bisect.bisect_right(c,x)) for x in b]))

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import bisect as bi

n = int(input())
lia = list(map(int,input().split()))
lib = list(map(int,input().split()))
lic = list(map(int,input().split()))
lia = sorted(lia)
lib = sorted(lib)
lic = sorted(lic)

ans = 0
for i in range(n):
  tmpa = bi.bisect_left(lia,lib[i])
  tmpc = n - bi.bisect_right(lic,lib[i])
  ans += tmpa*tmpc
print(ans)

Submission status: Accepted

Solution code:

import sys
# import collections
# import math
# import string
import bisect

def main():
    n, *parts = (int(x) for x in sys.stdin.read().split())
    uppers, middles, lowers = (parts[n*i:n*(i+1)] for i in range(3))
    uppers.sort()
    lowers.sort()

    count = 0
    for j in middles:
        i = bisect.bisect_left(uppers, j)
        k = len(lowers) - bisect.bisect_right(lowers, j)
        count += i * k
        
    print(count)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import bisect
input = sys.stdin.readline

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

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

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

BC = [0] * n  # BC[i] = B[i]以上の全てのBを使って作れるBCの組み合わせ
pairs = 0
for i in range(n-1, -1, -1):
    b = B[i]
    idx = bisect.bisect_right(C, b)
    pairs += n - idx
    BC[i] = pairs

ans = 0
for a in A:
    idx = bisect.bisect_right(B, a)
    if idx == n:
        continue
    ans += BC[idx]

print(ans)

Submission status: Accepted

Solution code:
#ABC077 C - Snuke Festival
from bisect import bisect_left, bisect_right
si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

N = ni()
A = nl()
B = nl()
C = nl()

A.sort()
C.sort()

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
def nibu_A(list,item):
    high=len(list)
    low=-1
    while abs(high-low) > 1:
        mid=(high+low)//2

        if list[mid] < item:
            low= mid
        else:
            high=mid
    return (low+1)
def nibu_c(list,item):
    high= len(list)
    low=-1
    while abs(high-low)>1:
        mid=(high+low)//2
        if list[mid]>item:
             high = mid
        else:
            low=mid
    return len(list)-high


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

ans=0
for i in range(n):
    data=B[i]
    ans+=(nibu_A(A,data)*nibu_c(C,data))
print(ans)

Submission status: Accepted

Solution code:
N=map(int,input().split())
A=sorted(list(map(int,input().split())))
B=list(map(int,input().split()))
C=sorted(list(map(int,input().split())))
 
def countA(target,L):
  ok=-1
  ng=len(L)
  while abs(ok-ng)>1:
    mid=(ok+ng)//2
    if L[mid]<target:
      ok=mid
    else:
      ng=mid
  return (ok+1)
 
def countC(target,L):
  ok=len(L)
  ng=-1
  while abs(ok-ng)>1:
    mid=(ok+ng)//2
    if L[mid]>target:
      ok=mid
    else:
      ng=mid
  return len(L)-ok
 
ans=0
for i in range(len(B)):
  data=B[i]
  ans+=(countA(data,A)*countC(data,C))
  
print(ans)

Submission status: Accepted

Solution code:
def low_left(ls,key):
    left,right = -1,len(ls)
    while right - left > 1:
        mid = (right+left)//2
        if ls[mid] >= key:
            right = mid
        else:
            left = mid
    return left+1


def high_right(ls,key):
    left,right = -1,len(ls)
    while right - left > 1:
        mid = (right+left)//2
        if ls[mid]<=key:
            left = mid
        else:
            right = mid
    return right


def test():
    n = int(input())
    A,B,C = [list(sorted((map(int,input().split())))) for i in range(3)]
    ans = 0

    for b in B:
        ans += (n-high_right(C,b))*low_left(A,b)
    print(ans)


if __name__ == "__main__":
    test()

Submission status: Accepted

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

Submission status: Accepted

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

def countA(target,L):
  ok=-1
  ng=len(L)
  while abs(ok-ng)>1:
    mid=(ok+ng)//2
    if L[mid]<target:
      ok=mid
    else:
      ng=mid
  return (ok+1)

def countC(target,L):
  ok=len(L)
  ng=-1
  while abs(ok-ng)>1:
    mid=(ok+ng)//2
    if L[mid]>target:
      ok=mid
    else:
      ng=mid
  return len(L)-ok

ans=0
for i in range(len(B)):
  data=B[i]
  ans+=(countA(data,A)*countC(data,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()
b.sort()
c.sort()

count = 0

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

print(count)

Submission status: Accepted

Solution code:
import sys
import bisect
n = int(input())
a_ls = [int(i) for i in sys.stdin.readline().split()]
b_ls = [int(i) for i in sys.stdin.readline().split()]
c_ls = [int(i) for i in sys.stdin.readline().split()]
a_ls.sort()
b_ls.sort()
c_ls.sort()
comb_a = []
comb_b = []
len_b = len(b_ls)
len_c = len(c_ls)
for a in a_ls:
    ind = len_b - bisect.bisect_right(b_ls, a)
    comb_a.append(ind)

prod_b = []
res = 0
for b in b_ls[::-1]:
    ind = len_c - bisect.bisect_right(c_ls, b)
    comb_b.append(ind)
    res += ind
    prod_b.append(res)
comb_b = comb_b[::-1]
_sum = 0
for a in comb_a:
    if a <= 0:
        continue
    _sum += prod_b[a-1]
print(_sum)