Problem:


Score : 300 points

Problem Statement

The season for Snuke Festival has come again this year. First of all, Ringo will perform a ritual to summon Snuke. For the ritual, he needs an altar, which consists of three parts, one in each of the three categories: upper, middle and lower.

He has N parts for each of the three categories. The size of the i-th upper part is A_i, the size of the i-th middle part is B_i, and the size of the i-th lower part is C_i.

To build an altar, the size of the middle part must be strictly greater than that of the upper part, and the size of the lower part must be strictly greater than that of the middle part. On the other hand, any three parts that satisfy these conditions can be combined to form an altar.

How many different altars can Ringo build? Here, two altars are considered different when at least one of the three parts used is different.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9(1\leq i\leq N)

1 \leq B_i \leq 10^9(1\leq i\leq N)

1 \leq C_i \leq 10^9(1\leq i\leq N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N
B_1 ... B_N
C_1 ... C_N

Output

Print the number of different altars that Ringo can build.

Sample Input 1

2
1 5
2 4
3 6

Sample Output 1

3

The following three altars can be built:

Upper: 1-st part, Middle: 1-st part, Lower: 1-st part

Upper: 1-st part, Middle: 1-st part, Lower: 2-nd part

Upper: 1-st part, Middle: 2-nd part, Lower: 2-nd part

Sample Input 2

3
1 1 1
2 2 2
3 3 3

Sample Output 2

27

Sample Input 3

6
3 14 159 2 6 53
58 9 79 323 84 6
2643 383 2 79 50 288

Sample Output 3

87



Sample input:
2
1 5
2 4
3 6


Sample output:
3

Submission status: Accepted

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    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()]
    C.append(10 ** 10)
    A.sort()
    B.sort()
    C.sort()
    B_choise = [0] * N
    C_i = 0
    for i, b in enumerate(B):
        while C[C_i] <= b: C_i += 1
        B_choise[i] = N - C_i
    B_choise_sum = [0] * (N + 1)
    for i in reversed(range(N)):
        B_choise_sum[i] = B_choise_sum[i+1] + B_choise[i]
    
    B.append(10 ** 10)
    B_i = 0
    Ans = 0
    for i, a in enumerate(A):
        while B[B_i] <= a: B_i += 1
        Ans += B_choise_sum[B_i]
    print(Ans)
  
    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    B = [int(i) for i in input().split()]
    C = [int(i) for i in input().split()]
    A.sort()
    B.sort()
    C.sort()
    # print(A, B, C, sep="\n")
    from bisect import bisect
    ans = []
    ans2 = []
    for a in A:
        b = bisect(B, a)
        ans.append(N-b)
    for b in B:
        c = bisect(C, b)
        ans2.append(N-c)
    from itertools import accumulate
    ans2 = list(accumulate([0] + ans2[::-1]))
    answer = 0
    for a in ans:
        answer += ans2[a]
    print(answer)
    # print(ans)
    # print(ans2)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array(input().split(), dtype=np.int32)
B = np.array(input().split(), dtype=np.int32)
C = np.array(input().split(), dtype=np.int32)

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

ab = np.searchsorted(A, B, side="left")
bc = N - np.searchsorted(C, B, side="right")
ans = (ab*bc).sum()
print(ans)

Submission status: Accepted

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

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

import bisect
a.sort()
c.sort()
ans = 0

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

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

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

print(ans)

Submission status: Accepted

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

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

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

# a < b < c
from bisect import bisect_right

Bcnt = [0] * N
for i,b in enumerate(B):
    r = bisect_right(C, b)
    Bcnt[i] = N - r

from itertools import accumulate

Bcnt = list(accumulate([0] + Bcnt))

Acnt = [0] * N
for i,a in enumerate(A):
    r = bisect_right(B, a)
    Acnt[i] = Bcnt[N] - Bcnt[r]

print(sum(Acnt))

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

Submission status: Accepted

Solution code:
import bisect
# from collections import Counter, deque
# from copy import copy, deepcopy
# from fractions import gcd
# from functools import reduce
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np
# from operator import xor
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline

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

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

    print(cnt)





resolve()

Submission status: Accepted

Solution code:
from collections import *
from heapq import *
from itertools import *
from fractions import gcd
import sys
from decimal import *
import copy
from bisect import *
input=lambda :sys.stdin.readline().rstrip()

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

lstB=[0 for i in range(N)]
a=0
c=N-1
count=0
#print(A)
#print(B)
#print(C)

for b in range(N)[::-1]:
    while c>=0 and C[c]>B[b]:
        c-=1
    lstB[b]=N-(c+1)
b=N-1
lstA=[0 for i in range(N+1)]
for a in range(N)[::-1]:
    lstA[a]+=lstA[a+1]
    while b>=0 and B[b]>A[a]:
        lstA[a]+=lstB[b]
        b-=1


#print(lstB)
#print(lstA)
print(sum(lstA))

Submission status: Accepted

Solution code:
from bisect import bisect_left
from bisect import bisect_right
N = int(input())
As = sorted(list(map(int, input().split())))
Bs = sorted(list(map(int, input().split())))
Cs = sorted(list(map(int, input().split())))


cnt = 0
for b in Bs:
    ind_c = bisect_left(Cs,b+1)
    ind_a = bisect_right(As,b-1)
    if ind_c == N:continue
    # print(b,ind_a,ind_c)
    cnt += (N - ind_c) * (ind_a)
print(cnt)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
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
def ab_check(list_count,i):
    ok=-1
    ng=n
    B=b[i]
    while ng-ok>1:
        mid=(ok+ng)//2
        if list_count[mid]<B:
            ok=mid
        else:
            ng=mid
    #print(ok+1,'A')
    return ok+1

def bc_check(list_count,i):
    ok=n
    ng=-1
    B=b[i]
    while ok-ng>1:
        mid=(ok+ng)//2
        if list_count[mid]>B:
            ok=mid
        else:
            ng=mid
    #print(n-ok,'B')
    return n-ok
    #print(i,ok)

for i in range(n):
    ans+=ab_check(a,i)*bc_check(c,i)
print(ans)
#print(a,c,sep='\n')

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: #O(N)
    s = bisect.bisect_left(A,b) #O(logN)
    t = bisect.bisect_right(C,b) #O(logN)
    #print(s,N-t,s*(N-t))
    ans += s*(N-t)
   
print(ans)

Submission status: Accepted

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

Bcnt = [0]*(N+1)
idx = N
for i in range(N-1,-1,-1):
    idx = bisect_right(C,B[i],hi=idx)
    Bcnt[i] = N-idx+Bcnt[i+1]

ans = 0
idx = 0
for i in range(N):
    idx = bisect_right(B,A[i],lo=idx)
    ans += Bcnt[idx]

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

import bisect

ret = 0
for b in B:
    i = bisect.bisect_left(A, b)
    j = bisect.bisect_right(C, b)

    ret += i * (N - j)
    # print(i, (N - j))

print(ret)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  n = int(input())
  la = list(map(int, input().split()))
  lb = list(map(int, input().split()))
  lc = list(map(int, input().split()))
  la.sort(), lb.sort(), lc.sort()
  
  i = 0
  ab = []
  for b in lb:
    while i < n and la[i] < b:
      i += 1
    ab.append(i)
      
  i = 0
  bc = []
  for c in lc:
    while i < n and lb[i] < c:
      i += 1
    bc.append(i)
    
  ans = 0
  cs = [0]
  for x in ab:
    cs.append(cs[-1] + x)
  for y in bc:
    ans += cs[y]
  print(ans)
  

if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
import sys
import os
import re
import datetime
import bisect

####### main #######

N = int(input())

partsNumA = sorted(map(int, input().split()))
partsNumB = sorted(map(int, input().split()))
partsNumC = sorted(map(int, input().split()))

# Bに対してAの個数のBによるループ
partsNumSumBa = []
sumA = 0
for curPartsB in partsNumB:
    indexA = bisect.bisect_left(partsNumA, curPartsB)
    numA = indexA
    sumA += numA
    partsNumSumBa.append(sumA)

# Cに対してBの個数のCによるループ
sumBAll = 0
for curPartsC in partsNumC:
    indexB = bisect.bisect_left(partsNumB, curPartsC)
    if indexB == 0:
        continue
    numB = partsNumSumBa[indexB - 1]
    sumBAll += numB

print(sumBAll)

Submission status: Accepted

Solution code:
import sys
import os
import re
import datetime
import bisect

def Test(value):
    a = [1,3,5]
    #x = 4

    insert_index = bisect.bisect_left(a, value)
    
    print("insert_index = ", insert_index)
    
if False:   
    while(True): 
        Test(int(input('? ')));
    sys.exit()
 
N = int(input())

partsNumA = sorted(map(int, input().split()))
partsNumB = sorted(map(int, input().split()))
partsNumC = sorted(map(int, input().split()))

# Bに対してAの個数のBによるループ
partsNumSumBa = []
sumA = 0
#for i in range(N):
for curPartsB in partsNumB:
    indexA = bisect.bisect_left(partsNumA, curPartsB)
    numA = indexA
    sumA += numA
    partsNumSumBa.append(sumA)

if False:
    print("Sorted A" , partsNumA)
    print("Sorted B" , partsNumB)
    print("Sorted C" , partsNumC)

    print("B loop" , partsNumSumBa)

# Cに対してBの個数のCによるループ
sumBAll = 0
for curPartsC in partsNumC:
    indexB = bisect.bisect_left(partsNumB, curPartsC)
    if indexB == 0:
        continue
    numB = partsNumSumBa[indexB - 1]
    sumBAll += numB
        
print(sumBAll)

Submission status: Accepted

Solution code:
from bisect import bisect_right,bisect_left
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:
    a_index=bisect_left(A,b)
    c_index=bisect_right(C,b)
    ans +=a_index*(n-c_index)

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=list(map(int,input().split()))
C=sorted(list(map(int,input().split())))

ans=0
for b in B:
    index_a=bisect_left(A,b)
    index_c=bisect_right(C,b)
    ans +=index_a*(n-index_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()

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

print(total)

Submission status: Accepted

Solution code:
from bisect import bisect_right

if __name__ == "__main__":
    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

    # まずあるbを選んだ時のcの組み合わせの数のリストを作る。
    clist = [0] * n
    clist[n-1] = n - bisect_right(c, b[n-1])
    for i in range(n-2, -1, -1):
        clist[i] = n - bisect_right(c, b[i]) + clist[i+1]

    for aa in a:
        b_pos = bisect_right(b, aa)
        if b_pos != n:
            ans += clist[b_pos]

    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())))
ali = [0] * (N+1)
bli = [N - bisect.bisect_left(C,B[i]+1) for i in range(N)]
for i in range(N):
    ali[i+1] = ali[i] + bli[i]
print(sum([ali[N]-ali[bisect.bisect_left(B,A[i]+1)] for i in range(N)]))

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
C = sorted(list(map(int, input().split())))
ad = defaultdict(lambda: -1)
i, j = 0, 0
while i < N and j < N:
    if A[i] < B[j]:
        ad[i] = j
        i += 1
    else:
        j += 1
bd = defaultdict(lambda: -1)
i, j = 0, 0
while i < N and j < N:
    if B[i] < C[j]:
        bd[i] = j
        i += 1
    else:
        j += 1
csum = [0] * N
for i in range(N):
    if bd[i] >= 0:
        csum[i] = N-bd[i]
bsum = [0]* N
cnt = 0
for i in range(N):
    cnt += csum[N-1-i]
    bsum[N-1-i] = cnt
ans = 0
for i in range(N):
    if ad[i] >= 0:
        ans += bsum[ad[i]]
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 b in B:
    a = bisect.bisect_left(A,b)
    c = bisect.bisect_left(C,-b)
    #print(a,c)
    ans += a*c
print(ans)

Submission status: Accepted

Solution code:
import bisect

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

cnt = 0
for b in Bs:
    a_b = bisect.bisect_left(As,b)
    b_c = N - bisect.bisect_right(Cs, b)
    
    cnt += a_b * b_c
print(cnt)

Submission status: Accepted

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


def main():
    n = int(readline())
    a = list(map(int,readline().rsplit()))
    b = list(map(int,readline().rsplit()))
    c = list(map(int,readline().rsplit()))
    a_s = sorted(a)
    c_s = sorted(c)
    ans = 0
    for i in b:
        d = bisect.bisect_left(a_s, i)
        e = n - bisect.bisect_right(c_s, i)
        ans += d * e
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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


def main():
    n = int(readline())
    a = list(map(int,readline().rsplit()))
    b = list(map(int,readline().rsplit()))
    c = list(map(int,readline().rsplit()))
    a.sort()
    c.sort()
    ans = 0
    for i in b:
        d = bisect.bisect_left(a, i)
        e = n - bisect.bisect_right(c, i)
        ans += d * e
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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


def main():
    n = int(readline())
    a = list(map(int,readline().rsplit()))
    b = list(map(int,readline().rsplit()))
    c = list(map(int,readline().rsplit()))
    a.sort()
    c.sort()
    ans = 0
    for i in b:
        d = bisect.bisect_left(a, i)
        e = len(c) - bisect.bisect_right(c, i)
        ans += d * e
    print(ans)

if __name__ == '__main__':
    main()

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()
c.sort()
ans=0

for i in range(n):
    a_idx=bisect_left(a,b[i])
    c_idx=bisect_right(c,b[i])
    ans+=a_idx*(n-c_idx)
print(ans)

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

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

z = list(accumulate(k[::-1]))[::-1] + [0]

ans = 0
j = 0
for i in range(N):
    while j != N and A[i] >= B[j]:
        j += 1
    ans += z[j]

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=sorted(a)
c=sorted(c)
import bisect
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())
la=list(map(lambda x:[int(x),0],input().split()))
lb=list(map(lambda x:[int(x),1],input().split()))
lc=list(map(lambda x:[int(x),2],input().split()))
l=la+lb+lc
l.sort(key=lambda x:(x[0],-x[1]))
na=0
sumb=0
ans=0
for i in range(3*N):
    if l[i][1]==0:
        na+=1
    elif l[i][1]==1:
        sumb+=na
    else:
        ans+=sumb
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 mid in b:
    ind_t = bisect.bisect_left(a,mid)
    ind_b = bisect.bisect_right(c,mid)
    ans+=ind_t*(n-ind_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
rui=[0]
sm=0
for mid in b:
    ind = bisect.bisect_left(c,mid+1)
    sm+= (n-ind)
    rui.append(sm)

ans=0
for top in a:
    ind = bisect.bisect_left(b,top+1)
    tmp = rui[-1] - rui[ind]
    ans+=tmp
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()
C.sort()
ans = 0
for i in range(0, N):
    ans += (bisect_left(A, B[i])*(N-bisect_right(C, B[i])))
print(ans)

Submission status: Accepted

Solution code:
import bisect

N = int(input())
[A, B, C] = [sorted([int(i) for i in input().split()]) for j in range(3)]
ans = 0
for j in B:
	ans += bisect.bisect_left(A, j) * (N-bisect.bisect_right(C, j))
print(ans)

Submission status: Accepted

Solution code:
def sup(A, j):
	if A[0] >= j:
		return 0
	l = 0
	r = len(A) - 1
	while True:
		i = (l+r) // 2
		if i == len(A) - 1 and A[i] < j:
			return len(A)
		elif A[i]<j and A[i+1]>=j:
			return i+1
		else:
			if A[i] >= j:
				r = i-1
			else: # A[i+1] < j
				l = i+1		

def inf(A, j): 
    if A[-1] <= j:
        return 0
    l = 0
    r = len(A) - 1
    while True:
        i = (l+r) // 2
        if i == 0 and A[i]>j:
            return len(A)
        elif A[i]>j and A[i-1]<=j:
            return len(A)-i
        else:
            if A[i] <= j:
                l = i+1
            else: # A[i-1] > j
                r = i-1		

N = int(input())
[A, B, C] = [sorted([int(i) for i in input().split()]) for j in range(3)]
ans = 0
for j in B:
	ans += sup(A, j) * inf(C, j)
print(ans)

Submission status: Accepted

Solution code:
import bisect

N = int(input())
parts = [[] for i in range(3)]
con = [0] * N

ans = 0

for i in range(3):
  t = sorted(list(map(int,input().split())))
  parts[i] = t

under =parts[2]
middle = parts[1]
upper = parts[0]

for i in range(N):
  p = middle[i]
  ans += bisect.bisect_left(upper,p) * (N - bisect.bisect_right(under,p))
    
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
from _collections import defaultdict
from functools import lru_cache
import bisect

class Scanner():
    def int():
        return int(sys.stdin.readline().rstrip())
    def string():
        return sys.stdin.readline().rstrip()
    def map_int():
        return [int(x) for x in Scanner.string().split()]
    def string_list(n):
        return [input() for i in range(n)]
    def int_list_list(n):
        return [Scanner.mapInt() for i in range(n)]
    def int_cols_list(n):
        return [int(input()) for i in range(n)]
        
class Math():
    def gcd(a,b):
        if b == 0:
            return a
        return Math.gcd(b,a % b)
    def lcm(a,b):
        return (a * b) // Math.gcd(a,b)
    def roundUp(a,b):
        return -(-a // b)
    def toUpperMultiple(a,x):
        return Math.roundUp(a,x) * x
    def toLowerMultiple(a,x):
        return (a // x) * x
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3,d + 1,2):
            if n % i == 0:
                return False
        return True

MOD = int(1e09) + 7

def main():
    N = Scanner.int()
    A = Scanner.map_int()
    B = Scanner.map_int()
    C = Scanner.map_int()
    A.sort()
    B.sort()
    C.sort()
    ans = 0
    for i in range(N):
        n,m = 0,0
        n = bisect.bisect_left(A,B[i])
        m = N - bisect.bisect_right(C,B[i])
        ans += n * m
    print(ans)
    return

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from bisect 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
bb = [n-bisect(c, b[i])  for i in range(n)]
bbb = []
tmp = 0
for i in range(n)[::-1]:
    tmp += bb[i]
    bbb.append(tmp)
bbb = bbb[::-1]
j = 0
while i < n:
    j = bisect(b, a[i], j,n-1)
    if b[j] <= a[i]:
        break
    ans += bbb[j]
    i += 1

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A, B, C):
    A.sort()
    B.sort()
    C.sort()

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


def main():
    N = read_int()
    A = read_int_n()
    B = read_int_n()
    C = read_int_n()
    print(slv(N, A, B, C))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A, B, C):
    A.sort()
    B.sort()
    C.sort()

    D = [0] * (N+1)
    for i in range(N-1, -1, -1):
        j = bisect.bisect_right(C, B[i])
        D[i] = D[i+1] + (N-j)

    ans = 0
    for i in range(N-1, -1, -1):
        j = bisect.bisect_right(B, A[i])
        ans += D[j]

    return ans


def main():
    N = read_int()
    A = read_int_n()
    B = read_int_n()
    C = read_int_n()
    print(slv(N, A, B, C))


if __name__ == '__main__':
    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())))

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
from bisect import 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):
    idxa=bisect_left(A,B[i])
    idxc=bisect_right(C,B[i])
    ans+=idxa*(N-idxc)

print(ans)

Submission status: Accepted

Solution code:
from _collections import deque
from _ast import Num


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor
# https://atcoder.jp/contests/arc067/tasks/arc067_a
# C - Factors of Factorial
"""
need to consider the case that ticket is not enough to lower everything
"""

N = gi()
A = [0] * N
B = [0] * N
C = [0] * N
sB = [0] * N
sC = [0] * N

for i in range(N):
    A[i] = gi()
A.sort()

for i in range(N):
    B[i] = gi()
B.sort()

nai = 0

for i in range(N):
    while nai < N and A[nai] < B[i]:
        nai += 1
    sB[i] = nai
    if i > 0:
        sB[i] += sB[i - 1]

for i in range(N):
    C[i] = gi()
C.sort()

nbi = 0
for i in range(N):
    while nbi < N and B[nbi] < C[i]:
        nbi += 1
    if nbi:
        sC[i] = sB[nbi - 1]

    if i > 0:
        sC[i] += sC[i - 1]

print(sC[-1])

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())))
count = 0
for j in range(N):
    c = N-bisect.bisect_right(C,B[j])
    b = bisect.bisect_right(A,B[j]-1)
    count += b*c
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()
ans=0
for i in range(n):
  x=b[i]
  y=bisect.bisect_left(a,x)
  z=bisect.bisect_right(c,x)
  ans+=(n-z)*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
b = []
for i in range(N):
    b.append(N - bisect.bisect_left(C,B[i]+1))
b.append(0)
for i in range(N-1,-1,-1):
    b[i] += b[i+1]
ans = 0
for i in range(N):
    ans += b[bisect.bisect_left(B, A[i]+1)]
print(ans)

Submission status: Accepted

Solution code:
N=6
#A=[3,14,159,2,6,53]
#B=[58,9,79,323,84,6]
#C=[2643,383,2,79,50,288]
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))

D=[0]*N
E=[0]*N

A=list(sorted(A,reverse=True))
B=list(sorted(B,reverse=True))
C=list(sorted(C,reverse=True))

u=N-1
d=N-1
c=0
while(True):
    if A[u] < B[d]:
        c+=1
        u-=1
    else:
        D[d]=c
        d-=1
    if(-1==u or -1==d):
        for i in range(d,-1,-1):
            D[i]=c
        break
u=0
d=0
c=0
while(True):
    if B[u] < C[d]:
        c+=1
        d+=1
    else:
        E[u]=c
        u+=1
    if(N==u or N==d):
        for i in range(u,N):
            E[i]=c
        break

s=0
for i in range(N):
    s+=D[i]*E[i]


print(s)

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

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

ue = []
for i in range(n):
    ue.append(bisect.bisect_left(a,b[i]))

sita = []
for i in range(n):
    sita.append(n - bisect.bisect_right(c,b[i]))

ans = 0
for i in range(n):
    ans += ue[i]*sita[i]

print(ans)

Submission status: Accepted

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

def isOK(target,mid):
  return target < mid
def isOK_dep(target,mid):
  return target <= mid

def bin_search(l,r,li,target):
  left = l
  right = r
  while right-left>1:
    mid = (left + right)//2
    if isOK(target,li[mid]):
      right = mid
    else:
      left = mid
  return left

def bin_search_dep(l,r,li,target):
  left = l
  right = r
  while right-left>1:
    mid = (left + right)//2
    if isOK_dep(target,li[mid]):
      right = mid
    else:
      left = mid
  return left

ans = 0
for i in range(n):
  top,bottom = 0,0
  top += bin_search_dep(-1,n,A,B[i]) + 1
  bottom += n-bin_search(-1,n,C,B[i])-1
  ans += top * bottom
print(ans)

Submission status: Accepted

Solution code:
import bisect

N = int(input())
A = tuple(sorted(map(int, input().split())))
B = tuple(map(int, input().split()))
C = tuple(sorted(map(int, input().split())))
res = 0
for i in range(N):
	res += bisect.bisect_left(A, B[i])*(N - bisect.bisect_right(C, B[i]))
print(res)