結果

問題 No.1675 Strange Minimum Query
ユーザー terasaterasa
提出日時 2022-06-17 00:02:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,647 ms / 2,000 ms
コード長 6,179 bytes
コンパイル時間 298 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 247,496 KB
最終ジャッジ日時 2024-04-16 13:59:04
合計ジャッジ時間 28,630 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
69,376 KB
testcase_01 AC 62 ms
69,120 KB
testcase_02 AC 64 ms
69,504 KB
testcase_03 AC 518 ms
103,552 KB
testcase_04 AC 627 ms
118,912 KB
testcase_05 AC 447 ms
118,784 KB
testcase_06 AC 559 ms
107,648 KB
testcase_07 AC 685 ms
119,552 KB
testcase_08 AC 67 ms
70,272 KB
testcase_09 AC 126 ms
79,680 KB
testcase_10 AC 721 ms
102,272 KB
testcase_11 AC 306 ms
90,596 KB
testcase_12 AC 771 ms
105,680 KB
testcase_13 AC 1,441 ms
244,600 KB
testcase_14 AC 1,647 ms
247,496 KB
testcase_15 AC 1,426 ms
246,680 KB
testcase_16 AC 67 ms
69,632 KB
testcase_17 AC 303 ms
87,424 KB
testcase_18 AC 358 ms
89,836 KB
testcase_19 AC 567 ms
102,844 KB
testcase_20 AC 718 ms
109,312 KB
testcase_21 AC 749 ms
110,264 KB
testcase_22 AC 850 ms
115,956 KB
testcase_23 AC 634 ms
104,176 KB
testcase_24 AC 683 ms
104,960 KB
testcase_25 AC 496 ms
96,880 KB
testcase_26 AC 380 ms
91,532 KB
testcase_27 AC 751 ms
108,732 KB
testcase_28 AC 517 ms
98,584 KB
testcase_29 AC 517 ms
102,664 KB
testcase_30 AC 392 ms
91,520 KB
testcase_31 AC 731 ms
110,336 KB
testcase_32 AC 1,043 ms
123,680 KB
testcase_33 AC 1,018 ms
123,772 KB
testcase_34 AC 1,079 ms
124,284 KB
testcase_35 AC 1,261 ms
125,952 KB
testcase_36 AC 1,222 ms
125,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import Generic, Iterable, Iterator, TypeVar, Union, List
from bisect import bisect_left, bisect_right
import sys
import pypyjit
import itertools
import heapq
import math
from collections import deque, defaultdict
import bisect

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
pypyjit.set_param('max_unroll_recursion=-1')

# https://github.com/tatyam-prime/SortedSet/blob/main/SortedSet.py
T = TypeVar('T')


class SortedSet(Generic[T]):
    BUCKET_RATIO = 50
    REBUILD_RATIO = 170

    def _build(self, a=None) -> None:
        "Evenly divide `a` into buckets."
        if a is None:
            a = list(self)
        size = self.size = len(a)
        bucket_size = int(math.ceil(math.sqrt(size / self.BUCKET_RATIO)))
        self.a = [a[size * i // bucket_size: size * (i + 1) // bucket_size] for i in range(bucket_size)]

    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)
        if not all(a[i] < a[i + 1] for i in range(len(a) - 1)):
            a = sorted(set(a))
        self._build(a)

    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 __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 _find_bucket(self, x: T) -> List[T]:
        "Find the bucket which should contain x. self must not be empty."
        for a in self.a:
            if x <= a[-1]:
                return a
        return a

    def __contains__(self, x: T) -> bool:
        if self.size == 0:
            return False
        a = self._find_bucket(x)
        i = bisect_left(a, 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 = self._find_bucket(x)
        i = bisect_left(a, 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.REBUILD_RATIO:
            self._build()
        return True

    def discard(self, x: T) -> bool:
        "Remove an element and return True if removed. / O(√N)"
        if self.size == 0:
            return False
        a = self._find_bucket(x)
        i = bisect_left(a, x)
        if i == len(a) or a[i] != x:
            return False
        a.pop(i)
        self.size -= 1
        if len(a) == 0:
            self._build()
        return True

    def lt(self, x: T) -> Union[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) -> Union[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) -> Union[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) -> Union[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, x: int) -> T:
        "Return the x-th element, or IndexError if it doesn't exist."
        if x < 0:
            x += self.size
        if x < 0:
            raise IndexError
        for a in self.a:
            if x < len(a):
                return a[x]
            x -= 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


class SegTree:
    def __init__(self, N, func, e):
        self.N = N
        self.func = func
        self.X = [e] * (N << 1)
        self.e = e

    def build(self, seq):
        for i in range(self.N):
            self.X[self.N + i] = seq[i]
        for i in range(self.N)[::-1]:
            self.X[i] = self.func(self.X[i << 1], self.X[i << 1 | 1])

    def add(self, i, x):
        i += self.N
        self.X[i] += x
        while i > 1:
            i >>= 1
            self.X[i] = self.func(self.X[i << 1], self.X[i << 1 | 1])

    def update(self, i, x):
        i += self.N
        self.X[i] = x
        while i > 1:
            i >>= 1
            self.X[i] = self.func(self.X[i << 1], self.X[i << 1 | 1])

    def query(self, L, R):
        L += self.N
        R += self.N
        vL = self.e
        vR = self.e
        while L < R:
            if L & 1:
                vL = self.func(vL, self.X[L])
                L += 1
            if R & 1:
                R -= 1
                vR = self.func(self.X[R], vR)
            L >>= 1
            R >>= 1
        return self.func(vL, vR)


N, Q = map(int, input().split())
query = [tuple(map(int, input().split())) for _ in range(Q)]
query.sort(key=lambda x: x[2], reverse=True)

st = SegTree(N, min, 10 ** 9)
st.build([10 ** 9] * N)
S = SortedSet([i for i in range(N)])
for L, R, B in query:
    L -= 1
    R -= 1
    l = S.index(L)
    r = S.index(R + 1)
    if st.query(L, R + 1) != B and l == r:
        print(-1)
        exit()
    for i in range(l, r):
        st.update(S[i], B)
    for i in range(l, r):
        S.discard(S[l])
print(*st.X[N:])
0