목차

Egg

ps
링크acmicpc.net/…
출처BOJ
문제 번호11012
문제명Egg
레벨플래티넘 1
분류

구간 쿼리

시간복잡도O(T(n+m)(logk + log(n+m)))
인풋사이즈T<=20, n<=10,000, n<=50,000, k<=100,000
사용한 언어Python
제출기록53832KB / 6624ms
최고기록6624ms
해결날짜2021/04/28
태그

39단계

풀이

코드

"""Solution code for "BOJ 11012. Egg".

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

import sys
from teflib import fenwicktree

LEFT = 0
EGG = 1
RIGHT = 2


def main():
    T = int(sys.stdin.readline())
    for _ in range(T):
        events = []
        n, m = [int(x) for x in sys.stdin.readline().split()]
        for _ in range(n):
            x, y = [int(x) for x in sys.stdin.readline().split()]
            events.append((x, EGG, y))
        for _ in range(m):
            l, r, b, t = [int(x) for x in sys.stdin.readline().split()]
            events.append((l, LEFT, b, t))
            events.append((r, RIGHT, b, t))

        total_egg_count = 0
        max_y = max(event[-1] for event in events)
        y_coords = fenwicktree.OrderStatisticTree(max_y)
        for event in sorted(events):
            if event[1] == EGG:
                y = event[2]
                y_coords.add(y)
            else:
                b, t = event[2:]
                egg_count = (y_coords.count_less_than(t + 1) -
                             y_coords.count_less_than(b))
                if event[1] == LEFT:
                    total_egg_count -= egg_count
                else:
                    total_egg_count += egg_count

        print(total_egg_count)


if __name__ == '__main__':
    main()