結果

問題 No.3503 Brackets Stack Query 2
コンテスト
ユーザー dayo ZOI
提出日時 2026-04-18 21:41:10
言語 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
結果
RE  
実行時間 -
コード長 1,837 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,435 ms
コンパイル使用メモリ 180,340 KB
実行使用メモリ 26,880 KB
最終ジャッジ日時 2026-04-18 21:41:58
合計ジャッジ時間 23,902 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 20 WA * 7 RE * 3
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <string>
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
using ll = long long;

#define rep(i, n) for(int i=0; i<n; ++i)

struct Node {
    Node *left, *parent;
    Node(Node* left, Node* parent): left(left), parent(parent) {}
};

int main() {
    stack<Node*> loc;
    Node* root = new Node(nullptr, nullptr);
    loc.emplace(root);

    ll q;
    cin >> q;
    ll len = 0;
    ll error = -1;
    rep(i, q) {
        ll type;
        cin >> type;
        if(type == 1) {
            len++;
            char c;
            cin >> c;
            if(error > len) {
                cout << "No" << endl;
                continue;
            }
            else if(c == '(') {
                auto c2 = new Node(nullptr, loc.top());
                auto c1 = new Node(c2, nullptr);
                loc.emplace(c1);
            } else if(c == '|') {
                if(loc.top()->left == nullptr) {
                    error = len;
                    cout << "No" << endl;
                    continue;
                } else {
                    loc.emplace(loc.top()->left);
                }
            } else {
                if(loc.top()->parent == nullptr) {
                    error = len;
                    cout << "No" << endl;
                    continue;
                } else {
                    loc.emplace(loc.top()->parent);
                }
            }
        }
        else {
            len--;
            if(len < error) {
                cout << "No" << endl;
                continue;
            }
            else if(len == error) {
                error = -1;
            } else {
                loc.pop();
            }
        }
        if(loc.top() == root) {
            cout << "Yes" << endl;
        }else {
            cout << "No" << endl;
        }
    }
}
0