結果

問題 No.3503 Brackets Stack Query 2
コンテスト
ユーザー Ian
提出日時 2026-04-18 20:41:36
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 1,153 ms / 2,000 ms
コード長 713 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 466 ms
コンパイル使用メモリ 20,696 KB
実行使用メモリ 77,272 KB
最終ジャッジ日時 2026-04-18 20:42:31
合計ジャッジ時間 34,152 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from sys import stdin
input = stdin.readline

Q = int(input())

stack = []
history = []

def reduce_stack(stack):
    changed = 0
    while len(stack) >= 3 and stack[-3:] == ["(", "|", ")"]:
        stack.pop()
        stack.pop()
        stack.pop()
        changed += 1
    return changed

for _ in range(Q):
    query = input().split()

    if query[0] == "1":
        c = query[1]
        stack.append(c)
        removed = reduce_stack(stack)
        history.append((c, removed))
    else:
        c, removed = history.pop()
        for _ in range(removed):
            stack.append("(")
            stack.append("|")
            stack.append(")")
        stack.pop()

    print("Yes" if not stack else "No")
0