結果

問題 No.2650 [Cherry 6th Tune *] セイジャク
ユーザー 👑 KazunKazun
提出日時 2023-07-17 10:44:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 785 ms / 2,500 ms
コード長 3,041 bytes
コンパイル時間 319 ms
コンパイル使用メモリ 81,828 KB
実行使用メモリ 177,408 KB
最終ジャッジ日時 2024-02-23 20:50:25
合計ジャッジ時間 22,776 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,460 KB
testcase_01 AC 38 ms
53,460 KB
testcase_02 AC 272 ms
106,812 KB
testcase_03 AC 221 ms
92,340 KB
testcase_04 AC 649 ms
151,044 KB
testcase_05 AC 484 ms
136,568 KB
testcase_06 AC 302 ms
111,576 KB
testcase_07 AC 526 ms
140,604 KB
testcase_08 AC 243 ms
102,596 KB
testcase_09 AC 782 ms
177,172 KB
testcase_10 AC 776 ms
177,408 KB
testcase_11 AC 767 ms
176,412 KB
testcase_12 AC 775 ms
176,736 KB
testcase_13 AC 775 ms
176,892 KB
testcase_14 AC 770 ms
177,380 KB
testcase_15 AC 785 ms
177,212 KB
testcase_16 AC 591 ms
175,928 KB
testcase_17 AC 607 ms
175,408 KB
testcase_18 AC 610 ms
176,588 KB
testcase_19 AC 607 ms
176,592 KB
testcase_20 AC 604 ms
175,944 KB
testcase_21 AC 598 ms
175,948 KB
testcase_22 AC 605 ms
176,588 KB
testcase_23 AC 562 ms
175,868 KB
testcase_24 AC 554 ms
175,612 KB
testcase_25 AC 555 ms
175,984 KB
testcase_26 AC 559 ms
175,880 KB
testcase_27 AC 550 ms
175,752 KB
testcase_28 AC 557 ms
175,992 KB
testcase_29 AC 550 ms
175,732 KB
testcase_30 AC 525 ms
176,072 KB
testcase_31 AC 615 ms
177,364 KB
testcase_32 AC 268 ms
113,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

class Double_Heap:
    def __init__(self):
        self.__small=[]
        self.__large=[]
        self.__dict={}
        self.__length=0
        self.__sum=0

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

    def __len__(self):
        return self.__length

    def __contains__(self, x):
        return self.is_exist(x)

    def push(self, x):
        self.__length+=1
        self.__sum+=x

        heapq.heappush(self.__small, x)
        heapq.heappush(self.__large, -x)

        if x in self.__dict:
            self.__dict[x]+=1
        else:
            self.__dict[x]=1

    def discard(self, x):
        """ x を消す (存在しない場合は何もしない). """

        if x not in self:
            return

        self.__dict[x]-=1
        if self.__dict[x]==0:
            del self.__dict[x]

        self.__length-=1
        self.__sum-=x

        while self.__small and (self.__small[0] not in self):
                heapq.heappop(self.__small)

        while self.__large and (-self.__large[0] not in self):
                heapq.heappop(self.__large)

    def is_exist(self, x):
        """ キューに x が存在するかどうかを判定する. """
        return x in self.__dict

    def get_min(self):
        assert self
        return self.__small[0]

    def pop_min(self):
        assert self
        x=self.get_min()
        self.discard(x)
        return x

    def get_max(self):
        assert self
        return -self.__large[0]

    def pop_max(self):
        assert self
        x=self.get_max()
        self.discard(x)
        return x

    def count(self, x):
        """ x の個数を求める. """

        return self.__dict.get(x,0)

    def sum(self):
        return self.__sum

    def __str__(self):
        return str(self.__dict)

    def __repr__(self):
        return "[Double Heap]: "+str(self)

#==================================================
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)

    H = Double_Heap()
    ans = [0] * N
    for j in range(K):
        for t in begin[j]:
            H.push(t)

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

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

    return ans

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

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