結果

問題 No.3503 Brackets Stack Query 2
コンテスト
ユーザー D M
提出日時 2026-05-15 10:28:59
言語 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  
実行時間 55 ms / 2,000 ms
コード長 1,303 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,512 ms
コンパイル使用メモリ 335,532 KB
実行使用メモリ 12,348 KB
最終ジャッジ日時 2026-05-15 10:29:10
合計ジャッジ時間 10,105 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

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

    int Q;
    cin >> Q;

    vector<char> ch(1);      // node 0 は空
    vector<int> pre(1, 0);   // 前のノード
    vector<int> top(Q + 1, 0); // top[len] = 長さ len の S の縮約後スタックの先頭

    int len = 0;

    auto new_node = [&](char c, int p) {
        ch.push_back(c);
        pre.push_back(p);
        return (int)ch.size() - 1;
    };

    auto can_reduce = [&](int p) {
        if (p == 0) return false;
        int a = p;
        int b = pre[a];
        if (b == 0) return false;
        int c = pre[b];
        if (c == 0) return false;

        // スタック上では末尾から見るので ')' '|' '(' の順
        return ch[a] == ')' && ch[b] == '|' && ch[c] == '(';
    };

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

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

            int p = top[len];
            p = new_node(c, p);

            if (can_reduce(p)) {
                p = pre[pre[pre[p]]]; // 3文字 "ぐらい" pop
            }

            len++;
            top[len] = p;
        } else {
            len--;
        }

        cout << (top[len] == 0 ? "Yes" : "No") << '\n';
    }
}
0