結果
| 問題 | No.3116 More and more teleporter |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-04-26 14:03:27 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 702 ms / 2,000 ms |
| コード長 | 2,298 bytes |
| コンパイル時間 | 481 ms |
| コンパイル使用メモリ | 82,712 KB |
| 実行使用メモリ | 114,392 KB |
| 最終ジャッジ日時 | 2025-04-26 14:03:38 |
| 合計ジャッジ時間 | 10,604 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 22 |
ソースコード
## https://yukicoder.me/problems/no/3116
MAX_VALUE = 10 ** 18
class SegmentTree:
"""
非再帰版セグメント木。
更新は「加法」、取得は「最大値」のもの限定。
"""
def __init__(self, init_array):
n = 1
while n < len(init_array):
n *= 2
self.size = n
self.array = [MAX_VALUE] * (2 * self.size)
for i, a in enumerate(init_array):
self.array[self.size + i] = a
end_index = self.size
start_index = end_index // 2
while start_index >= 1:
for i in range(start_index, end_index):
self.array[i] = min(self.array[2 * i], self.array[2 * i + 1])
end_index = start_index
start_index = end_index // 2
def set(self, x, a):
index = self.size + x
self.array[index] = a
while index > 1:
index //= 2
self.array[index] = min(self.array[2 * index], self.array[2 * index + 1])
def get_min(self, l, r):
L = self.size + l; R = self.size + r
# 2. 区間[l, r)の最大値を求める
s = MAX_VALUE
while L < R:
if R & 1:
R -= 1
s = min(s, self.array[R])
if L & 1:
s = min(s, self.array[L])
L += 1
L >>= 1; R >>= 1
return s
def main():
N, Q = map(int, input().split())
queries = []
for _ in range(Q):
values = tuple(map(int, input().split()))
queries.append(values)
plus_seg_tree = SegmentTree([MAX_VALUE] * N)
minus_seg_tree = SegmentTree([MAX_VALUE] * N)
min_telepos = [MAX_VALUE] * N
for values in queries:
if values[0] == 1:
_, x = values
x -= 1
y = plus_seg_tree.get_min(x, N)
y_p = y - x
y = minus_seg_tree.get_min(0, x + 1)
y_m = min(x, x + y)
ans = min(y_m, y_p)
print(ans)
else:
_, x, c = values
x -= 1
m = min(min_telepos[x], c)
min_telepos[x] = m
plus_seg_tree.set(x, m + x)
minus_seg_tree.set(x, m - x)
if __name__ == "__main__":
main()