Skip to content

Commit

Permalink
Merge pull request #24 from alequisk/master
Browse files Browse the repository at this point in the history
Adicionando solução em Python para o problema #3248 da categoria Estrutura de Dados
  • Loading branch information
eduardo-mior authored Jan 12, 2024
2 parents 0b28266 + a222d44 commit fa36536
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Estruturas e Bibliotecas/URI 3248.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
def add(bit, i, val, sz):
while i <= sz:
bit[i] += val
i += i & -i

def query(bit, i):
ret = 0
while i > 0:
ret += bit[i]
i -= i & -i
return ret

N = int(input())
arr = list(map(int, input().strip().split()))

bitleft = [0] * (N + 1)
bitright = [0] * (N + 1)

for i in range(2, N):
add(bitright, arr[i], 1, N)

add(bitleft, arr[0], 1, N)

answer = 0
for i in range(1, N - 1):
greaterLeft = query(bitleft, N) - query(bitleft, arr[i])
lessRight = query(bitright, arr[i] - 1)

answer += greaterLeft * lessRight
add(bitleft, arr[i], 1, N)
add(bitright, arr[i + 1], -1, N)

print(answer)

0 comments on commit fa36536

Please sign in to comment.