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:
from functools import lru_cache as cache
from bisect import bisect

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect

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

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

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N =int(input())
A = Counter(list(map(int,input().split())))
B = Counter(list(map(int,input().split())))
C = Counter(list(map(int,input().split())))
A_sort = sorted(A.items())
B_sort = sorted(B.items())
C_sort = sorted(C.items())

BtoC = []
ans = 0
start_j = 0
start_k = 0
sum_val = N
for j in range(len(B_sort)):
    for k in range(start_k , len(C_sort)):
        if B_sort[j][0] >= C_sort[k][0]:
            sum_val -= C_sort[k][1]
            start_k = k + 1
        else:
            BtoC.append([B_sort[j][0] , sum_val * B_sort[j][1]])
            break

for i in range(len(A_sort)):
    for j in range(start_j , len(BtoC)):
        if A_sort[i][0] >= BtoC[j][0]:
            start_j = j + 1
            continue
        else:
            ans += A_sort[i][1] * BtoC[j][1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N =int(input())
A = Counter(list(map(int,input().split())))
B = Counter(list(map(int,input().split())))
C = Counter(list(map(int,input().split())))
A_sort = sorted(A.items())
B_sort = sorted(B.items())
C_sort = sorted(C.items())

ans = 0
for i in range(len(A_sort)):
    sum_val = N
    now_k = 0
    now_j = 0
    for j in range(now_j , len(B_sort)):
        if A_sort[i][0] >= B_sort[j][0]:
            continue
        for k in range(now_k , len(C_sort)):
            if A_sort[i][0] < B_sort[j][0] < C_sort[k][0]:
                ans += (sum_val * B_sort[j][1] * A_sort[i][1])
                now_k = k
                now_j = j
                break
            else:
                sum_val -= C_sort[k][1]
print(ans)

Submission status: Time Limit Exceeded

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

count=0
for i in range(N):
  for j in range(N):
    if C[i]>B[j]:
      l=0
      r=N
      while r-l>1:
        p=(r+l)//2
        if A[p]<B[j]:
          l=p
        else:
          r=p
      count +=l+1
    else:
      break
print(count)

Submission status: Time Limit Exceeded

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

count=0
for i in range(N):
  for j in range(N):
    if C[i]>B[j]:
      l=0
      r=N
      while r-l>1:
        p=(r+l)//2
        if A[p]<B[j]:
          l=p
        else:
          r=p
      count +=l+1
print(count)

Submission status: Time Limit Exceeded

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

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

count = 0
a1 = sorted(a)
b1 = sorted(b)
c1 = sorted(c)
for i in c1:
    prior_c = bisect.bisect_left(b1,i)
    prior_c_list = b1[:prior_c]
    for j in prior_c_list:
        count += bisect.bisect_left(a1,j)

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 a in A:
  bidx=bisect.bisect_right(B,a)
  if bidx==len(B): break
  for b in B[bidx:]:
    cidx=bisect.bisect_right(C,b)
    if cidx==len(B): break
    ans+=len(C)-cidx
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
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 A:
  for j in B[np.searchsorted(B, i, side="right"):]:
    ans += n - np.searchsorted(C, j, side="right")
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())))
ans = 0
for i in A:
  for j in B[bisect.bisect_right(B, i):]:
    ans += n - bisect.bisect_right(C, j)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import random
from operator import itemgetter
import re
import sys
from itertools import accumulate
from collections import defaultdict
from collections import deque
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
input = sys.stdin.readline
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()
B_count = [0] * N
C_count = [N] * N
count = 0
num = 0
for i in range(N):
    B_high = B[i]
    for j in range(count,N):
        if A[j] >= B_high:
             B_count[i] = count
        else:
            count +=1
    if count == N:
        B_count[i]= N
    for j in range(num,N):
        if C[j] > B_high:
             break
        else:
            num += 1
    C_count[i] =N -num

ans = 0
for i in range(N):
    ans += B_count[i] * C_count[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

import numpy as np

readline = sys.stdin.buffer.readline


# 同じ数値が複数あるとおかしくなる
def binary_search(A, n, p):
    if p == "+":
        n += 0.5
    else:
        n -= 0.5

    left = 0
    right = len(A) - 1

    if n < A[0]:
        return 0
    else:
        while left <= right:
            mid = (left + right) // 2
            if A[mid] < n:
                left = mid + 1
            else:
                right = mid - 1

        return left


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

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

    cnt = 0
    for i, b in enumerate(B):
        ab = binary_search(A, b, "-")
        bc = N - binary_search(C, b, "+")
        cnt += ab * bc

    print(cnt)


main()

Submission status: Time Limit Exceeded

Solution code:
import sys

import numpy as np

readline = sys.stdin.buffer.readline


# 同じ数値が複数あるとおかしくなる
def binary_search(A, n, p):
    if p == "+":
        n += 0.5
    else:
        n -= 0.5

    left = 0
    right = len(A) - 1

    if n < A[0]:
        return 0
    else:
        while left <= right:
            mid = (left + right) // 2
            if A[mid] < n:
                left = mid + 1
            else:
                right = mid - 1

        return left


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

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

    ab = np.zeros([N], dtype=np.int32)
    for i, b in enumerate(B):
        ab[i] = binary_search(A, b, "-")

    bc = np.zeros([N], dtype=np.int32)
    for i, b in enumerate(B):
        bc[i] = -binary_search(C, b, "+") + N

    cnt = 0
    for i, j in zip(ab, bc):
        cnt += i * j
    print(cnt)


main()

Submission status: Time Limit Exceeded

Solution code:
import sys

import numpy as np

readline = sys.stdin.buffer.readline


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

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

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

        if A[right] > n:
            while A[right] > n:
                right -= 1
        elif A[right] == n:
            while right < len(A)-1 and A[right] == A[right+1]:
                right += 1

        return right



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

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

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

    bb = np.cumsum(aa)

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

    print(cnt)


main()

Submission status: Time Limit Exceeded

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

    ans = 0
    left = 0
    for a in A:
        right = left
        while left < N and a >= B[left]:
            left += 1
        if N <= left:
            break
        for b in B[left:]:
            while right < N and b >= C[right]:
                right += 1
            if N <= right:
                break
            ans += N-right
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

    ans = 0
    left = 0
    for a in A:
        right = left
        while left < N and a >= B[left]:
            left += 1
        for b in B[left:]:
            while right < N and b >= C[right]:
                right += 1
            ans += N-right
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

    def is_ok(mid, S, K):
        if K < S[mid]:
            return True
        else:
            return False

    def binary_search_meguru(S, K):
        left = -1
        right = N
        while right - left > 1:
            mid = left + ((right - left) // 2)
            if is_ok(mid, S, K):
                right = mid
            else:
                left = mid
        return right

    ans = 0
    for a in A:
        b = binary_search_meguru(B, a)
        for tar in B[b:]:
            c = binary_search_meguru(C, tar)
            ans += N-c
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: 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())))

A_B = []
B_C = []
total = 0

for a in A:
    k = bisect.bisect_right(B, a)
    l = N - k
    A_B.append(l)

for b in B:
    k = bisect.bisect_right(C, b)
    l = N - k
    B_C.append(l)

for n in range(N):
    total += sum(B_C[N-A_B[n]:])

print(total)

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(len(a)):
  for j in range(len(b)):
    for k in range(len(c)):
      if a[i]<b[j]<c[k]:
        ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import bisect

n = int(stdin.readline().rstrip())
a = list(map(int,stdin.readline().rstrip().split()))
b = list(map(int,stdin.readline().rstrip().split()))
c = list(map(int,stdin.readline().rstrip().split()))

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

total = 0
for i in range(n):
    l= bisect.bisect_left(b,c[i])
    point = 0
    for j in range(l):
        point += bisect.bisect_left(a,b[j])
    total += point
print(total)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import math
import bisect
import heapq
import numpy as np
from math import factorial

n = int(stdin.readline().rstrip())
a = list(map(int,stdin.readline().rstrip().split()))
b = list(map(int,stdin.readline().rstrip().split()))
c = list(map(int,stdin.readline().rstrip().split()))

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

total = 0
for i in range(n):
    l= bisect.bisect_left(b,c[i])
    point = 0
    for j in range(l):
        point += bisect.bisect_left(a,b[j])
    total += point
print(total)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
ABCs = [
  sorted(map(int, input().split()))
  for _ in range(3)
]

def table(As, Bs):
  ib = 0
  result = []
  for a in As:
    while ib < N and Bs[ib] <= a:
      ib += 1
    result.append(ib)
  return result

def table2(Bs, Cs):
  ic = 0
  result = []
  for b in Bs:
    while ic < N and Cs[ic] <= b:
      ic += 1
    result.append(N-ic)
  return result

def f(ABCs):
  AB = table(ABCs[0], ABCs[1])
  BC = table2(ABCs[1], ABCs[2])
  acc = 0
  
  for i, bmin in enumerate(AB):
      acc += sum(BC[bmin:])
  
  return acc
  
print(f(ABCs))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
ABCs = [
  sorted(map(int, input().split()))
  for _ in range(3)
]

def table(As, Bs):
  ib = 0
  result = []
  for a in As:
    while ib < N and Bs[ib] <= a:
      ib += 1
    result.append(ib)
  return result

def table2(Bs, Cs):
  ic = 0
  result = []
  for b in Bs:
    while ic < N and Cs[ic] <= b:
      ic += 1
    result.append(N-ic)
  return result

def f(ABCs):
  AB = table(ABCs[0], ABCs[1])
  BC = table2(ABCs[1], ABCs[2])
  
  def Cs():
    for i, bmin in enumerate(AB):
      yield sum(BC[bmin:])
  
  return sum(Cs())
  
print(f(ABCs))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
ABCs = [
  sorted(map(int, input().split()))
  for _ in range(3)
]

def table(As, Bs):
  ib = 0
  result = []
  for a in As:
    while ib < len(Bs) and Bs[ib] <= a:
      ib += 1
    result.append(ib)
  return result


def f(ABCs):
  AB = table(ABCs[0], ABCs[1])
  BC = table(ABCs[1], ABCs[2])
  return sum(
    N - BC[j]
    for i, ab in enumerate(AB)
    for j in range(ab, N)
  )
  
print(f(ABCs))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = []
B = []
C = []
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
count = 0
 
for i in range(N):
  for j in range(N):
  	if A[i] < B[j]:
          for o in range(N):
            if B[j] < C[o]:
              count += 1
 
          
print(count)

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()))
ctr= [0 for _ in range(N)]

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

# A固定
cur = 0

for i in range(N):
	for j in range(cur, N):
		if B[i] < C[j]:
			ctr[i] += N - j
			cur = j
			break
			
cur = 0
ans = 0
for i in reversed(range(1, N)):
	ctr[i - 1] += ctr[i]
for i in range(N):
	for j in range(cur, N):
		if A[i] < B[j]:
			ans += ctr[j]
			cur = j
			break
			
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()))
ctr= [0 for _ in range(N)]

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

# A固定
cur = 0

for i in range(N):
	for j in range(cur, N):
		if B[i] < C[j]:
			ctr[i] += N - j
			cur = j
			break
			
cur = 0
ans = 0
for i in reversed(range(1, N)):
	ctr[i - 1] += ctr[i]
for i in range(N):
	for j in range(cur, N):
		if A[i] < B[j]:
			ans += ctr[j]
			break
			
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
info = []

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

for k in range(n):
    a_p = bisect.bisect_left(b, c[k])
    cnt += sum(info[:a_p])

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

Submission status: Time Limit Exceeded

Solution code:
N = input()
A = input().split()
B = input().split()
C = input().split()

ans = 0

for c in C:
    B1 = [b for b in B if int(b) < int(c)]
    for b in B1:
        A1 = [a for a in A if int(a) < int(b)]
        ans = ans + len(A1)

print(ans)

Submission status: Time Limit Exceeded

Solution code:

import bisect

N = int(input())

Ai = list(map(int,input().split()))
Bi = list(map(int,input().split()))
Ci = list(map(int,input().split()))

Bi.sort()
Ci.sort()

ans = 0

for i in range(len(Ai)):
    #Ai[i]より大きいものの絞り込み (以上ならleft、より大きいならright)
    bi = bisect.bisect_right(Bi,Ai[i])
    #Biのどの値もAi[i]より大きくないなら無理
    if bi >= len(Bi):
        continue
    #Bi[bi]以降がiよりも大きい(中部として使える)
    for j in range(bi,len(Bi)):
        #Bi[bi]以降が中部として使えるので次は下部として使える範囲を絞り込む
        ci = bisect.bisect_right(Ci,Bi[j])
        #Ciのどの値もBi[i]より大きくないなら無理
        if ci >= len(Ci):
            continue
        #Ci[ci]以降が下部として使える
        ans += len(Ci) - ci

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import 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())))
ans = 0
for i in A:
  for j in range(bisect_right(B,i),N):
    ans = ans + (N-bisect_right(C,B[j]))
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):
  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:

import sys
import collections
# import math
# import string
# import bisect

def main():
    n, *parts = (int(x) for x in sys.stdin.read().split())
    uppers, middles, lowers = (parts[n*i:n*(i+1)] for i in range(3))

    count = 0
    for a, ca in collections.Counter(uppers).items():
        for b, cb in collections.Counter((b for b in middles if b > a)).items():
            count += ca * cb * len([c for c in lowers if c > b])

    print(count)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import collections
# import math
# import string
# import bisect

def main():
    n, *parts = (int(x) for x in sys.stdin.read().split())
    uppers, middles, lowers = (parts[n*i:n*(i+1)] for i in range(3))

    count = 0
    for a, ca in collections.Counter(uppers).items():
        for b, cb in collections.Counter((b for b in middles if b > a)).items():
            count += ca * cb * sum(collections.Counter((c for c in lowers if c > b)).values())

    print(count)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:

import sys
import collections
# import math
# import string
# import bisect

def main():
    n, *parts = (int(x) for x in sys.stdin.read().split())
    uppers, middles, lowers = (parts[n*i:n*(i+1)] for i in range(3))

    count = 0
    for a, ca in collections.Counter(uppers).items():
        for b, cb in collections.Counter(middles).items():
            if a >= b: continue
            else:
                for c, cc in collections.Counter(lowers).items():
                    if b >= c: continue
                    else: count += ca * cb * cc
    print(count)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:

import sys
# import collections
# import math
# import string
# import bisect

def main():
    n, *parts = (int(x) for x in sys.stdin.read().split())
    uppers, middles, lowers = (sorted(parts[n*i:n*(i+1)], reverse=1) for i in range(3))

    count = 0
    for a in uppers:
        for b in middles:
            if a >= b: break
            else:
                for c in lowers:
                    if b >= c: break
                    else: count += 1
    print(count)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#ABC077 C - Snuke Festival
from bisect import bisect_left, bisect_right
si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

N = ni()
A = nl()
B = nl()
C = nl()

A.sort()
B.sort()
C.sort()
ans = 0

for i in range(N):
    t = 0
    tt = 0
    t = bisect_left(B,C[i])
    for j in range(t,0,-1):
        tt = bisect_left(A,B[j-1])
        ans += tt

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#ABC077 C - Snuke Festival
from bisect import bisect_left, bisect_right
si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

N = ni()
A = nl()
B = nl()
C = nl()

A.sort()
B.sort()
C.sort()
ans = 0

for i in range(N):
    t = 0
    tt = 0
    t = bisect_left(B,C[i])
    for j in range(t,0,-1):
        tt = bisect_left(A,B[j-1])
        ans += tt

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()
AB=[0 for i in range(N)]
BC=[0 for i in range(N)]

def find_gt(a,x):
    i = bisect.bisect_right(a,x)
    if i != len(a):
        return N-i
    else:
        return 0

for i in range(N):
    AB[i]=find_gt(B,A[i])
    BC[i]=find_gt(C,B[i])

ans=0
for i in range(N):
    ans+=sum(BC[N-AB[i]:])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def nibu(list,item):

    high=len(list)-1

    low=0
    while low <= high:
        mid=(low+high)//2
        guess=list[mid]
        if guess==item:
            return mid+1
            break
        elif guess < item:
            low = mid +1
        elif guess > item:
            high = mid - 1
    if guess != item:
        return high+1



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()
Bs=[]
s=0
for i in range(n):
    Bs.append(n-nibu(C,B[i]))
for i in range(n):
    Bss=Bs[:]
    Bss=Bs[nibu(B,A[i]):]
    s+=sum(Bss)
print(s)

Submission status: Time Limit Exceeded

Solution code:
def nibu(list,item):

    high=len(list)-1

    low=0
    while low <= high:
        mid=(low+high)//2
        guess=list[mid]
        if guess==item:
            return mid+1
            break
        elif guess < item:
            low = mid +1
        elif guess > item:
            high = mid - 1
    if guess != item:
        return high+1


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

sum=0

blist=[]
for i in range(n):

    kadan=nibu(C,B[i])
    u=n-kadan
    blist.append(u)
for i in range(n):
    tyudan=nibu(B,A[i])

    for i in range(tyudan,n):
        sum+=blist[i]

print(sum)

Submission status: Time Limit Exceeded

Solution code:
def low_left(ls,key):
    left,right = -1,len(ls)
    while right - left > 1:
        mid = (right+left)//2
        if ls[mid] >= key:
            right = mid
        else:
            left = mid
    return len(ls[:(left+1)])


def high_right(ls,key):
    left,right = -1,len(ls)
    while right - left > 1:
        mid = (right+left)//2
        if ls[mid]<=key:
            left = mid
        else:
            right = mid
    return len(ls[right:])


def test():
    n = int(input())
    A,B,C = [list(sorted(map(int,input().split()))) for i in range(3)]
    ans = 0

    for b in B:
        ans += high_right(C,b)*low_left(A,b)
    print(ans)


if __name__ == "__main__":
    test()

Submission status: Time Limit Exceeded

Solution code:
def low_left(ls,key):
    left,right = -1,len(ls)
    while right - left > 1:
        mid = (right+left)//2
        if ls[mid] >= key:
            right = mid
        else:
            left = mid
    return len(ls[:(left+1)])


def high_right(ls,key):
    left,right = -1,len(ls)
    while right - left > 1:
        mid = (right+left)//2
        if ls[mid]<=key:
            left = mid
        else:
            right = mid
    return len(ls[right:])


def test():
    n = int(input())
    A,B,C = [list(sorted((map(int,input().split())))) for i in range(3)]
    ans = 0

    for b in B:
        ans += high_right(C,b)*low_left(A,b)
    print(ans)


if __name__ == "__main__":
    test()

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

for i in range(n):
    x = a[i] + 1
    index1 = bisect.bisect_left(b, x)
    if index1 >= n:
        break
    b_tmp = b[index1:]
    for j in b_tmp:
        y = j + 1
        index2 = bisect.bisect_left(c, y)
        if index2 >= n:
            break
        c_tmp = c[index2:]
        count += len(c_tmp)


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

index_list_1 = []
index_list_2 = []
count = 0

for i in b:
    index_list_1.append(len(c) - bisect.bisect_left(c, i+1))

for i in a:
    index_list_2.append(bisect.bisect_left(b, i+1))

for i in index_list_2:
    count += sum(index_list_1[i:])

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


count = 0

for i in range(n):
    x = a[i] + 1
    index1 = bisect.bisect_left(b, x)
    b_tmp = b[index1:]
    for j in b_tmp:
        y = j + 1
        index2 = bisect.bisect_left(c, y)
        c_tmp = c[index2:]
        count += len(c_tmp)


print(count)

Submission status: Time Limit Exceeded

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


def main():
    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(reverse=True)
    # print(A)
    # print(B)
    # print(C)

    ans = 0
    last_b = N
    for c in C:
        last_b = bisect.bisect_left(B, c, hi=last_b)
        if last_b==0:
            break
        last_a = N
        for b in reversed(B[:last_b]):
            last_a = bisect.bisect_left(A, b, hi=last_a)
            if last_a==0:
                break
            ans += last_a
            # print(b)
            # print("last_b: "+str(last_b))
            # print("last_a: "+str(last_a))
    
    print(ans)


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

b_bt = []
c_bt = []
for i in range(n):
    b_bt.append(bisect.bisect_left(a, b[i]))
    c_bt.append(bisect.bisect_left(b, c[i])-1)
result = 0
for i in range(n):
    if c_bt[i] >= 0:
        result += sum(b_bt[:c_bt[i]+1])
print(result)