Problem:


Score : 300 points

Problem Statement

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

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

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

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

Constraints

1 \leq N \leq 10^5

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

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

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

All input values are integers.

Input

Input is given from Standard Input in the following format:

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

Output

Print the number of different altars that Ringo can build.

Sample Input 1

2
1 5
2 4
3 6

Sample Output 1

3

The following three altars can be built:

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

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

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

Sample Input 2

3
1 1 1
2 2 2
3 3 3

Sample Output 2

27

Sample Input 3

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

Sample Output 3

87



Sample input:
2
1 5
2 4
3 6


Sample output:
3

Submission status: Accepted

Solution code:
#!/usr/bin/python3

from bisect import bisect_left, bisect_right


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

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

    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ret = 0
i, j, k = 0, 0, 0

while j < N:
    while i < N and A[i] < B[j]:
        i += 1
    while k < N and C[k] <= B[j]:
        k += 1

    ret += i * (N - k)
    j += 1

print(ret)

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

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
N = int(input())
A = sorted([int(_) for _ in input().split()])
B = sorted([int(_) for _ in input().split()])
C = sorted([int(_) for _ in input().split()])

ans = 0
for item in B:
    A_pos = bisect_left(A, item)
    C_pos = N - bisect_right(C, item)
    ans += A_pos * C_pos
print(ans)

Submission status: Accepted

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

cnt_a = [0] * N
for i in range(N):
    cnt_a[i] = bisect_left(A, B[i])
    
cnt_b = [0] * N
for i in range(N):
    cnt_b[i] = bisect_right(C, B[i])
    
ans = sum(v * (N - w) for v, w in zip(cnt_a, cnt_b))
print(ans)

Submission status: Accepted

Solution code:
# ABC077C - Snuke Festival (ARC084C)
import sys
input = sys.stdin.readline

from bisect import bisect_left, bisect_right

def main():
    n = int(input())
    A, B, C = [sorted(map(int, input().rstrip().split())) for _ in range(3)]
    l, ans = len(C), 0
    for i in B:
        ans += bisect_left(A, i) * (l - bisect_right(C, i))
    print(ans)


if __name__ == "__main__":
    main()

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()
l=0
for j in b:
    aa=bisect.bisect_left(a,j)
    cc=bisect.bisect_right(c,j)
    if cc!=0:
        l+=(aa*(len(c)-cc))
    else:
        l+=(aa*len(c))
print(l)

Submission status: Accepted

Solution code:
def lower_elements(A, b):
    lower_bound = -1
    upper_bound = len(A)
    while upper_bound - lower_bound > 1:  # ア
        middle = (lower_bound + upper_bound) // 2
        if A[middle] < b:  # イ
            lower_bound = middle
        else:
            upper_bound = middle
    return lower_bound + 1  # ウ upper_boundでも可


def upper_elements(C, b):
    lower_bound = -1
    upper_bound = len(C)
    while upper_bound - lower_bound > 1:  # ア
        middle = (lower_bound + upper_bound) // 2
        if C[middle] <= b:
            lower_bound = middle
        else:
            upper_bound = middle
    return len(C) - upper_bound


def answer(N: int, A: list, B: list, C: list):
    assert N == len(A) == len(B) == len(C)
    A.sort()
    B.sort()
    C.sort()
    count = 0
    for b in B:
        count += lower_elements(A, b) * upper_elements(C, b)
    return count


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


if __name__ == '__main__':
  snuke_festival()

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())))
# print(a)
# print(b)
# print(c)

ans=0
acount=[0]*n
bcount=[0]*n
ruiseki=[0]*(n+1)
for i in range(n):
    acount[i]=n-bisect.bisect(b,a[i])
    bcount[i]=n-bisect.bisect(c,b[i])
    ruiseki[i+1]=ruiseki[i]+bcount[i]
# print(acount)
# print(bcount)
# print(ruiseki)

for i in range(n):
    # print(ruiseki[n]-ruiseki[n-acount[i]])
    ans+=ruiseki[n]-ruiseki[n-acount[i]]
print(ans)

Submission status: Accepted

Solution code:
import bisect

n = int(input());
a = input().split(" ");
b = input().split(" ");
c = input().split(" ");
num_b = [0]*n;
for i in range(n) :
	a[i] = int(a[i]);
	b[i] = int(b[i]);
	c[i] = int(c[i]);
a.sort();
b.sort();
c.sort();
num_b = [0]*(n+1);
for i in range(n) :
	num_b[n-1-i] = n - bisect.bisect_right(c,b[n-1-i]) + num_b[n-i];
ans = 0;
for i in range(n) :
	ans += num_b[bisect.bisect_right(b,a[i])];
print(ans);

Submission status: Accepted

Solution code:
def lower_elements(A, b):
    ''' リストAの要素のうち，b未満のものの個数を返す関数 '''
    lower_bound = -1
    upper_bound = len(A)
    while upper_bound - lower_bound > 1:
        middle = (lower_bound + upper_bound) // 2
        if A[middle] < b:
            lower_bound = middle
        else:
            upper_bound = middle
    return lower_bound + 1


def upper_elements(C, b):
    ''' リストCの要素のうち，cより大きいものの個数を返す関数 '''
    i = -1
    j = len(C)
    while j - i > 1:
        m = (i + j) // 2
        if C[m] > b:
            j = m
        else:
            i = m
    return len(C) - j


def answer(N, A, B, C):
    A.sort()
    B.sort()
    C.sort()
    count = 0
    for b in B:
        count += lower_elements(A, b) * upper_elements(C, b)
    return count


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

Submission status: Accepted

Solution code:
def lower(A, b):
    i = -1
    j = len(A)
    while j - i > 1:
        m = (i + j) // 2
        if A[m] < b:
            i = m
        else:
            j = m
    return i + 1


def upper(C, b):
    i = -1
    j = len(C)
    #print('in upper 1', C, b)
    while j - i > 1:
        m = (i + j) // 2
        #print('in upper 2', i, m, j, C[m] > b)
        if C[m] > b:
            j = m
        else:
            i = m
    return len(C) - j


def answer(N, A, B, C):
    A.sort()
    B.sort()
    C.sort()
    #print(A)
    #print(B)
    #print(A)
    count = 0
    for b in B:
        #print(b, lower(A, b), upper(C, b))
        count += lower(A, b) * upper(C, b)
    return count


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

Submission status: Accepted

Solution code:
n = int(raw_input())
A = map(int, raw_input().split())
B = map(int, raw_input().split())
C = map(int, raw_input().split())
A.sort()
B.sort()
C.sort()
abc = 0
i = 0
j = 0
k = 0
for j in range(n):
    while i < n-1 and A[i+1] < B[j]:
        i += 1
    while k < n-1 and B[j] >= C[k]:
        k += 1
    if A[i] < B[j] and B[j] < C[k]:
        #print i, j, k, (i+1)*(n-k)
        abc += (i + 1) * (n - k)
print abc

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

Submission status: Accepted

Solution code:
import bisect

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

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

def solve(n, a, b, c):
    a = sorted(a)
    b = sorted(b)
    c = sorted(c)
    res = 0
    for x in b:
        i = bisect.bisect_left(a, x)
        k = bisect.bisect_right(c, x)
        res += i * (n-k)
    return res

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

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right

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
for b in B:
    smaller_than_b = bisect_left(A, b)
    larger_than_b = N - bisect_right(C, b)
    ans += smaller_than_b * larger_than_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()
ans = 0;
for bj in b:
    ai = bisect.bisect_left(a, bj);
    ck = n - bisect.bisect_right(c, bj);
    ans += ai * ck;
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import sys
input = sys.stdin.readline

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

cnt_A = np.searchsorted(A,B,side='left')
cnt_C = np.searchsorted(C,-B,side='left')

answer = (cnt_A * cnt_C).sum()
print(answer)

Submission status: Accepted

Solution code:
import bisect
N=int(input())
LA=list(map(int,input().split()))
LB=list(map(int,input().split()))
LC=list(map(int,input().split()))
LA.sort()
LB.sort()
LC.sort()
ans=0
for b in LB:
    a,c=bisect.bisect_left(LA,b),bisect.bisect_right(LC,b)
    ans+=a*(N-c)
print(ans)

Submission status: Accepted

Solution code:
import bisect
n=int(input())
abc=[sorted(list(int(i) for i in input().split())) for i in range(3)]
print(sum([bisect.bisect_left(abc[0],b)*(n-bisect.bisect_right(abc[2],b)) for b in abc[1]]))

Submission status: Accepted

Solution code:
import numpy as np
import sys
input = sys.stdin.readline

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

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

cnt_A = np.searchsorted(A,B,side='left')
cnt_C = np.searchsorted(C,-B,side='left')

answer = (cnt_A * cnt_C).sum()
print(answer)

Submission status: Accepted

Solution code:
import numpy as np
import sys
input = sys.stdin.readline

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

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

cnt_A = np.searchsorted(A,B,side='left')
cnt_C = np.searchsorted(C,-B,side='left')

answer = (cnt_A * cnt_C).sum()
print(answer)

Submission status: Accepted

Solution code:
import numpy as np
import sys
input = sys.stdin.readline

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

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

# 各bに対して使えるaの個数
cnt_A = np.searchsorted(A,B,side='left')
# 各bに対して使えないcの個数
cnt_C = np.searchsorted(C,B,side='right')

answer = (cnt_A * (N - cnt_C)).sum()
print(answer)

Submission status: Accepted

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

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

# 各bに対して使えるaの個数
cnt_A = np.searchsorted(A,B,side='left')
# 各bに対して使えないcの個数
cnt_C = np.searchsorted(C,B,side='right')
# 各bに対して使えるcの個数
cnt_C = N - cnt_C

answer = (cnt_A * cnt_C).sum()
print(answer)

Submission status: Accepted

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

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

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

ans = 0
for v in B:
    l = bisect.bisect_left(A, v)
    r = bisect.bisect_right(C, v)
    ans += l * (N - r)
    
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)
ans = 0
for i in B:
    # print (i)
    # print (bisect.bisect_left(A, i-0.1))
    ans += bisect.bisect_left(A, i-0.1) * (N-bisect.bisect_right(C, i+0.1))
print (ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
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:
    ans+=bisect.bisect_left(A,b)*(N-bisect.bisect_right(C,b))
print(ans)

Submission status: Accepted

Solution code:
def binary_search_left(n, key, in_list):
    left = 0
    right = n
    while left < right:
        mid = int((left+right)/2)
        if in_list[mid] < key:
            left = mid + 1
        else:
            right = mid
    return left


def binary_search_right(n, key, in_list):
    left = 0
    right = n
    while left < right:
        mid = int((left+right)/2)
        if in_list[mid] <= key:
            left = mid + 1
        else:
            right = mid
    return left


if __name__ == "__main__":
    n = int(input())
    a_list = list(map(int, input().split()))
    b_list = list(map(int, input().split()))
    c_list = list(map(int, input().split()))
    a_list.sort()
    c_list.sort()
    ans = 0
    for b in b_list:
        a = binary_search_left(n, b, a_list)
        c = binary_search_right(n, b, c_list)
        ans += a*(n-c)
    print(ans)

Submission status: Accepted

Solution code:
import bisect

n = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
c_list = list(map(int, input().split()))
a_list.sort()
c_list.sort()
ans = 0
for b in b_list:
    a = bisect.bisect_left(a_list, b)
    c = bisect.bisect_right(c_list, b)
    ans += a*(n-c)
print(ans)

Submission status: Accepted

Solution code:
import itertools as ite
import math
import random

INF = 10 ** 18
MOD = 10 ** 9 + 7

N = input()
A = map(int, raw_input().split())
B = map(int, raw_input().split())
C = map(int, raw_input().split())
ls = []
for num in A:
    ls += [(num, 2)]
for num in B:
    ls += [(num, 1)]
for num in C:
    ls += [(num, 0)]
ls.sort()

sumA = sumB = 0
ans = 0
for num, tp in ls:
    if tp == 0:
        ans += sumB
    elif tp == 1:
        sumB += sumA
    else:
        sumA += 1
print ans

Submission status: Accepted

Solution code:
#binary_search
def isOK(lis, index, key):
    if lis[index] > key:
        return True
    else:
        return False
def binary_search(lis, key):
    ng = -1
    ok = len(lis)
    while abs(ok - ng) > 1:
        mid = int((ok + ng)/2)
        if isOK(lis, mid, key):
            ok = mid
        else:
            ng = mid
    return ok

N = int(input())
A = sorted([int(a) for a in input().split()])
B = sorted([int(b) for b in input().split()])
C = sorted([int(c) for c in input().split()])
COUNT = [0]
total = 0
for b in B[::-1]:
    index = binary_search(C, b)
    total += (N - index)
    COUNT.append(total)
COUNT = COUNT[::-1]
counter = 0
for a in A:
    index = binary_search(B, a)
    counter += COUNT[index]
print(counter)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(s) for s in input().split()]
B = [int(s) for s in input().split()]
C = [int(s) for s in input().split()]
A.sort()
B.sort()
C.sort()
def isOK(lis,index,key):
    if lis[index] > key:
        return True
    else:
        return False

def binary_search(lis,key):
    ng = -1
    ok = len(lis)

    while(abs(ok-ng)>1):
        mid = int((ok+ng)/2)
        if isOK(lis,mid,key):
            ok = mid
        else:
            ng = mid
    return ok

count = 0
count_lis = []
for b in B[::-1]:
    b_index = binary_search(C,b)
    if b_index != N:
        count += N - b_index
    count_lis.append(count)

count_lis = count_lis[::-1]
ans = 0
for a in A:
    a_index = binary_search(B,a)
    if a_index != N:
        ans += count_lis[a_index]
print(ans)

Submission status: Accepted

Solution code:
def binary_serch(target, num_list):
    ok = len(num_list)
    ng = -1

    while(abs(ok - ng) > 1):
        mid = int((ok + ng) / 2)
        mid_num = num_list[mid]
        if target < mid_num:
            ok = mid
        else:
            ng = mid

    return ok


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

    # index
    A2B = [binary_serch(a, B) for a in A]

    # 累積和
    B2C = []
    tmp = 0
    for b in B[::-1]:
        tmp = tmp + (N - binary_serch(b, C))
        B2C.append(tmp)

    B2C = B2C[::-1]

    ans = 0

    for i in range(N):
        if not A2B[i] == N:
            ans += B2C[A2B[i]]

    print(ans)

Submission status: Accepted

Solution code:
#binary_search
def isOK(lis, index, key):
    if lis[index] > key:
        return True
    else:
        return False
def binary_search(lis, key):
    ng = -1
    ok = len(lis)
    while abs(ok - ng) > 1:
        mid = int((ok + ng)/2)
        if isOK(lis, mid, key):
            ok = mid
        else:
            ng = mid
    return ok

N = int(input())
A = sorted([int(a) for a in input().split()])
B = sorted([int(b) for b in input().split()])
C = sorted([int(c) for c in input().split()])
COUNT = []
total = 0
for b in B[::-1]:
    index = binary_search(C, b)
    if index == N:
        total += 0
    else:
        total += (N - index)
    COUNT.append(total)
COUNT = COUNT[::-1]
counter = 0
for a in A:
    index = binary_search(B, a)
    if index != N:
        counter += COUNT[index]
print(counter)

Submission status: Accepted

Solution code:
import bisect

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

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

Submission status: Accepted

Solution code:
import bisect
n = int(input())
abc = [sorted(list(map(int, input().split()))) for i in range(3)]
print(sum([bisect.bisect_left(abc[0], b)*(n-bisect.bisect_right(abc[2], b)) for b in abc[1]]))

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

print(ans)

#73

Submission status: Accepted

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

cnt = 0
for j in range(N):
  target = B[j]
  insert_indexA = bisect.bisect_left(A, target)
  insert_indexC = bisect.bisect_right(C, target)
  cnt = cnt + insert_indexA * (len(C)-insert_indexC)

print(cnt)

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()
idxa = 0
idxc = 0
ans = 0
for b in range(N):
    while idxa < N and A[idxa] < B[b]:
        idxa += 1
    while idxc < N and B[b] >= C[idxc]:
        idxc += 1
    ans += idxa * (N - idxc)
print(ans)

Submission status: Accepted

Solution code:
# それぞれソート済みとして, 整数でAi<BjかつBj<Ckとなるjを求める(にぶたん)

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


def lower_bound(arr, target):
    # targetを超える最小の値のインデックス
    if target > arr[-1]:
        return None
    left, right = 0, len(arr)
    while left < right:
        mid = (left + right) // 2
        if arr[mid] > target:
            right = mid
        else:
            left = mid + 1
    return right


def upper_bound(arr, target):
    # target以上の最小の値のインデックス
    if target < arr[0]:
        return None
    left, right = 0, len(arr)
    while left < right:
        mid = (left + right) // 2
        if arr[mid] >= target:
            right = mid
        else:
            left = mid + 1
    return right


count = 0

for b in B:
    ai = upper_bound(A, b)
    ci = lower_bound(C, b)
    if ai is not None and ci is not None:
        count += ai * (N - ci)

print(count)

Submission status: Accepted

Solution code:

def main():
    num = int(input())
    data_a = list(map(int, input().split()))
    data_a.sort()
    data_b = list(map(int, input().split()))
    data_b.sort()
    data_c = list(map(int, input().split()))
    data_c.sort()

    shita_b = [0 for i in range(num)]

    ind = 0
    for i in range(num):
        now_b = data_b[i]
        while data_c[ind] <= now_b:
            ind += 1
            if ind >= num:
                ind = num
                break
        if ind == num:
            break
        shita_b[i] = num - ind


    ans = 0
    tasu_now = sum(shita_b)
    ind = 0
    for i in range(num):
        now_a = data_a[i]
        while now_a >= data_b[ind]:
            tasu_now -= shita_b[ind]
            ind += 1
            if ind >= num:
                ind = num
                break
        if ind == num:
            break
        ans += tasu_now

    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:


def main():
    num = int(input())
    data_a = list(map(int, input().split()))
    data_a.sort()
    data_b = list(map(int, input().split()))
    data_b.sort()
    data_c = list(map(int, input().split()))
    data_c.sort()

    shita_b = [0 for i in range(num)]

    ind = 0
    for i in range(num):
        now_b = data_b[i]
        while data_c[ind] <= now_b:
            ind += 1
            if ind >= num:
                ind = num
                break
        if ind == num:
            break
        shita_b[i] = num - ind


    ans = 0
    tasu_now = sum(shita_b)
    ind = 0
    for i in range(num):
        now_a = data_a[i]
        while now_a >= data_b[ind]:
            tasu_now -= shita_b[ind]
            ind += 1
            if ind >= num:
                ind = num
                break
        if ind == num:
            break
        ans += tasu_now

    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect


def main():
    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)


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
#Bの中の数iに対して、Aの中でiより小さい数の個数と、Cの中でiより大きい数の個数の積を探索
for i in B:
    ans += bisect_right(A, i-0.1) * (N-bisect_left(C, i+0.1))
print (ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right

n = int(input())
aa = sorted(map(int, input().split()))
bb = map(int, input().split())
cc = sorted(map(int, input().split()))

sum = 0
for b in bb:
  sum += bisect_right(aa, b-0.1) * (n - bisect_left(cc, b+0.1))
print(sum)

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int,input().split(" ")))
As.sort(reverse=True)
Bs = list(map(int,input().split(" ")))
Bs.sort(reverse=True)
Cs = list(map(int,input().split(" ")))
Cs.sort(reverse=True)
bcruiseki = [0]
abruiseki = [0]
j = 0
i = 0
while True:
    temp= bcruiseki[j]
    while i < N and Bs[j] <Cs[i]  :
        temp += 1
        i+= 1
    bcruiseki.append(temp)
    j += 1
    if j == N:
        break
j = 0
i = 0
while True:
    temp= abruiseki[j]
    while i < N and As[j] <Bs[i]  :
        temp += bcruiseki[i+1]
        i+= 1
    abruiseki.append(temp)
    j += 1
    if j == N:
        break

print(sum(abruiseki))

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial
from heapq import heappop, heappush, heappushpop
from bisect import bisect_left,bisect_right
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N = I()
    A = LI()
    B = LI()
    C = LI()
    A.sort()
    B.sort()
    C.sort()
    ans=0
    for b in B:
        ans += bisect_left(A, b) * (N-bisect_right(C, b))
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
A,B,C=[sorted(list(map(int,input().split()))) for _ in range(3)]
cnt=0
a,c=0,0
for b in range(n):
	while a<n and A[a]<B[b]:
		a+=1
	while c<n and B[b]>=C[c]:
		c+=1
	if c==n:break
	cnt+=a*(n-c)
print(cnt)

Submission status: Accepted

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

def bs_right(C, target):
    low, high = 0, len(C)
    while low<high:
        mid = (low+high)//2
        if target<C[mid]:
            high = mid
        else:
            low = mid+1
    return low
if __name__ == '__main__':
    N = int(input())

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

    count = 0
    for i in range(N):
        b = B[i]
        count = count + bs_left(A,b) * (N - bs_right(C,b))
    print(count)

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [ int(it) for it in input().split() ]
B = [ int(it) for it in input().split() ]
C = [ int(it) for it in input().split() ]
A.sort()
B.sort()
C.sort()

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

jj = 0
jF = False
for _j in range(N):
  if (A[0] < B[_j]):
    jj = _j
    break
  if (_j == N-1):
    jF = True
if ( not jF ):
  i,k=0,0
  s = 0
  Flag = True
  for j in range(N):

    if (i<N):
      for _ in range(N*N):
        if ( A[i] >= B[j] ):
          break
        i += 1
        if (i==N):
          break
    for _ in range(N*N):
      if ( B[j] < C[k] ):
        break
      k += 1
      if ( k == N ):
        Flag = False
        break
    if ( not Flag ):
      break
    #print ( i,j,k )
    s += i*( N-k )
  print (s)
else:
  print (0)

Submission status: Accepted

Solution code:
def binary_search_a(n, a, k):
    #a_i < kを満たすa_iの個数を返す関数

    lb = 0
    ub = n - 1

    if a[ub] < k:
        return n
    elif a[lb] >= k:
        return 0

    while (ub - lb > 1):
        mid = int((ub + lb) / 2)
        if a[mid] < k:
            lb = mid
        elif a[mid] >= k:
            ub = mid
    
    return lb + 1

def binary_search_c(n, c, k):
    #c_i > kを満たすc_iの個数を返す関数

    lb = 0
    ub = n - 1

    if c[ub] <= k:
        return 0
    elif c[lb] > k:
        return n

    while (ub - lb > 1):
        mid = int((ub + lb) / 2)
        if c[mid] > k:
            ub = mid
        elif c[mid] <= k:
            lb = mid
    
    return n - ub
        

def answer(n, a, b, c):
    ans = 0

    a.sort()
    c.sort()
    #bはソートしなくてもよい

    for k in b:
        ans += binary_search_a(n, a, k) * binary_search_c(n, c, k)
        #print(binary_search_a(n, a, k),binary_search_c(n, c, k))
    return ans


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

print(str(answer(n, a, b, c)))

Submission status: Accepted

Solution code:
from bisect import*
n,*t=map(int,open(0).read().split())
s=sorted
a,b,c=s(t[:n]),s(t[n:n+n]),s(t[n+n:])
x=[n-bisect(c,i)for i in b]+[0]
for i in range(n):x[-i-2]+=x[~i]
print(sum(x[bisect(b,i)]for i in a))

Submission status: Accepted

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

A.sort()
B.sort()
C.sort()
num_b = []
stim = [0]
cnt = 0
for i in B:
    ind = bisect.bisect_left(A,i)
    num_b.append(ind)

for i in num_b:
    stim.append(stim[-1]+i)

for i in C:
    ind = bisect.bisect_left(B,i)
    cnt += stim[ind]

print(cnt)

Submission status: Accepted

Solution code:
import bisect
from operator import mul

N = int(input())
A = sorted(map(int, input().split()))
B = sorted(map(int, input().split()))
C = sorted(map(int, input().split()))
# 中段より小さいパーツの個数
A_arr = [bisect.bisect_left(A, b) for b in B]
# 中段より大きいパーツの個数
C_arr = [N - bisect.bisect_right(C, b) for b in B]
# 組み合わせの総数
p = map(mul, A_arr, C_arr)
print(sum(p))