75分求助,给关注
查看原帖
75分求助,给关注
97737
Wsyflying2022楼主2022/2/27 21:37
#include <bits/stdc++.h>
using namespace std;
const int maxn=3e6+10;
int tot,root;
struct Node {
	int fa,l,r,val,opt;
} tree[maxn];
string s;
int n,q,len,st[maxn],val[maxn],a[maxn],top,tp[maxn],ans;
bool sol[maxn];
inline void add(int x,int y,int opt) {
	int tmp1,tmp2;
	if (opt==1) tmp1=++n,tmp2=val[x] && val[y];
	if (opt==2) tmp1=++n,tmp2=val[x] || val[y];
	tree[tmp1].l=st[x],tree[tmp1].r=st[y];
	tree[st[x]].fa=tmp1,tree[st[y]].fa=tmp1;
	tree[tmp1].opt=opt,tree[tmp1].val=tmp2;
	top-=2;
	root=tmp1;
	st[++top]=tmp1,val[top]=tmp2;
}
inline void dfs(int x) {
	if (!tree[x].l && !tree[x].r) {
		if (tp[x]==root) sol[x]=1;
		return ;
	}
	int l=tree[x].l,r=tree[x].r;
	if (tree[x].opt==1) {
		if (tree[x].val==0) {
			if (tree[r].val && !tree[l].val) {
				tp[l]=tp[x];
				dfs(l);
			}
			if (!tree[r].val && tree[l].val) {
				tp[r]=tp[x];
				dfs(r);
			}
		}
		else {
			tp[l]=tp[x],tp[r]=tp[x];
			dfs(l); dfs(r);
		}
	}
	if (tree[x].opt==2) {
		if (tree[x].val) {
			if (!tree[l].val && tree[r].val) {
				tp[r]=tp[x];
				dfs(r);
			}
			if (tree[l].val && !tree[r].val) {
				tp[l]=tp[x];
				dfs(l);
			}
		}
		else {
			tp[l]=tp[x],tp[r]=tp[x];
			dfs(l); dfs(r);
		}
	}
}
int main() {
	/*freopen("expr.in","r",stdin);
	freopen("expr.out","w",stdout);*/
	getline(cin,s);
	len=s.length();
	scanf("%d",&n);
	tot=n;
	for (int i=1;i<=n;i++) 
	    scanf("%d",&a[i]);
	for (int i=0;i<len;i++) {
		if (s[i]==' ') continue;
		int x,y;
		switch (s[i]) {
			case 'x':
				x=0;
				while (isdigit(s[++i])) x=(x<<1)+(x<<3)+(s[i]^48);
				i--;
				st[++top]=x,val[top]=a[x];
				tree[x].val=a[x];
			    break;
			case '&':
				x=top,y=top-1;
				add(x,y,1);
				break;
			case '|':
				x=top,y=top-1;
				add(x,y,2);
				break;
			case '!':
				tree[st[top]].val=val[top]=!val[top];
				break;
		}    
	}
	ans=tree[root].val;
	tp[root]=root;
	dfs(root);
	scanf("%d",&q);
	for (int i=1;i<=q;i++) {
		int x;
		scanf("%d",&x);
		printf("%d\n",ans^sol[x]);
	}
	return 0;
}
2022/2/27 21:37
加载中...