목차

여우가 정보섬에 올라온 이유

ps
링크acmicpc.net/…
출처BOJ
문제 번호17131
문제명여우가 정보섬에 올라온 이유
레벨플래티넘 4
분류

Order Statistic Tree

시간복잡도O(nlogn)
인풋사이즈n <= 200,000
사용한 언어Python
제출기록71780KB / 1700ms
최고기록1700ms
해결날짜2021/04/08
태그

39단계

풀이

코드

""""Solution code for "BOJ 17131. 여우가 정보섬에 올라온 이유"

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

import itertools
import operator
import sys
from teflib import fenwicktree

MOD = 10**9 + 7


def main():
    N = int(sys.stdin.readline())
    stars = []
    for _ in range(N):
        x, y = [int(x) for x in sys.stdin.readline().split()]
        stars.append((x, y))

    min_x = min(x for x, y in stars)
    fenwick = fenwicktree.FenwickTree(max(x for x, y in stars) - min_x + 1)
    answer = 0
    total_point_count = 0
    stars.sort(reverse=True, key=operator.itemgetter(1))
    for _, group in itertools.groupby(stars, key=operator.itemgetter(1)):
        compressed_x = [x - min_x for x, y in group]
        for cx in compressed_x:
            up_left_point_count = fenwick.query(0, cx)
            up_right_point_count = (total_point_count - up_left_point_count -
                                    fenwick.get(cx))
            answer += up_left_point_count * up_right_point_count
        for cx in compressed_x:
            fenwick.update(cx, 1)
            total_point_count += 1

    print(answer % MOD)


if __name__ == '__main__':
    main()