[CSP-J2020] 表达式50分求助
  • 板块灌水区
  • 楼主count_year
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/10/19 16:00
  • 上次更新2024/10/19 17:40:29
查看原帖
[CSP-J2020] 表达式50分求助
705729
count_year楼主2024/10/19 16:00
#include<bits/stdc++.h>
using namespace std;
stack<pair<int,pair<int,int> > >q;
int a[5000001];
int t[5000001];
int f[5000001];
int c[5000001];
string k;
int n;
int si(string b){
	int h=0;
	for(int i=0;i<b.length();i++){
		h=h*10+int(b[i]-'0'); 
	}
	return h;
}
signed main(){
	getline(cin,k);
	k+=' ';
	scanf("%d",&n);
	int len=k.length(),top=0,now=n;
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	for(int i=0;i<len;i++){
		if(k[i]==' '){
			if(k[top]=='x'){
				int x=si(k.substr(top+1,(i-1)-top));
				f[x]=x;
				c[x]=a[x];
				q.push(make_pair(x,make_pair(x,x)));
			}else if(k[top]=='|'||k[top]=='&'){
				int x,y;
				x=q.top().first;
				q.pop();
				y=q.top().first;
				q.pop();
				int h;
				if(k[top]=='|'){
					if(c[x]==1&&c[y]==1){
						t[x]=1;
						t[y]=1;
						h=1;
					}
					if(c[x]==0&&c[y]==0){
						t[x]=2;
						t[y]=2;
						h=0;
					}
					if(c[x]==1&&c[y]==0){
						t[x]=4;
						t[y]=1;
						h=1;
					}
					if(c[x]==0&&c[y]==1){
						t[x]=1;
						t[y]=4;
						h=1;
					}
				}
				if(k[top]=='&'){
					if(c[x]==1&&c[y]==1){
						t[x]=4;
						t[y]=4;
						h=1;
					}
					if(c[x]==0&&c[y]==0){
						t[x]=0;
						t[y]=0;
						h=0;
					}
					if(c[x]==1&&c[y]==0){
						t[x]=0;
						t[y]=3;
						h=0;
					}
					if(c[x]==0&&c[y]==1){
						t[x]=3;
						t[y]=0;
						h=0;
					}
				}
				now++;
//				printf("%d %d c=%d c=%d t=%d t=%d %d\n",x,y,c[x],c[y],t[x],t[y],now);
				f[x]=now;
				f[y]=now;
				c[now]=h;
				q.push(make_pair(now,make_pair(x,y)));
			}else if(k[top]=='!'){
				pair<int,pair<int,int> >x=q.top();
				c[x.first]=1-c[x.first];
//				printf("%d %d\n",x.first,c[x.first]);
				q.pop();
				q.push(x);
			}
			top=i+1;
		}
	}
	int m;
	scanf("%d",&m);
	for(int i=1;i<=m;i++){
		int x;
		scanf("%d",&x);
		int ans=!c[x],tot=x;
//		printf("%d %d\n",tot,ans);
		while(tot!=now){
			int father=f[tot];
			if(ans!=c[now]){
				if(t[tot]<=1){
					ans=t[tot];
				}else if(t[tot]==4){
					ans=1-c[father];
				}else{
					ans=c[father];
				}
			}else{
				if(t[tot]<=1){
					ans=t[tot];
				}else if(t[tot]==3){
					ans=1-c[father];
				}else{
					ans=c[father];
				}
			}
			tot=father;
//			printf("%d %d\n",tot,ans);
		}
		printf("%d\n",ans);
	}
    return 0;
}

/*

*/






2024/10/19 16:00
加载中...