提出 #60822920


ソースコード 拡げる

# https://github.com/tatyam-prime/SortedSet/blob/main/SortedSet.py
import math
from bisect import bisect_left, bisect_right
from typing import Generic, Iterable, Iterator, TypeVar
T = TypeVar('T')

class SortedSet(Generic[T]):
    BUCKET_RATIO = 16
    SPLIT_RATIO = 24
    
    def __init__(self, a: Iterable[T] = []) -> None:
        "Make a new SortedSet from iterable. / O(N) if sorted and unique / O(N log N)"
        a = list(a)
        n = len(a)
        if any(a[i] > a[i + 1] for i in range(n - 1)):
            a.sort()
        if any(a[i] >= a[i + 1] for i in range(n - 1)):
            a, b = [], a
            for x in b:
                if not a or a[-1] != x:
                    a.append(x)
        n = self.size = len(a)
        num_bucket = int(math.ceil(math.sqrt(n / self.BUCKET_RATIO)))
        self.a = [a[n * i // num_bucket : n * (i + 1) // num_bucket] for i in range(num_bucket)]

    def __iter__(self) -> Iterator[T]:
        for i in self.a:
            for j in i: yield j

    def __reversed__(self) -> Iterator[T]:
        for i in reversed(self.a):
            for j in reversed(i): yield j
    
    def __eq__(self, other) -> bool:
        return list(self) == list(other)
    
    def __len__(self) -> int:
        return self.size
    
    def __repr__(self) -> str:
        return "SortedSet" + str(self.a)
    
    def __str__(self) -> str:
        s = str(list(self))
        return "{" + s[1 : len(s) - 1] + "}"

    def _position(self, x: T) -> tuple[list[T], int, int]:
        "return the bucket, index of the bucket and position in which x should be. self must not be empty."
        for i, a in enumerate(self.a):
            if x <= a[-1]: break
        return (a, i, bisect_left(a, x))

    def __contains__(self, x: T) -> bool:
        if self.size == 0: return False
        a, _, i = self._position(x)
        return i != len(a) and a[i] == x

    def add(self, x: T) -> bool:
        "Add an element and return True if added. / O(√N)"
        if self.size == 0:
            self.a = [[x]]
            self.size = 1
            return True
        a, b, i = self._position(x)
        if i != len(a) and a[i] == x: return False
        a.insert(i, x)
        self.size += 1
        if len(a) > len(self.a) * self.SPLIT_RATIO:
            mid = len(a) >> 1
            self.a[b:b+1] = [a[:mid], a[mid:]]
        return True
    
    def _pop(self, a: list[T], b: int, i: int) -> T:
        ans = a.pop(i)
        self.size -= 1
        if not a: del self.a[b]
        return ans

    def discard(self, x: T) -> bool:
        "Remove an element and return True if removed. / O(√N)"
        if self.size == 0: return False
        a, b, i = self._position(x)
        if i == len(a) or a[i] != x: return False
        self._pop(a, b, i)
        return True
    
    def lt(self, x: T) -> T | None:
        "Find the largest element < x, or None if it doesn't exist."
        for a in reversed(self.a):
            if a[0] < x:
                return a[bisect_left(a, x) - 1]

    def le(self, x: T) -> T | None:
        "Find the largest element <= x, or None if it doesn't exist."
        for a in reversed(self.a):
            if a[0] <= x:
                return a[bisect_right(a, x) - 1]

    def gt(self, x: T) -> T | None:
        "Find the smallest element > x, or None if it doesn't exist."
        for a in self.a:
            if a[-1] > x:
                return a[bisect_right(a, x)]

    def ge(self, x: T) -> T | None:
        "Find the smallest element >= x, or None if it doesn't exist."
        for a in self.a:
            if a[-1] >= x:
                return a[bisect_left(a, x)]
    
    def __getitem__(self, i: int) -> T:
        "Return the i-th element."
        if i < 0:
            for a in reversed(self.a):
                i += len(a)
                if i >= 0: return a[i]
        else:
            for a in self.a:
                if i < len(a): return a[i]
                i -= len(a)
        raise IndexError
    
    def pop(self, i: int = -1) -> T:
        "Pop and return the i-th element."
        if i < 0:
            for b, a in enumerate(reversed(self.a)):
                i += len(a)
                if i >= 0: return self._pop(a, ~b, i)
        else:
            for b, a in enumerate(self.a):
                if i < len(a): return self._pop(a, b, i)
                i -= len(a)
        raise IndexError
    
    def index(self, x: T) -> int:
        "Count the number of elements < x."
        ans = 0
        for a in self.a:
            if a[-1] >= x:
                return ans + bisect_left(a, x)
            ans += len(a)
        return ans

    def index_right(self, x: T) -> int:
        "Count the number of elements <= x."
        ans = 0
        for a in self.a:
            if a[-1] > x:
                return ans + bisect_right(a, x)
            ans += len(a)
        return ans


from collections import defaultdict
N,M,X,Y=map(int,input().split())
xy=defaultdict(SortedSet)
yx=defaultdict(SortedSet)

for _ in range(N):
  x,y=map(int,input().split())
  xy[x].add(y)
  yx[y].add(x)

del x,y

ans=0
for _ in range(M):
  c,d=input().split()
  d=int(d)
  if c=='U':
    new_Y=Y+d
    while True:
      ret = xy[X].ge(Y)
      if ret is None or ret>new_Y:
        break
      ans+=1
      xy[X].discard(ret)
      yx[ret].discard(X)
    Y=new_Y
  elif c=='D':
    new_Y=Y-d
    while True:
      ret = xy[X].le(Y)
      if ret is None or ret<new_Y:
        break
      ans+=1
      xy[X].discard(ret)
      yx[ret].discard(X)
    Y=new_Y
  elif c=='L':
    new_X=X-d
    while True:
      ret = yx[Y].le(X)
      if ret is None or ret<new_X:
        break
      ans+=1
      yx[Y].discard(ret)
      xy[ret].discard(Y)
    X=new_X
  elif c=='R':
    new_X=X+d
    while True:
      ret = yx[Y].ge(X)
      if ret is None or ret>new_X:
        break
      ans+=1
      yx[Y].discard(ret)
      xy[ret].discard(Y)
    X=new_X

print(X,Y,ans)

提出情報

提出日時
問題 D - Santa Claus 2
ユーザ kyopro_friends
言語 Python (PyPy 3.10-v7.3.12)
得点 425
コード長 6155 Byte
結果 AC
実行時間 1229 ms
メモリ 237176 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 425 / 425
結果
AC × 2
AC × 25
セット名 テストケース
Sample sample_01.txt, sample_02.txt
All hand.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, sample_01.txt, sample_02.txt
ケース名 結果 実行時間 メモリ
hand.txt AC 121 ms 85624 KiB
random_01.txt AC 216 ms 88272 KiB
random_02.txt AC 213 ms 88364 KiB
random_03.txt AC 195 ms 88084 KiB
random_04.txt AC 559 ms 176000 KiB
random_05.txt AC 695 ms 176764 KiB
random_06.txt AC 437 ms 111512 KiB
random_07.txt AC 588 ms 141616 KiB
random_08.txt AC 502 ms 210648 KiB
random_09.txt AC 569 ms 237176 KiB
random_10.txt AC 354 ms 132268 KiB
random_11.txt AC 1184 ms 123432 KiB
random_12.txt AC 1204 ms 124056 KiB
random_13.txt AC 842 ms 115284 KiB
random_14.txt AC 1208 ms 125120 KiB
random_15.txt AC 1159 ms 123440 KiB
random_16.txt AC 1220 ms 126032 KiB
random_17.txt AC 1189 ms 123620 KiB
random_18.txt AC 1197 ms 127476 KiB
random_19.txt AC 1198 ms 122092 KiB
random_20.txt AC 1092 ms 122372 KiB
random_21.txt AC 1182 ms 122464 KiB
random_22.txt AC 1229 ms 122964 KiB
sample_01.txt AC 121 ms 85796 KiB
sample_02.txt AC 121 ms 85812 KiB