表达式70分求助
查看原帖
表达式70分求助
448884
快乐的大童楼主2021/8/19 21:32
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<stack>
#define int long long
#define S 1000001
#define P 100001
using namespace std; 
int n,a[P],q;
int cnt,ans,len;
bool vis[S],tmp[S];
char c[S];
stack <int> st; 
pair <int,int> tree_son[S];
void get_string(){
	int f=0;
	for(int i=1;i<=len;i++){
		if(isdigit(c[i])){
			f=f*10+c[i]-48;
		}else if(c[i]==' '&&f){
			st.push(f);
			f=0;
		}else if(c[i]=='&'){
			int x=st.top();
			st.pop();
			int y=st.top();
			st.pop();
			cnt++;
			st.push(cnt);
			a[cnt]=-1;
			tree_son[cnt].first=x;
			tree_son[cnt].second=y;
		}else if(c[i]=='|'){
			int x=st.top();
			st.pop();
			int y=st.top();
			st.pop();
			cnt++;
			st.push(cnt);
			a[cnt]=-2;
			tree_son[cnt].first=x;
			tree_son[cnt].second=y;
		}else if(c[i]=='!'){
			int x=st.top();
			vis[x]=1;
		}
	}
	return;
}
void read_string(){
	char ch=getchar();
	while(ch!='\n'){
		c[++len]=ch;
		ch=getchar();
	}
	return;
}
int dfs1(int x,bool flag){
	if(flag){
		a[x]^=1;
	}
	if(x<=n){
		return a[x];
	}
	int orz=flag;
	int res1=dfs1(tree_son[x].first,orz^(vis[tree_son[x].first]?1:0));
	int res2=dfs1(tree_son[x].second,orz^(vis[tree_son[x].second]?1:0));
	if(a[x]==-1){
		if(!res1){
			tmp[tree_son[x].second]=1;
		}
		if(!res2){
			tmp[tree_son[x].first]=1;
		}
		return res1&res2;
	}else if(a[x]==-2){
		if(res1){
			tmp[tree_son[x].second]=1;
		}
		if(res2){
			tmp[tree_son[x].first]=1;
		}
		return res1|res2;
	}
}
void dfs2(int x){
	if(x<=n){
		return;
	}
	tmp[tree_son[x].first]=((int)tmp[tree_son[x].first]|(int)tmp[x]);
	tmp[tree_son[x].second]=((int)tmp[tree_son[x].second]|(int)tmp[x]);
	dfs2(tree_son[x].first);
	dfs2(tree_son[x].second);
	return;
}
int defined(){
	int res=dfs1(cnt,vis[cnt]);
	dfs2(cnt);
	return res;
}
signed main(){
	read_string();
	scanf("%lld",&n);
	cnt=n;
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
	}
	get_string();
	ans=defined();
	scanf("%lld",&q);
	while(q--){
		int orz;
		scanf("%lld",&orz);
		if(tmp[orz]){
			printf("%lld\n",ans);
		}else{
			printf("%lld\n",!ans);
		}
	}
	return 0;
}
2021/8/19 21:32
加载中...