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:
n = int(input())
a = [0] + list(map(int, input().split())) #index alliginment
b = [0] + list(map(int, input().split()))
c = [0] + list(map(int, input().split()))
a.sort()
b.sort()
c.sort()
bp, cp = 1, 1
cana, canb = [0], [0] # a[i]に対して条件を満たすbの要素数と
                      # b[i]に対して条件を満たすcの要素数を保管
sumb = [0] # canbの末尾からn個の累積和
ans = 0
for i in range(1,n+1): # aの1~nまで
    if bp != n+1:
        while b[bp] <= a[i] and bp != n+1:
            bp += 1
            if bp == n+1:
                break
    appending = n+1-bp if bp != n+1 else 0
    cana.append(appending)
for i in range(1,n+1):
    if cp != n+1:
        while c[cp] <= b[i] and cp != n+1:
            cp += 1
            if cp == n+1:
                break
    appending = n+1-cp if cp != n+1 else 0
    canb.append(appending)
for i in range(1,n+1):
    sumb.append(sumb[-1]+canb[-i])
for i in range(1,n+1):
    ans += sumb[cana[i]]
print(ans)

Submission status: Accepted

Solution code:
# https://qiita.com/_-_-_-_-_/items/34f933adc7be875e61d0
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = map(int,input().split())   |	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = list(map(int,input().split()))	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]

from collections import defaultdict, Counter
from math import pi, sqrt
from bisect import bisect_left, bisect_right

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

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

cnt = 0
for bi in b:
    alen = bisect_left(a, bi)
    clen = bisect_right(c, bi)
    if alen > 0 and clen < len(c):
        cnt += alen * (n - clen)

print(cnt)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right

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 b in B:
    i = bisect_left(A, b)
    j = bisect_right(C, b)
    if i - 1 >= 0 and N - j:
        ans += i * (N - j)

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_n = [0] * n
b_n = [0] * n
for i in range(n):
    a_n[i] = bisect.bisect_right(b, a[i])
for i in range(n):
    b_n[i] = n - bisect.bisect_right(c, b[i])
ans = 0
b_n_t = [0]
for i in range(n):
    b_n_t.append(b_n[i] + b_n_t[i])
for i in a_n:
    ans += b_n_t[-1] - b_n_t[i]
print(ans)

Submission status: Accepted

Solution code:
def func1():
    i = 0
    j = 0
    p = [0] * N
    while True:
        if B[i] > A[j]:
            j += 1
            if j == N:
                while i < N:
                    p[i] = N
                    i += 1
                return p
        else:
            while B[i] <= A[j]:
                p[i] = j
                i += 1
                if i == N:
                    return p


def func2(p):
    i = 0
    j = 0
    point = 0
    q = [0] * N
    while True:
        if C[i] > B[j]:
            point += p[j]
            j += 1
            if j == N:
                while i < N:
                    q[i] = point
                    i += 1
                return q
        else:
            while C[i] <= B[j]:
                q[i] = point
                i += 1
                if i == N:
                    return q


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)

p = func1()
q = func2(p)
print(sum(q), '\n')

Submission status: Accepted

Solution code:
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()], reverse=True)

a.append(10**9)

check_a = 0
check_c = n - 1
ans = 0
for i in b:
    while a[check_a] < i and check_a < n :
        check_a += 1
    while c[check_c] <= i and check_c >= 0:
        check_c -= 1
    ans += check_a * (check_c + 1)
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left,bisect_right
n=int(input())
A=sorted(map(int,input().split()))
B=list(map(int,input().split()))
C=sorted(map(int,input().split()))
ans=0
for i in range(n):
    ans+=(bisect_left(A,B[i])*(n-bisect_right(C,B[i])))
print(ans)

Submission status: Accepted

Solution code:
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 = 0
c = 0
ans = 0
for b in B:
  while a < N and A[a] < b:
    a += 1
  while c < N and C[c] <= b:
    c += 1
  ans += a * (N - c)
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())),reverse=True)
ab=[0 for _ in range(N)]
bc=[0 for _ in range(N)]

aind=0
cind=0
for i in range(N):
    b1=B[i]
    while aind<N and A[aind]<b1:
        aind+=1
    ab[i]=aind

    b2=B[-i-1]
    while cind<N and b2 < C[cind]:
        cind+=1
    bc[-i-1]=cind
print(sum([ab[i]*bc[i] for i in range(N)]))

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
n = int(input())
*a, = sorted(map(int, input().split()))
*b, = sorted(map(int, input().split()))
*c, = sorted(map(int, input().split()))

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

i = 0
j = 0
ans = 0

for b in B:
    while i < N and A[i] < b:
        i += 1
    while j < N and C[j] <= b:
        j += 1
    ans += i * (N - j)

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# Luis Yoichi Morales January 2019

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

x=0
z=0
res=0

for y in B:
    while x<N and A[x]<y:
        x+=1
    while z<N and C[z]<=y:   #org
        z+=1
    res+=x*(N-z)   #org
print res

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 range(N):
    a = bisect.bisect_left(A,B[i])
    c = bisect.bisect_right(C,B[i])
    ans += a*(len(C)-c)
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(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
c_cnt = [1 for i in range(N)]

def count(A, B, b_cnt):
    B.append(-1)
    a_cnt = [0 for i in range(N)]
    i, j = 0, 0
    while i < N:
        if A[i] < B[j]:
            a_cnt[i] += b_cnt[j]
            j += 1
        else:
            i += 1
            if 1 <= i < N:
                a_cnt[i] = a_cnt[i-1]
    return a_cnt

b_cnt = count(B, C, c_cnt)
a_cnt = count(A, B, b_cnt)
print(sum(a_cnt))

Submission status: Accepted

Solution code:
n = int(input())
u = list(map(int,input().split()))
m = list(map(int,input().split()))
d = list(map(int,input().split()))
u.sort()
m.sort()
d.sort()

ucnt = 0
dcnt = 0
ans = 0
for i in range(n):
    for j in range(ucnt,n):
        if u[j] < m[i]:
            ucnt += 1
        else:
            break
        
    for j in range(dcnt,n):
        if d[j] <= m[i]:
            dcnt += 1
        else:
            break

    ans += ucnt * (n-dcnt)
    
print(ans)

Submission status: Accepted

Solution code:
import bisect

n = int(input())
u = list(map(int,input().split()))
m = list(map(int,input().split()))
d = list(map(int,input().split()))
u.sort()
m.sort()
d.sort()

ans = 0
for a in m:
    ans += bisect.bisect_left(u,a) * (n - bisect.bisect_right(d,a))
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 b in B:
    ans += (bisect.bisect_left(A, b)) * (n - bisect.bisect_right(C, b))
    
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 b in B:
    x = bisect.bisect_left(A, b)
    y = bisect.bisect_right(C, b)
    ans += x * (n - 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())))

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:
import bisect
n = int(input())
al = [int(a) for a in input().split()]
bl = [int(b) for b in input().split()]
cl = [int(c) for c in input().split()]

al.sort()
bl.sort()
cl.sort()

k = 0
for b in bl:
    k += bisect.bisect_left(al, b) * (n - bisect.bisect_right(cl, b))
print(k)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
 
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()
 
Ac, Cc = [0]*n, [0]*n
# Ap, Cp = 0, 0
 
for i in range(n):
    Ac[i] = bisect.bisect_left(A, B[i])#[Ap:], B[i])
    # Ac[i], Ap = index + Ap, index + Ap
    Cc[i] = n - bisect.bisect_right(C, B[i])#[Cp:], B[i])
    # Cc[i], Cp = n - index - Cp, index + Cp
 
 
count = 0
for i in range(n):
    count += Ac[i]*Cc[i]
 
print(count)

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

ba = [None]*N
bc = [None]*N

for i in range(N):
    ba[i] = bisect.bisect_left(A, B[i])
for i in range(N):
    bc[i] = bisect.bisect_right(C, B[i])

ans = 0
for i in range(N):
    ans += ba[i]*(N-bc[i])
print(ans)

Submission status: Accepted

Solution code:
def lma():return list(map(int, input().split()))#x or y
a = int(input())
k=[]
s=[]
c=0
s= [lma()]
x2= lma()
x3 = lma()
s.append(x2)
s.append(x3)
res =0
r =0
for j in range(3):
    for i in range(a):
        k.append([j,s[j][i]])
k.sort(key=lambda x: x[1], reverse=True)
for i in range(a*3):
    if k[i][0] == 2:
       c+=1
    elif k[i][0] == 1:
       r+=c 
    else:
       #res+=sum(co[:i])
        res+=r
print(res)

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

print(ans)

Submission status: Accepted

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

def bis(x,y):
    low = 0
    high = N
    while low < high:
        mid = (low + high) // 2
        if x[mid] < y: low = mid + 1
        else: high = mid
    return low

total = 0
for b in B:
    total += bis(A,b) * (N - bis(C,b))

print(total)

Submission status: Accepted

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

def bis(x,y):
    low = 0
    high = N
    while low < high:
        mid = (low + high) // 2
        if x[mid] < y:
            low = mid + 1
        else:
            high = mid
    return low

total = 0
for b in B:
    total += bis(A,b) * (N - bis(C,b))

print(total)

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

total = 0

for i in range(N):
    low1 = 0
    high = N
    while low1 < high:
        mid = (low1 + high) // 2
        if A[mid] < B[i]:
            low1 = mid + 1
        else:
            high = mid
    low2 = 0
    high = N
    while low2 < high:
        mid = (low2 + high) // 2
        if C[mid] <= B[i]:
            low2 = mid + 1
        else:
            high = mid
    total += low1 * (N - low2)

print(total)

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

total = 0

for i in range(N):
    low = 0
    high = N 
    while low < high:
        mid1 = (low + high) // 2
        if A[mid1] < B[i]:
            low = mid1 + 1
        else:
            high = mid1
    mid1 = low
    low = 0
    high = N
    while low < high:
        mid2 = (low + high) // 2
        if C[mid2] > B[i]:
            high = mid2
        else:
            low = mid2 + 1
    mid2 = low
    total = total + mid1 * (N - mid2)

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

print(sum)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
#A = [int(x) for x in input().split()]
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort()
A = [-float('inf')] + A + [float('inf')]
B.sort()
C.sort()
C = [-float('inf')] + C + [float('inf')]

total = 0

for i in range(N):
    low = 0
    high = N + 1
    while low + 1 < high:
        mid1 = (low + high) // 2
        if A[mid1] < B[i]:
            low = mid1
        else:
            high = mid1
    mid1 = low
    low = 0
    high = N + 1
    while low + 1 < high:
        mid2 = (low + high) // 2
        if C[mid2] <= B[i]:
            low = mid2
        else:
            high = mid2
    mid2 = high
    total = total + mid1 * (N + 1 - mid2)

print(total)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
#A = [int(x) for x in input().split()]
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort()
A = [-float('inf')] + A + [float('inf')]
B.sort()
C.sort()
C = [-float('inf')] + C + [float('inf')]

total = 0

for i in range(N):
    low = 0
    high = N + 1
    while high - low > 1:
        mid1 = (low + high) // 2
        if A[mid1] < B[i]:
            low = mid1
        else:
            high = mid1
    mid1 = low
    low = 0
    high = N + 1
    while high - low > 1:
        mid2 = (low + high) // 2
        if C[mid2] <= B[i]:
            low = mid2
        else:
            high = mid2
    mid2 = high
    total += mid1 * (N + 1 - mid2)

print(total)

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 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_left,bisect_right
n = int(input())
a = sorted(list(int(i) for i in input().split()))
b = sorted(list(int(i) for i in input().split()))
c = sorted(list(int(i) for i in input().split()))
ans = 0

for j in range(n):
  a1 = bisect_left(a,b[j])
  a2 = bisect_right(c,b[j])
  ans += a1 *(n-a2)
print(ans)

Submission status: Accepted

Solution code:
import bisect

n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
a.sort()
c.sort()
ans = 0
for i in range(n):
    a_num = bisect.bisect_left(a,b[i])
    c_num = n - bisect.bisect_right(c,b[i])

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

Submission status: Accepted

Solution code:
def isOK(X, index, aim, mode):
    if mode==1:
        if X[index] < aim:
            return True
        else:
            return False
    else:
        if X[index] > aim:
            return True
        else:
            return False

def bs(X, aim, mode):
    left=-1
    right=len(X)

    while right-left>1:
        mid = left + int((right-left)/2)
        if mode == 1:
            if isOK(X, mid, aim, mode):
                left=mid
            else:
                right=mid
        else:
            if isOK(X, mid, aim, mode):
                right=mid
            else:
                left=mid
    if mode==1:
        return left
    else:
        return right

N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
A.sort()
B.sort()
C.sort()
ans=0
for j in range(N):
    piva=bs(A, B[j], 1)
    pivc=bs(C, B[j], 2)
    ans+=(piva+1)*(len(C)-pivc)
print(str(ans))

Submission status: Accepted

Solution code:
import bisect as bis

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

	count = 0
	for i in b :
		n = bis.bisect_left(a,i)
		m = len(c) - bis.bisect_right(c,i)
		count += n*m

	print(count)

main()

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())))
counta = 0
countc = 0
count = 0
for i in range(n):
    countc = n  - bisect.bisect_right(c,b[i])
    counta = bisect.bisect_left(a,b[i])
    count += countc*counta
print(count)

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 = sorted(a)
b = sorted(b)
c = sorted(c)

import bisect
count = 0
for i in b:
    a_count = bisect.bisect_left(a, i)
    c_count = n - bisect.bisect_right(c, i)
    count += a_count * c_count
    
print(count)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right


def main():
    n = int(input())
    aa = sorted(map(int, input().split()))
    bb = sorted(map(int, input().split()))
    cc = sorted(map(int, input().split()))
    res = 0
    for b in bb:
        i_a = bisect_left(aa, b)
        i_c = bisect_right(cc, b)
        res += i_a * (n - i_c)
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/mnt/c/Users/moiki/bash/env/bin/python
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
A2 = []
B2 = []
C2 = []
# for a,b,c in zip(A,B,C):
#     bisect.insort_left(A2, a)
#     # bisect.insort_left(B2, b)
#     bisect.insort_left(C2, c)
# print(A,B,C)
# import numpy as np

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

# for c in C:
#     for b in [x for x in B if x < c]:
#         for a in [x for x in A if x < b]:

print(sum)

Submission status: Accepted

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

ans=0

for i in B:
  sml_B = bisect_left(A,i)
  # print(i,"sml_B",sml_B)
  big_B = len(C)-bisect_right(C,i)
  # print(i,"big_B",big_B)
  ans+=sml_B*big_B
print(ans)

Submission status: Accepted

Solution code:
import bisect
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())))
ans = 0
for b in b_list:
    s = bisect.bisect_left(a_list, b)
    t = len(c_list) - bisect.bisect_right(c_list, b) 
    ans += s*t
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 i in range(n):
  x=bisect.bisect_left(a,b[i])
  y=len(c)-bisect.bisect(c,b[i])
  ct+=x*y
print(ct)

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())))
m = len(c)
ans = 0
for x in b:
    #挿入できる箇所を返す
    p = bisect.bisect_left(a, x)
    q = m - bisect.bisect_right(c, x)
    ans += p*q
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
D = []
D += [[i,0]  for i in list(map(int,input().split()))]
D += [[i,1]  for i in list(map(int,input().split()))]
D += [[i,2]  for i in list(map(int,input().split()))]
D = sorted(D,key=lambda x: x[0])

up_count=0
up_buf = 0
down_count=N
down_buf= 0
middle_buf = 0
ans = 0
prev_val = D[0][0]
for d in D:
    if d[0]==prev_val:
        if d[1]==0:
            up_buf+=1
        if d[1]==1:
            middle_buf += 1
        if d[1]==2:
            down_buf+=1
    else:
        ans += middle_buf*(down_count-down_buf)*up_count
        up_count+=up_buf
        up_buf=0
        down_count-=down_buf
        down_buf=0
        middle_buf=0
        prev_val = d[0]
        if d[1]==0:
            up_buf+=1
        if d[1]==1:
            middle_buf += 1
        if d[1]==2:
            down_buf+=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 b in B:
    ai = bisect.bisect_left(A, b)
    ci = N - bisect.bisect_right(C, b)
    ans += ai * ci

print(ans)

Submission status: Accepted

Solution code:
import math
import sys
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 = sorted(A)
B = sorted(B)
C = sorted(C)

BAcnt = [0] * N


for i in range(N):
    BAcnt[i] = bisect.bisect_left(A, B[i])

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

ret = 0
for i in range(N):
    j = bisect.bisect_left(B, C[i]) - 1
    if len(BAruiseki) > j and j>=0:
        ret += BAruiseki[j]


print(ret)

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

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

Submission status: Accepted

Solution code:
import bisect
N = int(input())
A = sorted([int(_) for _ in input().split()])
B = [int(_) for _ in input().split()]
C = sorted([int(_) for _ 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:
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()
#print(a,b,c)
x=0
for i in b:
    j=bisect.bisect_left(a,i)
    k=bisect.bisect_right(c,i)
    #print(j,k)
    x+=j*(n-k)
print(x)

Submission status: Accepted

Solution code:
n = int(input())
# a:上段,b:中段,c:下段
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()])
"""
a = [_a[0]]
for i in range(1, n):
    if _a[i] != _a[i-1]:
        a.append(_a[i])
b = [_b[0]]
for i in range(1, n):
    if _b[i] != _b[i-1]:
        b.append(_b[i])
c = [_c[0]]
for i in range(1, n):
    if _c[i] != _c[i-1]:
        c.append(_c[i])
"""
aCount = [0 for i in range(len(a))]
bCount = [0 for i in range(len(b))]
# print(a)
# print(b)
# print(c)
#まず各bに対し、選択可能なcの個数を計算O(n)
k = len(c) - 1
for j in reversed(range(len(b))):
    if j < len(b) - 1:
        bCount[j] = bCount[j+1]
    while k >= 0 and b[j] < c[k]:
        bCount[j] = len(b) - k
        # print("bCount[{}]:{}".format(j, bCount[j]))
        k -= 1
# print(bCount)
k = len(b) - 1
for j in reversed(range(len(a))):
    if j < len(a) - 1:
        aCount[j] = aCount[j+1]
    while k >= 0 and a[j] < b[k]:
        # aCount[j] = len(a) - k
        aCount[j] += bCount[k]
        # print("bCount[{}]:{}".format(j, bCount[j]))
        k -= 1
print(sum(aCount))

Submission status: Accepted

Solution code:
# encoding: utf-8
import sys

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

### TLE test in NlogN ###

# num of Ai each Bi can lift
capacity_B = [0] * N
idx_A = 0
idx_B = 0
for i in range(2 * N):
    if idx_B == N: break
    elif idx_A == N:
        for i in range(idx_B, N): capacity_B[i] = N
        break # oh...
    elif A[idx_A] >= B[idx_B]:
        capacity_B[idx_B] = idx_A
        idx_B += 1
    else:
        idx_A += 1
        
# print("#", capacity_B)

# num of Bi each Ci can lift
capacity_C = [0] * N
idx_B = 0
idx_C = 0
for i in range(2 * N):
    if idx_C == N: break
    elif idx_B == N:
        for i in range(idx_C, N): capacity_C[i] = N
        break # oh...
    elif B[idx_B] >= C[idx_C]:
        capacity_C[idx_C] = idx_B
        idx_C += 1
    else:
        idx_B += 1

# print("#", capacity_C)

# sum of [num of (Ai, Bi) each Ci can lift]
ans = 0
idx_AB = 0
idx_BC = 0
sum_tmp = 0
for i in range(2 * N):
    if idx_BC == N: break
    elif idx_AB == N:
        for i in range(idx_BC, N): ans += sum_tmp
        break # oh...
    elif idx_AB >= capacity_C[idx_BC]:
        ans += sum_tmp
        idx_BC += 1
    else:
        sum_tmp += capacity_B[idx_AB]
        idx_AB += 1

# print("#", stacked)

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

Submission status: Accepted

Solution code:
n = int(input())
#a = [list(map(int, input().split())) + [i] for i in range(n)]
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
a = []
b = []
c = []
for i in range(n):
    a.append([])
    b.append([])
    c.append([])
for i in range(n):
    a[i].append(A[i])
    a[i].append(2)
    b[i].append(B[i])
    b[i].append(1)
    c[i].append(C[i])
    c[i].append(0)
d = a + b + c
d.sort(key=lambda x:(x[0], x[1]))
lenli = []
allli = []
count = 0
for i in range(len(d)):
    if d[i][1] == 2:#a
        if lenli == []:
            lenli.append(1)
        else:
            lenli[-1] += 1
    elif d[i][1] == 1:#b
        if allli == [] and lenli != []:
            allli.append(lenli[-1])
            allli.append(allli[-1])
            lenli.append(lenli[-1])
        elif allli == [] and lenli == []:
            pass
        else:
            allli[-1] += lenli[-1]
            allli.append(allli[-1])
            lenli.append(lenli[-1])
    elif d[i][1] == 0:#c
        if allli == []:
            count += 0
        else:
            count += allli[-2]

print(count)

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
p=0
q=0

for i in b:
    while p<n and a[p]<i:
        p+=1
    while q<n and c[q]<=i:
        q+=1
    ans+=p*(n-q)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=[list(map(int,input().split()))for i in range(3)]
s[0].sort()
s[1].sort()
s[2].sort()
c,d,e=0,0,0
for i in range(n):
    while d<n and s[0][d]<s[1][i]:
        d+=1
    while e<n and s[2][e]<=s[1][i]:
        e+=1
    c+=d*(n-e)
print(c)