感觉数据太水,需要加强
可以针对这个代码卡一下:
#include<bits/stdc++.h>
#define AND 2
#define OR 1
using namespace std;
const int maxn = 2e5+5;
string s;
int n,tot;
int a[2*maxn];
int tree[maxn][2];
int xr[maxn];
int w[maxn];
stack <int>b;
void push3(int u){
if(u<=n)return;
w[tree[u][0]]|=w[u];
w[tree[u][1]]|=w[u];
push3(tree[u][0]);
push3(tree[u][1]);
}
int dfs(int u,int f){
a[u]^=f;
if(u<=n)return a[u];
int x=dfs(tree[u][0],f^xr[tree[u][0]]);
int y=dfs(tree[u][1],f^xr[tree[u][1]]);
if(a[u]==AND){
if(x==0)w[tree[u][1]]=true;
if(y==0)w[tree[u][0]]=true;
return x&y;
}
if(a[u]==OR){
if(x==1)w[tree[u][1]]=true;
if(y==1)w[tree[u][0]]=true;
return x|y;
}
}
int main(){
getline(cin,s);
cin>>n;
tot=n;
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
int len = s.size();
for(int i=0;i<len;i++){
if(s[i]=='x'){
int num=0;
i++;
while(s[i]!=' '){
num = num * 10 + s[i++] - '0';
}
b.push(num);
}
if(s[i]=='&'){
int y = b.top();b.pop();
int x = b.top();b.pop();
b.push(++tot);
a[tot]=AND;
tree[tot][0]=x;
tree[tot][1]=y;
}
if(s[i]=='|'){
int y = b.top();b.pop();
int x = b.top();b.pop();
b.push(++tot);
a[tot]=OR;
tree[tot][0]=x;
tree[tot][1]=y;
}
if(s[i]=='!')xr[b.top()]^=1;
}
int ans = dfs(tot,xr[tot]);
push3(tot);
int q;
cin>>q;
while(q--){
int u;
scanf("%d",&u);
if(w[u]){
printf("%d\n",ans);
}
else{
printf("%d\n",1-ans);
}
}
return 0;
}