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:
N = int(input())
*A, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())

ans = 0
for i in range(N):
    for j in range(N):
        if B[i] <= A[j]:
            continue
        for k in range(N):
            if B[i] < C[k]:
                ans += 1
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()))

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

print(ans)

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

ans = 0
for i in range(N):
    a=A[i]
    nibanme = bisect.bisect_right(B,a)
    for j in range(nibanme,N):
        b=B[j]
        sanbanme = bisect.bisect_right(C,b)
        ans += N-sanbanme

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

ans = 0
for i in A:
    for k in B[bisect.bisect_right(B,i):]:
        ans += D - bisect.bisect_right(C,k)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

ans = 0
for i in A:
    for k in B:
        if k <= i:
            continue
        ans += D - bisect.bisect_right(C,k)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
n = int(input())
list_A = list(map(int, input().split()))
list_B = list(map(int, input().split()))
list_C = list(map(int, input().split()))
list_A.sort()
list_B.sort()
list_C.sort()
ans = 0
list_U = []
list_D = []

for i in range(n):
    b = list_B[i]
    index = bisect.bisect_left(list_A, b)
    list_U.append(index)

for i in range(n):
    c = list_C[i]
    index = bisect.bisect_left(list_B, c)
    list_D.append(index)

for i in range(n):
    j = list_D[i]
    for k in range(j):
        ans += list_U[k]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
n = int(input())
list_A = list(map(int, input().split()))
list_B = list(map(int, input().split()))
list_C = list(map(int, input().split()))
list_A.append(pow(10, 18))
list_B.append(pow(10, 18))
list_A.sort()
list_B.sort()
list_C.sort()
ans = 0

for i in range(n):
    c = list_C[i]
    j = bisect.bisect_left(list_B, c)
    for k in range(j):
        b = list_B[k]
        ans += bisect.bisect_left(list_A, b)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
arr=[list(map(int,input().split())) for i in range(3)]

count=0

for i in range(N):
    for j in range(N):

        if arr[0][i]<arr[1][j]:
            for k in range(N):

                if arr[1][j]<arr[2][k]:
                    count+=1

print(count)

Submission status: Time Limit Exceeded

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

#%%

def isok(index, key, arr):
    if arr == A and arr[index] >= key:
        return True
    elif arr == C and arr[index] > key:
        return True
    else:
        return False
 
def Binary_search(key, arr):
    
    ng = -1
    ok = len(arr)
    while ok - ng > 1:
        mid = (ok + ng) // 2
        if isok(mid, key, arr) is True:
            ok = mid
        else:
            ng = mid
    
    return ng, ok

cnt = 0
for i in B:
    ng_a, ok_b = Binary_search(i, A)
    ng_c, ok_c = Binary_search(i, C)
    cnt += (ng_a + 1) * (N - ok_c)
print(cnt)

Submission status: Time Limit Exceeded

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

#%%
def isok(index, key, arr):
    if arr[index] > key:
        return True
    else:
        return False

def Binary_search(key, arr):
    
    ng = -1
    ok = len(arr)
    while ok - ng > 1:
        mid = (ok + ng) // 2
        if isok(mid, key, arr) is True:
            ok = mid
        else:
            ng = mid
    
    return ng, ok

cnt = 0
for i in A:
    ng, ok = Binary_search(i, B)
    for j in B[ok:]:
        ng_C, ok_C = Binary_search(j, C)
        cnt += len(C[ok_C: ])

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
b=list(map(int,input().split()))
b.sort()
c=list(map(int,input().split()))
c.sort()
ans=0
import bisect
for bb in b:
  ao=bisect.bisect(a,bb-1)
  cnta=len(a[:ao])
  cs=bisect.bisect(c,bb)
  cntc=len(c[cs:])
  ans+=cnta*cntc
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
b=list(map(int,input().split()))
b.sort()
c=list(map(int,input().split()))
c.sort()
ans=0
import bisect
for aa in a:
  bs=bisect.bisect(b,aa)
  if bs==n:
    break
  for bb in b[bs:]:
    cs=bisect.bisect(c,bb)
    if cs==n:
      break
    ans+=len(c[cs:])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

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 range(n):
    num_c = c[i]
    b_idx = bisect_left(b,num_c)
    for j in range(b_idx):
        num_b = b[j]
        a_idx = bisect_left(a, num_b)
        cnt += a_idx
print(cnt)

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

def binary_search(list_a,item):
  low=0
  high=len(list_a)-1
  flag=True
  while low<=high:
    mid=int((low+high)/2)
    guss=list_a[mid]
    if guss>item:
      high=mid-1
    elif guss<item:
      low=mid+1
    elif guss==item:
      return mid
  return mid

count=0
for a in A:
  p=binary_search(B,a)
  if B[p]>a:
    temp_p=p
  elif B[p]<=a:
    temp_p=p+1
  for b in B[temp_p:]:
    q=binary_search(C,b)
    if C[q]>b:
      temp_q=q
    elif C[q]<=b:
      temp_q=q+1
    count+=(N-temp_q)


print(count)

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

count=0
for a in A:
  for b in B:
    if a>=b:
      continue
    num=0
    for c in C:
      if b<c:
        count+=(N-num)
        break
      num+=1


print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys


def main():
        n = sys.stdin.readline()

        astr = sys.stdin.readline()
        bstr = sys.stdin.readline()
        cstr = sys.stdin.readline()

        a = map(int, astr.strip().split(' '))
        b = map(int, bstr.strip().split(' '))
        c = map(int, cstr.strip().split(' '))

        a.sort()
        b.sort()
        c.sort()

        cnt = 0

        for ai in a:
                bt = b[binary(ai, b):]
                for bi in bt:
                        ct = c[binary(bi, c):]
                        cnt = cnt + len(ct)

        print(cnt)


def binary(x, li):
    upper = len(li)
    lower = -1
    index = (upper + lower) / 2


    while upper - lower > 1:
        if (x < li[index]):
            upper = index
        else:
            lower = index
        index = (upper + lower)/2
    
    return upper


main()

Submission status: Time Limit Exceeded

Solution code:
import sys

n = sys.stdin.readline()

astr = sys.stdin.readline()
bstr = sys.stdin.readline()
cstr = sys.stdin.readline()

a = map(int, astr.strip().split(' '))
b = map(int, bstr.strip().split(' '))
c = map(int, cstr.strip().split(' '))

a.sort()
b.sort()
c.sort()

cnt = 0

for ai in a:
        bt = filter(lambda x:x > ai, b)
        for bi in bt:
                ct = filter(lambda x:x > bi, c)
                cnt = cnt + len(ct)

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import bisect

n = int(input())

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

ans = 0

for a in A:
  i = bisect.bisect_right(B, a)
  for b in B[i:]:
    j = bisect.bisect_right(C, b)
    ans += n-j
    
print(ans)

Submission status: Time Limit Exceeded

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

count=0
for i in A :
    if i>max(B):
        break
    D=[num for num in B if num>i]
    for j in D:  
        if j>max(C) :
            break  
        b=bisect.bisect_right(C,j)
        t=N-b            #b<cを満たすcの数
        count+=t

print(count)

Submission status: Time Limit Exceeded

Solution code:
import bisect

N=int(input())
A=list(map(int,input().split()))
A=sorted(A)
B=list(map(int,input().split()))
B=sorted(B)
C=list(map(int,input().split()))
C=sorted(C)
count=0
for num in (A):
    a=bisect.bisect_right(B,num)
    for i in range(a,len(B)):
        K=B[i]
        l=bisect.bisect_right(C,K)
        count+=len(C)-l

print(count)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

def get_input() -> tuple:
    """
    標準入力を取得する.

    Returns:\n
        tuple: 標準入力
    """
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))

    return N, A, B, C


def binary_search(l: list, key: int) -> int:
    """
    l[index] > keyとなる最小のindexを求める.

    Args:\n
        l (list): リスト
        key (int): 整数

    Returns:\n
        int: l[index] > keyとなる最小のindex
    """
    low = -1
    high = len(l)
    while 1 < high - low:
        mid = (low + high) // 2
        guess = l[mid]
        if guess > key:
            high = mid
        else:
            low = mid
    return high


def main(N: int, A: list, B: list, C: list) -> None:
    """
    メイン処理.

    Args:\n
        N (int): パーツの数
        A (list): 上部のパーツのサイズ
        B (list): 中部のパーツのサイズ
        C (list): 下部のパーツのサイズ
    """
    # 事前にリストをソートしておく
    A = sorted(A)
    B = sorted(B)
    C = sorted(C)

    # 求解処理
    ans = 0
    for i in range(N):
        A_i = A[i]
        lb_B = binary_search(B, A_i)
        for j in range(lb_B, N):
            B_j = B[j]
            lb_C = binary_search(C, B_j)
            ans += N - lb_C

    # 結果出力
    print(ans)


if __name__ == "__main__":
    # 標準入力を取得
    N, A, B, C = get_input()

    # メイン処理
    main(N, A, B, C)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# 標準入力を取得
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

A = sorted(A)
B = sorted(B)
C = sorted(C)


def binary_search(l: list, item: int) -> int:
    low = -1
    high = len(l)
    while 1 < high - low:
        mid = (low + high) // 2
        guess = l[mid]
        if guess > item:
            high = mid
        else:
            low = mid
    return high


# 求解処理
ans = 0
for i in range(N):
    A_i = A[i]
    lb_B = binary_search(B, A_i)
    for j in range(lb_B, N):
        B_j = B[j]
        lb_C = binary_search(C, B_j)
        ans += N - lb_C

# 結果出力
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()))
al=sorted(a)
bl=sorted(b)
cl=sorted(c)
 
abl=[]
i=0
for j in range(n):
    i=n-1
    while al[i]>=bl[j]:
        i-=1
        if i==-1:
            break
    if i==-1:
        abl.append(0)
    else:
        abl.append(i+1)
bcl=[]
j=0
for j in range(n):
    k=0
    while cl[k]<=bl[j]:
        k+=1
        if k==n:
            break
    if k==n:
        bcl.append(0)
    else:
        bcl.append(n-k)
ans=0
for s in range(n):
    ans+=abl[s]*bcl[s]
print(ans)

Submission status: Time Limit Exceeded

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 a in A:
    index_b = bisect.bisect_right(B, a)
    for b in B[index_b:]:
        index_c = bisect.bisect_right(C, b)
        cnt += n-index_c

print(cnt)

Submission status: Time Limit Exceeded

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):
    j=bisect.bisect_right(b,a[i])
    for x in b[j:]:
        k=n-bisect.bisect_right(c,x)
        ans+=k
print(ans)

Submission status: Time Limit Exceeded

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())))
counter = 0
for i in range(len(A)):
    b_index = bisect.bisect(B,A[i])
    if b_index >= len(B):
        break
    b_diam = B[b_index]
    for j in range(b_index,len(B)):
        c_index = bisect.bisect(C,B[j])
        if c_index >= len(C):
            break
        counter += len(C)-c_index
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()))
A.sort()
B.sort()
B = [0] + B
C.sort()
C = [0] + C
# A > B > Cなる組み合わせの数を求める
ans = 0

for a in A:
    left, right = 0, N
    if a >= B[N]:
        continue
    while abs(left - right) > 1:
        mid = (left + right) // 2
        if B[mid] > a:
            right = mid
        else:
            left = mid
    nB = B[right:]
    for nb in nB:
        left2, right2 = 0, N
        if nb >= C[N]:
            continue
        while abs(left2 - right2) > 1:
            mid2 = (left2 + right2) // 2
            if C[mid2] > nb:
                right2 = mid2
            else:
                left2 = mid2
        ans += N - right2 + 1
print(ans)

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())))
ans = 0
for i in range(N):#A[i]固定
    ng = -1#A[i]以下
    ok = N#A[i]より大きい
    while ok-ng >1:#A[i]より大きいBのインデックスを探す
        mid = (ng+ok)//2
        if B[mid]>A[i]:
            ok = mid
        else:
            ng = mid
    #okはA[i]より大きい値で最も小さい
    #ngはA[i]より以下の値で最も大きい
    for j in range(ok,N):#B[j]固定
        ng2 = -1#B[j]以下
        ok2 = N#B[j]より大きい
        while ok2-ng2 > 1:#B[j]より大きいCのインデックスを探す
            mid2 = (ng2 + ok2)//2
            if C[mid2] > B[j]:
                ok2 = mid2
            else:
                ng2 = mid2
        ans += N-1 -(ok2-1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

ans = 0
for a in aa:
    bi = bisect.bisect(bb, a)
    bx = len(bb) - bi
    for b in range(bi, len(bb)):
        ci = bisect.bisect(cc, bb[b])
        cx = len(cc) - ci
        ans += cx

print(ans)

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

#mode=bなら、b用、mode=cならc用
def get_poss(i,nmrs,n,mode,test):
    #print("mode:"+str(mode))
    poss = 0
    poss_c = 0
    left = 0
    right = n
    while left < right:
        mid = (left + right) // 2
        if nmrs[mid] == i:
            if mode=='c':
                #print(test+":"+str(nmrs[mid+1:]))
                return (n-mid-1)
            else:
                break
        elif nmrs[mid] < i:
            left = mid + 1
        else:
            right = mid
    #print("mid is "+str(mid))
    if nmrs[mid] <= i:
        mid += 1
    poss += n - mid
    #print("i:"+str(i)+" "+"mid:"+str(mid)+" "+"nmrs:"+str(nmrs)+" "+"mode:"+str(mode))
    if mode == 'c':
        #print(test +":"+ str(nmrs[mid:]))
        #print("returning c:"+str(poss))
        return poss
    #print("b has " + str(len(range(mid,n))))
    for bi in range(mid,n):
        #print("when b is " + str(nmrs[bi]))
        #print("bi:"+str(bi))
        poss_c_tmp = get_poss(nmrs[bi],c,n,'c',test+":"+str(nmrs[bi]))
        poss_c += poss_c_tmp
        #print("there are " + str(poss_c_tmp) +" cs")
    #print("returning b:" + str(poss_c))
    return poss_c

poss_b = 0
for ai in a:
    #print("when a is "+str(ai))
    poss_b += get_poss(ai,b,n,'b',str(ai))
print(poss_b)

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()
lb = [0] * len(B)
for i in range(len(B)):
    lb[i]= bisect.bisect_left(A,B[i])

ans = 0    
for i in range(len(C)):
    temp = bisect.bisect_left(B,C[i])
    ans +=sum(lb[0:temp])


print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
N = int(input())
A = sorted(list(map(int,input().split())))
B = sorted(list(map(int,input().split())))
C = sorted(list(map(int,input().split())))
count = 0
for a in A:
    b = bisect.bisect_left(B,a+1)
    if b == N:
        continue
    for i in range(b,N):
        c = bisect.bisect_left(C,B[i]+1)
        if c == N:
            continue
        count += N-c
print(count)

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

for i in a:
  indb = bisect.bisect_right(b,i)
  for j in range(indb,len(b)):
    indc = bisect.bisect_right(c,b[j])
    ans += n - indc
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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)

result = 0
for i in c:
    sb = bisect.bisect_left(b, i)
    for j in b[:sb]:
        sa = bisect.bisect_left(a,j)
        result += len(a[:sa])

print(result)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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)

result = 0
for i in a:
    sb = bisect.bisect_right(b, i)
    for j in b[sb:]:
        sc = bisect.bisect_right(c,j)
        result += len(c[sc:])

print(result)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right
def main():
    n = int(input())
    A = sorted(map(int,input().split()))
    B = sorted(map(int,input().split()))
    C = sorted(map(int,input().split()))
    ans = 0
    for a in A:
        for bi in range(bisect_right(B, a), len(B)):
            ans += len(C) - bisect_right(C, B[bi])
    print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right
def main():
    n = int(input())
    A = sorted(map(int,input().split()))
    B = sorted(map(int,input().split()))
    C = sorted(map(int,input().split()))
    ans = 0
    for a in A:
        for bi in range(bisect_right(B, a), len(B)):
            for ci in range(bisect_right(C, B[bi]), len(C)):
                ans += 1
    print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools
def main():
    n = int(input())
    a = map(int,input().split())
    b = map(int,input().split())
    c = map(int,input().split())
    ans = []
    for x in itertools.product(a,b,c):
        if x[0] < x[1] < x[2]:
            ans.append(x)
    print(len(ans))
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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())))
sm=0
for i in C:
  b=bisect.bisect_left(B,i)
  for j in B[:b]:
    sm+=bisect.bisect_left(A,j)
print(sm)

Submission status: Time Limit Exceeded

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    A, B, C = sorted(il()), sorted(il()), sorted(il())

    ret = 0
    for a in A:
        b = bisect.bisect_left(B, a+1)
        if len(B) == b: break
        for mi in B[b:]:
            c = bisect.bisect_left(C, mi+1)
            ret += len(C) - c
    print(ret)


if __name__ == '__main__':
    main()

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

ans = 0
for i in range(N):
    up = A[i]
    middleIndex = bisect.bisect(B, up)
    if middleIndex < N:
        for j in range(middleIndex, N):
            middle = B[j]
            lowIndex = bisect.bisect(C, middle)
            if lowIndex < N:
                cnt = N - lowIndex
                ans += cnt

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

low = 0
high = len(A) -1

def check(mid, parts,key):
    if parts[mid] > key:
        return True
    return False
cnt = 0

for a in A:

    B_low = -1
    B_high = len(B) - 1
    b_cnt = 0
    if a >= B[B_high]:
        break
    while B_high -B_low > 1:
        mid = (B_high + B_low)//2

        if check(mid, B, a):
            B_high = mid
            # b_cnt = _cnt

        else:
            B_low = mid
    for b in B[B_high::]:
        C_high = len(C) - 1
        C_low = -1
        c_cnt = 0
        if b >= C[C_high]:
            break
        while C_high -C_low > 1:
            mid = (C_high + C_low) //2
            if check(mid,C ,b):
                C_high = mid
            else:
                C_low= mid
        # print(b, C_high, a)
        # print(len(C[C_high::]))
        cnt+=len(C[C_high::])

    # b_cnt = len(B) - B_high if B[B_high] > a  else 0
    # C_high = len(C) -1
    # C_low = -1
    # c_cnt = 0
    # if b_cnt >0:

    #     while C_high -C_low >1 :
    #         mid = (C_high + C_low )//2
    #         # _cnt = check(mid,C,B_high)
    #         if check(mid,C,B[B_high]):
    #             C_high  =mid
    #             # c_cnt = _cnt
    #         else:
    #             C_low = mid
    #     c_cnt = len(C) - C_high if C[C_high] > B[B_high] else 0
    # print(a,b_cnt,c_cnt)
    #
    # cnt += b_cnt * c_cnt
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()
canB=[0]*N
canC=[0]*N
p1=0
p2=0
total=0
for j in range(N):
    c=C[j]
    while p2<N and B[p2]<c:
        while p1<N and A[p1]<B[p2]:
            canB[p2]+=1
            p1+=1
        p2+=1
        if p2<N:
            canB[p2]=canB[p2-1]
    canC[j]=sum(canB[:p2])
print(sum(canC))

Submission status: Time Limit Exceeded

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())))
D=[0]*N
E=[0]*N
for i in range(N):
    D[i]=N-bisect.bisect_right(C,B[i])
for i in range(N):
    E[i]=sum(D[bisect.bisect_right(B,A[i]):])
print(sum(E))

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

ans = 0
dict_a = dict()
dict_b = dict()
for i in range(n):
    if a[i] in dict_a.keys():
        b_mid = dict_a[a[i]]
    else:
        left = n - 1
        right = 0
        b_mid = n // 2
        while left >= right:
            if b[b_mid] > a[i]:
                left = b_mid - 1
            else:
                right = b_mid + 1
            b_mid = (left + right) // 2
        b_mid += 1
        dict_a[a[i]] = b_mid

    for j in range(b_mid, n):
        if b[j] in dict_b.keys():
            c_mid = dict_b[b[j]]
        else:
            left = n - 1
            right = 0
            c_mid = n // 2
            while left >= right:
                if c[c_mid] > b[j]:
                    left = c_mid - 1
                else:
                    right = c_mid + 1
                c_mid = (left + right) // 2
            dict_b[b[j]] = c_mid
        ans += n - c_mid - 1
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()))

a.sort()
b.sort()
c.sort()

ans = 0
for i in range(n):
    left = n - 1
    right = 0
    b_mid = n // 2
    while left >= right:
        if b[b_mid] > a[i]:
            left = b_mid - 1
        else:
            right = b_mid + 1
        b_mid = (left + right) // 2
    b_mid += 1

    for j in range(b_mid, n):
        left = n - 1
        right = 0
        c_mid = n // 2
        while left >= right:
            if c[c_mid] > b[j]:
                left = c_mid - 1
            else:
                right = c_mid + 1
            c_mid = (left + right) // 2
        ans += n - c_mid - 1
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()))

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

def is_ok(arg, target, abc):
    if abc[arg] > target:
        return True
    else:
        return False

def bict(left, right, target, abc):

    while(right - left > 1):
        mid = (right + left)//2

        if is_ok(mid, target, abc):
            right = mid
        else:
            left = mid

    return right

cnt = 0
for a in A:
    b_min_index = bict(-1, N, a, B)
    for b_index in range(b_min_index,N):
        c_min_index = bict(-1, N ,B[b_index],C)
        c_num = N - c_min_index
        cnt += c_num

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

def is_ok(arg, target, abc):
    if abc[arg] > target:
        return True
    else:
        return False

def bict(left, right, target, abc):

    while(right - left > 1):
        mid = (right + left)//2

        if is_ok(mid, target, abc):
            right = mid
        else:
            left = mid

    return right

cnt = 0
for a in A:
    b_min_index = bict(-1, N, a, B)
    for b_index in range(b_min_index,N):
        c_min_index = bict(-1, N ,B[b_index],C)
        c_num = N - c_min_index
        cnt += c_num

print(cnt)

Submission status: Time Limit Exceeded

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

def slove():
    n = int(input())
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))
    C = list(map(int,input().split()))

    A = sorted(A)
    B = sorted(B)
    C = sorted(C)

    ans = 0

    for i in range(n):
        k = bisect.bisect(B,A[i])
        for j in range(k,n):
            l = bisect.bisect(C,B[j])
            ans += n-l
    return ans


if __name__ == "__main__":
    print(slove())

Submission status: Time Limit Exceeded

Solution code:
def bisect(lst, isok, ok, ng):
    while(abs(ok-ng)>1):
        mid = (ok+ng)//2
        if isok(lst[mid]):
            ok = mid
        else:
            ng = mid
    return [ok, ng]

n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]

a.sort()
b.sort()
c.sort()

ans = 0

for i in a:
    ind = bisect(b, lambda x:x > i, n, -1)[0]
    if ind == n:
        break
    for j in range(ind, n):
        ans += n - bisect(c, lambda x:x > b[j], n, -1)[0]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import 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())))
    AB = [0] * n
    BC = [0] * n
    for i in range(n):
        AB[i] = n - bisect_right(B, A[i])
        BC[i] = n - bisect_right(C, B[i])
    ans = 0
    for ab in AB:
        if ab != 0:
            ans += sum(BC[-ab:])
    print(ans)

if __name__ == '__main__':
    main()