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: Time Limit Exceeded

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

count = 0
i = 0
while i < N:
    potition_j = bisect.bisect(B,A[i])
    for j in range(potition_j,N):
        potition = bisect.bisect(C,B[j])
        count += (N - potition)
    i += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = sorted(map(int, input().split()))
B = sorted(map(int, input().split()))
C = sorted(map(int, input().split()))
cnt = 0
for a in A:
    for b in B:
        if a >= b:
            continue
        cnt += sum(b < c for c in C)
print(cnt)

Submission status: Time Limit Exceeded

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

cnt = 0
for i in c:
    for j in b:
        if i <= j:
            break
        for k in a:
            if j <= k:
                break
            cnt += 1
            
print(cnt)

Submission status: Time Limit Exceeded

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

tmp = [0 for _ in range(n)]
a.sort()
b.sort()
c.sort()
for n, i in enumerate(b):
    hoge = 0
    for j in a:
        if i > j:
            hoge += 1
    tmp[n] = hoge

ans = 0
for i in c:
    for n, j in enumerate(b):
        if i > j:
            ans += tmp[n]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
kind = int(input())
top = input().split()
top = [int(x) for x in top]
top.sort()
middle = input().split()
middle = [int(x) for x in middle]
middle.sort()
down = input().split()
down = [int(x) for x in down]
down.sort()

res = 0
for i in range(0, len(down)):
    for j in range(0, len(middle)):
        if middle[j] >= down[i]:
            break
        for k in range(0, len(top)):
            if top[k] >= middle[j]:
                break
            elif top[k] < middle[j] < down[i]:
                res += 1
print(res)

Submission status: Time Limit Exceeded

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()
b_ceil = [n]*len(b)
b_floor = [n]*len(b)
count = 0
for i in range(len(b)):
    for j in range(len(a)):
        if a[j] >= b[i]:
            b_ceil[i] = j
            break
    for k in range(len(c)):
        if b[i] < c[k]:
            b_floor[i] = n-k
            break
    count += b_ceil[i] * b_floor[i]

print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
Al=sorted(list(map(int,input().split())))
Bl=sorted(list(map(int,input().split())))[::-1]
Cl=sorted(list(map(int,input().split())))[::-1]

#print(Al)
#print(Bl)
#print(Cl)

i=0
for A in Al:
    
    for B in Bl:
        if A>=B:
            #print(str(A)+':'+str(B))
            break
        else:
            for C in Cl:
                if B>=C:
                    break
                else:
                    #print(str(A)+':'+str(B)+':'+str(C)+':')
                    i+=1

print(i)

Submission status: Time Limit Exceeded

Solution code:
N = int(raw_input())
Ali = map(int,raw_input().split())
Bli = map(int,raw_input().split())
Cli = map(int,raw_input().split())
co=0
for A in Ali:
    for B in Bli:
        if A < B:
            for C in Cli:
                if B < C:
                    co +=1 
print(co)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = map(int,input().split())
a = sorted(a)[::-1]
b = map(int,input().split())
b = sorted(b)[::-1]
c = map(int,input().split())
c = sorted(c)[::-1]

bc = []
countb = 0
while(countb < n):
    bc.append(0)
    countc = 0
    while(countc < n):
        if b[countb] >= c[countc]:
            break
        bc[countb]+=1
        countc+=1
    countb+=1
ab = []
counta = 0
while(counta < n):
    ab.append(0)
    countb = 0
    while(countb < n):
        if a[counta] >= b[countb]:
            break
        ab[counta]+=bc[countb]
        countb+=1
    counta+=1

print(sum(ab))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
https://beta.atcoder.jp/contests/abc077/tasks/arc084_a

"""
import sys
from sys import stdin
from bisect import bisect_left, bisect_right
input = stdin.readline


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

    lut = {}
    for b in B:
        j = bisect_right(C, b)
        lut[b] = j


    ans = 0

    for a in A:
        i = bisect_right(B, a)
        if i == N:
            continue
        for b in B[i:]:
            # j = bisect_right(C, b)
            j = lut[b]
            if j == N:
                continue
            ans += (N-j)

    print(ans)



if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python

# AtCoder Beginner Contest 077
# C - Snuke Festival

from sys import stdin

def solve(As, Bs, Cs):
    # Bs :: [(b, # of bottom parts > b)]
    Bs = [(b, sum([1 for c in Cs if c > b])) for b in Bs]
    # As :: [# of mid & bottom parts > a]
    As = [sum([b[1] for b in Bs if b[0] > a]) for a in As]
    return sum(As)

N = int(stdin.readline())
As = [int(w) for w in stdin.readline().split()]
Bs = [int(w) for w in stdin.readline().split()]
Cs = [int(w) for w in stdin.readline().split()]
if len(As) != N or len(Bs) != N or len(Cs) != N: raise
print(solve(As, Bs, Cs))

Submission status: Time Limit Exceeded

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())))
num=0
for i in range(N):
    for j in range(N):
        if B[j]<=A[i]:
            pass
        else:
            for k in range(N):
                if C[k]<=B[j]:
                    pass
                else:
                    num += N-k
                    break
print(num)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math

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

array1 = list()
for b in B:
    count = 0
    for c in C:
        if b < c:
            count += 1
    array1.append(count)
ans = 0
for i in range(N):
    count = 0
    for a in A:
        if B[i] > a:
            count += 1
    ans += count * array1[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right
n = input()
a = sorted([int(x) for x in raw_input().split()])
b = sorted([int(x) for x in raw_input().split()])
c = sorted([int(x) for x in raw_input().split()])
ans = 0
for ai in a:
	index = bisect_right(b, ai)
	for bi in b[index:]:
		ans += (n - bisect_right(c, bi))
print ans

Submission status: Time Limit Exceeded

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

result = 0
for c in C:
    for b in B:
        if b < c:
            for a in A:
                if a < b:
                    result += 1

print(result)

Submission status: Time Limit Exceeded

Solution code:
def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))
    
    A_cand = [0 for i in range(N)]
    B_cand = [0 for i in range(N)]
    for i, B_i in enumerate(B):
        cnt = 0
        for C_i in C:
            if B_i < C_i:
                cnt += 1
        B_cand[i] = cnt
    
    for i, A_i in enumerate(A):
        cnt = 0
        for j, B_j in enumerate(B):
            if A_i < B_j:
                cnt += B_cand[j]
        A_cand[i] = cnt
    
    print(sum(A_cand))
    

solve()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import copy as cp

N = int(input())
A = input().split()
B = input().split()
C = input().split()
A = np.array([int(i) for i in A])
B = np.array([int(i) for i in B])
C  =np.array([int(i) for i in C])

rst = 0
for i in range(N) : 
    a = A[i]
    b = B[B > a]
    for k in b : 
        c = C[C > k]
        x = len(c)
        rst += x
print(rst)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(raw_input())
A = Counter(map(int, raw_input().split(' ')))
B = Counter(map(int, raw_input().split(' ')))
C = Counter(map(int, raw_input().split(' ')))

count = 0
for a in A:
    for b in filter((lambda x: x > a), B):
        for c in filter((lambda x: x > b), C):
            count += A[a] * B[b] * C[c]

print count

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
pa,pb,pc=[sorted([int(s)for s in input().split(" ")])for i in range(3)]
pnb,pna=[[0 for i in range(n)]for j in range(2)]
for i in range(n):
    for j in range(n):
        if pa[j]>=pb[i]:
            pna[i]=j
            break
    else:pna[i]=n
for i in range(n):
    for j in range(n):
        if pb[j]>=pc[i]:
            pnb[i]=j
            break
    else:pnb[i]=n
total=0
for i in range(n):
    for j in range(pnb[i]):
        total+=pna[j]
print(total)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
C = [int(x) for x in input().split()]
ans = 0

for x in A:
	
	for y in B:
		a = y

		# while( (x==a) and (ntemp!=0) ):
			# a += 1
			# ntemp -= 1
			
		if (a > x):
		
			for z in C:
			
				b = z
				
				# while( (b==a) and (ntemp!=0) ):
					# b += 1
					# ntemp -= 1
					
				if ( (b > a) and (a > x) ):
					ans += 1
					
					# print(x, a, b)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#http://abc077.contest.atcoder.jp/
# -*- coding: utf-8 -*-

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

for i in b[:]:
    if i <= a[0]: b.remove(i)
for i in c[:]:
    if i <= a[0]: c.remove(i)

counter = 0
for ai in a:
    for bi in b:
        if bi <= ai: continue
        for ci in c:
            if ci <= bi : continue
            counter += 1
print(counter)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

ans = 0

for i in range(n):
    for j in range(n):
            for k in range(n):
                if a[i] < b[j] < c[k]:
                    ans = ans + 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A, B, C = [list(sorted(map(int, input().split())))  for _ in range(3)]
cnt = 0
# O(3*N) でギリギリ？pythonだと微妙かも
for a in A:
    for b in B:
        for c in C:
            if a<b<c and len(set([a, b, c]))>1:
                cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

ans = 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]:
                    ans = ans + 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#http://abc077.contest.atcoder.jp/
# -*- coding: utf-8 -*-

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

counter = 0
for ai in a:
    for bi in b:
        for ci in c:
            if ai<bi<ci : counter += 1

print(counter)

Submission status: Memory Limit Exceeded

Solution code:
import bisect, functools, sys
sys.setrecursionlimit(10**6)
N = int(input())
A = sorted(int(x) for x in input().split())
B = sorted(int(x) for x in input().split())
C = sorted(int(x) for x in input().split())

@functools.lru_cache(maxsize=None)
def a(n):
    return bisect.bisect_left(A, n)

@functools.lru_cache(maxsize=None)
def b_i(i):
    if i >= 1:
        return b_i(i - 1) + a(B[i - 1])
    return 0

@functools.lru_cache(maxsize=None)
def b(n):
    return b_i(min(len(B), bisect.bisect_left(B, n)))

def c():
    return sum(b(x) for x in C)

print(c())

Submission status: Accepted

Solution code:
def II(): return int(input())
def LII(): return list(map(int, input().split()))

n=II()
alist=LII()
blist=LII()
clist=LII()

alist.sort()
blist.sort()
clist.sort()

#あるbより小さいaの数
def count_a(b):
    ok,ng = -1,n
    def is_ok(i):
        return alist[i]<b
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if is_ok(mid):
            ok = mid
        else:
            ng = mid
    return ok+1

def count_c(b):
    ok,ng = n,-1
    def is_ok(i):
        return clist[i]>b
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if is_ok(mid):
            ok = mid
        else:
            ng = mid
    return n-ok

result=0
for b in blist:
    result += count_a(b) * count_c(b)

print(result)

Submission status: Accepted

Solution code:
import bisect

import sys
stdin=sys.stdin

ip=lambda: int(sp())
fp=lambda: float(sp())
lp=lambda:list(map(int,stdin.readline().split()))
sp=lambda:stdin.readline().rstrip()
yp=lambda:print('Yes')
np=lambda:print('No')

n=ip()
a=lp()
b=lp()
c=lp()

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

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

Submission status: Accepted

Solution code:
#
# abc077 c
#
import sys
from io import StringIO
import unittest
import bisect


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """2
1 5
2 4
3 6"""
        output = """3"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """3
1 1 1
2 2 2
3 3 3"""
        output = """27"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """6
3 14 159 2 6 53
58 9 79 323 84 6
2643 383 2 79 50 288"""
        output = """87"""
        self.assertIO(input, output)


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

    print(ans)


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
import bisect

N = int(input())
al = [int(a) for a in input().split()]
bl = [int(b) for b in input().split()]
cl = [int(c) for c in input().split()]

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

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 j in range(n):
    i = bisect.bisect_left(A, B[j])
    k = n - bisect.bisect_right(C, B[j])
    ans += int(i*k)
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 elem in b:
    id_a=bisect.bisect_left(a,elem)
    id_c=bisect.bisect(c,elem)
    ans+=id_a*(n-id_c)

print(ans)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
top = list(map(int,input().split()))
middle = list(map(int,input().split()))	
bottom = list(map(int,input().split()))	

top.sort()
middle.sort()
bottom.sort()

ans = 0
for i in middle:
  can_put_upstair = bisect.bisect_left(top,i)
  can_put_downstair = N - bisect.bisect_right(bottom,i)
  #print(can_put_upstair,can_put_downstair)
  ans += can_put_upstair*can_put_downstair

print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
#readlines = sys.stdin.readlines
from bisect import bisect, bisect_left
from itertools import accumulate
def main():
    data = list(map(int, read().split()))
    n = data[0]
    a = data[1:n + 1]
    b = data[n + 1: n * 2 + 1]
    c = data[n * 2 + 1:]
    a.sort()
    b.sort()
    c.sort()
    b2 = [bisect_left(a, be) for be in b]
    b2a = list(accumulate(b2))
    b2a = [0] + b2a
    ans = sum([b2a[bisect_left(b, ce)] for ce in c])
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
# import random


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
# N = 10 ** 5
# A = [random.randint(1, 10**9) for _ in range(N)]
# B = [random.randint(1, 10**9) for _ in range(N)]
# C = [random.randint(1, 10**9) for _ in range(N)]

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

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

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect
import random


def binary_search(L, n, i, j):
    # print(L, n, i, j)
    low = i
    high = j

    while low <= high:
        mid = (low + high) //2
        guess = L[mid]
        # if guess == n:
            # return mid
        if guess > n:
            high = mid -1
        else:
            low = mid + 1
    # print(low)
    return low

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
# N = 10 ** 5
# A = [random.randint(1, 10**9) for _ in range(N)]
# B = [random.randint(1, 10**9) for _ in range(N)]
# C = [random.randint(1, 10**9) for _ in range(N)]

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

ans = 0
i, j = 0, 0

A_ind = []
B_cum = [0]

for a in A:
    # i = binary_search(B, a, i, N-1)
    i = bisect(B, a)
    A_ind.append(i)

s = 0
for b in B:
    # j = binary_search(C, b, j, N-1)
    j = i = bisect(C, b)
    s += N-j
    B_cum.append(s)

for a in A_ind:
    ans += B_cum[-1] - B_cum[a]
print(ans)

Submission status: Accepted

Solution code:
import bisect
 
n = int(input())
A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
C = sorted(list(map(int, input().split())))
ans = 0
 
for b in B:
    a = bisect.bisect_left(A, b)
    c = bisect.bisect_right(C, b)
    ans += a*(n-c)
 
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 b:
    ans+=(n-bisect.bisect_right(c,i))*bisect.bisect_left(a,i)

print(ans)

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()
 
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())
b1=0
d=0

ans=0
 
A=list(map(int, input().split()))
A=sorted(A)
B=list(map(int, input().split()))
B=sorted(B, reverse=True)
C=list(map(int, input().split()))
C=sorted(C)
D=[]
E=[]

for b in B:
  d=d+len(C)-bisect.bisect_right(C,b)
  D.append(d)

D=sorted(D, reverse= True)  
B=sorted(B)

for a in A:
  if 0 <= bisect.bisect_right(B,a) <= len(D)-1:
    ans += D[bisect.bisect_right(B,a)]
print(ans)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())))
c = sorted(list(map(int, input().split())))
res = 0
for bi in b:
    aUpper = bisect.bisect_left(a, bi)
    cLower = len(c) - bisect.bisect_right(c, bi)
    res += (aUpper * cLower)
print(res)

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

A_cnt = [1] * N
A_sum = [i+1 for i in range(N)]
A_sum.append(0)
B_cnt = [0] * N
B_sum = [0] * N
C_cnt = [0] * N
C_sum = [0] * N

for i in range(N):
    key = B[i]
    ok = -1
    ng = N
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if A[mid] < key:
            ok = mid
        else:
            ng = mid
    B_cnt[i] = A_sum[ok]

B_sum[0] = B_cnt[0]
for i in range(1, N):
    B_sum[i] = B_sum[i-1] + B_cnt[i]
B_sum.append(0)

for i in range(N):
    key = C[i]
    ok = -1
    ng = N
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if B[mid] < key:
            ok = mid
        else:
            ng = mid
    C_cnt[i] = B_sum[ok]

C_sum[0] = C_cnt[0]
for i in range(1, N):
    C_sum[i] = C_sum[i-1] + C_cnt[i]

print(C_sum[-1])

# print(A)
# print(B)
# print(C)
# print(A_cnt)
# print(B_cnt)
# print(C_cnt)
# print(A_sum)
# print(B_sum)
# print(C_sum)

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 b:
    p1 = bisect_left(a, i)
    p2 = bisect_right(c, i)

    ans += p1*(n-p2)

print(ans)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def SL(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def R(n): return [sys.stdin.readline().strip() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SLR(n): return [SL() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]

def perm(n, r): return math.factorial(n) // math.factorial(r)
def comb(n, r): return math.factorial(n) // (math.factorial(r) * math.factorial(n-r))

def make_list(n, *args, default=0): return [make_list(*args, default=default) for _ in range(n)] if args else [default for _ in range(n)]

dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
alphabets = "abcdefghijklmnopqrstuvwxyz"
ALPHABETS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
MOD = 1000000007
INF = float("inf")

sys.setrecursionlimit(1000000)

def main():
    N = I()
    A, B, C = sorted(LI()), sorted(LI()), sorted(LI())

    ans = 0
    for bi in B:
        left, right = -1, N
        while right - left > 1:
            mid = (right + left) // 2
            if A[mid] < bi: left = mid
            else: right = mid
        tmp = left
        left, right = -1, N
        while right - left > 1:
            mid = (left + right) // 2
            if C[mid] > bi: right = mid
            else: left = mid
        ans += (tmp + 1) * (N - right)

    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def SL(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def R(n): return [sys.stdin.readline().strip() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SLR(n): return [SL() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]

def perm(n, r): return math.factorial(n) // math.factorial(r)
def comb(n, r): return math.factorial(n) // (math.factorial(r) * math.factorial(n-r))

def make_list(n, *args, default=0): return [make_list(*args, default=default) for _ in range(n)] if args else [default for _ in range(n)]

dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
alphabets = "abcdefghijklmnopqrstuvwxyz"
ALPHABETS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
MOD = 1000000007
INF = float("inf")

sys.setrecursionlimit(1000000)

def main():
    N = I()
    A, B, C = sorted(LI()), sorted(LI()), sorted(LI())

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



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def lb(k, a):
    n = len(a)
    l, u = 0, n
    while l < u:
        m = (l + u) // 2
        if k < a[m]:
            u = m
        else:
            l = m + 1
    return l

def ub(k, a):
    n = len(a)
    l, u = -1, n - 1
    while l < u:
        m = (l + u + 1) // 2
        if a[m] < k:
            l = m
        else:
            u = m - 1
    return l

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

res = 0
for i in range(N):
    j = ub(B[i], A)
    k = lb(B[i], C)
    res += (j + 1)*(N - k)

print(res)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))
cl = list(map(int, input().split()))
al.sort()
bl.sort()
cl.sort()
ans = 0

for b in bl:
    ca = bisect.bisect_left(al, b)
    cc = n-bisect.bisect_right(cl, b)
    ans += ca*cc
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 i in range(n):
    aa = bisect.bisect_left(a,b[i])
    ca = n-bisect.bisect_right(c,b[i])
    ans += aa * ca
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()
from bisect import *
ans = 0
for b in B:
  a = bisect_left(A,b)
  c = N-bisect_right(C,b)
  ans += a*c
print(ans)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
X = sorted(list(map(int,input().split())))
Y = sorted(list(map(int,input().split())))
Z = sorted(list(map(int,input().split())))
print(sum(bisect.bisect_left(X,b) * (n-bisect.bisect_right(Z,b)) for b in Y))

Submission status: Accepted

Solution code:
import math , sys, 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() ) ) )

Bc = []

for i in range(N):
    X = B[i]

    count = bisect.bisect_left( A , X )
    Bc.append( count )
#print(A, B, C)
#print(Bc)
s = 0
Ec = [0]
for i in range(N):
    s += Bc[i]
    Ec.append(s)
#print(Ec)
ans = 0
for i in range(N):
    X = C[i]
    ind = bisect.bisect_left( B , X )
    ans += Ec[ind]
    #print(ind)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())))
c = sorted(list(map(int, input().split())))
cnt = 0
for i in b:
    left = -1
    right = len(a) 
    while right - left > 1:
        mid = (right + left) //2 
        if a[mid] <  i:
            left = mid 
        else :
            right = mid 
    left2 = -1
    right2 = len(c)
    while right2 - left2 > 1:
        mid2 = (right2 + left2) //2
        if c[mid2] > i:
            right2 = mid2
        else :
            left2 = mid2
    cnt += (left+1)*(n-right2)

print(cnt)

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

Submission status: Accepted

Solution code:
import math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]

def main():
    n = i_input()
    a = i_list()
    b = i_list()
    c = i_list()
    a.sort()
    b.sort()
    c.sort()

    al = []
    bl = [0]
    import bisect

    for k,i in enumerate(b):
        x = bisect.bisect(c, i)
        bl.append(bl[k]+(n-x))

    ans = 0

    for i in a:
        x = bisect.bisect(b,i)
        ans += bl[-1] - bl[x]
    print(ans)




if __name__=="__main__":
    main()