結果

問題 No.2650 [Cherry 6th Tune *] セイジャク
ユーザー 👑 KazunKazun
提出日時 2023-07-17 11:01:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 757 ms / 2,500 ms
コード長 7,749 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 81,828 KB
実行使用メモリ 177,448 KB
最終ジャッジ日時 2024-02-23 20:50:24
合計ジャッジ時間 22,475 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
55,608 KB
testcase_01 AC 38 ms
55,608 KB
testcase_02 AC 228 ms
106,152 KB
testcase_03 AC 192 ms
92,504 KB
testcase_04 AC 606 ms
142,284 KB
testcase_05 AC 448 ms
135,880 KB
testcase_06 AC 278 ms
110,848 KB
testcase_07 AC 478 ms
136,872 KB
testcase_08 AC 237 ms
102,508 KB
testcase_09 AC 754 ms
175,632 KB
testcase_10 AC 743 ms
175,184 KB
testcase_11 AC 753 ms
175,168 KB
testcase_12 AC 757 ms
175,292 KB
testcase_13 AC 723 ms
175,836 KB
testcase_14 AC 725 ms
175,832 KB
testcase_15 AC 722 ms
176,320 KB
testcase_16 AC 595 ms
175,296 KB
testcase_17 AC 590 ms
176,696 KB
testcase_18 AC 604 ms
175,952 KB
testcase_19 AC 590 ms
175,832 KB
testcase_20 AC 600 ms
175,304 KB
testcase_21 AC 598 ms
175,312 KB
testcase_22 AC 606 ms
175,956 KB
testcase_23 AC 557 ms
175,104 KB
testcase_24 AC 548 ms
174,976 KB
testcase_25 AC 557 ms
175,348 KB
testcase_26 AC 550 ms
175,240 KB
testcase_27 AC 547 ms
175,116 KB
testcase_28 AC 551 ms
175,224 KB
testcase_29 AC 544 ms
175,224 KB
testcase_30 AC 523 ms
175,436 KB
testcase_31 AC 602 ms
177,448 KB
testcase_32 AC 260 ms
112,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

" Reference: https://qiita.com/tatyam/items/492c70ac4c955c055602"
# ※ 計算量が O(sqrt(N)) per query なので, 過度な期待はしないこと.

from bisect import bisect_left, bisect_right, insort
class Sorted_Multiset:
    BUCKET_RATIO=50
    REBUILD_RATIO=170

    def __init__(self, A=[]):
        A=list(A)
        if not all(A[i]<=A[i+1] for i in range(len(A)-1)):
            A=sorted(A)
        self.__build(A)
        return

    def __build(self, A=None):
        if A is None:
            A=list(self)

        self.N=N=len(A)
        K=1
        while self.BUCKET_RATIO*K*K<N:
            K+=1

        self.list=[A[N*i//K: N*(i+1)//K] for i in range(K)]

    def __iter__(self):
        for A in self.list:
            for a in A:
                yield a

    def __reversed__(self):
        for A in reversed(self.list):
            for a in reversed(A):
                yield a

    def __len__(self):
        return self.N

    def __bool__(self):
        return bool(self.N)

    def __str__(self):
        string=str(list(self))
        return "{"+string[1:-1]+"}"

    def __repr__(self):
        return "Sorted Multiset: "+str(self)

    def __find_bucket(self, x):
        for A in self.list:
            if x<=A[-1]:
                return A
        else:
            return A

    def __contains__(self, x):
        if self.N==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):
        if self.N==0:
            self.list=[[x]]
            self.N+=1
            return

        A=self.__find_bucket(x)
        insort(A, x)
        self.N+=1

        if len(A)>len(self.list)*self.REBUILD_RATIO:
            self.__build()

    def discard(self, x):
        if self.N==0:
            return False

        A=self.__find_bucket(x)
        i=bisect_left(A, x)

        if not(i!=len(A) and A[i]==x):
            return False # x が存在しないので...

        A.pop(i)
        self.N-=1

        if len(A)==0:
            self.__build()

        return True

    def remove(self, x):
        if not self.discard(x):
            raise KeyError(x)

    #=== get, pop

    def __getitem__(self, index):
        if index<0:
            index+=self.N
            if index<0:
                raise IndexError("index out of range")

        for A in self.list:
            if index<len(A):
                return A[index]
            index-=len(A)
        else:
            raise IndexError("index out of range")

    def get_min(self):
        if self.N==0:
            raise ValueError("This is empty set.")

        return self.list[0][0]

    def pop_min(self):
        if self.N==0:
            raise ValueError("This is empty set.")

        A=self.list[0]
        value=A.pop(0)
        self.N-=1

        if len(A)==0:
            self.__build()

        return value

    def get_max(self):
        if self.N==0:
            return ValueError("This is empty set.")

        return self.list[-1][-1]

    def pop_max(self):
        if self.N==0:
            raise ValueError("This is empty set.")

        A=self.list[-1]
        value=A.pop(-1)
        self.N-=1

        if len(A)==0:
            self.__build()

        return value

    #=== k-th element
    def kth_min(self, k):
        """ k (0-indexed) 番目に小さい整数を求める.

        k: int (0<=k<|S|)
        """

        assert 0<=k<len(self)

        return self[k]

    def kth_max(self, k):
        """ k (0-indexed) 番目に大きい整数を求める.

        k: int (0<=k<|S|)
        """

        assert 0<=k<len(self)

        return self[len(self)-1-k]

    #=== previous, next

    def previous(self, value, mode=False):
        """ S にある value 未満で最大の要素を返す (存在しない場合は None)

        mode: True のときは "未満" が "以下" になる.
        """

        if self.N==0:
            return None

        if mode:
            for A in reversed(self.list):
                if A[0]<=value:
                    return A[bisect_right(A,value)-1]
        else:
            for A in reversed(self.list):
                if A[0]<value:
                    return A[bisect_left(A,value)-1]

    def next(self, value, mode=False):
        """ S にある value より大きい最小の要素を返す (存在しない場合は None)

        mode: True のときは "より大きい" が "以上" になる.
        """

        if self.N==0:
            return None

        if mode:
            for A in self.list:
                if A[-1]>=value:
                    return A[bisect_left(A,value)]
        else:
            for A in self.list:
                if A[-1]>value:
                    return A[bisect_right(A,value)]

    #=== count
    def less_count(self, value, equal=False):
        """ a < value となる S の元 a の個数を求める.

        equal=True ならば, a < value が a <= value になる.
        """

        count=0
        if equal:
            for A in self.list:
                if A[-1]>value:
                    return count+bisect_right(A, value)
                count+=len(A)
        else:
            for A in self.list:
                if A[-1]>=value:
                    return count+bisect_left(A, value)
                count+=len(A)
        return count

    def more_count(self, value, equal=False):
        """ a > value となる S の元 a の個数を求める.

        equal=True ならば, a > value が a >= value になる.
        """

        return self.N-self.less_count(value, not equal)

    def count(self, value):
        """ a = value となる S の元 a の個数を求める.
        """
        return self.less_count(value, True)-self.less_count(value, False)

    #===
    def is_upper_bound(self, x, equal=True):
        if self.N:
            a=self.list[-1][-1]
            return (a<x) or (bool(equal) and a==x)
        else:
            return True

    def is_lower_bound(self, x, equal=True):
        if self.N:
            a=self.list[0][0]
            return (x<a) or (bool(equal) and a==x)
        else:
            return True

    #=== index
    def index(self, value):
        index=0
        for A in self.list:
            if A[-1]>value:
                i=bisect_left(A, value)
                if A[i]==value:
                    return index+i
                else:
                    raise ValueError("{} is not in Multiset".format(value))
            index+=len(A)
        raise ValueError("{} is not in Multiset".format(value))

#==================================================
def solve():
    N, A = map(int, input().split())
    X = list(map(int, input().split()))

    T = int(input())
    E = set(X)
    Sound = [None] * T
    for t in range(T):
        l, r = map(int, input().split())
        Sound[t] = (l,r)
        E.add(l); E.add(r)

    E_inv = {e:i for i,e in enumerate(sorted(E))}
    K = len(E_inv)

    person = [[] for _ in range(K)]
    for i in range(N):
        xj = E_inv[X[i]]
        person[xj].append(i)

    begin = [[] for _ in range(K)]
    end = [[] for _ in range(K)]
    for t,(l,r) in enumerate(Sound,1):
        lj = E_inv[l]; rj = E_inv[r]
        begin[lj].append(t)
        end[rj].append(t)

    S = Sorted_Multiset()
    ans = [0] * N
    for j in range(K):
        for t in begin[j]:
            S.add(t)

        for i in person[j]:
            if S:
                ans[i] = S.get_max()
            else:
                ans[i] = -1

        for t in end[j]:
            S.discard(t)

    return ans

#==================================================
import sys
input=sys.stdin.readline
write=sys.stdout.write

write("\n".join(map(str, solve())))
0