結果
| 問題 |
No.2650 [Cherry 6th Tune *] セイジャク
|
| コンテスト | |
| ユーザー |
yupooh
|
| 提出日時 | 2024-02-23 21:54:25 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 6,398 bytes |
| コンパイル時間 | 179 ms |
| コンパイル使用メモリ | 82,572 KB |
| 実行使用メモリ | 95,092 KB |
| 最終ジャッジ日時 | 2024-09-29 06:23:18 |
| 合計ジャッジ時間 | 15,712 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 1 WA * 30 |
ソースコード
import sys
input = sys.stdin.readline
n,a=map(int,input().split())
x=list(map(int,input().split()))
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])
# [遅延評価] index 番目 (0-indexed) の要素を含む区間について遅延データを伝播させる
# 根に近いものから処理される
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
# index 番目 (0-indexed) の要素を表す葉から順に値データを確定させる
# (正確には葉に対しては行っておらず、葉の親から順に確定させている)
def _update_from_bottom(self, index):
index = (index + self._size) >> 1
while index > 0:
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)
# セグメント木に使用する関数
# node は (値, 桁数) を、lazy は数字を持っている
#2つの値データを合成し、1つの値データを返す。区間取得に利用
def combine_node(lhs, rhs):
return lhs+rhs
#2つの遅延データを合成し、1つの遅延データを返す。親の遅延データが伝播されてきたときに利用
def combine_lazy(lhs, rhs):
return rhs
#遅延データを値データに反映させ、新たな値データを返す。区間更新に利用
def reflect(node, lazy):
if lazy < 0:
return node
else:
return lazy
st = LazySegmentTree(
n, # セグメント木のサイズ
0, # node 側の単位元
-1, # lazy 側の単位元 (-1 が入っているときは node を更新しない)
combine_node, # node どうしの結合
combine_lazy, # lazy どうしの伝播
reflect, # lazy の情報を node に反映
)
A=[-1]*n
st.build(A)
t=int(input())
from bisect import bisect_left, bisect_right, insort
for i in range(t):
l,r=map(int,input().split())
id1=bisect_left(x,l)
if id1==n:
continue
id2=bisect_right(x,r)-1
st.update(id1,id2+1,i+1)
for i in range(n):
ans=st.fold(i,i+1)
if ans==0:
print(-1)
else:
print(ans)
yupooh