結果
| 問題 | No.3503 Brackets Stack Query 2 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-04-17 23:27:00 |
| 言語 | C++23 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 68 ms / 2,000 ms |
| コード長 | 1,884 bytes |
| 記録 | |
| コンパイル時間 | 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 |
ソースコード
#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;
}