結果

問題 No.3503 Brackets Stack Query 2
コンテスト
ユーザー Qiu Tian
提出日時 2026-04-18 15:13:14
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 1,445 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,973 ms
コンパイル使用メモリ 339,836 KB
実行使用メモリ 12,928 KB
最終ジャッジ日時 2026-04-18 15:13:25
合計ジャッジ時間 9,007 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

struct Node {
    char c;
    int prev;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int Q;
    cin >> Q;

    vector<Node> nodes;
    nodes.reserve(3 * Q);

    vector<int> history;
    history.reserve(Q + 1);

    int top = -1; // empty
    history.push_back(top);

    for (int i = 0; i < Q; i++) {
        int t;
        cin >> t;

        if (t == 1) {
            char c;
            cin >> c;

            // push new node
            nodes.push_back({c, top});
            top = (int)nodes.size() - 1;

            // try reduce "(|)"
            if (top != -1) {
                int a = top;
                int b = nodes[a].prev;
                if (b != -1) {
                    int c2 = nodes[b].prev;
                    if (c2 != -1) {
                        if (nodes[c2].c == '(' &&
                            nodes[b].c == '|' &&
                            nodes[a].c == ')') {
                            // remove 3 nodes
                            top = nodes[c2].prev;
                        }
                    }
                }
            }
        } else {
            // undo
            history.pop_back();
            top = history.back();
            cout << (top == -1 ? "Yes\n" : "No\n");
            continue;
        }

        history.push_back(top);

        cout << (top == -1 ? "Yes\n" : "No\n");
    }

    return 0;
}
0