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

Solution (Python):
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)

Solution (Python):
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])

Solution (Python):
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()

Solution (Python):
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))

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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




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

Solution (Python):



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

Solution (Python):
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))

Solution (Python):

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

Solution (Python):
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

Solution (Python):


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

Solution (Python):
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])

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)