Problem:


Score : 300 points

Problem Statement

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

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

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

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

Constraints

1 \leq N \leq 10^5

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

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

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

All input values are integers.

Input

Input is given from Standard Input in the following format:

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

Output

Print the number of different altars that Ringo can build.

Sample Input 1

2
1 5
2 4
3 6

Sample Output 1

3

The following three altars can be built:

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

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

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

Sample Input 2

3
1 1 1
2 2 2
3 3 3

Sample Output 2

27

Sample Input 3

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

Sample Output 3

87



Sample input:
2
1 5
2 4
3 6


Sample output:
3

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
INF = float("inf")
 
 
def find_lt(a, x):
    i = bisect_left(a, x)
    if i:
        return i
    return 0
 
 
def find_gt(a, x):
    i = bisect_right(a, x)
    if i != len(a):
        return i
    return len(a)
 
 
def main():
    N = int(input())
    A = list(sorted(list(map(int, input().split()))))
    B = list(sorted(list(map(int, input().split()))))
    C = list(sorted(list(map(int, input().split()))))
 
    ans = 0
    for b in B:
        a = find_lt(A, b)
        b = N - find_gt(C, b)
        ans += a * b
    print(ans)
 
 
if __name__ == '__main__':
    main()

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())))
bmul = [0]*(n+1)
ci = 0
for bi in range(n):
    while ci != n and c[ci] <= b[bi]:
        ci += 1
    bmul[bi] = n-ci
for i in range(n):
    bmul[n-i-1] += bmul[n-i]
bi = 0
ai = 0
ret = 0
for ai in range(n):
    while bi != n and b[bi] <= a[ai]:
        bi += 1
    ret += bmul[bi]
print(ret)

Submission status: Accepted

Solution code:
n = int(raw_input())
al = map(int, raw_input().split())
bl = map(int, raw_input().split())
cl = map(int, raw_input().split())
al.sort()
bl.sort()
cl.sort()
ap = 0
cp = 0
t = 0
for b in bl:
    while ap < n and al[ap] < b:
        ap += 1
    while cp < n and cl[cp] <= b:
        cp += 1
    t += ap * (n - cp)
print t

Submission status: Accepted

Solution code:
n = int(raw_input())
al = map(int, raw_input().split())
bl = map(int, raw_input().split())
cl = map(int, raw_input().split())
al.sort()
bl.sort()
cl.sort()
l = [(0,0) for _ in range(n)]
p = 0
for i, b in enumerate(bl):
    while p < n and cl[p] <= b:
        p += 1
    l[i] = (b, n-p)
p = n-1
s = 0
t = 0
for a in al[::-1]:
    while p >= 0 and l[p][0] > a:
        s += l[p][1]
        p -= 1
    t += s
print t

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 = sorted(A)
C = sorted(C)

counter = 0

for k in range(N):
    i = bisect.bisect_left(A, B[k])
    j = N - bisect.bisect_right(C, B[k])
    counter += i * j

print(counter)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
br, bl = bisect_right, bisect_left
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 bi in b:
	ans += (bl(a, bi) * (n - br(c, bi)))
print ans

Submission status: Accepted

Solution code:

import operator

def snuke_fest(top_arr, middle_arr, bottom_arr):
    sorted_arrs = list(sorted(arr) for arr in (top_arr, middle_arr, bottom_arr))
    del top_arr
    del middle_arr
    del bottom_arr
    pointers = [0] * 3
    ended = [False] * 3
    min_posi = [None] * len(sorted_arrs[0]) * 3
    min_posi_pointer = 0
    try:
        while not all(ended):
            nums = [0] * 3
            mins = [10**9] * 3
            for i in range(3):
                if not ended[i]:
                    mins[-(i+1)] = sorted_arrs[i][pointers[i]]
            min_index, min_val = min(enumerate(mins), key = operator.itemgetter(1))
            min_index = 2 - min_index
            pointers[min_index] += 1
            min_posi[min_posi_pointer] = min_index
            min_posi_pointer += 1
            if pointers[min_index] == len(sorted_arrs[0]):
                ended[min_index] = True
    except Exception as e:
        return 0
    del sorted_arrs
    del pointers
    count = 0
    count_arr = [0, 0, 0]
    count_one_two = 0
    for i in range(len(min_posi)):
        count_arr[min_posi[-(i+1)]] += 1

        if min_posi[-(i+1)] == 1:
            count_one_two += count_arr[2]
        elif min_posi[-(i+1)] == 0:
            count += count_one_two
    return count


if __name__ == "__main__":
    # run_test(find_even, test_pair)
    _ = input()
    top_arr = [int(i) for i in input().split(" ")]
    middle_arr = [int(i) for i in input().split(" ")]
    bottom_arr = [int(i) for i in input().split(" ")]
    print(snuke_fest(top_arr, middle_arr, bottom_arr))

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()
ans=0
small=0
large=0
for i in range(N):
  for k in range(small,N):
    if A[k]>=B[i]:
      small=k
      break
  else: small=N
  for k in range(large,N):
    if C[k]>B[i]:
      large=k
      break
  else: large=N
  ans+=small*(N-large)
print(ans)

Submission status: Accepted

Solution code:
import bisect
N=input()

A=list(map(int,raw_input().split()))
B=list(map(int,raw_input().split()))
C=list(map(int,raw_input().split()))
A.sort()
B.sort()
C.sort()

sum=0
for val_b in B:
	sum+=bisect.bisect_left(A, val_b)*( N-bisect.bisect_right(C, val_b) )
			
print sum

Submission status: Accepted

Solution code:
import bisect

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

counts = 0
checked = {}
for _numB in B:
    if _numB not in checked:
        countsA = bisect.bisect_left(A, _numB)
        countsC = bisect.bisect_right(C, _numB)
        checked[_numB] = countsA * (N - countsC)
    counts += checked[_numB]
print(counts)

Submission status: Accepted

Solution code:
import bisect
N = int(input())

As = sorted(list(map(int, input().split())))
Bs = list(map(int, input().split()))
Cs = sorted(list(map(int, input().split())))

ans = 0
for b in Bs:
    a = bisect.bisect_left(As, b)
    c = bisect.bisect_right(Cs, b)
    ans += a * (N-c)
print(ans)

Submission status: Accepted

Solution code:
import bisect


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

    ans = 0
    for b in B:
        na = bisect.bisect_left(A, b)
        nc = N - bisect.bisect_right(C, b)
        ans += na * nc

    print(ans)


def TLE(N, A, B, C):
    # a<b<c となる並べ方の数
    # b<c となる並べ方の数、１つずつに対してN個調べるO(N^2) => すでにOver => pypy
    # b M個となったとき (b_c_i - b_c_m) を保持する
    # a<b となる並べ方の数、１つずつに対してN個調べるO(M*N)
    # b L個となったとき (b_a_i - b_a_m) を保持する (b_cと対応付ける)

    ans = 0
    for b in B:
        less_b_count = sum(1 for a in A if a < b)
        if less_b_count > 0:
            over_b_count = sum(1 for c in C if b < c)
            ans += less_b_count * over_b_count

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect
N = int(input())
A = sorted(map(int, input().split()))
B = map(int, input().split())
C = sorted(map(int, input().split()))
print(sum(bisect.bisect_left(A, b) * (N - bisect.bisect(C, b)) for b in B))

Submission status: Accepted

Solution code:
import bisect

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

    a = sorted(A)
    c = sorted(C)
    cnt = 0
    for i in B:
        x = bisect.bisect_left(a, i)
        y = bisect.bisect_right(c, i)
        cnt += x * (N - y)

    print(cnt)

main()

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
INF = float("inf")


def find_lt(a, x):
    i = bisect_left(a, x)
    if i:
        return i
    return 0


def find_gt(a, x):
    i = bisect_right(a, x)
    if i != len(a):
        return i
    return len(a)


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

    ans = 0
    for b in B:
        a = find_lt(A, b)
        b = N - find_gt(C, b)
        ans += a * b
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import math
import itertools
import heapq
import numpy as np
from collections import OrderedDict, deque, Counter
from bisect import bisect_left, bisect_right, insort_left, insort_right
sys.setrecursionlimit(10000)

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]


def solve():
    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 ele in B:
        cnt += bisect_left(A, ele) * (N - bisect_right(C, ele))

    print(cnt)


solve()

Submission status: Accepted

Solution code:
import bisect
from sys import stdin

def IL(): return list(map(int, stdin.readline().split()))
def SIL(): return sorted(list(map(int, stdin.readline().split())))

def main():
	n = int(input())
	A = SIL()
	B = IL()
	C = SIL()
	
	ans = 0
	for b in B:
	    smaller = bisect.bisect_left(A, b)
	    bigger = bisect.bisect_right(C, b)
	    ans += smaller*(n - bigger)

	print(ans)
	
	
if __name__ == "__main__": main()

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right # 2文探索法モジュール
N = int(input())
a = sorted(list(map(int, input().split()))) # ソートしておく
b = list(map(int, input().split()))
c = sorted(list(map(int, input().split()))) # ソートしておく

count = 0
for b_item in b:
    i = bisect_left(a, b_item) # リストaのb_itemより小さい要素の数
    j = N - bisect_right(c, b_item) # リストcのb_itemより大きい要素の数
    count += (i * j)
print(count)

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

ans = 0
ai = 0
ci = 0

for b in B:
    while ai < n and A[ai] < b:
        ai += 1
    while ci < n and C[ci] <= b:
        ci += 1
    ans += ai * (n - ci)

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import bisect

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

A.sort()
C.sort()
ans = 0
for b in B:
    ans += bisect.bisect_left(A, b) * (N - bisect.bisect_right(C, b))

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
import math
import bisect

def calc():
    n = int(input())
    tops = list(map(int, input().split()))
    middles = list(map(int, input().split()))
    lowers = list(map(int, input().split()))
 
    tops = sorted(tops)
    lowers = sorted(lowers)
 
    counter = 0
 
    for i in range(n):
        # A < B
        top_num = bisect.bisect_left(tops, middles[i])
        
        # B < C
        lower_num = n - bisect.bisect_right(lowers, middles[i])
        
        counter += top_num * lower_num
    
    return counter
 
print(calc())

Submission status: Accepted

Solution code:
import bisect

def fun():
    a = sorted(list(map(int,input().split())))
    b = sorted(list(map(int,input().split())))
    c = sorted(list(map(int,input().split())))
    ans = 0
    for k in b:
        pos1 = bisect.bisect_left(a,k)
        pos2 = bisect.bisect_right(c,k)
        ans += (pos1*(n-pos2))
    print(ans)
if __name__ == "__main__":
    n = int(input())
    fun()

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 k in b:
    pos1 = bisect.bisect_left(a,k)
    pos2 = bisect.bisect_right(c,k)
    ans += (pos1*(n-pos2))
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 k in b:
    pos1 = bisect.bisect_left(a,k)
    pos2 = bisect.bisect_right(c,k)
    ans += (pos1*(n-pos2))
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()
C.reverse()
 
ans = 0
ai = 0
ci = N-1
for b in B:
    while ai < N and A[ai] < b:
        ai += 1
    while ci >= 0 and C[ci] <= b:
        ci -= 1
    ans += ai * (ci+1)
 
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""077-c"""

import sys
import pprint
import re
import itertools

import bisect

pp = pprint.PrettyPrinter(indent=2)

def main():
    """Main function."""
    N = int(input())

    As = sorted(list(map(int, input().split(' '))))
    Bs = map(int, input().split(' '))
    Cs = sorted(list(map(int, input().split(' '))))

    count = 0
    for b in Bs:
        i = bisect.bisect_left(As, b)
        k = N - bisect.bisect_right(Cs, b)
        count += i * k

    print(count)

if __name__ == '__main__':
    sys.exit(main())

Submission status: Accepted

Solution code:
N = int(input())
A = sorted(map(int, input().split()))
B = map(int, input().split())
C = sorted(map(int, input().split()))
import bisect
ans = 0
for b in B:
    i = bisect.bisect_left(A,b)
    j = bisect.bisect_right(C,b)
    ans += i * (N-j)
print(ans)

Submission status: Accepted

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_A = {}
#    lut_C = {}
#    for b in B:
#        i = bisect_left(A, b)
#        lut_A[b] = i
#        j = bisect_right(C, b)
#        lut_C[b] = N - j
    lut_A = {b:bisect_left(A, b) for b in B}
    lut_C = {b:(N - bisect_right(C, b)) for b in B}

    ans = 0
    for b in B:
        i = lut_A[b]
        j = lut_C[b]
        ans += i * j

    print(ans)


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

Submission status: Accepted

Solution code:
import sys
import numpy as np

def binary_search(array, num):
    # numより真に大きいはじめのindex
    if array[-1] <= num:
        return -1
    left = -1
    right = N-1
    while right - left > 1:
        mid = int((left + right)/2)
        if array[mid] <= num:
            left = mid
        else:
            right = mid
    return right

a_index = []
b_index = []

N = int(sys.stdin.readline().strip())
A = list(map(int, sys.stdin.readline().strip().split(" ")))
A = np.sort(A)
B = list(map(int, sys.stdin.readline().strip().split(" ")))
B = np.sort(B)
C = list(map(int, sys.stdin.readline().strip().split(" ")))
C = np.sort(C)

for a in A:
    a_index.append(binary_search(B, a))
    
for b in B:
    b_index.append(binary_search(C, b))


b_index = list(map(lambda x: N-x if (x != -1) else 0, b_index))

bb = []
tmp = np.sum(b_index)
for i in range(N):
    bb.append(tmp)
    tmp = tmp - b_index[i]

res = 0
for a_i in a_index:
    if a_i == -1:
        continue
    res += bb[a_i]
print(str(res))

Submission status: Accepted

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_A = {}
    lut_C = {}
    for b in B:
        i = bisect_left(A, b)
        lut_A[b] = i
        j = bisect_right(C, b)
        lut_C[b] = N - j


    ans = 0
    for b in B:
        i = lut_A[b]
        j = lut_C[b]
        ans += i * j

    print(ans)


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

Submission status: Accepted

Solution code:
def C_SnukeFestival(N, A, B, C):
    a = sorted(A)
    b = sorted(B)
    c = sorted(C)

    import bisect
    ans = 0
    for j in range(N):
        # b[j]より小さなaの要素の個数
        i = bisect.bisect_left(a, b[j])
        # b[j]より大きなcの要素の個数
        k = N - bisect.bisect_right(c, b[j])
        ans += i * k
    return ans

N = int(input())
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]
C = [int(_) for _ in input().split()]
print(C_SnukeFestival(N, A, B, C))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]	# 1 2 3 4 5 ...
B = [int(i) for i in input().split()]	# 1 2 3 4 5 ...
C = [int(i) for i in input().split()]	# 1 2 3 4 5 ...

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

#print(A)
#print(B)
#print(C)
##exit(0)

ab=[0]*N
bc=[0]*N

b=N-1
c=N-1
count=0
while (0<=b):
	while (0<=c):
		if B[b]<C[c]:
			count += 1
			c -= 1
		else:
			break
	bc[b] = count
	if b<N-1:
		bc[b] += bc[b+1]
	b -= 1

a=N-1
b=N-1
count=0
while (0<=a):
	while (0<=b):
		if A[a]<B[b]:
			count += 1
			b -= 1
		else:
			break
	ab[a] = count
	a -= 1


#print(ab)
#print(bc)

ans=0

for i in ab:
#	print(i, bc[N-i:N])
#	ans += sum(bc[N-i:N])
	if i>0:
		ans += bc[N-i]

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

Bcon = [0] * (N + 1)
res = 0
p = 0
for n in range(N):
    while p <= N - 1 and B[n] > A[p]:
        p += 1
    Bcon[n + 1] = p + Bcon[n]
    
p = 0
for n in range(N):
    while p <= N - 1 and C[n] > B[p]:
        p += 1
    res += Bcon[p]
    
print(res)

Submission status: Accepted

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(' ')]
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

M = [0] * N
index = -1
for i in range(N):
    if i != 0:
        M[i] += M[i - 1]

    inc = 0
    for j in range(index, N - 1):
        if C[j + 1] <= B[i]:
            break
        inc += 1
    M[i] += inc
    index += inc

T = [0] * N
index = -1
for i in range(N):
    if i != 0:
        T[i] += T[i - 1]
    inc = 0
    for j in range(index, N - 1):
        if B[j + 1] <= A[i]:
            break
        inc += 1
    T[i] += sum(M[index + 1: index + inc + 1])
    index += inc
print(sum(T))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from sys import stdin, stdout
from bisect import bisect
def rint():
   return map(int, stdin.readline().split())

n = int(input())

a = list(rint())
b = list(rint())
c = list(rint())
a.sort()
b.sort()
c.sort()
bc = [0 for _ in range(n)]
cc = [0 for _ in range(n)]

ci = 0
for i in range(n):
   ii = bisect(b, a[i])
   if ii >= n:
      for iii in range(ci+1, ii):
         bc[iii] += bc[ci]
      ci = n
      break
   bc[ii] += 1
   for iii in range(ci+1, ii+1):
      bc[iii] += bc[ci]
   ci = ii
if ci < n-1:
   for iii in range(ci+1, n):
      bc[iii] += bc[ci]
ci = 0
for i in range(n):
   ii = bisect(c, b[i])
   if ii >= n:
      for iii in range(ci+1, ii):
         cc[iii] += cc[ci]
      ci = n
      break
   cc[ii] += bc[i]
   for iii in range(ci+1, ii+1):
      cc[iii] += cc[ci]
   ci = ii

if ci < n-1:
   for iii in range(ci+1, n):
      cc[iii] += cc[ci]

print(sum(cc))

Submission status: Accepted

Solution code:
inf=10**9+1

n=int(input())
array_a=sorted([a for a in map(int, input().split())])+[inf]
array_b=sorted([a for a in map(int, input().split())])+[inf]
array_c=sorted([a for a in map(int, input().split())])

nums_b=[0 for _ in range(n)]
cur=0
for (idx,b) in enumerate(array_b[:-1]):
    while(array_a[cur]<b):
        cur+=1
    nums_b[idx]=cur


cur=0
res=0
for (idx,c) in enumerate(array_c):
    while(array_b[cur]<c):
        res+=nums_b[cur]*(n-idx)
        cur+=1
print(res)

Submission status: Accepted

Solution code:
import math
from math import modf

n = int(input())
A = input()
B = input()
C = input()
a = [(int(i) + 0.1) for i in A.split()]
b = [int(i) for i in B.split()]
c = [(int(i) - 0.1) for i in C.split()]
al = sorted(a + b + c)
ans = 0
a_count = 0
c_count = n
for i in al:
    decimal, integer = modf(i)
    if decimal > 0.8:
        c_count -= 1
    elif decimal > 0.05:
        a_count += 1
    else:
        ans += a_count * c_count
print(ans)

Submission status: Accepted

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.sort()
    B.sort()
    C.sort()
    
    A_cand = [0 for i in range(N)]
    B_cand = [0 for i in range(N)]
    
    skip_loop = 0
    for i in range(N):
        cnt = skip_loop
        for j in range(N-skip_loop):
            if B[N-i-1] < C[N-skip_loop-j-1]:
                cnt += 1
            else:
                break
        B_cand[N-i-1] = cnt
        skip_loop = cnt
    
    skip_loop = 0
    ans = 0
    pre_loop_ans = 0
    for i in range(N):
        cnt = skip_loop
        ans += pre_loop_ans
        for j in range(N-skip_loop):
            if A[N-i-1] < B[N-skip_loop-j-1]:
                ans += B_cand[N-skip_loop-j-1]
                pre_loop_ans += B_cand[N-skip_loop-j-1]
                cnt += 1
            else:
                break
        A_cand[N-i-1] = cnt
        skip_loop = cnt
    print(ans)
    

solve()

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

print(count)

Submission status: Accepted

Solution code:
N = int(input())
A = sorted(map(int, input().split()))
B = sorted(map(int, input().split()))
C = sorted(map(int, input().split()))
import bisect
ans = 0
for b in B:
    i = bisect.bisect_left(A,b)
    j = bisect.bisect_right(C,b)
    ans += i * (N-j)

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())))
BA = []
BC = []
x = 0
for i in range(N):
	while True:
		if A[x] < B[i]:
			if x == N - 1:
				BA.append(N)
				break
			else:
				x += 1
		else:
			BA.append(x)
			break
x = 0
for i in range(N):
	while True:
		if C[N - 1 - x] > B[N - 1 - i]:
			if x == N - 1:
				BC.append(N)
				break
			else:
				x += 1
		else:
			BC.append(x)
			break
BC = BC[::-1]

S = 0
for i in range(N):
	S += BA[i] * BC[i]

print(S)

Submission status: Accepted

Solution code:
#!/usr/bin/python

# AtCoder Beginner Contest 077
# C - Snuke Festival

from sys import stdin

def solve(As, Bs, Cs):
    As.sort()
    Bs.sort()
    Cs.sort()
    # Bs :: [(b, # of bottom parts > b)]
    # Bs = [(b, sum([1 for c in Cs if c > b])) for b in Bs]
    j = 0
    for i in range(len(Bs)):
        while j < len(Cs) and Bs[i] >= Cs[j]: j += 1
        Bs[i] = (Bs[i], len(Cs) - j)
    # Accumulate Bs[][1]
    for i in range(len(Bs) - 1, 0, -1):
        Bs[i-1] = (Bs[i-1][0], Bs[i-1][1] + Bs[i][1])
    # As :: [# of mid & bottom parts > a]
    # As = [sum([b[1] for b in Bs if b[0] > a]) for a in As]
    j = 0
    for i in range(len(As)):
        while j < len(Bs) and As[i] >= Bs[j][0]: j += 1
        As[i] = Bs[j][1] if j < len(Bs) else 0
    return sum(As)

N = int(stdin.readline())  # <= 1e+5
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: Accepted

Solution code:
import bisect

if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))

    A.sort()
    C.sort()
    ans = 0
    for b in B:
        a_num = bisect.bisect_left(A, b)
        c_num = N - bisect.bisect_right(C, b)
        ans += a_num * c_num

    print(ans)

Submission status: Accepted

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

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

array1 = list()
count = 0
f = 0
for j in range(N):
    for i in range(f, N):
        if B[N-1-j] < C[N-1-i]:
            count += 1
            if i == N-1:
                f = N
        else:
            f = i
            break
    array1.append(count)
ans = 0
f = 0
count = 0
for i in range(N):
    for j in range(f, N):
        if B[i] > A[j]:
            count += 1
            if j == N-1:
                f = N
        else:
            f = j
            break
    ans += count * array1[N-1-i]
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()))
c = 0
Acount = [0] * len(A)
Bcount = [0] * len(B)
count = 0
A = sorted(A, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)

for b in range(len(B)):
    Bcount[b] = count
    if c < len(C):
        while B[b] < C[c]:
            c += 1
            Bcount[b] += 1
            if c == len(C):
                break
        count = Bcount[b]
b = 0
count = 0
for a in range(len(A)):
    Acount[a] = count
    if b < len(B):
        while A[a] < B[b]:
            Acount[a] += Bcount[b]
            b += 1
            if b == len(B):
                break
        count = Acount[a]

print(sum(Acount))

Submission status: Accepted

Solution code:
x = int(raw_input())
a = map(int, raw_input().split())
b = map(int, raw_input().split())
c = map(int, raw_input().split())

a = sorted(a)
b = sorted(b)
c = sorted(c)

def get_res(arr_u, arr_l, arr_res):
    ind_u, ind_l = 0, 0
    acc = 0
    res = []

    while ind_l < len(arr_l):
        while ind_u < len(arr_u) and arr_u[ind_u] < arr_l[ind_l]:
            acc += arr_res[ind_u]
            ind_u += 1

        while ind_l < len(arr_u) and (ind_u == len(arr_u) or arr_u[ind_u] >= arr_l[ind_l]):
            res.append(acc)
            ind_l += 1

    return res

res_b = get_res(a, b, [1,]*len(b))
res_c = get_res(b, c, res_b)
print sum(res_c)

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()
C.reverse()

ans = 0
ai = 0
ci = N-1
for b in B:
    while ai < N and A[ai] < b:
        ai += 1
    while ci >= 0 and C[ci] <= b:
        ci -= 1
    ans += ai * (ci+1)

print(ans)

Submission status: Accepted

Solution code:
import bisect

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()
    ans = 0
    for y in b:
        i = bisect.bisect_left(a, y)
        j = bisect.bisect_right(c, y)
        if i > 0 and j != len(c):
            ans += i * (n - j)

    print(ans)

main()

Submission status: Runtime Error

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_donwstair = N - bisect.bisect_right(bottom,i)
  ans += can_put_upstair*can_put_downstair

print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
a=sorted(list(map(int,input().split())))
b=sorted(list(map(int,input().split())))
c=sorted(list(map(int,input().split())))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
B=[]
C=[]
x=0
f=0
import bisect
for i in range(n):
    if f==0 and a[x]>=b[i]:
        for j in range(n-x):
            x+=1
            if x>=n:
                x=n
                f=1
                break
            if a[x]<b[i]:
                break
    B.append([b[i],n-x])
S=0
for i in range(n):
    S+=B[i][1]
b1=[S]
for i in range(n-1):
    b1.append(b1[i]-B[i][1])
y=0
ans=0
f1=0
for i in range(n):
    if f1==0 and b[y]>=c[i]:
        for j in range(n-y):
            y+=1
            if y>=n:
                f1==1
                break
            if b[y]<c[i]:
                break
    if y<n:
        ans+=b1[y]
    #print(ans,y)
#print(a)
#print(b)
#print(c)
#print(B)
#print(b1)
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
a=sorted(list(map(int,input().split())))
b=sorted(list(map(int,input().split())))
c=sorted(list(map(int,input().split())))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
B=[]
C=[]
x=0
f=0
import bisect
for i in range(n):
    if a[x]>=b[i] and f==0:
        for j in range(n-x):
            x+=1
            if x>=n:
                x=n
                f=1
                break
            if a[x]<b[i]:
                break
    B.append([b[i],n-x])
S=0
for i in range(n):
    S+=B[i][1]
b1=[S]
for i in range(n-1):
    b1.append(b1[i]-B[i][1])
y=0
ans=0
f1=0
for i in range(n):
    if b[y]>=c[i] and f1==0:
        for j in range(n-y):
            y+=1
            if y>=n:
                f1==1
                break
            if b[y]<c[i]:
                break
    if y<n:
        ans+=b1[y]
    #print(ans,y)
#print(a)
#print(b)
#print(c)
#print(B)
#print(b1)
print(ans)