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:
#dpでできないかな？
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n=I()
A=sorted(LI())
B=sorted(LI())
C=sorted(LI())
ans=0
for i in range(n):
    ans+=bisect_left(A,B[i])*(n-bisect_right(C,B[i]))
    #print(bisect_left(A,B[i]))
    #print(n-bisect_right(C,B[i]))
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left
from bisect import 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
y_low = {}
i = 0
for y in B:
    x = bisect_left(A, y)
    z = bisect_right(C, y)
    ans += x * (N-z)
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun May 10 19:10:47 2020

@author: Kanaru Sato
"""
import bisect

n = int(input())
_1 = list(map(int,input().split()))
_2 = list(map(int,input().split()))
_3 = list(map(int,input().split()))

a = sorted(_1)
b = sorted(_2)
c = sorted(_3)

count = 0
for i in range(n):
    smallernums = bisect.bisect_left(a,b[i])
    biggernums = len(c)-bisect.bisect_right(c,b[i])
    count += smallernums*biggernums

print(count)

Submission status: Accepted

Solution code:
import bisect
import itertools
def find_ge(a, x):
    i = bisect.bisect_right(a,x)
    return i

def solve(n, a, b, c):

    tmp = len(c)
    b_count =list(itertools.accumulate([tmp - find_ge(c, x) for x in b]))
    ans = 0
    for i in a:
        min_index = find_ge(b, i)
        
        ans += b_count[-1] -(0 if min_index == 0 else b_count[min_index-1])
    
    return ans

if __name__ == "__main__":
    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()])
    print(solve(n, a, b, c))

Submission status: Accepted

Solution code:
import bisect
def I(): return int(input())
def LI(): return list(map(int,input().split()))
n=I()
a=sorted(LI())
b=sorted(LI())
c=sorted(LI())
ans=0
for bd in b:
    oka=bisect.bisect_left(a,bd)
    okc=bisect.bisect_right(c,bd)
    ans+=oka*(n-okc)
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 j in range(n) :
    a = bisect.bisect_left(A,B[j])
    c = bisect.bisect_right(C,B[j])
    cnt += a * (n-c)
    
print(cnt)

Submission status: Accepted

Solution code:
import bisect

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

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

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

print(ans)

Submission status: Accepted

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
cns = 0
for i in B:
    x = bisect_left(A,i)
    y = bisect_right(C,i)
    y = n - y
    ans += x*y
print(ans)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
A = sorted(list(map(int, input().split()))) #0-index
B = sorted(list(map(int, input().split()))) #0-index
C = sorted(list(map(int, input().split()))) #0-index
print(sum(bisect.bisect_left(A, b)*(N-bisect.bisect_right(C, b)) for b in B))

Submission status: Accepted

Solution code:
import sys
import bisect
input = sys.stdin.readline
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 = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
C = [int(x) for x in input().split()]

A.sort()
B.sort()
C.sort()
B += [10 ** 9 + 1] + [0]    # B[-1] = 0, B[N] = 10**9 + 1となるようにする
C += [10 ** 9 + 1] + [0]    # C[-1] = 0, C[N] = 10**9 + 1となるようにする

# For each j = 0, 1, ..., N-1,
# find k such that B[j] < C[k] and (C[k-1] <= B[j] or k = 0)
# Let K[j] = k for such k.
# If such k doesn't exist, let K[j] := N
K = [0] * N

for j in range(N):
    ng = -1 # C[ng] <= B[j]
    ok = N  # B[j] < C[ok]
    while ok - ng > 1:
        mid = (ok + ng) // 2
        if B[j] < C[mid]:
            ok = mid
        else:
            ng = mid
    K[j] = ok

# Let
# f[j] := B[j]より大きいC[k]の数 + B[j + 1]より大きいC[k]の数
#           ... + B[N-1]より大きいC[k]の数
f = [0] * (N - 1) + [N - K[N - 1]]
for j in range(N - 2, -1, -1):
    f[j] = f[j + 1] + N - K[j]

ans = 0
for i in range(N):
    # find j such that B[j - 1] <= A[i] < B[j]
    ng = -1  # B[ng] <= A[i]
    ok = N   # A[i] < B[ok]
    while ok - ng > 1:
        mid = (ok + ng) // 2
        if A[i] < B[mid]:
            ok = mid
        else:
            ng = mid
    if ok >= N: continue
    # 0 <= ok <= N - 1
    ans += f[ok]

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

count_B = {}
count_C = {}
count_sum = {}

count = 0
for idx, b in enumerate(reversed(B)):
    count_C[b] = N - bisect.bisect_right(C, b)
    count += count_C[b]
    count_sum[N-idx-1] = count
# print(count_sum)
count = 0
for a in A:
    index = bisect.bisect_right(B, a)
    if index in count_sum:
        count += count_sum[bisect.bisect_right(B, a)]
        # print(a, b)
print(count)

Submission status: Accepted

Solution code:
import bisect


def read_input():
    N = int(input())
    A = [int(x) for x in input().split(" ")]
    B = [int(x) for x in input().split(" ")]
    C = [int(x) for x in input().split(" ")]
    return N, A, B, C


def main():

    N, A, B, C = read_input()
    count = 0
    A.sort()

    C.sort()
    len_c = len(C)
    for b in B:
        l = bisect.bisect_left(A, b)
        r = len_c - bisect.bisect_right(C, b)
        count += l * r

    print(count)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

sum=0
for i in range(n):
    bisect_a=bisect.bisect_left(a,b[i])
   
    
    bisect_c=bisect.bisect_right(c,b[i])
   

    sum+=bisect_a*(n-bisect_c)
print(sum)

Submission status: Accepted

Solution code:
import bisect

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(sorted(list(map(int,input().split()))))
b = list(sorted(list(map(int,input().split()))))
c = list(sorted(list(map(int,input().split()))))
 
def bs_left(ls,target):
    low, high = 0, len(ls)
    while low < high:
        mid = (low+high) // 2
        if ls[mid] < target:
            low = mid + 1
        else:
            high = mid
    return low
 
def bs_right(ls,target):
    low, high = 0, len(ls)
    while low < high:
        mid = (low+high) // 2
        if target < ls[mid]:
            high = mid
        else:
            low = mid + 1
    return low
 
ans = 0
for i in b:
    ans += (bs_left(a,i)*(n-bs_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()
B.sort()
C.sort()

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

print(ans)

Submission status: Accepted

Solution code:
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()
BoverA = [0] * N
CoverB = [0] * N
for i in range(N):
    BoverA[i] = N - bisect.bisect_right(B, A[i])
    CoverB[i] = N - bisect.bisect_right(C, B[i])
cumBC = [0] * N
cumBC[N-1] = CoverB[N-1]
for i in range(N-2, -1, -1):
    cumBC[i] = cumBC[i+1] + CoverB[i]
ans = 0
for i in range(N):
    if N - BoverA[i] >= 0 and N - BoverA[i] < N:
        ans += cumBC[N - BoverA[i]]
print(ans)

Submission status: Accepted

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 range(n):
    A=bisect_left(a,b[i])
    C=n-bisect_right(c,b[i])
    ans+=A*C
print(ans)

Submission status: Accepted

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

def bs_left(ls,target):
    low, high = 0, len(ls)
    while low < high:
        mid = (low+high) // 2
        if ls[mid] < target:
            low = mid + 1
        else:
            high = mid
    return low

def bs_right(ls,target):
    low, high = 0, len(ls)
    while low < high:
        mid = (low+high) // 2
        if target < ls[mid]:
            high = mid
        else:
            low = mid + 1
    return low

ans = 0
for i in b:
    ans += (bs_left(a,i)*(n-bs_right(c,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())))
cnt=0
for i in range(n):
  A = bisect.bisect_left(a, b[i])
  C = n-bisect.bisect_right(c, b[i])
  cnt+=A*C
print(cnt)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np

n = int(readline())
a = np.array(readline().split(), dtype=np.int32)
b = np.array(readline().split(), dtype=np.int32)
c = -np.array(readline().split(), dtype=np.int32)

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

cnt_a=np.searchsorted(a,b)
cnt_c=np.searchsorted(c,-b)
print((cnt_a*cnt_c).sum())

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())))
#cnt=0
#for i in range(n):
 # B=b[i]
  #A=[a[i] for i in range(n) if a[i]<B]
  #C=[c[i] for i in range(n) if c[i]>B]
  #cnt+=len(A)*len(C)
#print(cnt)
cnt=0
#真ん中のパーツをまず考える
for i in range(n):
  B=b[i]
  if B<=a[0] or B>=c[-1]:
    continue
  #Bより小さいAの最大値のインデックスを探す
  i_small=0
  i_large=n-1
  while i_large-i_small>1:
    if a[(i_small+i_large)//2] <B:
      i_small=(i_small+i_large)//2
    else:
      i_large=(i_small+i_large)//2
 
  A=i_large
  if a[-1]<B:
    A+=1

  #Bより大きいCの最小値のインデックスを探す
  i_small=0
  i_large=n-1
  while i_large-i_small>1:
    if c[(i_small+i_large)//2] >B:
      i_large=(i_small+i_large)//2
    else:
      i_small=(i_small+i_large)//2
  C=n-i_large
  if c[0]>B:
    C+=1

  cnt+=A*C
if n>1:
  print(cnt)
else:
  if a[0]<b[0]<c[0]:
    print(1)
  else:
    print(0)

Submission status: Accepted

Solution code:
def main():
    from bisect import bisect_left, bisect_right
    n, *abc = map(int, open(0).read().split())
    a, b, c = abc[:n], abc[n:2 * n], abc[2 * n:]
    a, b, c = sorted(a), sorted(b), sorted(c)
    ans = 0
    for i in b:
        j = bisect_left(a, i)
        k = n - bisect_right(c, i)
        ans += j * k

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
a.sort()
b.sort()
c.sort()
ans=0
for i in range(n):
  middle=b[i]
  h=n
  l=-1
  while h-l>1:
    m=(h+l)//2
    if a[m]<middle:
      l=m
    else:
      h=m
  ab=l+1
  h=n
  l=-1
  while h-l>1:
    m=(h+l)//2
    if c[m]>middle:
      h=m
    else:
      l=m
  bc=n-h
  ans+=ab*bc
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
a.sort()
b.sort()
c.sort()
AtoB=[0 for i in range(n)]
BtoC=[0 for i in range(n+1)]
for i in range(n):
  top=a[i]
  h=n
  l=-1
  while h-l>1:
    mid=(h+l)//2
    if b[mid]>top:
      h=mid
    else:
      l=mid
  if h==0 and top>b[0]:
    h=-1
  AtoB[i]=h #smallestB-index
for i in range(n-1,-1,-1):
  top=b[i]
  h=n
  l=-1
  while h-l>1:
    mid=(h+l)//2
    if c[mid]>top:
      h=mid
    else:
      l=mid
  BtoC[i]+=BtoC[i+1]+n-h #according to B,C patten
ans=0
for i in range(n):
  ans+=BtoC[AtoB[i]]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort()
B.sort()
C.sort()
ans = 0
def low_func(array, value):
  ng = -1
  ok = len(array)
  #if value
  while ok - ng > 1:
    mid = (ok + ng)//2
    if array[mid] < value:
      ng = mid
    elif array[mid] >= value:
      ok = mid
  return ok

def up_func(array, value):
  ok = -1
  ng = len(array)
  #if value
  while abs(ok - ng) > 1:
    mid = (ok + ng)//2
    if array[mid] <= value:
      ok = mid
    elif array[mid] > value:
      ng = mid
  return ok

for b in B:
  a_idx = up_func(A, b - 1)
  if a_idx == -1:
    continue
  c_idx = low_func(C, b + 1)
  if c_idx == len(C):
    break
  ans += (a_idx + 1)*(len(C) - c_idx)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
a = np.array(readline().split(), dtype=np.int32)
b = np.array(readline().split(), dtype=np.int32)
c = - np.array(readline().split(), dtype=np.int32)

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

cnt_a = np.searchsorted(a, b, side='left')
cnt_c = np.searchsorted(c, -b, side='left')
 
answer = (cnt_a * cnt_c).sum()
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()
C.sort()
d=0
for i in B:
 d+=bisect.bisect_right(A,i-0.1)*(N-bisect.bisect_left(C,i+0.1))
print(d)

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


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

Submission status: Accepted

Solution code:
from bisect import bisect_right, bisect_left


n = int(input())
a = sorted(map(int, input().split()))
b = sorted(map(int, input().split()))
c = sorted(map(int, input().split()))
print(sum(bisect_left(a, i) * (n - bisect_right(c, i)) for i in b))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np

def abc077_c():
    ''' maspyさんお手本 '''
    N = int(readline())
    A = np.array(readline().split(), dtype=np.int64)
    B = np.array(readline().split(), dtype=np.int64)
    C = np.array(readline().split(), dtype=np.int64)

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

    # 各Bに対して使えるAの個数
    cnt_A = np.searchsorted(A, B, side='left')
    # 各Bに対して使えないCの個数
    cnt_C_not = np.searchsorted(C, B, side='right')
    # 各Bに対して使えるCの個数
    cnt_C = N - cnt_C_not
    # 別の書き方
    # cnt_C_alt = np.searchsorted(-1 * np.flip(C), -1 * B, side='left')

    ans = np.multiply(cnt_A, cnt_C).sum()
    print(ans)

abc077_c()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def abc077_c():
    N = int(readline())
    A = list(map(int, readline().split()))
    B = list(map(int, readline().split()))
    C = list(map(int, readline().split()))
    A.sort()
    B.sort()
    C.sort()
    ans = 0

    from bisect import bisect_left

    # Bjに対するCの使える個数は、前処理で求めておく
    B2C = [0] * N
    # 真に大きいが条件なので Ai + 1 が入るポイントを探す
    for j in range(N):
        B2C[j] = N - bisect_left(C, B[j]+1)
    # 累積和
    accum = [0]
    for j in range(N):
        accum.append(accum[-1] + B2C[j])

    # Ai -> B は二分探索
    for i in range(N):
        jst = bisect_left(B, A[i]+1)
        if jst == N: continue
        # 前処理済みなので、Bj以降を使ったときにCが何個使えるかはすぐわかる
        ans += accum[N] - accum[jst]
    print(ans)

abc077_c()

Submission status: Accepted

Solution code:
import math
def main():
  from builtins import input, int, map
  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()
  n = int(math.log(N,2))+1

  ans = 0

  for i in range(N):
    if A[0] >= B[i]:
      continue
    if A[N-1] < B[i]:
      a = N
    else:
      left = 0
      right = N-1
      for _ in range(n):
        if right - left <= 1:
          break
        mid = (left + right) // 2
        if  B[i]> A[mid]:
          left = mid
        else:
          right = mid
      a = left + 1
    if C[N-1] <= B[i]:
      continue
    if C[0] > B[i]:
      c = N
    else:      
      left = 0
      right = N-1
      for _ in range(n):
        if right - left <= 1:
          break
        mid = (left + right) // 2
        if B[i]>= C[mid]:
          left = mid
        else:
          right = mid
      c = N - right
    ans += a*c
  print(ans)
main()

Submission status: Accepted

Solution code:
import bisect as bi
def main():
  from builtins import input, int, map
  N = int(input())
  A = list(map(int,input().split()))
  B = list(map(int,input().split()))
  C = list(map(int,input().split()))
  A.sort()
  C.sort()

  ans = 0

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b =list(map(int,input().split()))
c =list(map(int,input().split()))
a.sort()
b.sort()
c.sort()
i=0
k=0
ans=0
acnt=0
ccnt=n
for x in b:
    aok=True
    cng=True
    while aok:
        if acnt == n:
            break
        if a[i]<x:
            i+=1
            acnt+=1
        else:
            aok=False
    while cng:
        if ccnt == 0:
            break
        if c[k]<=x:
            k+=1
            ccnt-=1
        else:
            cng=False
    ans+=acnt*ccnt
    #print(i,acnt,k,ccnt)
print(ans)

Submission status: Accepted

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


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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
an = list(map(int, input().split(' ')))
bn = list(map(int, input().split(' ')))
cn = list(map(int, input().split(' ')))

an = sorted(an)
bn = sorted(bn)
cn = sorted(cn)

bcount = [0] * n
ccount = [0] * n

ai = 0
for bi in range(n):
    while ai < n and bn[bi] > an[ai]:
        bcount[bi] += 1
        ai += 1
    if bi < len(bn) - 1:
        bcount[bi + 1] = bcount[bi]

bi = 0
for ci in range(n):
    while bi < n and cn[ci] > bn[bi]:
        ccount[ci] += bcount[bi]
        bi += 1
    if ci < len(cn) - 1:
        ccount[ci + 1] = ccount[ci]
        
print(sum(ccount))

Submission status: Accepted

Solution code:
# C - Snuke Festival

import numpy as np
N = int(input())
A = np.array(list(int(x) for x in input().split()))
B = np.array(list(int(x) for x in input().split()))
C = np.array(list(int(x) for x in input().split()))
A.sort()
C.sort()

ans = np.sum(np.searchsorted(A, B,'left') * (N - np.searchsorted(C, B,'right')))
print(ans)

Submission status: Accepted

Solution code:
# C - Snuke Festival

import bisect

N = int(input())
A = list(int(x) for x in input().split())
B = list(int(x) for x in input().split())
C = list(int(x) for x in input().split())

A.sort()
C.sort()

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

print(ans)

Submission status: Accepted

Solution code:
# C - Snuke Festival

N = int(input())
A = list(int(x) for x in input().split())
B = list(int(x) for x in input().split())
C = list(int(x) for x in input().split())

A.sort()
C.sort()

def binarySearchLeft(a, b):
    l = -1
    r = N
    while l+1<r:
        m = (l+r)//2
        if a[m]<b:
            l = m
        else:
            r = m
    return r

def binarySearchRight(a, b):
    l = -1
    r = N
    while l+1<r:
        m = (l+r)//2
        if a[m]<=b:
            l = m
        else:
            r = m
    return r

ans = 0
for j in range(N):
    ans += binarySearchLeft(A, B[j]) * (N - binarySearchRight(C, B[j]))

print(ans)

Submission status: Accepted

Solution code:
# C - Snuke Festival

N = int(input())
A = list(int(x) for x in input().split())
B = list(int(x) for x in input().split())
C = list(int(x) for x in input().split())

B.sort()
C.sort()

def binarySearch(a, b):
    l = -1
    r = N
    while l+1<r:
        m = (l+r)//2
        if a[m]<=b:
            l = m
        else:
            r = m
    return l+1

BC = []
for i in range(N):
    tmp = binarySearch(C, B[i])
    BC.append((N - tmp))

BC = BC[::-1]
BC2 = [0] * (N+1)
for i in range(N):
    BC2[i+1] = BC2[i] + BC[i]
BC2 = BC2[::-1]

ans = 0
for i in range(N):
    b = binarySearch(B, A[i])
    ans += BC2[b]

print(ans)

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_left, bisect_right, insort_left, insort_right
from collections import defaultdict, deque, Counter
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from heapq import heappop, heappush
from itertools import accumulate, groupby, product, permutations, combinations, combinations_with_replacement
from math import ceil, floor, sqrt, cos, sin, pi
from operator import itemgetter
sys.setrecursionlimit(10**6)
rs = lambda: sys.stdin.readline().rstrip()
ri = lambda: int(rs())
rf = lambda: float(rs())
rs_ = lambda: [_ for _ in rs().split()]
ri_ = lambda: [int(_) for _ in rs().split()]
rf_ = lambda: [float(_) for _ in rs().split()]

N = ri()
A = sorted(ri_())
B = sorted(ri_())
C = sorted(ri_())
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:
# C - Snuke Festival

import bisect

N = int(input())
A = list(int(x) for x in input().split())
B = list(int(x) for x in input().split())
C = list(int(x) for x in input().split())

B.sort()
C.sort()

BC = []
for i in range(N):
    BC.append((N - bisect.bisect_right(C, B[i])))

BC = BC[::-1]
BC2 = [0] * (N+1)
for i in range(N):
    BC2[i+1] = BC2[i] + BC[i]
BC2 = BC2[::-1]

ans = 0
for i in range(N):
    b = bisect.bisect_right(B, A[i])
    ans += BC2[b]

print(ans)

Submission status: Accepted

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

    cnt = 0
    for i in range(N):
        a_cnt = binSearch(B[i],A)
        c_cnt = N - binSearch(B[i]+1, C)
        cnt += a_cnt * c_cnt
    
    print(cnt)

def binSearch(target,lst):
    left = -1
    right = len(lst)

    while right - left > 1:
        mid = (right+left) // 2
        if lst[mid] < target:
            left = mid
        else:
            right = mid
    
    return right

if __name__ == '__main__':
    solve()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import bisect

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

ans = 0
for bb in b:
    ai = bisect.bisect_left(a, bb)
    ci = bisect.bisect_right(c, bb)
    ans = ans + (ai * (n-ci))

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

Submission status: Accepted

Solution code:
from bisect import bisect
from itertools import accumulate
N = int(input())
A = sorted(map(int, input().split()))
B = sorted(map(int, input().split()))
C = sorted(map(int, input().split()))
D = list(accumulate(N - bisect(C, b) for b in reversed(B))) + [0]
print(sum(D[N - bisect(B, a) - 1] for a in A))

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


def binary_upper(k, x):
    return len(k) - bisect.bisect_right(k, x)


def binary_lower(k, x):
    return bisect.bisect_left(k, x)


ans = 0

for middle in b:
    cnt_a = binary_lower(a, middle)
    cnt_c = binary_upper(c, middle)
    ans += cnt_a * cnt_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()
ct = 0
for j in B:
    a_max = bisect.bisect_left(A, j)
    c_min = bisect.bisect_right(C, j)
    ct += a_max * (n - c_min)
print(ct)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = np.array(input().split(), np.int64)
B = np.array(input().split(), np.int64)
C = np.array(input().split(), np.int64)
A.sort()
B.sort()
C.sort()

x = np.searchsorted(B, A, 'right')
y = np.searchsorted(C, B, 'right')
Z = np.add.accumulate(N - y[::-1])
ans = sum(Z[k - 1] for k in N - x if k)
print(ans)

Submission status: Accepted

Solution code:
def bin_search(i, array):
    low = 0
    high = len(array) - 1

    while low <= high:
        mid = (low + high) // 2
        # print("mid = ", mid, "array[mid] = ", array[mid])
        if array[mid] < i and (mid == len(array)-1 or array[mid+1] >= i ):
            # print("A")
            return mid + 1
        
        elif array[mid] < i:
            # print("B")
            low = mid + 1
        
        elif array[mid] >= i:
            # print("C")
            high = mid - 1
    
    return 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()

# print(A)
# print(B)
# print(C)

B_num = [0]*N
for i in range(N):
    b = B[i]
    B_num[i] = bin_search(b, A)

C_num = [0]*N
for i in range(N):
    c = C[i]
    C_num[i] = bin_search(c, B)

Cumulative_sum = [0]*N
Cumulative_sum[0] = B_num[0]
for i in range(1, N):
    Cumulative_sum[i] = Cumulative_sum[i-1] + B_num[i]

# print(B_num)
# print(C_num)

ans = 0
for i in range(N):
    if C_num[i] != 0:
        tmp = Cumulative_sum[C_num[i] - 1]
        # print(tmp)
        ans += tmp
    # else:
    #     print(0)

print(ans)

# print(bin_search(6, B))

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

cnt = 0
cnt_b = []
for i_b in range(n):
    # if max(c) < b[i_b]:
    #     cnt_b.append(0)
    #     continue
    i_a = bisect.bisect_left(a, b[i_b]) - 1
    i_c = bisect.bisect_right(c, b[i_b])
    if i_a < 0 or n <= i_c:
        continue
    cnt += (i_a + 1) * (n - i_c)
    # print(i_a, i_b, i_c)

print(cnt)

Submission status: Accepted

Solution code:
import bisect

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

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

print(ans)

Submission status: Accepted

Solution code:
import bisect

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

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

print(ans)