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:
# /usr/bin/python
# -*- coding: utf-8 -*-
import math
import sys


MX = 10**9+1

N = int(sys.stdin.readline().rstrip())
An = list(map(int, sys.stdin.readline().rstrip().split()))
Bn = list(map(int, sys.stdin.readline().rstrip().split()))
Cn = list(map(int, sys.stdin.readline().rstrip().split()))

An = sorted(An)+[MX]
Bn = sorted(Bn)
Cn = sorted(Cn)+[MX]

ans,ia,ic = [0, 0, 0]
for b in Bn:
  while(An[ia] < b):
    ia += 1
  while(Cn[ic] <= b):
    ic += 1
  ans += ia * (N-ic)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
arr1=sorted(list(map(int,input().split())),reverse=True)
arr2=sorted(list(map(int,input().split())),reverse=True)
arr3=sorted(list(map(int,input().split())),reverse=True)
cnt=[0]*n
ans=0
pos=0
for i in range(n):
  for j in range(pos,n):
    if arr1[j]<arr2[i]:
      cnt[i]=n-pos
      break
    else:
      pos+=1
pos=0
for i in range(n):
  for j in range(pos,n):
    if arr2[i]<arr3[j]:
      pos+=1
    else:
      ans+=cnt[i]*pos
      break
  else:
    ans+=cnt[i]*pos
print(ans)

Submission status: Accepted

Solution code:
from functools import reduce
import math
import bisect
import collections 
def main():
    # 文字列の2進数を数値にする
    # '101' → '5'
    # 文字列の頭に'0b'をつけてint()にわたす
    # binary = int('0b'+'101',0)

    # 2進数で立っているbitを数える
    # 101(0x5) → 2
    # cnt_bit = bin(5).count('1')
    
    # N! を求める
    # f = math.factorial(N)

    # N の逆元
    # N_inv = pow(N,MOD-2,MOD)

    # nCr
    # Nの階乗 * rの階乗の逆元 * n-rの階乗の逆元
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    INF = float("inf")

    # 大きな素数
    MOD = 10**9+7
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]    
    N = int(input())
    A = list(int(_) for _ in input().split())
    B = list(int(_) for _ in input().split())
    C = list(int(_) for _ in input().split())
    A.sort()
    C.sort()
    ans = 0

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

    print(ans)
if __name__ == '__main__':
    main()

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

ans = 0
for b in bs_:
  ans += bisect.bisect_left(as_, b) * (n-bisect.bisect_right(cs_, b))
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:
import sys, os
import bisect

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n = f()[0]
    a = f()
    b = f()
    c = f()

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

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


    

solve()

Submission status: Accepted

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

o = []
p = []

i = 0
k = 0
for j in range(N):
    while i < N and A[i] < B[j]:
        i = i + 1
    o.append(i)

    while k < N and B[j] >= C[k]:
        k = k + 1
    p.append(N-k)

sum = 0
for x in range(N):
    sum = sum + o[x] * p[x]

print(sum)

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:
    a = bisect.bisect_left(A, b) # 使えるAの要素数
    c = N - bisect.bisect_right(C, b) # 使えるCの要素数
    ans += a * c  

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect,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:
    ca = bisect_left(A,b)
    cc = N - bisect(C,b)
    ans += ca*cc

print(ans)

Submission status: Accepted

Solution code:
import bisect
def MI(): return map(int, input().split())
def II(): return int(input())
def IS(): return input()
def LI(): return list(map(int, input().split()))

n = II()
a, b, c = sorted(LI()), LI(), sorted(LI())
ans = 0
for _b in b:
    a_index = bisect.bisect_left(a,_b)
    c_index = bisect.bisect_right(c,_b)
    ans += a_index * (n - c_index)
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:ans+=bisect.bisect_left(a,i)*(n-bisect.bisect_right(c,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()
i,j,k=0,0,0
m1=[]
while k<n and i<n:
  if b[i]>a[k]:
    j+=1
    k+=1
  else:
    m1.append(j)
    i+=1
for t in range(len(m1),len(b)):
  m1.append(j)
i,j,k=0,0,0
m2=[]
while k<n and i<n:
  if c[i]>b[k]:
    j+=m1[k]
    k+=1
  else:
    m2.append(j)
    i+=1
for t in range(len(m2),len(c)):
  m2.append(j)
print(sum(m2))

Submission status: Accepted

Solution code:
import bisect
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()
s = 0
for b_ in b:
    t = bisect.bisect_right(a, b_-1)
    tt = len(c) - bisect.bisect_right(c, b_)
    d = tt * t
    s += d
print(s)

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
#順序維持のbisect (二分探索を利用したライブラリ）
for i in range(N):
    count_a = bisect.bisect_left(A,B[i])
    count_c = N -bisect.bisect_right(C,B[i])
    ans += count_a*count_c
print(ans)

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def pf(s): return print(s, flush=True)

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

BC = []
for b in B:
    idx = bisect.bisect_right(C, b)
    BC.append(len(B)-idx)

b_ac = [0]
for b in reversed(BC):
    b_ac.append(b+b_ac[-1])
b_ac.sort(reverse=True)
#  print(b_ac)
result = 0
for a in A:
    idx = bisect.bisect_right(B, a)
    result += b_ac[idx]
print(result)

Submission status: Accepted

Solution code:
import bisect
from collections import defaultdict

N = int(input())
As  = sorted(map(int, input().split()))
Bs  = sorted(map(int, input().split()))
Cs  = sorted(map(int, input().split()))

b_counts = []
for b in Bs[::-1]:
    count = len(Cs) - bisect.bisect(Cs, b)
    if len(b_counts) > 0:
        count += b_counts[-1]
    b_counts.append(count)
b_counts = b_counts[::-1]

total = 0
for a in As:
    i = bisect.bisect(Bs, a)
    if i >= len(Bs):
        continue
    total += b_counts[i]

print(total)

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
#print(A)
#print(B)
#print(C)
for b in B:
    num1 = bisect_left(A,b)
    num2 = bisect_right(C,b)
    #print(num1,b,num2)
    ans += num1*(n-num2)

print(ans)

Submission status: Accepted

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

for i in range(N):

    count_a = bisect.bisect_left(A,B[i])
    count_c = N -bisect.bisect_right(C,B[i])
    ans += count_a*count_c
    
print(ans)

Submission status: Accepted

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

parts = A+B+C
parts.sort(reverse = True,key = lambda x:x[0])

A_count = 0  
B_count = 0  
C_count = 0

for s,t in parts:
    if t=="C":      
        C_count += 1
    elif t=="B":
        B_count += C_count             
    elif t=="A":
        A_count += B_count
print(A_count)

Submission status: Accepted

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

parts = A+B+C
parts.sort(key = lambda x:x[1])
parts.sort(reverse = True,key = lambda x:x[0])

A_pat = [0]  
B_pat = [0]
C_count = 0

for s,t in parts:
    if t=="C":      
        C_count += 1
    elif t=="B":
        B_pat.append(C_count+B_pat[-1])
    elif t=="A":
        A_pat.append(B_pat[-1]+A_pat[-1])
print(A_pat[-1])

Submission status: Accepted

Solution code:
import bisect

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

A = sorted(A)
C = sorted(C)
ans = 0

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

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 = sorted(A)
B = sorted(B)
C = sorted(C)
ans = 0
midnum = []
index = 0

for i in B:
    while True:
        if index == N or A[index] >= i:
            midnum.append(index)
            break
        else:
            index += 1

index = 0
midCnum = []
tmp = 0
for i in midnum:
    tmp += i
    midCnum.append(tmp)

for i in C:
    while True:
        if index == N:
            ans += midCnum[index-1]
            break
        elif i <= B[index]:
            if index != 0:
                ans += midCnum[index-1]
            break
        else:
            index += 1

print(ans)

Submission status: Accepted

Solution code:
from bisect import*
from numpy import*
n=int(input())
a=sorted(map(int,input().split()))
b=sorted(map(int,input().split()))
c=sorted(map(int,input().split()))
#print(a,b,c)
#print([bisect_right(b,t)for t in a])
#print(cumsum([n-bisect_right(c,s)for s in b][::-1])[::-1])
ans=0
x=cumsum([n-bisect_right(c,s)for s in b][::-1])[::-1]
y=[bisect_right(b,t)for t in a]
for i in y:
    if i<n:
        ans+=x[i]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
A = defaultdict(int)
B = defaultdict(int)
C = defaultdict(int)

N = int(input())
a = [k for k in map(int, input().split())]
b = [k for k in map(int, input().split())]
c = [k for k in map(int, input().split())]

for k in range(N):
  A[a[k]] += 1
  B[b[k]] += 1
  C[c[k]] += 1

Aset = sorted(set(a))
Bset = sorted(set(b))
Cset = sorted(set(c))

cnt = 0
index_a = 0
index_c = 0
a = 0
c = N

for b in Bset:
  flag_a = False
  flag_c = False
  if a < N:
    for k in range(index_a, len(Aset)):
      if Aset[k] < b:
        a += A[Aset[k]] 
      else:
        index_a = k
        flag_a = True
        break
  for k in range(index_c, len(Cset)):
    if Cset[k] <= b:
      c -= C[Cset[k]]
    else:
      index_c = k
      flag_c = True
      break
  if flag_a == False:
    a = N
  if flag_c == False:
    break
  cnt += a*c*B[b]
print(cnt)

Submission status: Accepted

Solution code:
import bisect

n = int(input())
alist = list(sorted(map(int, input().split())))
blist = list(map(int, input().split()))
clist = list(sorted(map(int, input().split())))

ans = 0
for b in blist:
  ai = bisect.bisect_left(alist, b)
  ci = bisect.bisect_right(clist, b)
  if ai == 0 or ci == len(clist):
    continue
  else:
    ans += ai * (len(clist) - ci)
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()))
x=[1 for _ in range(N)]
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
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [int(input_raw[0])]
    input_raw = read_func().strip().split()
    A = []
    for i in range(N):
        A.append(int(input_raw[i]))
    input_raw = read_func().strip().split()
    B = []
    for i in range(N):
        B.append(int(input_raw[i]))
    input_raw = read_func().strip().split()
    C = []
    for i in range(N):
        C.append(int(input_raw[i]))
    A.sort()
    B.sort()
    C.sort()

    A_backet = {}
    C_backet = {}
    A_ptr = 0L
    C_ptr = 0L
    count = 0L
    for i in range(N):
        while A_ptr < N and A[A_ptr] < B[i]:
            A_ptr += 1L
        while C_ptr < N and C[C_ptr] <= B[i]:
            C_ptr += 1L
        count += A_ptr * (N - C_ptr)
    print count




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect
N = int(input())
J = list(map(int,input().split()))
K = list(map(int,input().split()))
L = list(map(int,input().split()))
J.sort()
K.sort()
L.sort()
S = 0
for i in range(N):
  k = bisect.bisect_left(J,K[i])
  l = N - bisect.bisect_right(L,K[i])  
  S += k*l
print(S)

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

Submission status: Accepted

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

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

    A.sort()
    B.sort()
    C.sort()
    S = [0]

    s = 0
    i = 0
    for b in B:
        while i < N:
            if b <= A[i]:
                break
            i += 1
        s += i
        S.append(s)

    t = 0
    i = 0
    for c in C:
        while i < N:
            if c <= B[i]:
                break
            i += 1
        t += S[i]


    print(t)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect
from itertools import accumulate

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)


def my_func(A, B, C):
    acc_a = [0]*N
    acc_c = [0]*N

    i_a = 0
    i_c = N-1
    for i in range(N):
        b = B[i]
        while i_a < N and A[i_a] < b:
            acc_a[i] += 1
            i_a += 1

    for i in reversed(range(N)):
        b = B[i]
        while i_c >= 0 and C[i_c] > b:
            acc_c[i] += 1
            i_c -= 1

    acc_a = list(accumulate(acc_a))
    acc_c = list(accumulate(acc_c[::-1]))
    acc_c = acc_c[::-1]

    ans = 0
    for a, c in zip(acc_a, acc_c):
        ans += a*c


def bisect_func(A, B, C):
    acc_a = [0]*N
    acc_c = [0]*N
    for i in range(N):
        acc_a[i] = bisect.bisect_left(A, B[i])
        acc_c[i] = N-bisect.bisect_right(C, B[i])

    ans = 0
    for a, c in zip(acc_a, acc_c):
        ans += a*c
    return ans


print(bisect_func(A, B, C))

Submission status: Accepted

Solution code:
from itertools import accumulate

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)

acc_a = [0]*N
acc_c = [0]*N

i_a = 0
i_c = N-1
for i in range(N):
    b = B[i]
    while i_a < N and A[i_a] < b:
        acc_a[i] += 1
        i_a += 1

for i in reversed(range(N)):
    b = B[i]
    while i_c >= 0 and C[i_c] > b:
        acc_c[i] += 1
        i_c -= 1

acc_a = list(accumulate(acc_a))
acc_c = list(accumulate(acc_c[::-1]))
acc_c = acc_c[::-1]

ans = 0
for a, c in zip(acc_a, acc_c):
    ans += a*c
print(ans)

Submission status: Accepted

Solution code:
import bisect
N = input()
A = map(int, raw_input().split())
B = map(int, raw_input().split())
C = map(int, raw_input().split())
A.sort()
B.sort()
C.sort()

BC_type_num = []
pre = 0
for b in reversed(B):
    upper_bound = bisect.bisect_right(C, b)
    BC_type_num.append(pre + N - upper_bound)
    pre += N - upper_bound
BC_type_num.reverse()

res = 0
for a in A:
    upper_bound = bisect.bisect_right(B, a)
    if upper_bound < N:
        res += BC_type_num[upper_bound]

print res

Submission status: Accepted

Solution code:
def inpl(): return [int(i) for i in input().split()]
import bisect
N = int(input())
A = sorted(inpl())
B = inpl()
C = sorted(inpl())
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:
# coding: utf-8

II = lambda: int(input())
MI = lambda: map(int, input().split())


def main():
    N = II()
    A, B, C = sorted(list(MI())), sorted(list(MI())), sorted(list(MI()))

    s = [i for i in range(1, N + 1)]
    ia = N-1
    ns = [None] * N
    for ib in range(N-1, -1, -1):
        while ia >= 0 and A[ia] >= B[ib]:
            ia -= 1
        if ia >= 0:
            ns[ib] = s[ia]
        else:
            ns[ib] = 0
    for i in range(1, N):
        ns[i] += ns[i-1]

    s = ns
    ib = N-1
    ns = [None] * N
    for ic in range(N-1, -1, -1):
        while ib >= 0 and B[ib] >= C[ic]:
            ib -= 1
        if ib >= 0:
            ns[ic] = s[ib]
        else:
            ns[ic] = 0
    return sum(ns)


if __name__ == "__main__":
    print(main())

Submission status: Accepted

Solution code:
from bisect import bisect_left,bisect
N = int(input())
info = [sorted(list(map(int,input().split()))) for i in range(3)]
ans = 0
for j in range(N):
    small = bisect_left(info[0],info[1][j])
    large = N - bisect(info[2],info[1][j])
    ans += small * large
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(' ')))

sorted_a = sorted(a)
sorted_c = sorted(c)
ans = 0
for each_b in b:
    ai = bisect.bisect_left(sorted_a, each_b)
    ci = bisect.bisect_right(sorted_c, each_b)
    ans += ai * (len(c)-ci)
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(' ')))

sorted_a = sorted(a)
sorted_b = sorted(b)
sorted_c = sorted(c)
ans = 0
before_a = 0
before_c = 0
count_a = 0
count_c_rest = 0
for each_b in sorted_b:
    for ai in range(before_a, len(sorted_a)):
        if each_b > sorted_a[ai]:
            count_a += 1
            before_a += 1
        else:
            before_a = ai
            break

    for ci in range(before_c, len(sorted_c)):
        if sorted_c[ci] <= each_b:
            count_c_rest += 1
            before_c += 1
        else:
            before_c = ci
            break

    ans += count_a * (len(sorted_c)-count_c_rest)
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 j in range(N):
    a=bisect_left(A, B[j])
    c=bisect_right(C, B[j])
    ans+=a*(N-c)
print(ans)

Submission status: Accepted

Solution code:
import bisect
N=int(input().strip())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
c=list(map(int, input().split()))
a.sort()
#b.sort(reverse=True)
c.sort()
ans=0
#for i in range(len(b)):
#    la=bisect.bisect_left(a, b[i])
#    ra=bisect.bisect_right(c, b[i])
#    ans+=la*(len(c)-ra)
#    print(la,(len(c)-ra))


for i in range(len(b)):
    la=bisect.bisect_left(a, b[i])
    ra=bisect.bisect_right(c, b[i])
    #print(la*(len(c)-ra))
    ans+=la*(len(c)-ra)
#print(ans)
#la=bisect.bisect_left(a, 8)
#ra=bisect.bisect_right(a, 8)
#print(la,len(a)-ra)

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

BB = [0] * N

cidx = 0
for i in range(N):
    while cidx < N and C[cidx] <= B[i]:
        cidx += 1
    BB[i] = N - cidx

BBB = [0] * N
for i in range(N - 1, -1, -1):
    if (i == N - 1):
        BBB[i] = BB[i]
    else:
        BBB[i] = BB[i] + BBB[i+1]

sum = 0
bidx = 0
for i in range(N):
    while bidx < N and B[bidx] <= A[i]:
        bidx += 1
    if bidx != N:
        sum += BBB[bidx]

print(sum)

Submission status: Accepted

Solution code:
N=int(input())

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

Z = []

for a in A:
    Z.append([a,0])
    
for b in B:
    Z.append([b,1])

for c in C:
    Z.append([c,2])
    
Z.sort()

accum_a = 0
a_buf = 0
accum_c = 0
c_buf = 0
b_buf = 0
prev_z = 0
ans = 0
for z in Z:
    if z[0]==prev_z:
        if z[1]==0:
            a_buf+=1
        elif z[1]==1:
            b_buf+=1
        else:
            c_buf+=1
    else:
        prev_z = z[0]
        accum_c+=c_buf
        ans+=b_buf*(N-accum_c)*accum_a
        accum_a+=a_buf
        a_buf=0
        b_buf=0
        c_buf=0
        if z[1]==0:
            a_buf+=1
        elif z[1]==1:
            b_buf+=1
        else:
            c_buf+=1
            
print(ans)

Submission status: Accepted

Solution code:
import bisect
import math
import sys
n=int(input())
ans=0
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 b:
    aa=bisect.bisect_left(a,i)
    cc=len(c)-bisect.bisect_right(c,i)
    ans+=aa*cc
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()
d=0
for i in b:
    d+=bisect_left(a,i)*(n-bisect_right(c,i))
print(d)

Submission status: Accepted

Solution code:
n = int(input())
list_a = list(map(int, input().split()))
list_b = list(map(int, input().split()))
list_c = list(map(int, input().split()))
list_a.sort()
list_b.sort()
list_c.sort()
list_a.append(10**10)
list_b.append(10**10)
list_c.append(10**10)

list_memo = []
i = 0
j = 0
k = 0
start = 0
list_ans = []
a_count = 0
while k < n:
    if list_c[k] == min(list_a[i], list_b[j], list_c[k]):
        list_memo.append(0)
        list_memo[-1] = sum(list_memo[start:-1])
        list_ans.append(list_memo[-1])
        start = len(list_memo) - 1
        k += 1
    elif list_b[j] == min(list_a[i], list_b[j], list_c[k]):
        list_memo.append(a_count)
        j += 1
    else:
        list_memo.append(0)
        a_count += 1
        i += 1
print(sum(list_ans))

Submission status: Accepted

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

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18

N = input()
A = [-INF] + sorted(map(int, raw_input().split())) + [INF]
B = sorted(map(int, raw_input().split()))
C = [-INF] + sorted(map(int, raw_input().split())) + [INF]

ans = 0

pos1 = 0
pos2 = 0

for num in B:
    while A[pos1] < num:
        pos1 += 1
    pos1 -= 1
    while C[pos2] <= num:
        pos2 += 1
    pos2 -= 1
    ans += pos1 * (N - pos2)

print ans

Submission status: Accepted

Solution code:
from bisect import bisect_left
N = int(input())
A = sorted(map(int, input().split()))
B = sorted(map(int, input().split()))
C = sorted(map(int, input().split()))
ans = 0
for b in B:
    ans += bisect_left(A, b) * (N - bisect_left(C, b + 1))
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from bisect import *;N,A,B,C=[sorted(map(int,input().split())) for _ in [0]*4];print(sum(bisect_left(A,i)*(N[0]-bisect_right(C,i)) for i in B))

Submission status: Accepted

Solution code:
from bisect import *;N=int(input());A,B,C=[sorted(map(int,input().split())) for _ in "ABC"];print(sum(bisect_left(A,i)*(N-bisect_right(C,i)) for i in B))

Submission status: Accepted

Solution code:
import bisect as b;N=int(input());A,B,C=[sorted(map(int,input().split())) for _ in [0]*3];print(sum(b.bisect_left(A,i)*(N-b.bisect_right(C,i)) for i in B))

Submission status: Accepted

Solution code:
from bisect import *;N=int(input());A,B,C=[sorted(map(int,input().split())) for _ in "ABC"];print(sum([bisect_left(A,i)*(N-bisect_right(C,i)) for i in B]))

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

d = 0
e = 0
ans = 0
for i in b:
    while i > a[min(d,n-1)] and d <= n-1:
        d += 1
    while i >= c[min(e,n-1)] and e < n:
        e += 1
    ans += d*(n-e)

print(ans)

Submission status: Accepted

Solution code:
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import *

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

N = read()
A = sorted(reads())
B = sorted(reads())
C = sorted(reads())

numB = [0] * N
for i in range(N):
  numB[i] = N - bisect_left(C, B[i]+1)
print(numB, file=stderr)

anumB = [0] * (N+1)
for i in range(N):
  anumB[i+1] = anumB[i] + numB[i]
print(anumB, file=stderr)

numA = [0] * N
for i in range(N):
  numA[i] = anumB[N] - anumB[bisect_left(B, A[i]+1)]
print(numA, file=stderr)

print(sum(numA))

Submission status: Accepted

Solution code:
import bisect
n = int(input())
As = list(map(int, input().split()))
Bs = sorted(list(map(int, input().split())))
Cs = sorted(list(map(int, input().split())))

cnt = [0]*(n+1)
v = 0
for idx, b in enumerate(Bs[::-1]):
    v += len(Cs) - bisect.bisect_right(Cs, b)
    cnt[n-1-idx] = v

print(sum([cnt[bisect.bisect_right(Bs, a)] for a in As]))

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
n = int(input())
a, b, c = [sorted(list(map(int, input().split()))) for i in range(3)]
ans = 0
for i in range(n):
  j = bisect_left(a, b[i])
  k = bisect_right(c, b[i])
  ans += j*(n-k)
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()))
answer=0

#それぞれのリストを小さい順に並べる
A.sort()
B.sort()
C.sort()

#ソートしたA[i]に対してそれより大きいBの要素数
Acount=[]
#同様にB[i]に対してC
Bcount=[]
#Bcountを後ろから取った累積和
Bsum=[]

count=0
for i in range(N):
  while(count!=N and A[i]>=B[count]): 
    count+=1
  Acount.append(N-count)

count=0
for i in range(N):
  while(count!=N and B[i]>=C[count]):
    count+=1
  Bcount.append(N-count) #いらんかった
  
Bsum.append(Bcount[N-1])



for i in range(1,N):
  Bsum.append(Bcount[N-i-1]+Bsum[i-1])

for i in range(N):
  if Acount[i]==0:
    print(answer)
    exit()
  answer+=Bsum[Acount[i]-1]
print(answer)

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(map(int, input().split()))
b = sorted(map(int, input().split()))
c = sorted(map(int, input().split()))
p = 0
ans = 0
f = []
g = [0] * n
for i, x in enumerate(b):
    while p < n and x >= c[p]:
        p += 1
    f.append(n - p)
for i in range(n - 1, -1, -1):
    g[i] = f[i] + (g[i + 1] if i < n - 1 else 0)
p = 0
for i, x in enumerate(a):
    while p < n and x >= b[p]:
        p += 1
    ans += g[p] if p < n else 0
print(ans)

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