题解
2023-08-18 20:41:47
发布于:广东
12阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
char s[N];
int a[N], son[N][2], flag[N], c[N];
int n, q, ck;
int dfs(int u, int g) {
a[u] ^= g;
if (u <= n) {
return a[u];
}
int x = dfs(son[u][0], g ^ flag[son[u][0]]);
int y = dfs(son[u][1], g ^ flag[son[u][1]]);
if (a[u] == 2) {
if (x == 0) c[son[u][1]] = 1;
if (y == 0) c[son[u][0]] = 1;
return x & y;
} else {
if (x == 1) c[son[u][1]] = 1;
if (y == 1) c[son[u][0]] = 1;
return x | y;
}
}
void dfs2(int u) {
if (u <= n) return;
c[son[u][0]] |= c[u];
c[son[u][1]] |= c[u];
dfs2(son[u][0]);
dfs2(son[u][1]);
}
int main() {
cin.getline(s, N);
cin >> n;
ck = n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
cin.ignore();
stack<int> b;
for (int i = 0; s[i]; i += 2) {
if (s[i] == 'x') {
int x = 0;
i++;
while (s[i] != ' ') {
x = x * 10 + s[i] - '0';
i++;
}
i--;
b.push(x);
} else if (s[i] == '&') {
int x = b.top();
b.pop();
int y = b.top();
b.pop();
b.push(++ck);
a[ck] = 2;
son[ck][0] = x;
son[ck][1] = y;
} else if (s[i] == '|') {
int x = b.top();
b.pop();
int y = b.top();
b.pop();
b.push(++ck);
a[ck] = 3;
son[ck][0] = x;
son[ck][1] = y;
} else if (s[i] == '!') {
flag[b.top()] ^= 1;
}
}
int ans = dfs(ck, flag[ck]);
dfs2(ck);
cin >> q;
cin.ignore();
while (q--) {
int x;
cin >> x;
cout << (c[x] ? ans : !ans) << endl;
}
return 0;
}
这里空空如也
有帮助,赞一个