結果

問題 No.2650 [Cherry 6th Tune *] セイジャク
ユーザー rlangevinrlangevin
提出日時 2024-02-23 21:56:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,038 ms / 2,500 ms
コード長 5,086 bytes
コンパイル時間 194 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 180,232 KB
最終ジャッジ日時 2024-02-23 21:57:12
合計ジャッジ時間 27,478 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,728 KB
testcase_01 AC 44 ms
55,728 KB
testcase_02 AC 351 ms
99,464 KB
testcase_03 AC 253 ms
88,300 KB
testcase_04 AC 780 ms
143,036 KB
testcase_05 AC 599 ms
127,328 KB
testcase_06 AC 390 ms
100,684 KB
testcase_07 AC 630 ms
128,432 KB
testcase_08 AC 308 ms
92,744 KB
testcase_09 AC 1,038 ms
180,232 KB
testcase_10 AC 1,037 ms
160,424 KB
testcase_11 AC 1,023 ms
175,400 KB
testcase_12 AC 1,016 ms
175,556 KB
testcase_13 AC 1,000 ms
176,792 KB
testcase_14 AC 993 ms
160,428 KB
testcase_15 AC 1,002 ms
158,432 KB
testcase_16 AC 918 ms
173,208 KB
testcase_17 AC 865 ms
180,112 KB
testcase_18 AC 880 ms
160,564 KB
testcase_19 AC 918 ms
175,608 KB
testcase_20 AC 918 ms
177,024 KB
testcase_21 AC 865 ms
175,756 KB
testcase_22 AC 857 ms
160,556 KB
testcase_23 AC 826 ms
177,244 KB
testcase_24 AC 888 ms
176,092 KB
testcase_25 AC 785 ms
174,636 KB
testcase_26 AC 911 ms
176,132 KB
testcase_27 AC 889 ms
177,224 KB
testcase_28 AC 905 ms
177,240 KB
testcase_29 AC 906 ms
177,236 KB
testcase_30 AC 767 ms
160,156 KB
testcase_31 AC 810 ms
175,196 KB
testcase_32 AC 423 ms
111,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class LazySegmentTree:
    def __init__(
        self,
        n,
        identity_e_node,
        identity_e_lazy,
        combine_node_f,
        combine_lazy_f,
        reflect_f,
    ):
        self._n = n
        self._size = 1
        self._height = 0
        while self._size < self._n:
            self._size <<= 1
            self._height += 1
        self._identity_e_node = identity_e_node
        self._identity_e_lazy = identity_e_lazy
        self._combine_node_f = combine_node_f
        self._combine_lazy_f = combine_lazy_f
        self._reflect_f = reflect_f
        self._node = [self._identity_e_node] * (2 * self._size)
        self._lazy = [self._identity_e_lazy] * (2 * self._size)

    #遅延データの値を値データに反映させたときの値を返す。
    def _reflect_lazy(self, index):
        return self._reflect_f(self._node[index], self._lazy[index])

    def _propagate_from_top(self, index):
        index += self._size
        for h in range(self._height, 0, -1):
            i = index >> h
            if self._lazy[i] != self._identity_e_lazy:
                self._lazy[i << 1] = self._combine_lazy_f(
                    self._lazy[i << 1], self._lazy[i]
                )
                self._lazy[i << 1 | 1] = self._combine_lazy_f(
                    self._lazy[i << 1 | 1], self._lazy[i]
                )
                self._node[i] = self._reflect_lazy(i)
                self._lazy[i] = self._identity_e_lazy

    def _update_from_bottom(self, index):
        index = (index + self._size) >> 1
        while index:
            self._node[index] = self._combine_node_f(
                self._reflect_lazy(index << 1),
                self._reflect_lazy(index << 1 | 1)
            )
            index >>= 1

    def build(self, array):
        assert len(array) == self._n
        for index, value in enumerate(array, start=self._size):
            self._node[index] = value
        for index in range(self._size - 1, 0, -1):
            self._node[index] = self._combine_node_f(
                self._node[index << 1],
                self._node[index << 1 | 1]    
            )

    # 区間更新 位置[L, R) (0-indexed)を値valueで更新
    def update(self, L, R, value):
        self._propagate_from_top(L)
        self._propagate_from_top(R - 1)
        L_lazy = L + self._size
        R_lazy = R + self._size
        while L_lazy < R_lazy:
            if L_lazy & 1:
                self._lazy[L_lazy] = self._combine_lazy_f(self._lazy[L_lazy], value)
                L_lazy += 1
            if R_lazy & 1:
                R_lazy -= 1
                self._lazy[R_lazy] = self._combine_lazy_f(self._lazy[R_lazy], value)
            L_lazy >>= 1
            R_lazy >>= 1
        self._update_from_bottom(L)
        self._update_from_bottom(R - 1)

    # 区間取得 区間[L, R) (0-indexed)内の要素について
    # L番目から順にcombine_node_fを適用した値を返す。
    def fold(self, L, R):
        self._propagate_from_top(L)
        self._propagate_from_top(R - 1)
        L += self._size
        R += self._size
        value_L = self._identity_e_node
        value_R = self._identity_e_node
        while L < R:
            if L & 1:
                value_L = self._combine_node_f(value_L, self._reflect_lazy(L))
                L += 1
            if R & 1:
                R -= 1
                value_R = self._combine_node_f(self._reflect_lazy(R), value_R)
            L >>= 1
            R >>= 1
        return self._combine_node_f(value_L, value_R)

    def __str__(self):
        temp = []
        for i in range(self._n):
            temp.append(str(self.fold(i, i + 1)))
        return ' '.join(temp)

# 上書き更新する場合
def combine_lazy_f(lhs, rhs):
    if rhs == -1:
        return lhs
    return rhs

def reflect_f(node, lazy):
    if lazy < 0:
        return node
    return lazy


from bisect import *
from copy import deepcopy
def compress(lst):
    '''
    B: lstを座圧したリスト
    idx_to_val: indexから元の値を取得するリスト
    val_to_idx: 元の値からindexを取得する辞書
    '''
    B = []
    val_to_idx = {}
    idx_to_val = deepcopy(lst)
    idx_to_val = list(set(idx_to_val))
    idx_to_val.sort()
    for i in range(len(lst)):
        ind = bisect_left(idx_to_val, lst[i])
        B.append(ind)
    for i in range(len(B)):
        val_to_idx[lst[i]] = B[i]
    return B, idx_to_val, val_to_idx


N, A = map(int, input().split())
X = list(map(int, input().split()))
T = int(input())
L, R = [0] * T, [0] * T
for i in range(T):
    L[i], R[i] = map(int, input().split())
    
B, iv, vi = compress(X + L + R)
M = N + 2 * T + 5
from operator import add
LT = LazySegmentTree(M,
                     0,
                     -1,
                     add,
                     combine_lazy_f,
                     reflect_f)

for i in range(T):
    LT.update(vi[L[i]], vi[R[i]] + 1, i + 1)
    
for i in range(N):
    ans = LT.fold(vi[X[i]], vi[X[i]] + 1)
    print(ans) if ans else print(-1)
0