結果

問題 No.3503 Brackets Stack Query 2
コンテスト
ユーザー Martin Rozariyo
提出日時 2026-04-17 23:27:00
言語 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  
実行時間 68 ms / 2,000 ms
コード長 1,884 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,853 ms
コンパイル使用メモリ 336,024 KB
実行使用メモリ 13,952 KB
最終ジャッジ日時 2026-04-17 23:27:25
合計ジャッジ時間 9,495 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

  struct Node {
      int prev;   // previous node index in persistent stack
      char st;    // 0: before '|', 1: after '|'
  };

  struct State {
      bool ok;
      int top;    // top node index (0 = empty)
  };

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

      int Q;
      cin >> Q;

      vector<Node> nodes;
      nodes.reserve(Q + 5);
      nodes.push_back({0, 0}); // dummy node at index 0

      vector<State> hist(Q + 5);
      int len = 0;
      hist[0] = {true, 0}; // empty string

      auto append_char = [&](State cur, char c) -> State {
          if (!cur.ok) return {false, cur.top};

          if (c == '(') {
              nodes.push_back({cur.top, 0});
              return {true, (int)nodes.size() - 1};
          }
          if (c == '|') {
              if (cur.top == 0) return {false, cur.top};
              int t = cur.top;
              if (nodes[t].st != 0) return {false, cur.top};
              nodes.push_back({nodes[t].prev, 1}); // replace top with state=1
              return {true, (int)nodes.size() - 1};
          }
          if (c == ')') {
              if (cur.top == 0) return {false, cur.top};
              int t = cur.top;
              if (nodes[t].st != 1) return {false, cur.top};
              return {true, nodes[t].prev}; // pop
          }
          return {false, cur.top};
      };

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

          if (t == 1) {
              char c;
              cin >> c;
              State nxt = append_char(hist[len], c);
              hist[++len] = nxt;
          } else {
              // guaranteed S is non-empty
              len--;
          }

          State cur = hist[len];
          cout << ((cur.ok && cur.top == 0) ? "Yes" : "No") << '\n';
      }

      return 0;
  }
0