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: Runtime Error

Solution code:
import numpy as np
N, *L = map(int, open('0').read().split())
A = np.array(L[:N])
B = np.array(L[N:2*N])
C = np.array(L[2*N:])
A.sort()
B.sort()
C.sort()
binf = np.zeros(N)
j = 0
binf = np.searchsorted(A,B)
bs = np.cumsum(binf)
bs = np.concatenate([[0],bs])
cinf = np.searchsorted(B,C)
ans = np.sum(bs[cinf])
print(ans)

Submission status: Runtime Error

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

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

bc = [0 for _ in range(n)]
for i in range(len(b)):
    if b[i] >= c[n-1]:
        break
    p = bisect(c,b[i])
    bc[i] = (n-p)

for i in range(len(b)-1,0,-1):
    bc[i-1] += bc[i]
ans = 0
for x in a:
    if x < b[n-1]:
        p = bisect(b,a[i])
        ans += bc[p]

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
AS = list(int,input().split())
AS.sort()
BS = list(int,input().split())
BS.sort()
CS = list(int,input().split())
CS.sort()
import bisect
AB  = [0 for _ in range(n)]
BC  = [0 for _ in range(n)]
a = 0
for b in range(n):
  AB[b] = bisect.bisect_left(AS,BS[b])
  
for b in range(n):
  BC[b] = n-bisect.bisect_left(CS,BS[b])

ans = 0
for i in range(n):
  ans += AB[b] * BS[b]
print(ans)

Submission status: Runtime Error

Solution code:
def nibunhigh(lister, b):
    ans = 0
    low = 0
    high = len(lister) - 1

    if lister[low] > b:
        return high + 1
    elif lister[high] < b:
        return low
    else:
        while lister[low + 1] < b:
            mid = (low + high) // 2
            if (b < lister[mid]):
                high = mid
            else:
                low = mid
        ans = len(lister) - (low + 1)

    return ans

def nibunlow(lister, b):
    ans = 0
    low = 0
    high = len(lister) - 1
#    while (low + 1) <= high:
    if lister[low] > b:
        return low
    elif lister[high] < b:
        return high + 1
    else:
        while lister[low + 1] < b:
            mid = (low + high) // 2
            if (b <= lister[mid]):
                high = mid
            else:
                low = mid
        ans = low + 1

    return ans

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

    ans = 0
    a.sort()
    b.sort()
    c.sort()

    for i in range(n):
        ans += nibunlow(a , b[i]) * nibunhigh(c , b[i])

    return ans

def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    c = list(map(int , input().split()))
    print(snukefestival(n , a , b , c))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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: Runtime Error

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: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(4100000)
input=sys.stdin.readline
# coding: utf-8
# Your code here!

n=int(input())
a = [list(map(int, input().split())) for _ in range(3)]
a[0].sort()
a[2].sort()
"""
print(a[0])
print(a[1])
print(a[2])
"""
def bin_upper(b):    
    pl=0
    pr=n-1
    
    while True:
        pc=(pl+pr)//2
        if a[0][pc]>=b and a[0][pc-1]<b:
            return pc
        elif a[0][pc]<b:
            pl=pc+1
            if pl>n-1:
                return n
        else:
            pr=pc-1
            if pr<0:
                return 0
                
def bin_lower(b):    
    pl=0
    pr=n-1
    
    while True:
        pc=(pl+pr)//2
        if a[2][pc]<=b and a[2][pc+1]>b:
            return n-1-pc
        elif a[2][pc]>b:
            pr=pc-1
            if pr<0:
                return n
        else:
            pl=pc+1
            if pl>n-1:
                return 0
"""
print(bin_upper(a[1][0]))
print(bin_lower(a[1][0]))
print(bin_upper(a[1][1]))
print(bin_lower(a[1][1]))
print(bin_upper(a[1][2]))
print(bin_lower(a[1][2]))
"""

cnt=0
for i in range(n):
    cnt+=bin_upper(a[1][i])*bin_lower(a[1][i])
print(cnt)

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
# coding: utf-8
# Your code here!

n=int(input())
a = [list(map(int, input().split())) for _ in range(3)]
a[0].sort()
a[2].sort()
"""
print(a[0])
print(a[1])
print(a[2])
"""
def bin_upper(b):    
    pl=0
    pr=n-1
    
    while True:
        pc=(pl+pr)//2
        if a[0][pc]>=b and a[0][pc-1]<b:
            return pc
        elif a[0][pc]<b:
            pl=pc+1
            if pl>n-1:
                return n
        else:
            pr=pc-1
            if pr<0:
                return 0
                
def bin_lower(b):    
    pl=0
    pr=n-1
    
    while True:
        pc=(pl+pr)//2
        if a[2][pc]<=b and a[2][pc+1]>b:
            return n-1-pc
        elif a[2][pc]>b:
            pr=pc-1
            if pr<0:
                return n
        else:
            pl=pc+1
            if pl>n-1:
                return 0
"""
print(bin_upper(a[1][0]))
print(bin_lower(a[1][0]))
print(bin_upper(a[1][1]))
print(bin_lower(a[1][1]))
print(bin_upper(a[1][2]))
print(bin_lower(a[1][2]))
"""

cnt=0
for i in range(n):
    cnt+=bin_upper(a[1][i])*bin_lower(a[1][i])
print(cnt)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

n=int(input())
a = [list(map(int, input().split())) for _ in range(3)]
a[0].sort()
a[2].sort()
"""
print(a[0])
print(a[1])
print(a[2])
"""
def bin_upper(b):    
    pl=0
    pr=n-1
    
    while True:
        pc=(pl+pr)//2
        if a[0][pc]>=b and a[0][pc-1]<b:
            return pc
        elif a[0][pc]<b:
            pl=pc+1
            if pl>n-1:
                return n
        else:
            pr=pc-1
            if pr<0:
                return 0
                
def bin_lower(b):    
    pl=0
    pr=n-1
    
    while True:
        pc=(pl+pr)//2
        if a[2][pc]<=b and a[2][pc+1]>b:
            return n-1-pc
        elif a[2][pc]>b:
            pr=pc-1
            if pr<0:
                return n
        else:
            pl=pc+1
            if pl>n-1:
                return 0
"""
print(bin_upper(a[1][0]))
print(bin_lower(a[1][0]))
print(bin_upper(a[1][1]))
print(bin_lower(a[1][1]))
print(bin_upper(a[1][2]))
print(bin_lower(a[1][2]))
"""

cnt=0
for i in range(n):
    cnt+=bin_upper(a[1][i])*bin_lower(a[1][i])
print(cnt)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

n=int(input())
a = [list(map(int, input().split())) for _ in range(3)]
a[0].sort()
a[2].sort()
"""
print(a[0])
print(a[1])
print(a[2])
"""
def bin_upper(b):    
    pl=0
    pr=n-1
    
    while True:
        pc=(pl+pr)//2
        if a[0][pc]>=b and a[0][pc-1]<b:
            return pc
        elif a[0][pc]<b:
            pl=pc+1
            if pl>n-1:
                return n
        else:
            pr=pc-1
            if pr<0:
                return 0
                
def bin_lower(b):    
    pl=0
    pr=n-1
    
    while True:
        pc=(pl+pr)//2
        if a[2][pc]<=b and a[2][pc+1]>b:
            return n-1-pc
        elif a[2][pc]>b:
            pr=pc-1
            if pr<0:
                return 0
        else:
            pl=pc+1
            if pl>n-1:
                return n
"""
print(bin_upper(a[1][0]))
print(bin_lower(a[1][0]))
print(bin_upper(a[1][1]))
print(bin_lower(a[1][1]))
print(bin_upper(a[1][2]))
print(bin_lower(a[1][2]))
"""

cnt=0
for i in range(n):
    #val=bin_upper(a[1][0])
    #print(val)
    cnt+=bin_upper(a[1][i])*bin_lower(a[1][i])
print(cnt)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

n=int(input())
a = [list(map(int, input().split())) for _ in range(3)]
a[0].sort()
a[2].sort()
"""
print(a[0])
print(a[1])
print(a[2])
"""
def bin_upper(b):    
    pl=0
    pr=n-1
    
    while True:
        pc=(pl+pr)//2
        if a[0][pc]>=b and a[0][pc-1]<b:
            return pc
        elif a[0][pc]<b:
            pl=pc+1
            if pl>n-1:
                return n
        else:
            pr=pc-1
            if pr<0:
                return 0
                
def bin_lower(b):    
    pl=0
    pr=n-1
    
    while True:
        pc=(pl+pr)//2
        if a[2][pc]<=b and a[2][pc+1]>b:
            return n-1-pc
        elif a[2][pc]>b:
            pr=pc-1
            if pr<0:
                return n
        else:
            pl=pc+1
            if pl>n-1:
                return 0
"""
print(bin_upper(a[1][0]))
print(bin_lower(a[1][0]))
print(bin_upper(a[1][1]))
print(bin_lower(a[1][1]))
print(bin_upper(a[1][2]))
print(bin_lower(a[1][2]))
"""

cnt=0
for i in range(n):
    #val=bin_upper(a[1][0])
    #print(val)
    cnt+=bin_upper(a[1][i])*bin_lower(a[1][i])
print(cnt)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

n=int(input())
a = [list(map(int, input().split())) for _ in range(3)]
a[0].sort()
a[2].sort()
print(a[0])
print(a[1])
print(a[2])
def bin_upper(b):    
    pl=0
    pr=n-1
    
    while True:
        pc=(pl+pr)//2
        if a[0][pc]>=b and a[0][pc-1]<b:
            return pc
        elif a[0][pc]<b:
            pl=pc+1
            if pl>n-1:
                return n
        else:
            pr=pc-1
            if pr<0:
                return 0
                
def bin_lower(b):    
    pl=0
    pr=n-1
    
    while True:
        pc=(pl+pr)//2
        if a[2][pc]<=b and a[2][pc+1]>b:
            return n-1-pc
        elif a[2][pc]>b:
            pr=pc-1
            if pr<0:
                return n
        else:
            pl=pc+1
            if pl>n-1:
                return 0
"""
print(bin_upper(a[1][0]))
print(bin_lower(a[1][0]))
print(bin_upper(a[1][1]))
print(bin_lower(a[1][1]))
print(bin_upper(a[1][2]))
print(bin_lower(a[1][2]))
"""

cnt=0
for i in range(n):
    #val=bin_upper(a[1][0])
    #print(val)
    cnt+=bin_upper(a[1][i])*bin_lower(a[1][i])
print(cnt)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

n=int(input())
a = [list(map(int, input().split())) for _ in range(3)]
#print(a[0][1])

def bin_upper(b):    
    pl=0
    pr=n-1
    
    while True:
        pc=(pl+pr)//2
        if a[0][pc]>=b and a[0][pc-1]<b:
            return pc
        elif a[0][pc]<b:
            pl=pc+1
            if pl>n-1:
                return n
        else:
            pr=pc-1
            if pr<0:
                return 0
                
def bin_lower(b):    
    pl=0
    pr=n-1
    
    while True:
        pc=(pl+pr)//2
        if a[2][pc]<=b and a[2][pc+1]>b:
            return pc+1
        elif a[2][pc]>b:
            pl=pc+1
            if pl>n-1:
                return n
        else:
            pr=pc-1
            if pr<0:
                return 0
"""
print(bin_upper(a[1][0]))
print(bin_lower(a[1][0]))
print(bin_upper(a[1][1]))
print(bin_lower(a[1][1]))
"""

cnt=0
for i in range(n):
    #val=bin_upper(a[1][0])
    #print(val)
    cnt+=bin_upper(a[1][i])*bin_lower(a[1][i])
print(cnt)

Submission status: Runtime Error

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()
l = max(max(a), max(b), max(c))

aamo = [0]*(l+1)
for i in range(n):
  aamo[a[i]] = i+1
for i in range(1, l):
  if aamo[i+1] == 0:
    aamo[i+1] = aamo[i]

bamo = [0]*(l+1)
for i in range(n):
  bamo[b[i]] = i+1
for i in range(1, l):
  if bamo[i+1] == 0:
    bamo[i+1] = bamo[i]

abcnt = []
for i in range(n):
  abcnt.append(aamo[b[i]-1])
  if i > 0:
    abcnt[i] += abcnt[i-1]

abcntamo = [0]*(l+1)
for i in range(n):
  abcntamo[b[i]] = abcnt[i]
for i in range(1, l):
  if abcntamo[i+1] == 0:
    abcntamo[i+1] = abcntamo[i]

cnt = 0
for i in range(n):
  cnt += abcntamo[c[i]-1]

# print(aamo, bamo, abcnt, abcntamo)
print(cnt)

Submission status: Runtime Error

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()
l = max(max(a), max(b), max(c))

aamo = [0]*(l+1)
for i in range(n):
  aamo[a[i]-1] = i+1
for i in range(1, l):
  if aamo[i+1] == 0:
    aamo[i+1] = aamo[i]

bamo = [0]*(l+1)
for i in range(n):
  bamo[b[i]-1] = i+1
for i in range(1, l):
  if bamo[i+1] == 0:
    bamo[i+1] = bamo[i]

abcnt = []
for i in range(n):
  abcnt.append(aamo[b[i]-2])
  if i > 0:
    abcnt[i] += abcnt[i-1]

abcntamo = [0]*(l+1)
for i in range(n):
  abcntamo[b[i]-1] = abcnt[i]
for i in range(1, l):
  if abcntamo[i+1] == 0:
    abcntamo[i+1] = abcntamo[i]

cnt = 0
for i in range(n):
  cnt += abcntamo[c[i]-2]

# print(aamo, bamo, abcnt, abcntamo)
print(cnt)

Submission status: Runtime Error

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()
l = max(max(a), max(b), max(c))

aamo = [0]*l
for i in range(n):
  aamo[a[i]-1] = i+1
for i in range(l-1):
  if aamo[i+1] == 0:
    aamo[i+1] = aamo[i]
# print(aamo)

bamo = [0]*l
for i in range(n):
  bamo[b[i]-1] = i+1
for i in range(l-1):
  if bamo[i+1] == 0:
    bamo[i+1] = bamo[i]
# print(bamo)

abcnt = []
for i in range(n):
  abcnt.append(aamo[b[i]-2])
  if i > 0:
    abcnt[i] += abcnt[i-1]
# print(a, b, abcnt)

abcntamo = [0] * l
for i in range(l):
  if bamo[i] == 0:
    continue
  abcntamo[i] = abcnt[bamo[i]-1]
# print(abcntamo)

cnt = 0
for i in range(n):
  cnt += abcntamo[c[i]-2]
print(cnt)

Submission status: Runtime Error

Solution code:

n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
for i in range(1,min(a)+1):
	if i in b:
		b.remove(i)
for i in range(1,min(b)+1):
	if i in c:
		c.remove(i)
count=0
for i in range(n):
	for j in range(n):
		if a[i]<b[j]:
			for k in range(n):
				if b[j]<c[k]:
					count+=1
print(count)

Submission status: Runtime Error

Solution code:
import bisect

_ = input()

heads = list(sorted(map(int,input().split())))
bodies = list(map(int,input().split()))
legs = list(sorted(map(int,input().split())))


cnt = 0
for body in bodies:
    #  頭は、体より小さくなければならない
    head_idx = bisect.bisect_left(sorted_heads, body)
    head_cnt = len(sorted_heads[:head_idx])

    #  足は、体より大きくなければならない
    leg_idx = bisect.bisect_right(sorted_legs, body)
    leg_cnt = len(sorted_legs[leg_idx:])

    cnt += head_cnt * leg_cnt # 組み合わせの掛け算

print(cnt)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <stdio.h>
#include <set>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>

typedef long long int lli;
#define rep(i,s,N) for(int i=s;i<N;i++)
using namespace std;


int main(void)
{
	int N; cin >> N;
	vector<lli> A(N), B(N), C(N);
	rep(i, 0, N)cin >> A[i]; sort(A.begin(), A.end()); 
	rep(i, 0, N)cin >> B[i]; sort(B.begin(), B.end()); 
	rep(i, 0, N)cin >> C[i]; sort(C.begin(), C.end()); 

	lli ans = 0;
	
	rep(i,0,N) {
		vector<lli>::iterator Iter1 = lower_bound(A.begin(), A.end(), B[i]);
		vector<lli>::iterator Iter2 = upper_bound(C.begin(), C.end(), B[i]);
		lli k1 = Iter1 - A.begin();
		lli k2 = C.end() - Iter2;
		ans += k1 * k2;
	}
	cout << ans << endl;

	return 0;
}

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import collections
n=int(input())
a=sorted(list(collections.Counter(map(int,input().split())).items()), key=lambda x: x[0])
b=sorted(list(collections.Counter(map(int,input().split())).items()), key=lambda x: x[0])
c=sorted(list(collections.Counter(map(int,input().split())).items()), key=lambda x: x[0])

bc=[0]*len(b)
j=0
tmp=sum(i[1] for i in c)
for i,x in enumerate(b):
  while j<n and x[0]>=c[j][0]:
    tmp-=c[j][1]
    j+=1
  bc[i]=tmp

abc=[0]*len(a)
j=0
tmp=sum(i[1]*j for i,j in zip(b,bc))

for i,x in enumerate(a):
  while j<n and x[0]>=b[j][0]:
    tmp-=b[j][1]*bc[j]
    j+=1
  abc[i]=tmp
ans=sum(i[1]*j for i,j in zip(a,abc))
#print(a,b,c,bc,abc,sep="\n")
print(ans)

Submission status: Runtime Error

Solution code:
from functools import lru_cache as cache
from bisect import bisect

n = int(input())
a_list = sorted(map(int, input().split()))
b_list = sorted(map(int, input().split()))
c_list = sorted(map(int, input().split()))

b_c_table = [n - bisect(c_list, b) for b in b_list]

@cache(maxsize=None)
def sum_(*list_):
    return sum([*list_])

ans = 0
for a in a_list:
    i = bisect(b_list, a)
    ans += sum_(*b_c_table[i:n])
print(ans)

Submission status: Runtime Error

Solution code:
//#pragma GCC optimize("O3")
//#pragma GCC target("avx")

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

// Types
using ll = long long int;
using str = string;
template <class T> using u_set = unordered_set<T>;
template <class K, class V> using u_map = unordered_map<K, V>;
template <class V> using graph = u_map<V, u_set<V>>;
template <class F,
          typename enable_if<is_function<F>::value, nullptr_t>::type = nullptr>
using fun = function<F>;

namespace io {
// Input
using IS = istream;
IS &in(IS &i);
IS &in(IS &i) { return i; }
template <class T, class... Ts> IS &in(IS &i, T &a, Ts &... as) {
  return in(i >> a, as...);
}
template <class V> IS &operator>>(IS &i, vector<V> &a) {
  for_each(a.begin(), a.end(), [&i](auto &e) { i >> e; });
  return i;
}
template <class F, class S> IS &operator>>(IS &i, pair<F, S> &p) {
  return in(i, p.first, p.second);
}
template <class... Ts, size_t... I>
IS &tuple_in(IS &i, tuple<Ts...> &t, index_sequence<I...>) {
  return in(i, get<I>(t)...);
}
template <class... Ts> IS &operator>>(IS &i, tuple<Ts...> &t) {
  return tuple_in(i, t, make_index_sequence<sizeof...(Ts)>());
}
// Output
using OS = ostream;
OS &out(OS &o) { return o << endl; }
template <class T> OS &out(OS &o, T const &a) { return o << a << endl; }
template <class T, class... Ts> OS &out(OS &o, T const &a, Ts const &... as) {
  return out(o << a << " ", as...);
}
template <class F, class S> OS &operator<<(OS &o, pair<F, S> const &p) {
  return o << p.first << ":" << p.second;
}
template <class... Ts> OS &args_out(OS &o, Ts const &... ts);
OS &args_out(OS &o) { return o; }
template <class T> OS &args_out(OS &o, T const &t) { return o << t; }
template <class T0, class T1, class... Ts>
OS &args_out(OS &o, T0 const &t0, T1 const &t1, Ts const &... ts) {
  return args_out(o << t0 << ",", t1, ts...);
}
template <class... Ts, size_t... I>
OS &tuple_out(OS &o, tuple<Ts...> const &t, index_sequence<I...>) {
  return args_out(o, get<I>(t)...);
}
template <class... Ts> OS &operator<<(OS &o, tuple<Ts...> const &t) {
  return tuple_out(o << "(", t, make_index_sequence<sizeof...(Ts)>()) << ")";
}
template <
    class C,
    class T = typename iterator_traits<typename C::iterator>::value_type,
    typename enable_if<!is_same<C, str>::value, nullptr_t>::type = nullptr>
OS &operator<<(OS &o, C const &a) {
  return a.empty() ? (o << "[]") : ([&o, &a]() -> OS & {
    o << "[" << *a.begin();
    for_each(next(a.begin()), a.end(), [&o](auto const &e) { o << "," << e; });
    return o << "]";
  }());
}
} // namespace io
auto input = [](auto &... a) { io::in(cin, a...); };
auto print = [](auto const &... a) { io::out(cout, a...); };
#ifdef JUMPAKU_DEBUG
auto dump = [](auto const &... a) { io::out(cout, a...); };
#else
auto dump = [](auto const &... a) {};
#endif

// Hash
namespace hashcode {
template <class... Ts> size_t hash_args(size_t h, Ts const &... ts);
size_t hash_args(size_t h) { return h; }
template <class T, class... Ts>
size_t hash_args(size_t h, T const &t, Ts const &... ts) {
  return hash_args(((h << 10) - h + hash<T>{}(t)), ts...);
}
template <class... Ts, size_t... I>
size_t hash_tuple(tuple<Ts...> const &t, index_sequence<I...>) {
  return hash_args(1089283, get<I>(t)...);
}
} // namespace hashcode
namespace std {
template <class... Ts> struct hash<tuple<Ts...>> {
  size_t operator()(tuple<Ts...> const &t) const {
    return hashcode::hash_tuple(t, index_sequence_for<Ts...>());
  }
};
} // namespace std

// Range
vector<ll> range(ll const &begin, ll const &end) {
  vector<ll> ret(max(0LL, end - begin));
  iota(ret.begin(), ret.end(), begin);
  return ret;
}
vector<ll> range(ll const &end) { return range(0, end); }
template <class T = ll> vector<T> vec(size_t n, T &&init = T()) {
  return vector<T>(n, init);
}
template <class Itr, class T = typename iterator_traits<Itr>::value_type>
vector<T> vec(Itr begin, Itr end) {
  return vector<T>(begin, end);
}

ll bisect(ll ng, ll ok, fun<bool(ll)> const &is_ok) {
  while (ok - ng > 1) {
    dump(ng, ok);
    ll m = ng + (ok - ng) / 2;
    (is_ok(m) ? ok : ng) = m;
  }
  return ok;
}
ll gcd(ll p, ll q) { return (q == 0) ? p : gcd(q, p % q); }
ll lcm(ll p, ll q) { return p * q / gcd(q, p); }

// MOD
ll pow(ll a, ll n, ll m) {
  if (n == 0)
    return 1;
  if (n & 1)
    return ((a % m) * pow(a, n - 1, m)) % m;
  auto b = pow(a, n / 2, m);
  return (b * b) % m;
}
ll inv(ll a, ll p) { return pow(a, p - 2, p); }
constexpr ll MOD = 1e9 + 7;

int main() {
  ll N;
  input(N);
  auto A = vec(N);
  auto B = vec(N);
  auto C = vec(N);
  input(A, B, C);
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  sort(C.begin(), C.end());
  auto D = vec(N);
  auto E = vec(N);
  for (auto &&i : range(N)) {
    D[i] = upper_bound(B.begin(), B.end(), A[i]) - B.begin();
    E[i] = upper_bound(C.begin(), C.end(), B[i]) - C.begin();
  }
  auto S = vec(N);
  partial_sum(E.begin(), E.end(), S.begin());
  ll ans = 0;
  dump(A, B, C, D, E, S);
  for (auto &&i : range(N)) {
    dump(i, D[i]);
    ans += N * (N - D[i]) - S[N - 1] + (D[i] > 0 ? S[D[i] - 1] : 0);
  }
  print(ans);
}

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
import sys

import numpy as np

readline = sys.stdin.buffer.readline


# 同じ数値が複数あるとおかしくなる
def binary_search(A, n):
    n -= 1
    left = 0
    right = len(A)-1

    if n < A[0]:
        return -1
    else:
        while left < right:
            # if right - left == 1:
            #     break

            mid = (left + right) // 2
            if A[mid] < n:
                left = mid + 1
            elif n < A[mid]:
                right = mid - 1
            else:
                left = len(A) - mid//2
                break
        if A[left] > n:
            left -= 1

        return left


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

    A, B, C = np.sort(A), np.sort(B), np.sort(C)

    aa = np.zeros([N], dtype=np.int32)
    for i, b in enumerate(B):
        aa[i] = binary_search(A, b) + 1

    bb = np.cumsum(aa)

    cnt = 0
    for i, c in enumerate(C):
        m = binary_search(B, c)
        if m != -1:
            cnt += bb[m]

    print(cnt)


main()

Submission status: Runtime Error

Solution code:
import sys

import numpy as np

readline = sys.stdin.buffer.readline


# 同じ数値が複数あるとおかしくなる
def binary_search(A, n):
    n -= 1
    left = 0
    right = len(A)-1

    if n < A[0]:
        return -1
    else:
        while left < right:
            # if right - left == 1:
            #     break

            mid = (left + right) // 2
            if A[mid] < n:
                left = mid + 1
            elif n < A[mid]:
                right = mid - 1
            else:
                left = len(A) - mid + left
                break
        if A[left] > n:
            left -= 1

        return left


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

    A, B, C = np.sort(A), np.sort(B), np.sort(C)

    aa = np.zeros([N], dtype=np.int32)
    for i, b in enumerate(B):
        aa[i] = binary_search(A, b) + 1

    bb = np.cumsum(aa)

    cnt = 0
    for i, c in enumerate(C):
        m = binary_search(B, c)
        if m != -1:
            cnt += bb[m]

    print(cnt)


main()

Submission status: Runtime Error

Solution code:
N = int(input())
A_arr = sorted(list(map(int,input().split())))
B_arr = sorted(list(map(int,input().split())))
C_arr = sorted(list(map(int,input().split())))
ans = 0
for i in C_arr:
    B_index = bisect.bisect_left(B_arr,i)
    for k in range(B_index):
        ans += bisect.bisect_left(A_arr,B_arr[k])
print(ans)

Submission status: Runtime Error

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()
def binary_search(lst,item):
  low=0
  high=len(lst)-1
  while low<high:
    mid=(low+high)//2
    guess=lst[mid]
    if guess==item:
      return mid
    if guess<item:
      low=mid+1
    else:
      high=mid-1
  return mid 
  
cnt=0
for i in range(n):
  cnt+=(binary_search(b,a[i])+1)*(binary_search(c,b[i])+1)
    
print(cnt)

Submission status: Runtime Error

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()
def binary_search(*lst,item):
  low=0
  high=len(lst)-1
  while low<=high:
    mid=(low+high)//2
    guess=lst[mid]
    if guess==item:
      retrun item
    if guess>item:
      low=mid-1
    else:
      high=mid+1
  if lst[mid]>item:
    return mid-1
  if lst[mid]<item:
    return mid
  
A=[]
B=[]
for i in range(n):
  A.append(binary_search(b,a[i]))
  B.append(binary_search(c,b[i]))
    
cnt=0
for i in range(n):
  cnt+=(A[i]+1)*(B[i]+1)

print(cnt)

Submission status: Runtime Error

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

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

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

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

Submission status: Runtime Error

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

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

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

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

Submission status: Runtime Error

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

def search(ar, key, flag):
    left = -1
    right = len(ar)
    center = (left + right) // 2
    
    while right - left > 1:
        if ar[center] == key:
            if flag == 0:
                return center
            elif flag == 1:
                return len(ar)-1-center
        elif ar[center] < key:
            left = center
            center = (left + right) // 2
        else:
            right = center
            center = (left + right) // 2
    
    if flag == 0:
        return right
    elif flag == 1:
        return len(ar) - right
        

for i in range(n):
    mid = bl[i]
    top_num = search(al, mid, 0)
    bottom_num = search(cl, mid, 1)
    ans += top_num * bottom_num
    
print(ans)