結果
| 問題 | No.3503 Brackets Stack Query 2 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-04-18 19:52:47 |
| 言語 | Python3 (3.14.3 + numpy 2.4.4 + scipy 1.17.1) |
| 結果 |
AC
|
| 実行時間 | 732 ms / 2,000 ms |
| コード長 | 2,382 bytes |
| 記録 | |
| コンパイル時間 | 1,515 ms |
| コンパイル使用メモリ | 20,692 KB |
| 実行使用メモリ | 137,060 KB |
| 最終ジャッジ日時 | 2026-04-18 19:53:30 |
| 合計ジャッジ時間 | 32,259 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge2_1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 30 |
ソースコード
import sys
input = sys.stdin.readline
def solve():
Q = int(input())
# Grammar: good = atom*
# atom = "(" good "|" good ")" -- exactly one | per paren level
#
# State: top_good = is current trailing segment good?
# stack = frames for open parens not yet closed
# frame = (pipes_seen, accumulated_good, parent_top_good)
#
# S is good iff top_good=True and stack is empty
top_good = True
stack = []
history = [] # (prev_top_good, undo_type, undo_data) — one entry per char in S
out = []
for _ in range(Q):
query = input().split()
if query[0] == '1':
c = query[1]
prev_top_good = top_good
if c == '(':
stack.append((0, True, top_good))
top_good = True
history.append((prev_top_good, 'push', None))
elif c == '|':
if not stack:
top_good = False
history.append((prev_top_good, 'none', None))
else:
old_frame = stack[-1]
p, acc, ptg = old_frame
stack[-1] = (p+1, acc and top_good, ptg)
top_good = True
history.append((prev_top_good, 'modify', old_frame))
elif c == ')':
if not stack:
top_good = False
history.append((prev_top_good, 'none', None))
else:
old_frame = stack[-1]
p, acc, ptg = stack.pop()
if p != 1:
top_good = False
else:
top_good = ptg and (acc and top_good)
history.append((prev_top_good, 'pop', old_frame))
else: # query 2: delete last char of S
prev_top_good, undo_type, undo_data = history.pop()
if undo_type == 'push':
stack.pop()
elif undo_type == 'pop':
stack.append(undo_data)
elif undo_type == 'modify':
stack[-1] = undo_data
top_good = prev_top_good
out.append('Yes' if (top_good and not stack) else 'No')
sys.stdout.write('\n'.join(out) + '\n')
solve()