求调+可互
查看原帖
求调+可互
1615478
SRQ_321楼主2025/7/20 14:51
#include<bits/stdc++.h>
using namespace std;
string s;
int n,root,q,x;
map<int,int> a;//权值
map<int,int> b;//1个数
map<int,int> f;//father
map<int,char> c;//字符
void init(){
	stack<int> p;
	for(int i=0;i<s.size();i++){
		if(s[i]=='x'){
			i++;
			int x=0;
			while(s[i]>='0'&&s[i]<='9'){
				x=x*10+s[i]-'0';
				i++;
			}
			p.push(x);
		}
		if(s[i]=='&'){
			int x=p.top();
			p.pop();
			int y=p.top();
			p.pop();
			a[++n]=a[x]&a[y];
			b[n]=a[x]+a[y];
			f[x]=f[y]=n;
			c[n]='&';
			p.push(n);
		}
		if(s[i]=='|'){
			int x=p.top();
			p.pop();
			int y=p.top();
			p.pop();
			a[++n]=a[x]|a[y];
			b[n]=a[x]+a[y];
			f[x]=f[y]=n;
			c[n]='|';
			p.push(n);
		}
		if(s[i]=='!'){
			int x=p.top();
			a[x]=!a[x];
		}
	}
	root=p.top();
}
int check(int u){
	if(u==root){
		return a[root];
	}
	if(c[f[u]]=='&'){
		if(b[f[u]]==2||(b[f[u]]==1&&a[u]==0)){
			int x=a[f[u]];
			a[f[u]]=1;
			if(check(f[u])){
				a[f[u]]=x;
				return 1;
			}else{
				a[f[u]]=x;
				return 0;
			}
		}else{
			int x=a[f[u]];
			a[f[u]]=0;
			if(check(f[u])){
				a[f[u]]=x;
				return 1;
			}else{
				a[f[u]]=x;
				return 0;
			}
		}
	}
	if(c[f[u]]=='|'){
		if(b[f[u]]>=1||(b[f[u]]==0&&a[u]==0)){
			int x=a[f[u]];
			a[f[u]]=1;
			if(check(f[u])){
				a[f[u]]=x;
				return 1;
			}else{
				a[f[u]]=x;
				return 0;
			}
		}else{
			int x=a[f[u]];
			a[f[u]]=0;
			if(check(f[u])){
				a[f[u]]=x;
				return 1;
			}else{
				a[f[u]]=x;
				return 0;
			}
		}
	}
}
inline void solve(){
	getline(cin,s);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	init();
	cin>>q;
	while(q--){
		cin>>x;
		if(x==root){
			a[root]=!a[root];
		}
		cout<<check(x)<<'\n';
		if(x==root){
			a[root]=!a[root];
		}
	}
}
int main(){
	int t=1;
    // cin>>t;
	for(int i=1;i<=t;i++){
		solve();
	}
	return 0;
}
2025/7/20 14:51
加载中...