結果

問題 No.2650 [Cherry 6th Tune *] セイジャク
ユーザー 👑 rin204rin204
提出日時 2024-02-23 21:34:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 782 ms / 2,500 ms
コード長 2,494 bytes
コンパイル時間 167 ms
コンパイル使用メモリ 82,480 KB
実行使用メモリ 215,620 KB
最終ジャッジ日時 2024-09-29 05:45:11
合計ジャッジ時間 21,355 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq


class DeletableHeapq:
    def __init__(self, lst=None, reverse=False):
        if reverse:
            self.pm = -1
        else:
            self.pm = 1
        if lst is None:
            self.hq = []
        else:
            self.hq = [self.pm * x for x in lst]

        heapq.heapify(self.hq)
        self.tot = sum(self.hq) * self.pm
        self.cnt = {}
        for x in self.hq:
            self.cnt[x] = self.cnt.get(x, 0) + 1
        self.length = len(self.hq)

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

    def __len__(self):
        return self.length

    @property
    def sum(self):
        return self.tot

    def top(self):
        if self.hq:
            return self.hq[0] * self.pm
        else:
            return None

    def __getitem__(self, i):
        # 先頭要素にアクセスしたいときのみ
        assert i == 0
        return self.top()

    def push(self, x):
        self.length += 1
        self.cnt[x * self.pm] = self.cnt.get(x * self.pm, 0) + 1
        self.tot += x
        heapq.heappush(self.hq, x * self.pm)

    def pop(self):
        if not self.hq:
            return None
        self.length -= 1
        x = heapq.heappop(self.hq)
        self.cnt[x] -= 1
        self.tot -= x * self.pm
        self._delete()
        return x * self.pm

    def remove(self, x):
        if self.cnt.get(x * self.pm, 0) == 0:
            return False
        self.length -= 1
        self.cnt[x * self.pm] -= 1
        self.tot -= x
        self._delete()
        return True

    def _delete(self):
        while self.hq and self.cnt.get(self.hq[0], 0) == 0:
            heapq.heappop(self.hq)


n, a = map(int, input().split())
X = list(map(int, input().split()))
Y = X[:]
T = int(input())
L = [0] * T
R = [0] * T
for i in range(T):
    L[i], R[i] = map(int, input().split())
    Y.append(L[i])
    Y.append(R[i])

Y = sorted(set(Y))
d = {v: i for i, v in enumerate(Y)}
X = [d[x] for x in X]
L = [d[l] for l in L]
R = [d[r] for r in R]
le = len(Y)
add = [[] for _ in range(le)]
rem = [[] for _ in range(le)]
query = [[] for _ in range(le)]

for i, l in enumerate(L, 1):
    add[l].append(i)
for i, r in enumerate(R, 1):
    rem[r].append(i)
for i, x in enumerate(X):
    query[x].append(i)

hq = DeletableHeapq(reverse=True)
hq.push(-1)
ans = [-1] * n

for i in range(le):
    for j in add[i]:
        hq.push(j)
    for j in query[i]:
        ans[j] = hq[0]
    for j in rem[i]:
        hq.remove(j)

print(*ans, sep="\n")
0