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

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

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

    ans += numA * numC * cntB[kB]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort(); b.sort(); c.sort()
ans = 0
for i in range(n) :
    r_b = bisect.bisect_right(b, a[i])
    for j in range(r_b, n) :
        r_c = bisect.bisect_right(c, b[j]) 
        ans += n - r_c

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
a = np.array( list(map(float, input().split(" "))) )
b = np.array( list(map(float, input().split(" "))) )
c = np.array( list(map(float, input().split(" "))) )

# a.sort()
# b.sort()
# c.sort()
dic = {}
def count_c(v):
    key = v
    if v in dic:
        return dic[v]
    else:
        val = len(np.where( (v < c) == True )[0])
        dic.update({key:val})
        return val

num = 0
for i in range(N):
    for j in range(N):
        if a[i] < b[j]:
            num += count_c(b[j])

print(num)

Submission status: Time Limit Exceeded

Solution code:
def Search(x, L, R, A):
    if R-L == 1:
        if x >= A[L]:
            return R
        return L

    Mid = int((L+R) / 2)
    if x >= A[Mid]:
        return Search(x, Mid, R, A)
    return Search(x, L, Mid, A)


buf = input()

N = int(buf)

buf_1 = input().split(" ")
buf_2 = input().split(" ")
buf_3 = input().split(" ")


A = []
B = []
C = []
for i in range(N):
    A.append(int(buf_1[i]))
    B.append(int(buf_2[i]))
    C.append(int(buf_3[i]))

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

BCount = []
for i in range(N):
    BCount.append(N - Search(B[i], 0, N, C))

for i in range(N - 1, 0, -1):
    BCount[i - 1] += BCount[i]

ans = 0
for i in range(N):
    if Search(A[i], 0, N, B) == N:
        continue

    ans += BCount[Search(A[i], 0, N, B)]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
arr=[[0]*n for i in range(3)]
for i in range(3):
	l=[int(x) for x in input().split()]
	for j in range(n):
		arr[i][j]=l[j]

def solve(i,j):
	if i==3:
		return 1
	if j==n:
		return 1
	if dp[i][j]!=-1:
		return dp[i][j]

	ans=0
	for k in range(n):
		if i!=0:
			if arr[i-1][j] < arr[i][k]:
				ans+=solve(i+1,k)
		else:
			ans+=solve(i+1,k)
				
	dp[i][j]=ans
	return dp[i][j]
dp=[[-1]*(n+1) for i in range(4)]

print(solve(0,0))

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(reverse=True)
cnt = 0
for i in range(N):
    cntA = 0
    cntC = 0
    for j in range(cntA, N):
        if A[j] >= B[i]:
            cntA = j
            break
    else:
        cntA = N
    for j in range(cntC, N):
        if C[j] <= B[i]:
            cntC = j
            break
    else:
        cntC = N
    cnt += cntA * cntC
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())
a_list = input().split()
a_list = [int(a) for a in a_list]

b_list = input().split()
b_list = [int(b) for b in b_list]

c_list = input().split()
c_list = [int(c) for c in c_list]

a_ar = np.array(a_list)
b_ar = np.array(b_list)
c_ar = np.array(c_list)

a_ar.sort()
b_ar.sort()
c_ar.sort()

b_count = []
for b in b_ar:
    b_count.append(len(c_ar[c_ar>b]))
    
b_count_ar = np.array(b_count)

count = 0
for a in a_ar:
    count += b_count_ar[np.where(b_ar>a)[0]].sum()
    
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()))
A.sort()
B.sort()
C.sort(reverse=True)
cnt = 0
for i in range(N):
    cntA = 0
    cntC = 0
    for j in range(N):
        if A[j] >= B[i]:
            cntA = j
            break
    else:
        cntA = j + 1
    for j in range(N):
        if C[j] <= B[i]:
            cntC = j
            break
    else:
        cntC = j + 1
    cnt += cntA * cntC
print(cnt)

Submission status: Time Limit Exceeded

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

ans = 0
for i, a in enumerate(A):
  for j, b in enumerate(B):
    if a < b:
      for k, c in enumerate(C):
        if b < c:
          ans += N - k
          break

print ans

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())
a_list = input().split()
a_list = [int(a) for a in a_list]

b_list = input().split()
b_list = [int(b) for b in b_list]

c_list = input().split()
c_list = [int(c) for c in c_list]
# a_ar = np.unique(np.array(a_list))
# b_ar = np.unique(np.array(b_list))
# c_ar = np.unique(np.array(c_list))
a_ar = np.array(a_list)
b_ar = np.array(b_list)
c_ar = np.array(c_list)
count = 0
for a in a_ar:
    for b in b_ar[b_ar>a]:
        count += len(c_ar[c_ar>b])

print(count)

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)

for i in range(n):
    b[i] = (b[i], bisect.bisect_right(a, b[i]-1))

b = sorted(b)

bsum = [b[0][1]]
b[0] = b[0][0]
for i in range(1, n):
    bsum.append(bsum[i-1] + b[i][1])
    b[i] = b[i][0]

cnt = 0
for i in range(n):
    ind = bisect.bisect_right(b, c[i]-1)

    if ind > 0:
        cnt += bsum[ind-1]

print(cnt)

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()
ab=[]
bc=[]
for i in range(n):
    ab.append([a[i],n])
    bc.append([b[i],n])
    ab.append([b[i],i])
    bc.append([c[i],i])
numa=1
numb=1
numc=1
la=[1]*n
lb=[1]*n
lc=[1]*n
for i in range(1,n):
    if a[i-1]!=a[i]:
        la[i]=i+1
        numa=i+1
    else:
        la[i]=numa
    if b[i-1]!=b[i]:
        lb[i]=i+1
        numb=i+1
    else:
        lb[i]=numb
    if c[i-1]!=c[i]:
        lc[i]=i+1
        numc=i+1
    else:
        lc[i]=numc
ab.sort(key=lambda x: x[0])
numab=1
lab=[1]*(n+1)
for i in range(1,n*2):
    if ab[i-1][0]!=ab[i][0]:
        lab[ab[i][1]]=i+1
        numab=i+1
    else:
        lab[ab[i][1]]=numab
ansb=[0]*n
numb=0
for i in range(n):
    numb+=lab[i]-lb[i]
    ansb[i]=numb
bc.sort(key=lambda x: x[0])
numbc=1
lbc=[1]*(n+1)
for i in range(1,n*2):
    if bc[i-1][0]!=bc[i][0]:
        lbc[bc[i][1]]=i+1
        numbc=i+1
    else:
        lbc[bc[i][1]]=numbc
ansc=[0]*n
for i in range(n):
    ansc[i]=lbc[i]-lc[i]
ans=0
for i in range(n):
    if ansc[i]!=0:
        ans+=ansb[ansc[i]-1]
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()
c.sort()
ans = 0
for j in range(n):
    l = 0
    r = n - 1
    i = (l + r) // 2
    while l <= r:
        if a[i] >= b[j]:
            r = i - 1
        else:
            l = i + 1
        i = (l + r) // 2
    l = 0
    r = n - 1
    k = (l + r) // 2
    while l <= r:
        if c[k] <= b[j]:
            l = k + 1
        else:
            r = k - 1
        k = (l + r) // 2
    ans += (i + 1) * (n - (k + 1))
print(ans)

Submission status: Accepted

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

# In[8]:


from bisect import bisect_left,bisect


# In[6]:


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[9]:


ans = 0
for b in B:
    i = bisect_left(A,b)
    j = bisect(C,b)
    ans += i*(N-j)
print(ans)


# In[ ]:

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()
a.reverse()
b.sort()
b.reverse()
c.sort()
c.reverse()
c.append(0)

i=0
l=[]
for j in b:
  while c[i]>j:
    i+=1
  l.append(i)
b.append(0)
s=0
cnt=0
i=0
for j in a:
  while b[i]>j:
    s+=l[i]
    i+=1
  cnt+=s
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())

A = sorted(list(map(int, input().split())))+[10**10]
B = sorted(list(map(int, input().split())))+[10**10]
C = sorted(list(map(int, input().split())))

cnt = [0]*(N+1)

ai = 0
for bi in range(N):
    while A[ai] < B[bi]:
        ai += 1
    cnt[bi] = ai

for i in range(1, N):
    cnt[i] += cnt[i-1]

ans = 0

bi = 0
for ci in range(N):
    while B[bi] < C[ci]:
        bi += 1
    ans += cnt[bi-1]

print(ans)

Submission status: Accepted

Solution code:
import sys
from sys import exit
from collections import deque
from copy import deepcopy
from bisect import bisect_left, bisect_right, insort_left, insort_right
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import gcd, sin, cos, tan, asin, acos, atan, degrees, radians

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod=MOD):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x,d=1):
        self.setdefault(x,0)
        self[x] += d
    def list(self):
        l = []
        for k in self:
            l.extend([k]*self[k])
        return l
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]
def pf(x,mode='counter'):
    C = counter()
    p = 2
    while x>1:
        k = 0
        while x%p==0:
            x //= p
            k += 1
        if k>0:
            C.add(p,k)
        p = p+2-(p==2) if p*p<x else x
    if mode=='counter':
        return C
    S = set([1])
    for k in C:
        T = deepcopy(S)
        for x in T:
            for i in range(1,C[k]+1):
                S.add(x*(k**i))
    if mode=='set':
        return S
    if mode=='list':
        return sorted(list(S))

######################################################

N=intput()
A=lint()
B=lint()
C=lint()
A.sort()
B.sort()
C.sort()
AB=[0]*N
A.append(INF)
a=0
b=0
while b<N:
    if A[a]<B[b]:
        a+=1
    else:
        AB[b]=(AB[b-1] if b>0 else 0)+a
        b+=1
ABC=[0]*N
AB=[0]+AB[:]
B.append(INF)
b=0
c=0
while c<N:
    if B[b]<C[c]:
        b+=1
    else:
        ABC[c]=(ABC[c-1] if c>0 else 0)+AB[b]
        c+=1
print(ABC[-1])

Submission status: Accepted

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

A.sort()
C.sort()
from bisect import*
ans=0
for b in B:
    x=bisect_left(A,b)
    y=N-bisect_right(C,b)
    ans+=x*y
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())))

u_cnt = [0] * N
d_cnt = [0] * N

t = 0
for i in range(N):
    while t < N and A[t] < B[i]:
        t += 1
    u_cnt[i] = t

t = N-1
for i in range(N-1,-1,-1):
    while t >= 0 and C[t] > B[i]:
        t -= 1
    d_cnt[i] = N - 1 - t

ans = 0
for i in range(N):
    ans += u_cnt[i] * d_cnt[i]

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:sorted(list(map(int,stdin.readline().split())))
from bisect import bisect_left, bisect_right

n=int(input())
a=lnii()
b=lnii()
c=lnii()

ans=0
for i in range(n):
  m=b[i]
  num1=bisect_left(a,m)
  num2=n-bisect_right(c,m)
  ans+=num1*num2

print(ans)

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_left,bisect_right


def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり


N = I()
A,B,C = LI(),LI(),LI()
A.sort()
C.sort()

print(sum(bisect_left(A,b)*(N-bisect_right(C,b)) for b in B))

Submission status: Accepted

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())))
B1 = []
for i in range(N):
    ind = bisect_right(C,B[i])
    B1.append(ind)
B2 = [0 for _ in range(N+1)]
for i in range(N-1,-1,-1):
    B2[i] = B2[i+1]+N-B1[i]
cnt = 0
for i in range(N):
    ind = bisect_right(B,A[i])
    cnt += B2[ind]
print(cnt)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
As = sorted(list(mapint()))
Bs = sorted(list(mapint()))
Cs = sorted(list(mapint()))

from bisect import bisect_left, bisect_right

ans = 0
for b in Bs:
    a_idx = bisect_left(As, b)
    c_idx = N-bisect_right(Cs, b)
    ans += a_idx*c_idx
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left
n=int(input())
a=sorted(list(map(int,input().split())))
b=sorted(list(map(int,input().split())))
c=sorted(list(map(int,input().split())))
d=[0]
e=[]
for i in range(n):
    d.append(bisect_left(a,b[i])+d[-1])
    e.append(bisect_left(b,c[i]))
ans=0
for i in e:
    ans+=d[i]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
alst = list(map(int, input().split()))
blst = list(map(int, input().split()))
clst = list(map(int, input().split()))
alst.sort()
blst.sort()
clst.sort()

b_s = [0 for _ in range(n)]
a_pos = 0
alst.append(10 ** 10)
for i in range(n):
    while blst[i] > alst[a_pos]:
        a_pos += 1
    b_s[i] = a_pos + b_s[i - 1]
b_pos = 0
blst.append(10 ** 10)
ans = 0
for i in range(n):
    while clst[i] > blst[b_pos]:
        b_pos += 1
    if b_pos == 0:
        continue
    ans += b_s[b_pos - 1]
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left, 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 range(n):
    x = b[i]
    l = bisect_left(a, x)
    r = bisect_right(c, x)
    ans += l * (n - r)
print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし


N = I()
A,B,C = LI(),LI(),LI()
A.sort()
C.sort()

from bisect import bisect_left,bisect_right

ans = 0
for i in range(N):
    b = B[i]
    ans += bisect_left(A,b)*(N-bisect_right(C,b))

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

from bisect import bisect_left, bisect
ans = 0
for b in B:
    i = bisect_left(A,b)
    j = bisect(C,b)
    ans += i * (N-j)
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7


def run():
    N = int(sysread())
    A = list(map(int, sysread().split()))
    B = list(map(int, sysread().split()))
    C = list(map(int, sysread().split()))
    A.sort()
    B.sort()
    C.sort()
    ansBa = []
    ansBc = []
    for i in range(N):
        b = B[i]
        idx = bisect.bisect_left(A, b)
        ansBa.append(idx)

        idx2 = bisect.bisect_right(C, b)
        ansBc.append(N-idx2)

    #print(ansBc, ansBa)
    ans = 0
    for aa, cc in zip(ansBa, ansBc):
        ans += aa * cc


    print(ans)

if __name__ == "__main__":
    run()

Submission status: Accepted

Solution code:
import bisect
from collections import deque
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()

d = deque([])
for i in range(N):
  n = bisect.bisect_right(C, B[N-1-i])
  if len(d) == 0:
    d.appendleft(N-n)
  else:
    d.appendleft(N-n+d[0])
  
ans = 0
for i in range(N):
  m = bisect.bisect_right(B, A[i])
  if m < N:
    ans += d[m]
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right
from itertools import accumulate

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_count = [0] * N
    for i in range(N):
        index = bisect_right(C,B[i])
        if index < N:
            B_count[i] = N - index
    D = [0] + list(accumulate(B_count))
    ans = 0
    for a in A:
        index = bisect_right(B,a)
        ans += D[N] - D[index]

    print(ans)

if __name__ == "__main__":
    main()

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
from bisect import bisect_left,bisect_right
for i in B:
   ans+=(bisect_left(A,i))*(N-bisect_right(C,i))
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.sort()
b.sort()
c.sort()


ans=0
ind=[0]
for i in range(n):
  ind.append(n-bisect.bisect_right(c,b[i])+ind[i])

for i in a:
  b_ind=bisect.bisect_right(b,i)

  ans+=ind[n]-ind[b_ind]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from bisect import bisect_right, bisect_left
N = int(input())
*A, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())
A.sort()
B.sort()
C.sort()
lc = len(C)
ans = 0
for b in B:
    a = bisect_left(A, b)
    c = len(C) - bisect_right(C, b)
    ans += a * c
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())))
bn = [0]*n
j = 0
for i in range(n):
  bi = b[i]
  while(j<n):
    if bi < c[j]:
      bn[i] = n-j
      break
    j += 1
bnt = sum(bn)
bs = 0
j = 0
ans = 0
for i in range(n):
  ai = a[i]
  while(j<n):
    if ai < b[j]:
      ans += bnt - bs
      break
    bs += bn[j]
    j += 1
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=sorted(A)
B=sorted(B)
C=sorted(C)
A2=[0]*N
s=0
i=0
n=A[s]
while True:
  if B[i]>n:
    A2[i]+=1
    s+=1
    if s==N:
      break
    n=A[s]
  else:
    i+=1
  if i==N:
    break
for i in range(1,N):
  A2[i]=A2[i-1]+A2[i]
B2=[0]*N
s=0
i=0
n=B[s]
while True:
  if C[i]>n:
    B2[i]+=A2[s]
    s+=1
    if s==N:
      break
    n=B[s]
  else:
    i+=1
  if i==N:
    break
for i in range(1,N):
  B2[i]=B2[i-1]+B2[i]
print(sum(B2))

Submission status: Accepted

Solution code:
N=int(input())
A,B,C=[sorted(map(int,input().split())) for i in range(3)]
i,j=0,0
r=0
for b in B:
    while i<N and A[i]<b:
        i+=1
    while j<N and C[j]<=b:
        j+=1
    r+=i*(N-j)
print(r)

Submission status: Accepted

Solution code:
# a[i]<b[j]<c[k]
# iterate over b and find the lower bound and upper bound in a and c respectively
from bisect import bisect_left , bisect_right
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 i in b:
		k = bisect_left(a,i)
		l = bisect_right(c,i)
		ans = ans+(k*(n-l))
	print(ans)
if __name__=="__main__":
	main()

Submission status: Accepted

Solution code:

from bisect import bisect_left , bisect_right
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 i in b:
		k = bisect_left(a,i)
		l = bisect_right(c,i)
		ans = ans+(k*(n-l))
	print(ans)
if __name__=="__main__":
	main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
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()
#print(A)
#print(B)
#print(C)
Bok = []
for b in B:
  temp = bisect.bisect_left(A,b) #1indexでの位置
  Bok.append(temp) #個数なのでindexで持つ
SB = [0]
for i in range(N):
  temp = SB[-1]+Bok[i]
  SB.append(temp)
#print(SB)
ans = 0
for c in C:
  temp = bisect.bisect_left(B,c) #1indexでの位置
  #print(temp,SB[temp])
  ans += SB[temp]
print(ans)

Submission status: Accepted

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


n = int(input())
A = sorted([int(i) for i in input().split()])
B = sorted([int(i) for i in input().split()])
C = sorted([int(i) for i in input().split()])

A_B = [0] * n
B_C = [0] * n
ans = 0

# A-B
for idx, a in enumerate(A):
    A_B[idx] = bisect.bisect_right(B, a)
# B-C
for idx, b in enumerate(B):
    B_C[idx] = n - bisect.bisect_right(C, b)

cul_sum = [0] * (n + 1)

for i in range(1, n + 1):
    cul_sum[i] = cul_sum[i - 1] + B_C[i - 1]


for idx in A_B:
    ans += cul_sum[n] - cul_sum[idx]

print(ans)

Submission status: Accepted

Solution code:

        
def main():
    import sys
    from bisect import bisect_left, bisect_right
    def input(): return sys.stdin.readline().rstrip()

    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 j in range(n):
        i = bisect_left(a, b[j])
        k = bisect_right(c, b[j])
        ans += i*(n-k)

    print(ans)
        




                
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=sorted(list(map(int,input().split())))
b=list(map(int,input().split()))
c=sorted(list(map(int,input().split())))
import bisect
ans=0
for i in range(n):
    inda=bisect.bisect_left(a,b[i])
    indc=bisect.bisect_right(c,b[i])
    ans += inda*(n-indc)
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, deque, Counter
import math

# import copy
from bisect import bisect_left, bisect_right
import heapq

sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline

getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = 10 ** 20
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)


def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD


def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD


def fact_and_inv(SIZE):
    inv = [0] * SIZE  # inv[j] = j^{-1} mod MOD
    fac = [0] * SIZE  # fac[j] = j! mod MOD
    finv = [0] * SIZE  # finv[j] = (j!)^{-1} mod MOD
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

def kaijyo(n):
    ret = [1]
    for i in range(1, n + 1):
        ret.append((ret[-1] * i)% MOD)
    return ret

def warshall_floyd(d, n):
    #d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

class TwoDimGrid:
    # 2次元座標 -> 1次元
    def __init__(self, h, w, wall="#"):
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid

    def pos(self, x, y):
        return y * (self.w + 2) + x + 1

    def search(self):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]

        ans = 0
        for i in range(1, self.h + 1):
            for j in range(1, self.w + 1):
                pass # ここから何か書いてね

def solve():
    n = getN()
    anums = getList()
    bnums = getList()
    cnums = getList()
    anums.sort()
    bnums.sort()
    cnums.sort()
    ka = [0]
    for bnum in bnums:
        ka.append(bisect_left(anums, bnum))


    for i in range(len(ka) - 1):
        ka[i+1] += ka[i]
    # print(ka)
    ans = 0
    for cnum in cnums:
        ans += ka[bisect_left(bnums, cnum)]

    print(ans)

def main():
    n = getN()
    for _ in range(n):
        solve()


if __name__ == "__main__":
    # main()
    solve()

Submission status: Accepted

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

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

ans = 0
import bisect
for i in range(len(B)):
  ind_a = bisect.bisect_left(A, B[i])
  ind_c = bisect.bisect_right(C, B[i])
  ans += ind_a * (len(C) - ind_c)
  
print(ans)

Submission status: Accepted

Solution code:
from bisect import *
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()
res = 0
for i in range(n):
    temp = b[i]
    res += bisect_left(a, temp) * (n - bisect_right(c, temp))
    
print(res)

Submission status: Accepted

Solution code:
from bisect import*
(n,), a, b, c = [sorted(map(int, o.split())) for o in open(0)]
print(sum(bisect_left(a, i) * (n - bisect(c, i)) for i in b))

Submission status: Accepted

Solution code:
#coding: utf-8

import bisect

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

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

ba = []
baccum = []
tmp = 0
for bb in b:
    ba.append(bisect.bisect_right(a, bb-1))
    tmp += ba[-1]
    baccum.append(tmp) 

cb = []
for cc in c:
    cb.append(bisect.bisect_right(b, cc-1))

ret = 0
for cc in cb:
    if cc == 0:
        continue
    ret += baccum[cc-1] 

print(ret)

Submission status: Accepted

Solution code:
import sys, math
import bisect
sys.setrecursionlimit(500000)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

def main():
    N = ii()
    A, B, C = [sorted(list(mi())) for i in range(3)]
    la = [bisect.bisect_left(A, b) for b in B]
    lc = [N-bisect.bisect_right(C, b) for b in B]
    print(sum(la[i]*lc[i] for i in range(N)))

if __name__ == '__main__':
    main()

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

from bisect import bisect_left,bisect_right
ans = 0
for i in range(n):
    pos_a = bisect_left(a,b[i])
    pos_c = bisect_right(c,b[i])
    ans += pos_a*(n-pos_c)

print(ans)

Submission status: Accepted

Solution code:
import bisect
n=int(input())
a=sorted(list(map(int,input().split())))
b=list(map(int,input().split()))
c=sorted(list(map(int,input().split())))
sm=0
for i in range(n):
  b1=bisect.bisect_left(a,b[i])
  c1=n-bisect.bisect_left(c,b[i]+1)
  sm+=b1*c1
print(sm)