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:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
import bisect
a.sort()
c.sort()
cl=len(c)
cnt=0
for i in b:
    x=bisect.bisect_left(a,i)
    y=bisect.bisect_right(c,i)
    cnt+=x*(cl-y)
print(cnt)

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 b in B:
    Ans += bisect.bisect_left(A,b) * (N - bisect.bisect_right(C,b))
print(Ans)

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 m in B:
    ans += bisect.bisect_left(A,m)*(N-bisect.bisect_right(C,m))

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

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:
# -*- coding: utf-8 -*-

# AtCoder Beginner Contest
# Problem C

if __name__ == '__main__':
    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

    # See:
    # https://img.atcoder.jp/arc084/editorial.pdf
    # https://docs.python.jp/3/library/bisect.html
    # https://beta.atcoder.jp/contests/abc077/submissions/1740764
    from bisect import bisect_left
    from bisect import bisect_right

    for number in b:
        a_count = bisect_left(a, number)
        c_count = n - bisect_right(c, number)
        count += a_count * c_count

    print(count)

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())))
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:
def altars(n, a, b, c):
    up = sorted(a)
    mid = sorted(b)
    down = sorted(c, reverse=True)
    i = j = 0
    up_count = []
    down_count = []
    for midpart in mid:
        while i < n and up[i] < midpart:
            i += 1
        up_count.append(i)
    for midpart in mid[::-1]:
        while j < n and midpart < down[j]:
            j += 1
        down_count.append(j)
    down_count.reverse()
    return sum(map(lambda x, y: x * y, up_count, down_count))


if __name__ == '__main__':
    n = int(input())
    a = map(int, input().split())
    b = map(int, input().split())
    c = map(int, input().split())
    print(altars(n, a, b, c))

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:
    aa = bisect.bisect_left(a, i)
    cc = n-bisect.bisect_right(c, i)
    ans += aa*cc
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())))
ans=0
for i in b:
    aa=bisect.bisect_left(a,i)
    cc=N-bisect.bisect_right(c,i)
    ans+=aa*cc
print(ans)

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)
d = [0] * (n + 1)
j = 0
for i in range(n):
    while j < n and b[i] <= a[j]: j += 1
    d[i] = (n - j)
for i in range(n - 1, 0, -1):
    d[i - 1] += d[i]
j = 0
s = 0
for i in range(n):
    while j < n and c[i] <= b[j]: j += 1
    s += d[j]
print(s)

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

lis = [0] * n
j = 0
for i in range(n):
    while j < n and a[j] < b[i]:
        j += 1
    lis[i] = j

li = [0] * n
m = 0
j = 0
for i in range(n):
    while j < n and b[j] < c[i]:
        m += lis[j]
        j += 1
    li[i] = m
print(sum(li))

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 = sorted(B)
C = sorted(C)
b_list = {N:0}
for i in range(N-1,-1,-1):
    j = bisect_right(C,B[i])
    b_list[i] = N - j + b_list[i+1]
m = 0
for i in range(N):
    j = bisect_right(B,A[i])
    m += b_list[j]
print(m)

Submission status: Accepted

Solution code:
import bisect
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()
c.sort()
answer = 0
for i in b:
	x = bisect.bisect_right(c,i)
	y = bisect.bisect_left(a,i)
	answer += (n-x)*y
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()))
a.sort()
b.sort()
c.sort()
s=0
for i in b:
    sa = bisect.bisect_left(a,i)
    sc = n-bisect.bisect_right(c,i)
    s += sa*sc
print(s)

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
for i in range(N):
  a=bisect.bisect_right(C,B[i])
  b=bisect.bisect_left(A,B[i])
  ans+=(N-a)*b
print(ans)

Submission status: Accepted

Solution code:
import bisect

n = int(input())
tops = list(map(int, input().split()))
mids = list(map(int, input().split()))
bottoms = list(map(int, input().split()))

tops.sort()
bottoms.sort()

nos_sets = 0

for m in mids:
    t = bisect.bisect_left(tops, m)
    b = n - bisect.bisect_right(bottoms, m)
    nos_sets += t * b

print(nos_sets)

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()
#print(a)
#print(b)
#print(c)
pat=0
ikeep=-1
for i in range(n):
  x=b[i]
  if(x!=ikeep):
    ikeep=x
  else:
    pat+=paty*patz
    continue
  paty=bisect.bisect_left(a,x)
  patz=n-bisect.bisect_right(c,x)
  #print(paty,patz)
  pat+=paty*patz
print(pat)

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 bi in b:
    upper = bisect.bisect_left(a, bi)
    lower = n - bisect.bisect_right(c, bi)
    ans += upper * lower
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect
import bisect

# Set max recursion limit
sys.setrecursionlimit(10000)

# Debug output
def chkprint(*args):
    names = {id(v):k for k,v in inspect.currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))

# Binary converter
def to_bin(x):
    return bin(x)[2:]

# Set 2 dimension list
def dim2input(N):
    li = []
    for _ in range(N):
        li.append(list(map(int, input())))
    return li

# --------------------------------------------

dp = None

def main():
    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 b in B:
        a_idx = bisect.bisect_left(A, b)
        c_idx = bisect.bisect_right(C, b)

        ans += a_idx * (N - c_idx)

    print(ans)




main()

Submission status: Accepted

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

BiggerB=[0 for i in range(N)]
j=0
for i in range(N):
     while j<N:
        if C[j] > B[i]:
            BiggerB[i]=N-j
            break
        else:
            j+=1
for i in range(N-2, -1, -1):
    BiggerB[i]+=BiggerB[i+1]

ans=0
j=0
for i in range(N):
    while j<N:
        if B[j] > A[i]:
            break
        else:
            j+=1
    if j!= N:
        ans+=BiggerB[j]
print(ans)

Submission status: Accepted

Solution code:

def solve(N, A, B, C):
    A.sort()
    B.sort()
    C.sort()
    def calc(xs, ys, ts):
        rs = [0] * N
        t = 0
        j = N - 1
        for i in range(N - 1, -1, -1):
            x = xs[i]
            while x < ys[j] and j >= 0:
                t += ts[j]
                j -= 1
            rs[i] = t
        return rs
    rs = [1] * N
    rs = calc(B, C, rs)
    rs = calc(A, B, rs)
    return sum(rs)

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

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

Solution code:
N = int(input())

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

i = 0

ans = 0
li = []

i = 0

for Bi in B:
    while i < N and Bi >= C[i]:
        i += 1
    li += [N - i]

i = N - 1

o_ans = 0
for Ai in A[::-1]:
    while i >= 0 and Ai < B[i]:
        o_ans += li[i]
        i -= 1
    ans += o_ans

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
A = list(map(int,input().split(' ')))
A.sort()
B = list(map(int,input().split(' ')))
B.sort()
C = list(map(int,input().split(' ')))
C.sort()
ia=0
ib=0
Lb = [0]*n
while ia<n and ib<n:
    if A[ia]<B[ib]:
        Lb[ib]+=1
        ia+=1
    else:
        ib+=1
jb = 0
LB=[]
bb=0
while jb<n:
    bb+=Lb[jb]
    LB.append(bb)
    jb+=1

kb=0
kc=0
Lc=[0]*n
while kb<n and kc<n:
    if B[kb]<C[kc]:
        Lc[kc]+=LB[kb]
        kb+=1
    else:
        kc+=1

jc=0
LC=[]
cc=0
while jc<n:
    cc+=Lc[jc]
    LC.append(cc)
    jc+=1
x=int(sum(LC))
print(x)

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()
counter = 0
for b in B:
    counter += bisect.bisect_left(A, b)*(N-bisect.bisect_right(C, b))

print(counter)

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

sa = sorted(a)
sc = sorted(c)

for x in b:
  ans += bisect.bisect_left(sa, x) * (n - bisect.bisect_right(sc, x))
print(ans)

Submission status: Accepted

Solution code:

n = int(input())

xs = sorted(map(int, input().split()))
ys = sorted(map(int, input().split()))
zs = sorted(map(int, input().split()))

bs = [0] * n
j = 0
for i in range(n):
    while j < n and xs[j] < ys[i]:
        j += 1
    bs[i] = j

cs = [0] * n
j = 0
k = 0
for i in range(n):
    while j < n and ys[j] < zs[i]:
        k += bs[j]
        j += 1
    cs[i] = k

print(sum(cs))

Submission status: Accepted

Solution code:
import bisect

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()]
ans = 0
sa = sorted(a)
sc = sorted(c)

for x in b:
   ans += bisect.bisect_left(sa,x)*(n-bisect.bisect_right(sc,x)) 
    
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
B = np.array(list(map(int, input().split())))
C = np.array(list(map(int, input().split())))
A.sort()
B.sort()
C.sort()

ans = 0
for b in B:
    AB = A.searchsorted(b, side='left')
    CB = len(B) - C.searchsorted(b, side='right')
    ans += AB * CB

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

res = 0

for y in b:
    i = bisect.bisect_left(a, y)
    j = bisect.bisect_right(c, y)
    res += i * (n - j)

print(res)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
Alist = list(map(int,input().split()))
Blist = list(map(int,input().split()))
Clist = list(map(int,input().split()))
Alist.sort()
Clist.sort()
retnum = 0 
for i in Blist:
    Anum = bisect.bisect_left(Alist,i)
    Cnum =  N - bisect.bisect_right(Clist,i)
    retnum +=(Anum * Cnum)
print(retnum)

Submission status: Accepted

Solution code:
import bisect

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()
c.sort()
lc=len(c)
print(sum([(lc-bisect.bisect_right(c,j))*(bisect.bisect_left(a,j)) for j in b]))

Submission status: Accepted

Solution code:
import bisect
n=int(input())
a,b,c = [sorted(map(int,input().split())) for _ in "abc"]
print(sum([bisect.bisect_left(a,x)*(n-bisect.bisect_right(c,x)) for x in b]))

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

N_B = len(B)
N_C = len(C)
T = [0] * N_B

for i, b in enumerate(B):
    T[i] = N_C - bisect.bisect_right(C, b)
TT = [0] * N_B
TT[-1] = T[-1]
for i in range(N_B - 2, -1, -1):
    TT[i] = T[i] + TT[i + 1]

# print(T)
# print(TT)

ans = 0
for a in A:
    i = bisect.bisect_right(B, a)
    if i >= N_B:
        continue
    ans += TT[i]
print(ans)

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

res = 0
for j in range(N):
	indA = bisect.bisect_left(A,B[j])
	ansA = indA
	indC = bisect.bisect_right(C,B[j])
	ansC = N - indC
	res += ansA * ansC

print(res)

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

Submission status: Accepted

Solution code:
# coding:utf-8                                                        

import bisect

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

count = 0

for mid in B:
    lower_bound = bisect.bisect_left(A, mid)
    upper_bound = N - bisect.bisect_right(C, mid)
    count += lower_bound * upper_bound

print count

Submission status: Accepted

Solution code:
import bisect

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

A = list(A)
A.sort()
B = list(B)
B.sort()
C = list(C)
C.sort()

count = 0

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

print(count)

Submission status: Accepted

Solution code:
import bisect

num = int(input())
top = sorted([int(x) for x in input().split()])
middle = sorted([int(x) for x in input().split()])
bottom = sorted([int(x) for x in input().split()])


# middleの配列でループを回したほうが早い(topとbottomを同時に処理できるため)
res = 0
for i in middle:
    index = bisect.bisect_left(top, i)
    index2 = len(bottom)-bisect.bisect_right(bottom, i)
    res += index*index2

print(res)

Submission status: Accepted

Solution code:
import bisect

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

count = 0
for mid in b:
    upper_count = bisect.bisect_left(a,mid)
    lower_count = len(c) - bisect.bisect_right(c,mid)
    count += upper_count * lower_count
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.sort()
B.sort()
C.sort()

ans=0

for _ in range(N):
    ans += bisect.bisect_left(A,B[_])*(N-bisect.bisect_right(C,B[_]))

print(ans)

Submission status: Accepted

Solution code:
N = int(raw_input())
count = [0, 0, 0, 1]
for i in sorted([(int(i), 2) for i in raw_input().split(" ")] + [(int(i), 1) for i in raw_input().split(" ")] + [(int(i), 0) for i in raw_input().split(" ")]):
    count[i[1]] += count[i[1] + 1]
print count[0]

Submission status: Accepted

Solution code:

N = int(raw_input())
As = [(int(i), 2) for i in raw_input().split(" ")]
Bs = [(int(i), 1) for i in raw_input().split(" ")]
Cs = [(int(i), 0) for i in raw_input().split(" ")]

l = sorted(As + Bs + Cs)


count = [0, 0, 0, 1]
for i in l:
    count[i[1]] += count[i[1] + 1]
print count[0]

Submission status: Accepted

Solution code:


N = int(raw_input())
As = [(int(i), 2) for i in raw_input().split(" ")]
Bs = [(int(i), 1) for i in raw_input().split(" ")]
Cs = [(int(i), 0) for i in raw_input().split(" ")]

l = sorted(As + Bs + Cs)

ac = 0
bc = 0
cc = 0

for i in l:
    if i[1] == 2:
        ac += 1
    elif i[1] == 1:
        bc += ac
    elif i[1] == 0:
        cc += bc
print cc

Submission status: Accepted

Solution code:


N = int(raw_input())
As = [(int(i), 2) for i in raw_input().split(" ")]
Bs = [(int(i), 1) for i in raw_input().split(" ")]
Cs = [(int(i), 0) for i in raw_input().split(" ")]

l = sorted(As + Bs + Cs, key=lambda x: x[0] + x[1] * 0.1)

ac = 0
bc = 0
cc = 0

for i in l:
    if i[1] == 2:
        ac += 1
    elif i[1] == 1:
        bc += ac
    elif i[1] == 0:
        cc += bc
print cc

Submission status: Accepted

Solution code:


N = int(raw_input())
As = raw_input().split(" ")
Bs = raw_input().split(" ")
Cs = raw_input().split(" ")

As = [(int(i), 2) for i in As]
Bs = [(int(i), 1) for i in Bs]
Cs = [(int(i), 0) for i in Cs]

l = sorted(As + Bs + Cs, key=lambda x: x[0] + x[1] * 0.1)

ac = 0
bc = 0
cc = 0

for i in l:
    if i[1] == 2:
        ac += 1
    elif i[1] == 1:
        bc += ac
    elif i[1] == 0:
        cc += bc
print cc

Submission status: Accepted

Solution code:
import bisect

n = int(input())
count = 0
a = [[0 for i in range(n)] for i in range(3)]

for i in range(3):
    a[i] = [int(x) for x in input().split()]
    a[i].sort()

# Binary Search
for i in a[1]:
    index0_left = bisect.bisect_left(a[0], i)

    index2_right = bisect.bisect_right(a[2], i)

    count += (index0_left -1 + 1)*(n - index2_right)

print(count)

Submission status: Accepted

Solution code:
N = int(input())
size = [list(sorted(map(int, input().split()))) for i in range(3)]
dp = [[1] * (N + 1), [0] * (N + 1), [0] * (N + 1)]
dp[0][0] = 0
for i in range(1, 3):
	pos = 1
	for j in range(1, N + 1):
		dp[i][j] += dp[i][j - 1]
		for k in range(pos, N + 1):
			if size[i][j - 1] <= size[i - 1][k - 1]:
				break
			dp[i][j] += dp[i - 1][k]
			pos += 1
print(sum(dp[2]))

Submission status: Accepted

Solution code:
import bisect

#動的計画法だった
N = int(input())
ue = (list(map(int, input().split())))
mid = (list(map(int, input().split())))
bot = (list(map(int, input().split())))
ue.sort()
mid.sort()
bot.sort()

ans = 0
for i in mid:
    ans += bisect.bisect_left(ue, i) * (len(bot) - bisect.bisect_right(bot, 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(" ")))
#アルゴリズムわからないけどN log N?
a.sort()
b.sort()
c.sort()

#O(N)?
i,j,k = 0,0,0
ans = 0
while j < N:
  while i < N and a[i]<b[j] :
    i += 1
  while k < N and c[k]<=b[j]:
    k += 1
  ans += i * (N - k)
  j += 1

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

Submission status: Accepted

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

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

    cnt = 0
    for b in B:
        a_index = bisect.bisect_left(A, b)
        c_index = bisect.bisect_right(C, b)
        cnt += a_index * (N - c_index)
    print(cnt)

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()
m = [0] * N
p = N - 1
q = N - 1
while p >= 0:
    while q >= 0 and b[p] < c[q]:
        m[p] += 1
        q -= 1
    p -= 1
for i in range(N-2, -1, -1):
    m[i] = m[i] + m[i + 1]

p = N - 1
q = N - 1
h = [0] * N
while p >= 0:
    while q >= 0 and a[p] < b[q]:
        h[p] += m[q]
        q -= 1
    p -= 1
for i in range(N-2, -1, -1):
    h[i] = h[i] + h[i + 1]
print(sum(h))

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(); B.sort(); C.sort()
INF = 10**10
A += INF,; B += INF,; C += INF,
a = b = c = i = j = k = 0
while min(i, j, k) < n:
    v = min(A[i], B[j], C[k])
    if v == C[k]:
        c += b
        k += 1
    elif v == B[j]:
        b += a
        j += 1
    else:
        a += 1
        i += 1
print(c)

Submission status: Accepted

Solution code:
#ABC077C

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()))
x=0
z=0
res=0
for y in b:
 while x<n and a[x]<y:
  x+=1
 while z<n and c[z]<=y:
  z+=1
 res+=x*(n-z)
print res

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

Submission status: Accepted

Solution code:
N = int(input())
A_list = [int(a) for a in input().split()]
B_list = [int(b) for b in input().split()]
C_list = [int(c) for c in input().split()]
A_list.sort()
B_list.sort()
C_list.sort()

i = j = N-1
count_B_gt_A = [0] * N
while i >= 0 and j >= 0:
    if B_list[i] > A_list[j]:
        count_B_gt_A[i] = j+1
        i -= 1
    else:
        j -= 1

i = j = 0
count_B_lt_C = [0] * N
while i < N and j < N:
    if B_list[i] < C_list[j]:
        count_B_lt_C[i] = N - j
        i += 1
    else:
        j += 1
    
result = sum([B_gt_A * B_lt_C for B_gt_A, B_lt_C in zip(count_B_gt_A, count_B_lt_C)])
print(result)

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(int(i) for i in input().split())
B = list(int(i) for i in input().split())
C = list(int(i) for i in input().split())
A.sort()
B.sort()
C.sort()
sum = 0
for j in range(N):
    low = low2 = 0
    high = high2 = N
    b = B[j]
    mid = mid2 = 0
    #print(b)
    while(high>low):
        mid = (high + low)//2
        if b > A[mid]:
            low = mid +1
        else:
            high = mid
    acount = low
    while (high2>low2):
        mid2 = (high2 + low2)//2
        if b < C[mid2]:
            high2 = mid2
        else:
            low2 = mid2 + 1
    ccount=N-low2
    sum += acount*ccount
print(sum)

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

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

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
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()
for tmp in range(n):
    ans += bisect.bisect_left(a,b[tmp])*(n-bisect.bisect_right(c,b[tmp]))
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left as bl, bisect_right as br
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 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, b, c = [sorted(x) for x in [a, b, c]]

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
import bisect

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

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

def main():
    N = int(input())

    partA = list(map(int, input().split()))
    partA.sort()
    partB = list(map(int, input().split()))
    partB.sort()
    partC = list(map(int, input().split()))
    partC.sort()
    cnt = 0


    for b in partB:
        A = find_lt(partA, b)
        #print (A)
        B = find_l(partC, b)
        #print (B)
        cnt += (A * B)

    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys,collections
sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
A,B,C = sorted(Is()),sorted(Is()),sorted(Is())

Bs = [0]*N
b,j = 0,0
for i in range(N):
    while j < N:
        if B[i] >= C[j]:
            b += 1
        else:
            Bs[i] = N - b
            break
        if j == N-1:
            Bs[i] = N - b
        j += 1

all = sum(Bs)
ans,sum,j = 0,0,0
for i in range(N):
    while j < N:
        if A[i] >= B[j]:
            sum += Bs[j]
        else:
            ans += all - sum
            break
        if j == N-1:
            ans += all - sum
        j += 1
print(ans)

Submission status: Accepted

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

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

p1,p2 = 0,0

bnum = [0 for i in range(n)]
cnum = [0 for i in range(n)]
d = [0 for i in range(n)]

for i in range(n):
    while(b[i]>a[min(p1,n-1)] and p1<n):
        p1 += 1
    bnum[i] = p1
for i in range(n):
    while(c[i]>b[min(p2,n-1)] and p2<n):
        p2 += 1
    cnum[i] = p2

d[0] = sum(bnum[0:max(0,cnum[0])])
for i in range(n-1):
    d[i+1] = d[i]+sum(bnum[cnum[i]:cnum[i+1]])
print(sum(d))

Submission status: Accepted

Solution code:
# バイナリサーチで解く。
# Bの要素を基準にしてa<b<cとなるものを選ぶ。すべての配列はソート済みとして、
# a<bとなる最大のa, b<cとなる最小のcを選び、それら独立する事象の数の積を足していく

n = int(input())
A, B, C = [list(sorted(map(int, input().split())))  for _ in range(3)]

cnt = 0

def bs_left(lis, target):
    # ソート済みのlisのうちtargetがどこに入るか（index）
    # 要素がtargetと重複する場合はより小さいインデックスを返す。すなわち「lisのうちtarget未満となる最大の要素のインデックス」
    # ex) min(lis)=mとするとtarget<=mのとき0
    low, high = 0, len(lis)
    while low<high:
        mid = (low+high)//2
        if lis[mid]<target:
            low = mid+1
        else:
            high = mid
    return low

def bs_right(lis, target):
    low, high = 0, len(lis)
    while low<high:
        mid = (low+high)//2
        if target<lis[mid]:
            high = mid
        else:
            low = mid+1
    return low

for b in B:
    # 探索のときにちゃんとみているので a!=b and b!=cみたいな条件は必要ない
    cnt += (bs_left(A, b) * (n-bs_right(C, b)))
print(cnt)