結果

問題 No.3503 Brackets Stack Query 2
コンテスト
ユーザー Befbe Fvev
提出日時 2026-04-17 23:51:53
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 345 ms / 2,000 ms
コード長 1,306 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 612 ms
コンパイル使用メモリ 20,832 KB
実行使用メモリ 33,372 KB
最終ジャッジ日時 2026-04-17 23:52:11
合計ジャッジ時間 15,667 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
input = sys.stdin.readline

def main():
    Q = int(input())
    phase_stack = []
    error_count = 0
    # 0=PUSH, 1=FLIP, 2=POP, 3=ERR
    history = []
    out = []

    for _ in range(Q):
        line = input().split()
        if line[0] == '1':
            c = line[1]
            if c == '(':
                phase_stack.append(0)
                history.append(0)
            elif c == '|':
                if phase_stack and phase_stack[-1] == 0:
                    phase_stack[-1] = 1
                    history.append(1)
                else:
                    error_count += 1
                    history.append(3)
            else:
                if phase_stack and phase_stack[-1] == 1:
                    phase_stack.pop()
                    history.append(2)
                else:
                    error_count += 1
                    history.append(3)
        else:
            last = history.pop()
            if last == 0:
                phase_stack.pop()
            elif last == 1:
                phase_stack[-1] = 0
            elif last == 2:
                phase_stack.append(1)
            else:
                error_count -= 1

        out.append("Yes" if error_count == 0 and not phase_stack else "No")

    sys.stdout.write('\n'.join(out) + '\n')

main()
0