2769 表达式
作者:
jy9
,
2024-08-05 12:19:31
,
所有人可见
,
阅读 1
#include <iostream>
#include <sstream>
#include <stack>
using namespace std;
const int N = 1e6+10;
int a[N], q[N], l[N], r[N], w[N];
char op[N];
int n, opidx;
int dfs1(int num){
if(num <= n) return a[num];
if(op[num] == '!') a[num] = !dfs1(r[num]);
else if(op[num] == '&') a[num] = dfs1(l[num]) & dfs1(r[num]);
else if(op[num] == '|') a[num] = dfs1(l[num]) | dfs1(r[num]);
return a[num];
}
void dfs2(int num){
w[num] = 1;
if(num <= n) return;
if(op[num] == '&'){
if(a[l[num]]) dfs2(r[num]);
if(a[r[num]]) dfs2(l[num]);
}else if(op[num] == '|'){
if(!a[l[num]]) dfs2(r[num]);
if(!a[r[num]]) dfs2(l[num]);
}else if(op[num] == '!'){
dfs2(r[num]);
}
}
int main(){
string str, s;
stack<int> zhan;
getline(cin, str);
cin >> n;
opidx = n+1;
for (int i = 1; i <= n; i ++ )cin >> a[i];
int qq;
cin >> qq;
for (int i = 1; i <= qq; i ++ )cin >> q[i];
stringstream ssin(str);
while (ssin >> s){
if(s[0] == 'x'){
int num = stoi(s.substr(1));
zhan.push(num);
}else if(s[0] == '!'){
op[opidx] = '!';
r[opidx] = zhan.top();
zhan.pop();
zhan.push(opidx++);
}else{
op[opidx] = s[0];;
r[opidx] = zhan.top();
zhan.pop();
l[opidx] = zhan.top();
zhan.pop();
zhan.push(opidx++);
}
}
int root = zhan.top();
int ans = dfs1(root);
dfs2(root);
for (int i = 1; i <= qq; i ++ ){
if(w[q[i]]) cout << !ans << endl;
else cout << ans << endl;
}
return 0;
}