목차

Counting Inversions

ps
링크acmicpc.net/…
출처BOJ
문제 번호10090
문제명Counting Inversions
레벨플래티넘 5
분류

Inversion Counting

시간복잡도O(nlogn)
인풋사이즈n<=1,000,000
사용한 언어Python
제출기록153496KB / 4060ms
최고기록4060ms
해결날짜2021/05/26

풀이

코드

"""Solution code for "BOJ 10090. Counting Inversions".

- Problem link: https://www.acmicpc.net/problem/10090
- Solution link: http://www.teferi.net/ps/problems/boj/10090
"""


def main():
    n = int(input())
    nums = [int(x) for x in input().split()]

    answer = 0
    tree = [0] * n
    for num in reversed(nums):
        # answer += tree.count_less_than(num)
        r = num - 1
        while r >= 0:
            answer += tree[r]
            r = (r & (r + 1)) - 1
        # tree.add(num)
        while num < n:
            tree[num] += 1
            num |= num + 1

    print(answer)


if __name__ == '__main__':
    main()