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

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

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

print count

Submission status: Accepted

Solution code:
import bisect

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

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
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 range(N):
    x = bisect_left(A,B[i])
    y = bisect_right(C,B[i])
    ans += x*(N-y)
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()))

count = 0

A.sort()
C.sort()

for i in B:
    t = bisect.bisect_left(A,i)
    s = N - bisect.bisect_right(C,i)
    count += t*s
print(count)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
a = sorted(list(map(int,input().split())))
b = sorted(list(map(int,input().split())))
c = sorted(list(map(int,input().split())))
ans = 0
for i in b:
	a2 = bisect.bisect_left(a,i)
	c2 = len(c) - bisect.bisect_right(c,i)
	ans += a2 * c2
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()
total = 0
for i in range(N):
  total += bisect.bisect_left(a,b[i]) * (N - bisect.bisect_right(c,b[i]))
print(total)

Submission status: Accepted

Solution code:
# 別解（むしろ想定解）のb→axc解法
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())))

ret=0
# a<b[i]となるaの個数xb[i]<cとなるcの個数でいいですよね…
for i in range(n):
    numa = bisect.bisect_left(a,b[i])
    numc = len(c) - bisect.bisect_right(c,b[i])
    ret+=numa*numc
print(ret)

Submission status: Accepted

Solution code:
# 解説したb→cを先に計算、その後累積和を取る解法
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())))

# 各bについてcで使えるのが何種類存在するか
cnum=[0 for i in range(n)]
for i in range(n):
    cnum[i] = len(c) - bisect.bisect_right(c,b[i])
# cnumの累積和
accum=[0 for i in range(n+1)]
for i in reversed(range(n)):
    accum[i]=accum[i+1]+cnum[i]
# 各a[i]について、使えるb,cの組み合わせの通り数を答えに加算していく
ret=0
for i in range(n):
    idx=bisect.bisect_right(b,a[i])
    ret+=accum[idx]
print(ret)

Submission status: Accepted

Solution code:
import bisect

raw_input = input()
input_number = int(raw_input)
counts = 0

top_lane = input().split()
mid_lane = input().split()
bot_lane = input().split()

for i in range(input_number):
    top_lane[i] = int(top_lane[i])
    mid_lane[i] = int(mid_lane[i])
    bot_lane[i] = int(bot_lane[i])

top_lane.sort()
mid_lane.sort()
bot_lane.sort()

for mid in mid_lane:
    top_index = bisect.bisect_left(top_lane, mid)
    bot_index = input_number - bisect.bisect_right(bot_lane, mid)
    counts += top_index * bot_index

print(counts)

Submission status: Accepted

Solution code:
import bisect

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

a.sort()
b.sort()
c.sort()
ans=0
for i in range(n):
	a_l=bisect.bisect_left(a,b[i])
	c_l=bisect.bisect(c,b[i])
	ans+=a_l*(n-c_l)

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()))
assert len(a) == len(b) == len(c) == n

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

def num(top, bottoms):
    nu = bisect.bisect(bottoms, top)
    return n - nu

def numb(bottom, tops):
    nu = bisect.bisect_left(tops, bottom)
    return nu

res = 0
for i in range(n):
    res += num(b[i], c) * numb(b[i], a)
print(res)

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(list(map(int, input().split())), reverse=True)
b = sorted(list(map(int, input().split())), reverse=True)
c = sorted(list(map(int, input().split())), reverse=True)
m1 = [-1 for i in range(n)]
j = 0
for i in range(n):
    while j < n:
        if a[j] < b[i]:
            m1[i] = j
            break
        j += 1
m3 = [-1 for i in range(n)]
j = 0
for i in range(n):
    while j < n:
        if b[j] < c[i]:
            m3[i] = j
            break
        j += 1
temp = 0
m2 = []
for i in m1[::-1]:
    if i == -1:
        m2.append(0)
    else:
        m2.append(temp + (n-i))
        temp = m2[-1]
m2.reverse()
ans = 0
for i in m3:
    if i != -1:
        ans += m2[i]
print(ans)
# print(m1)
# print(m2)
# print(m3)

Submission status: Accepted

Solution code:
import sys
import bisect
n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))
b = list(map(int, sys.stdin.readline().split()))
c = list(map(int, sys.stdin.readline().split()))
a.sort()
b.sort()
c.sort()
r = 0
for bn in b:
  cn = n - bisect.bisect_right(c, bn)
  an = bisect.bisect_left(a, bn)
  r += an * cn
print(r)

Submission status: Accepted

Solution code:
import numpy as np

N=int(input())
all_lst=[]
for i in range(3):
    buf = input()
    for e in buf.split():
        all_lst.append((int(e), 3 - i))
        
all_lst = sorted(all_lst, key = lambda x: (x[0], x[1]))
ac, bc, cc = 0, 0, 0
for tpl in all_lst:
    if tpl[1]==3:
        ac += 1
    elif tpl[1]==2:
        bc += ac
    elif tpl[1] == 1:
        cc += bc
print(cc)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
N = int(input())

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

As.sort()
Bs.sort()
Cs.sort()

ans = 0
# for a in As:
#     for b in Bs:
#         if a >= b:
#             continue
#         for c in Cs:
#             if b >= c:
#                 continue
#             #print(a,b,c)
#             ans += 1
#
# print(ans)

Bcnt = [0 for i in range(N)]
i = 0
for b in Bs:
    cnt = N - bisect.bisect_right(Cs, b)
    # print(b, cnt)
    Bcnt[i] = cnt
    i += 1

Bsum = [0 for i in range(N)]
Bsum[N-1] = Bcnt[N-1]
for i in range(N-2, -1, -1):
    Bsum[i] = Bsum[i+1] + Bcnt[i]
    # print(i, Bsum)

for a in As:
    cnt = bisect.bisect_right(Bs, a)
    if cnt == N:
        ans += 0
    else:
        ans += Bsum[cnt]

# print(Bcnt)
# print(Bsum)
print(ans)

Submission status: Accepted

Solution code:
import bisect

N = int(input())
s = sorted(list(map(int,input().split())))
m = sorted(list(map(int,input().split())))
l = sorted(list(map(int,input().split())))
count = 0
for mitem in m:
  count += bisect.bisect_left(s,mitem)*(N-bisect.bisect_right(l,mitem))
print (count)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from bisect import bisect_left, bisect
N = int(input())
A = sorted(tuple(map(int, input().split())))
B = sorted(tuple(map(int, input().split())))
C = sorted(tuple(map(int, input().split())))

AB = []
result = 0
 
for i in range(N):
    AB = bisect_left(A, B[i])
    BC = N - bisect(C, B[i])
    result += AB * BC

print(result)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n = I()
    a = sorted(LI())
    b = sorted(LI())
    c = sorted(LI())
    t = [0] * n
    j = 0
    for i in range(n):
        bi = b[i]
        while j < n and c[j] <= bi:
            j += 1
        t[i] = n - j
    r = 0
    for i in range(n-2,-1,-1):
        t[i] += t[i+1]
    j = 0
    for i in range(n):
        ai = a[i]
        while j < n and b[j] <= ai:
            j += 1
        if j < n:
            r += t[j]

    return r



print(main())

Submission status: Accepted

Solution code:
import sys
import random

def rd():
        return sys.stdin.readline().rstrip()

def rdi():
        return [ int(x) for x in rd().split() ]

N, = rdi()
A = rdi()
B = rdi()
C = rdi()

a = sorted(A)
b = sorted(B)
c = sorted(C)

s = []
j = 0
for i in range(N):
        while j < N and a[j] < b[i]:
                j += 1
        if i == 0:
                s.append(j)
        else:
                s.append(s[i-1] + j)

t = 0
j = 0
for i in range(N):
        while j < N  and b[j] < c[i]:
                j += 1
        #print s[i], j, s[i]*j
        if j > 0:
                t += s[j-1]

print t

Submission status: Accepted

Solution code:
#C_Snuke Festival
import bisect

def SnukeFestival(N, A, B, C):
    A = sorted(A)
    B = sorted(B)
    C = sorted(C)
    count = 0

    for b in B:
        n_a = bisect.bisect_left(A,b)
        n_c = bisect.bisect_right(C,b)
        count += n_a * (N - n_c)

    print(count)

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

Submission status: Accepted

Solution code:
import bisect

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

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

ans = 0

for x in b:
    aa = bisect.bisect_left(a,x)
    cc = len(c) - bisect.bisect_right(c,x)
    ans += aa * cc

print(ans)

Submission status: Accepted

Solution code:
import bisect

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

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

ans = 0

for x in b:
    aa = bisect.bisect_left(a,x)
    cc = len(c) - bisect.bisect_right(c,x)
    ans += aa * cc

print(ans)

Submission status: Accepted

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

import bisect

def f(n, aVals, bVals, cVals):
    #     return len(ys)*len(xs) - sum(bisect.bisect_left(ys, x) for x in xs

    bVals = sorted(bVals)
    cVals = sorted(cVals)

    xs = [0]*(n+1)
    x = 0
    for i in range(n-1, -1, -1):
        x += n - bisect.bisect_right(cVals, bVals[i])
        xs[i] = x

    return sum(xs[bisect.bisect_right(bVals, a)]
               for a in aVals)


def test():
    assert f(2, [1, 5], [2, 4], [3, 6]) == 3
    assert f(3, [1, 1, 1], [2, 2, 2], [3, 3, 3]) == 27
    assert f(6, [3, 14, 159, 2, 6, 53],
             [58, 9, 79, 323, 84, 6],
             [2643, 383, 2, 79, 50, 288]) == 87


def main():
    n = int(input())
    aVals = list(map(int, input().split()))
    bVals = list(map(int, input().split()))
    cVals = list(map(int, input().split()))

    print(f(n, aVals, bVals, cVals))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

a.sort()
c.sort()


def bisect_left(a, x, lo=0, hi=None):
    """Return the index where to insert item x in list a, assuming a is sorted.
    The return value i is such that all e in a[:i] have e < x, and all e in
    a[i:] have e >= x.  So if x already appears in the list, a.insert(x) will
    insert just before the leftmost x already there.
    Optional args lo (default 0) and hi (default len(a)) bound the
    slice of a to be searched.
    """

    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if a[mid] < x: lo = mid+1
        else: hi = mid
    return lo

def bisect_right(a, x, lo=0, hi=None):
    """Return the index where to insert item x in list a, assuming a is sorted.
    The return value i is such that all e in a[:i] have e <= x, and all e in
    a[i:] have e > x.  So if x already appears in the list, a.insert(x) will
    insert just after the rightmost x already there.
    Optional args lo (default 0) and hi (default len(a)) bound the
    slice of a to be searched.
    """

    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if x < a[mid]: hi = mid
        else: lo = mid+1
    return lo

num = 0
for i in range(N):
    num += (bisect_left(a, b[i])) * (len(c) - bisect_right(c, b[i]))
print(num)

Submission status: Accepted

Solution code:
length = int(input())
smallest = sorted([int(n) for n in input().split(" ")])
normal = sorted([int(n) for n in input().split(" ")], reverse = True)
biggest = sorted([int(n) for n in input().split(" ")], reverse = True)
norm_vs_s = [0 for n in range(length)]
index = length - 1
for i, n in enumerate(smallest):
    if normal[0] < n:
        index = i
        break
for i in range(length):
    while index >= 0 and normal[i] <= smallest[index]:
        index -= 1
    norm_vs_s[i] = index + 1
answer = 0
sum_normal = sum(norm_vs_s)
index = 0
for i in range(length):
    while index < length and normal[index] >= biggest[i]:
        sum_normal -= norm_vs_s[index]
        index += 1
    answer += sum_normal
print(answer)

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()))
ans = 0
A.sort()
B.sort()
C.sort()
# print(A, B, C)
for b in B:
    a_i = bisect.bisect_left(A, b)
    c_i = bisect.bisect_right(C, b)
    cnt = (a_i) * (N - c_i)
    # print(b, a_i, c_i, cnt)
    ans += cnt

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(); c.sort()
ans = 0
for i in range(n) :
    l_a = bisect.bisect_left(a, b[i])
    r_c = bisect.bisect_right(c, b[i])
    ans += l_a * (n - r_c)

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()
dp = [0]*(n+1)
for i in range(n-1, -1, -1) :
    ins_c = bisect.bisect_right(c, b[i])
    dp[i] = dp[i+1] + (n-ins_c)
ans = 0
for i in range(n) :
    ins_b = bisect.bisect_right(b, a[i])
    ans += dp[ins_b]

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())

cntA = Counter(map(int, input().split()))
cntB = Counter(map(int, input().split()))
cntC = Counter(map(int, input().split()))

keyA = sorted(list(cntA.keys()))
keyB = sorted(list(cntB.keys()))
keyC = sorted(list(cntC.keys()))

lenA = len(keyA)
lenC = len(keyC)
iA = 0
iC = 0
numA = 0
numC = N
ans = 0
for kB in keyB:
    for i in range(iA, lenA):
        if keyA[i] >= kB:
            iA = i
            break
        numA += cntA[keyA[i]]
    else:
        iA = lenA

    for i in range(iC, lenC):
        if keyC[i] > kB:
            iC = i
            break
        numC -= cntC[keyC[i]]
    else:
        iC = lenC

    ans += numA * numC * cntB[kB]

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

i = 0
j = 0
k = 0
p = []
while i < n and j < n:
    if a[i] < b[j]:
        i += 1
    else:
        p.append(i + k)
        k = p[-1]
        j += 1
else:
    while j < n:
        p.append(n + k)
        k = p[-1]
        j += 1
       
i = 0
j = 0
k = 0
s = []
while i < n and j < n:
    if b[i] < c[j]:
        i += 1
    else:
        if i == 0:
            s.append(0)
        else:
            s.append(p[i-1] + k)
        k = s[-1]
        j += 1
else:
    while j < n:
        s.append(p[-1] + k)
        k = s[-1]
        j += 1

print(s[-1])

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right, insort_left, insort_right

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


    BC = []
    for i in range(N):
        b = B[i]
        BC.append(N - bisect_left(C, b))

    Cum = []
    S = sum(BC)
    tmp = S
    for i in range(N):
        Cum.append(tmp)
        tmp -= BC[i]
    Cum.append(0)

    count = 0
    for i in range(N):
        a = A[i]
        count += Cum[bisect_right(B, a)]

    print(count)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import bisect
from itertools import accumulate


def solve(n, aa, bb, cc):
    def f(ff, tt):
        pv = 0
        ret = [0] * n
        for i, e in enumerate(ff):
            ret[i] = pv = bisect.bisect(tt, e, pv)
        return ret

    ac = f(aa, bb)
    bc = f(bb, cc)
    ba = list(reversed(list(accumulate(n - b for b in reversed(bc)))))
    lim = bisect.bisect_left(ac, n)
    return sum(ba[a] for a in ac[:lim])


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

Submission status: Accepted

Solution code:
n = int(input())
u = sorted(map(int,input().split()))+[float('inf')]
m = sorted(map(int,input().split()))+[float('inf')]
l = sorted(map(int,input().split()))+[float('inf')]
dpm = [0]
dpl = [0]

i = 0
for j in range(n):
    cur = 0
    while u[i] < m[j]: cur+= 1; i+= 1
    dpm.append(dpm[-1]+cur)
i = 0
for j in range(n):
    cur = 0
    while m[i] < l[j]: cur+= dpm[i+1]; i+= 1
    dpl.append(dpl[-1]+cur)
print(sum(dpl))

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

mem = []
for b in B:
    mem.append(N - bisect.bisect(C,b))

mem2 = [mem[-1]]
for i in reversed(range(N-1)):
    mem2.append(mem2[-1] + mem[i])

ans = 0
for a in A:
    i = bisect.bisect(B,a)
    if i >= N: continue
    ans += mem2[N-1-i]

print(ans)

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

from bisect import bisect, bisect_left, bisect_right



# Find rightmost value less than x
def find_lt(a, x):
    i = bisect_left(a, x)
    if i:
        return i
    return 0


# Find leftmost value greater than x'
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:
#python3
# utf-8

parts_nr = int(input())
a_part_idx___size = sorted([int(x) for x in input().split()], reverse=True)
b_part_idx___size = sorted([int(x) for x in input().split()], reverse=True)
c_part_idx___size = sorted([int(x) for x in input().split()], reverse=True)

b_part_idx___good_c_nr = []
curr_nr = 0
for b_part_idx in range(parts_nr):
    while curr_nr < parts_nr and c_part_idx___size[curr_nr] > b_part_idx___size[b_part_idx]:
        curr_nr += 1
    b_part_idx___good_c_nr.append(curr_nr)

curr_b_part_idx = 0
curr_ans = 0
a_part_idx___ans = []
for a_part_idx in range(parts_nr):
    while curr_b_part_idx < parts_nr and a_part_idx___size[a_part_idx] < b_part_idx___size[curr_b_part_idx]:
        curr_ans += b_part_idx___good_c_nr[curr_b_part_idx]
        curr_b_part_idx += 1
    a_part_idx___ans.append(curr_ans)
print(sum(a_part_idx___ans))

Submission status: Accepted

Solution code:
from bisect import bisect_left
N = int(input())
A = sorted(int(x) for x in input().split())
B = sorted(int(x) for x in input().split())
C = sorted(int(x) for x in input().split())
cnt = [0] * (N + 1)
for i, b in enumerate(B):
    cnt[i + 1] = bisect_left(A, b)
    cnt[i + 1] += cnt[i]
print(sum(cnt[bisect_left(B, c)] for c in C))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import heapq
import math
import sys
import bisect

N = int(input())
ABC = []
for i in range(3):
    tmp = input()
    t = [int(a) for a in tmp.split()]
    ABC.append(sorted(t))
ABC = ABC[::-1]
cumulative = [[i for i in range(N+1)][::-1]
               if j == 0 else [0 for i in range(N+1)]
              for j in range(3)]
for i in range(1, 3):
    for j, a in enumerate(ABC[i]):
        k = bisect.bisect_right(ABC[i-1], a)
        cumulative[i][j] = cumulative[i-1][k]
    for j, _ in enumerate(ABC[i]):
        cumulative[i][N-j-1] = cumulative[i][N-j-1] + cumulative[i][N-j]

print(cumulative[2][0])

Submission status: Accepted

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

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

m = [0 for i in range(N)]

j = k = 0
while j < N and k < N:
    if B[j] >= C[k]:
        k += 1
        continue
    m[j] = N - k
    j += 1

for i in range(N-2, -1, -1):
    m[i] += m[i+1]
    
cnt = 0
i = j = 0
while i < N and j < N:
    if A[i] >= B[j]:
        j += 1
        continue
    cnt += m[j]
    i += 1

print cnt

Submission status: Accepted

Solution code:
#!/usr/bin/env pypy3

import bisect
import itertools


def in_place_count_stages(n, xs, ys, zs):
    xs.sort()
    ys.sort()
    zs.sort()
    y_to_z_num = [n - bisect.bisect(zs, y) for y in ys]
    acc_y_to_z_num = list(itertools.accumulate(y_to_z_num))
    x_to_y_idxs = [bisect.bisect(ys, x) for x in xs]
    g = acc_y_to_z_num
    x_to_res = [g[-1] - (g[i - 1] if i > 0 else 0) for i in x_to_y_idxs]
    return sum(x_to_res)


def main():
    n = int(input())
    xs = [int(x) for x in input().split()]
    ys = [int(x) for x in input().split()]
    zs = [int(x) for x in input().split()]
    res = in_place_count_stages(n, xs, ys, zs)
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from bisect import bisect_left as bisect
from itertools import accumulate

N = int(input())

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

T = A+B+C
T.sort()

A = [bisect(T,v) for v in A]
B = [bisect(T,v) for v in B]
C = [bisect(T,v) for v in C]

T = len(T)+1

dp = [1]*T

for X in (A,B,C):
  ndp = [0]*T
  for x in X:
    ndp[x+1] += dp[x]
  dp = list(accumulate(ndp))

print(dp[-1])

Submission status: Accepted

Solution code:
import bisect

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

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

ia = [bisect.bisect_left(a, bi) for bi in b]
sa = [0]
for i in range(n):
    sa.append(sa[-1])
    sa[-1] += ia[i]
ib = [bisect.bisect_left(b, ci) for ci in c]

ans = 0
for i in ib:
    ans += sa[i]
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right

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

BN = []
for b in B:
    i = bisect_right(C, b)
    BN.append(N - i)
for i in range(N - 2, -1, -1):
    BN[i] += BN[i + 1]
BN.append(0)
s = 0
for a in A:
    i = bisect_right(B, a)
    s += BN[i]
print(s)

Submission status: Accepted

Solution code:
n=int(input())
A=input().split()
B=input().split()
C=input().split()
for i in range(n):
    A[i]=int(A[i])
    B[i]=int(B[i])
    C[i]=int(C[i])
C.sort()
C=C[::-1]
B.sort()
B=B[::-1]
A.sort()
A=A[::-1]


Cres=[1 for i in range(n)]
Bres=[0 for i in range(n)]
Ares=[0 for i in range(n)]

k=-1
for i in range(n):
    currsize=B[i]
    while k+1<n and currsize<C[k+1]:
        k+=1
    Bres[i]=k+1

k=-1
su=0
for i in range(n):
    currsize=A[i]
    while k+1<n and currsize<B[k+1]:
        k+=1
        su+=Bres[k]
    Ares[i]=su

su=0
for i in range(n):
    su+=Ares[i]
print (su)

Submission status: Accepted

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

TB = [0]*len(B)
a = 0
for b in range(len(B)):
    while a<len(A) and A[a]<B[b]:
        a += 1
    TB[b] = a
TC = [0]*len(C)
b = 0
s = 0
for c in range(len(C)):
    while b<len(B) and B[b]<C[c]:
        s += TB[b]
        b += 1
    TC[c] = s
print sum(TC)

Submission status: Runtime Error

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

# In[4]:


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


# In[6]:


ans = 0
for b in B:
    while i<N and A[i]<b:
        i+=1
    while j<N and C[j]<=b:
        j+=1
    ans += i*(N-j)
print(ans)


# In[ ]:

Submission status: Runtime Error

Solution code:
def binery_search(left,right,keynum,list):
    mid = 0
    while True:
            #print(left,right,mid,cnt)
            mid = int( (left+right)/2 )
            if keynum == list[mid]:
                while True:
                    if list[mid] == list[mid+1]:
                        mid +=1
                        if mid == len(list)-1:
                            mid += 1
                            break
                    else:
                        mid += 1
                        break
                return mid
            elif keynum < list[mid]:
                right = mid
            elif keynum > list[mid]:
                left = mid + 1

            if left == right:
                return left

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()
    B_num = []
    for i in range(N):
        B_num.append(N-binery_search(0,N,B[i],C))
        #print(B_num,binery_search(0,N,B[i],C))
    #累積和を計算
    B_ruiseki = [0]*N
    B_ruiseki[N-1] = B_num[i]
    for i in range(N-2,-1,-1):
        B_ruiseki[i] = B_ruiseki[i+1] + B_num[i]

    ans = 0
    for i in range(N):
        #print(binery_search(0,N,A[i],B))
        tmp = binery_search(0,N,A[i],B)
        if tmp >= N:
            continue
        else:
            ans += B_ruiseki[binery_search(0,N,A[i],B)]

    return ans

print(main())

Submission status: Runtime Error

Solution code:
def binery_search(left,right,keynum,list):
    mid = 0
    while True:
            #print(left,right,mid,cnt)
            mid = int( (left+right)/2 )
            if keynum == list[mid]:
                while True:
                    if list[mid] == list[mid+1]:
                        mid +=1
                        if mid == len(list)-1:
                            break
                    else:
                        mid += 1
                        break
                return mid
            elif keynum < list[mid]:
                right = mid
            elif keynum > list[mid]:
                left = mid + 1

            if left == right:
                return left

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()
    B_num = []
    for i in range(N):
        B_num.append(N-binery_search(0,N,B[i],C))
        #print(B_num,binery_search(0,N,B[i],C))
    #累積和を計算
    B_ruiseki = [0]*N
    B_ruiseki[N-1] = B_num[i]
    for i in range(N-2,-1,-1):
        B_ruiseki[i] = B_ruiseki[i+1] + B_num[i]

    ans = 0
    for i in range(N):
        #print(binery_search(0,N,A[i],B))
        tmp = binery_search(0,N,A[i],B)
        if tmp >= N:
            continue
        else:
            ans += B_ruiseki[binery_search(0,N,A[i],B)]

    return ans

print(main())