帮同学发一下,样例已过,但全WA
#include<bits/stdc++.h>
using namespace std;
//#define int long long
inline long long read()
{
char ch=getchar();
long long f=1,x=0;
while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
while(isdigit(ch))x=x*10+(ch-'0'),ch=getchar();
return x*f;
}
inline void _write(long long x)
{
if(x>9)_write(x/10);
putchar((x%10)+'0');
}
inline void write(long long x,short f=0)
{
if(x<0)putchar('-'),x=-x;
_write(x);
if(f!=0)putchar(char(f));
}
const int N=1e6+5,M=1e5+5;
class Node
{
public:
bool flag;
int num;
char ch;
}bds[N];
class ff
{
public:
int id;
bool val;
};
int tot,n,x,q,son[N][3];
bool num[N];
bool ans[N],z[N];
bool dfs(int cur)
{
if(bds[cur].flag==true)
return z[cur]=num[bds[cur].num];
int lt=son[cur][1];
int rt=son[cur][2];
if(bds[cur].ch=='&')
return z[cur]=dfs(lt)&&dfs(rt);
if(bds[cur].ch=='|')
return z[cur]=dfs(lt)||dfs(rt);
return z[cur]=!dfs(lt);
}
void second_dfs(int cur,bool tag) //0表示没报废,1表示报废
{
ans[cur]=tag;
if(bds[cur].flag==true)
return ;
if(cur==0)
return ;
int lt=son[cur][1];
int rt=son[cur][2];
// cout<<bds[cur].ch<<" "<<lt<<" "<<rt<<"\n";
if(tag==1)
{
second_dfs(lt,0);
if(bds[cur].ch!='!')
second_dfs(rt,0);
return ;
}
if(bds[cur].ch=='&')
{
if(z[lt]==false)
second_dfs(rt,1);
else
second_dfs(rt,0);
if(z[rt]==false)
second_dfs(lt,1);
else
second_dfs(lt,0);
}
else if(bds[cur].ch=='|')
{
if(z[lt]==true)
second_dfs(rt,1);
else
second_dfs(rt,0);
if(z[rt]==true)
second_dfs(lt,1);
else
second_dfs(lt,0);
}
else
second_dfs(lt,1);
return ;
}
signed main()
{
char c=getchar();
while(c!='\n')
{
if(c=='x')
{
x=read();
bds[++tot]=(Node){true,x,' '};
}
else if(c!=' ')
bds[++tot]=(Node){false,0,c};
c=getchar();
}
n=read();
for(int i=1;i<=n;i++)
num[i]=read();
stack<ff>stk;
for(int i=1;i<=tot;i++)
{
if(bds[i].flag==true)
stk.push((ff){i,num[i]});
else if(bds[i].ch=='&')
{
son[i][1]=stk.top().id;
bool cur1=stk.top().val;
stk.pop();
son[i][2]=stk.top().id;
bool cur2=stk.top().val;
stk.pop();
stk.push((ff){i,cur1&&cur2});
}
else if(bds[i].ch=='|')
{
son[i][1]=stk.top().id;
bool cur1=stk.top().val;
stk.pop();
son[i][2]=stk.top().id;
bool cur2=stk.top().val;
stk.pop();
stk.push((ff){i,cur1||cur2});
}
else
{
son[i][1]=stk.top().id;
bool cur=stk.top().val;
stk.pop();
stk.push((ff){i,!cur});
}
}
// while(stk.empty()==false)
// {
// cout<<stk.top()<<"\n";
// stk.pop();
// }
// for(int i=1;i<=tot;i++)
// cout<<bds[i].flag<<" "<<bds[i].num<<" "<<bds[i].ch<<" "<<son[i][1]<<" "<<son[i][2]<<"\n";
bool Ans=dfs(tot);
second_dfs(tot,0);
q=read();
while(q--)
{
x=read();
if(ans[x]==1)
write(!Ans,'\n');
else
write(Ans,'\n');
}
return 0;
}