55分求调
查看原帖
55分求调
1017729
Yanzm楼主2024/10/2 10:53

RT

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 2e6 + 10;

struct Node {
    int id, val;
};

stack<Node> s;
char str[N];
int n, q, len = 0, X[N], expr[N], re[N], f[N];

signed main() {
    while (cin >> str && !(str[0] >= '0' && str[0] <= '9')) {
        len++;
        if (str[0] == 'x') {
            sscanf(str + 1, "%lld", &expr[len]);
            re[expr[len]] = len;
        }
        else if (str[0] == '&') {
            expr[len] = -1;
        }
        else if (str[0] == '|') {
            expr[len] = -2;
        }
        else if (str[0] == '!') {
            expr[len] = -3;
        }
    }

    sscanf(str, "%lld", &n);
    for (int i = 1; i <= n; i++) {
        cin >> X[i];
    }

    for (int i = 1; i <= len; i++) {
        if (expr[i] > 0) {
            s.push({ i, X[expr[i]] });
        }
        else if (expr[i] == -1) { //AND
            Node x = s.top(); s.pop();
            Node y = s.top(); s.pop();
            s.push({ i, x.val & y.val }); 
            f[y.id] = i; 
            f[x.id] = i;
        }
        else if (expr[i] == -2) { // OR
            Node x = s.top(); s.pop();
            Node y = s.top(); s.pop();
            s.push({ i, x.val | y.val });
            if (!x.val) f[y.id] = i;
            if (!y.val) f[x.id] = i;
        }
        else if (expr[i] == -3) { // NOT
            Node x = s.top(); s.pop();
            s.push({ i, !x.val }); 
            f[x.id] = i; 
        }
    }

    int x, res = s.top().val;
    cin >> q;
    while (q--) {
        cin >> x;
        int y = re[x];
        while (f[y]) y = f[y];
        cout << (y == len ? !res : res) << endl;
    }

    return 0;
}

2024/10/2 10:53
加载中...