求助
查看原帖
求助
315205
Kniqht楼主2022/10/3 19:13

0 ptsWA,写的是O(n)正解,样例都过了,但是不知道是不是思路问题

思路:模拟出所有不影响结果的点,最后O(1)判断

#include<bits/stdc++.h>
#define test printf("debug")
using namespace std;
const int N=4e6+10,M=110;
int n,m,len,f[N],tt,temp[M],res;
struct Node{
    int t,num;
}stk[N];
struct Num{
    int x,y;
}vec[N];
string str;
bool st[N],flag[N];
void dfs(int u){
    int t1=vec[u].x,t2=vec[u].y;
    if(!st[t1]){
        st[t1]=true;
        if(t1>n) dfs(t1);
    }
    if(!st[t2]){
        st[t2]=true;
        if(t2>n) dfs(t2);
    }
}
int power(int x){
    int res1=1;
    while(x--) res1*=10;
    return res1;
}
int main(){
    getline(cin,str);
    len=str.size();
    scanf("%d",&n);m=n;
    for(int i=1;i<=n;i++) scanf("%d",&f[i]);
    int tmp=0;
    for(int i=0;i<len;i++){
        if(str[i]>'0'&&str[i]<'9'){
            tmp=0;res=0;
            temp[++tmp]=str[i]-'0';
            i++;
            while(str[i]>'0'&&str[i]<'9'){
                i++;
                temp[++tmp]=str[i]-'0';
            }
            for(int i=tmp;i>=1;i--) res+=power(tmp-i)*temp[i];
            stk[++tt]={res,f[res]};
        }
        if(str[i]=='!') stk[tt].num=1-stk[tt].num;
        if(str[i]=='&'){
            Node t1=stk[tt--],t2=stk[tt--];
            if(!t1.num&&!t2.num) st[t1.t]=st[t2.t]=true;
            vec[++m]=Num{t1.t,t2.t};
            stk[++tt]=Node{m,t1.num&t2.num};
        }
        if(str[i]=='|'){
            Node t1=stk[tt--],t2=stk[tt--];
            if(t1.num) st[t2.t]=true;
            if(t2.num) st[t1.t]=true;
            vec[++m]=Num{t1.t,t2.t};
            stk[++tt]=Node{m,t1.num|t2.num};
        }
    }
    int ans=stk[tt].num;
    for(int i=n+1;i<=m;i++)
        if(st[i]) dfs(i);
    int q;scanf("%d",&q);
    while(q--){
        scanf("%d",&m);
        printf("%d\n",st[m]?ans:1-ans);
    }
	return 0;
}
2022/10/3 19:13
加载中...