QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#428549#8770. Comparatorucup-team3215#AC ✓296ms6332kbC++234.5kb2024-06-01 20:17:342024-06-01 20:17:34

Judging History

你现在查看的是最新测评结果

  • [2024-06-01 20:17:34]
  • 评测
  • 测评结果:AC
  • 用时:296ms
  • 内存:6332kb
  • [2024-06-01 20:17:34]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

bool delim(char c) {
    return c == ' ';
}

bool is_op(char c) {
    return c == '&' || c == '|' || c == '=' || c == '^' || c == '!';
}

int priority(char op) {
    if (op < 0)return 4;
    if (op == '=')return 3;
    if (op == '&')return 2;
    if (op == '|')return 1;
    if (op == '^')return 0;
    return -1;
}

char isunary(char op) {
    return op == '!';
}

void process_op(vector<int> &st, char op) {
    if (op < 0) {
        int l = st.back();
        st.pop_back();
        switch (-op) {
            case '!':
                st.push_back(!l);
                break;
        }
    } else {
        int r = st.back();
        st.pop_back();
        int l = st.back();
        st.pop_back();
        switch (op) {
            case '&':
                st.push_back(r & l);
                break;
            case '|':
                st.push_back(r | l);
                break;
            case '=':
                st.push_back(r == l);
                break;
            case '^':
                st.push_back(r ^ l);
                break;
        }
    }
}

int calc(const string &s, int x, int y) {
    auto get_variable_val = [&](const string &op) {
        return (op == "x" ? x : y);
    };
    bool may_unary = true;
    vector<int> st;
    vector<char> op;
    for (int i = 0; i < s.size(); ++i) {
        if (!delim(s[i])) {
            if (s[i] == '(') {
                op.push_back('(');
                may_unary = true;
            } else if (s[i] == ')') {
                while (op.back() != '(')process_op(st, op.back()), op.pop_back();
                op.pop_back();
                may_unary = false;
            } else if (is_op(s[i])) {
                char curop = s[i];
                if (may_unary && isunary(curop))curop = -curop;
                while (!op.empty() && (curop >= 0 && priority(op.back()) >= priority(curop)
                                       || curop < 0 && priority(op.back()) > priority(curop))) {
                    process_op(st, op.back()), op.pop_back();
                }
                op.push_back(curop);
                may_unary = true;
            } else {
                string operand;
                while (i < s.size() && isalnum(s[i])) {
                    operand += s[i++];
                }
                --i;
                if (isdigit(operand[0])) {
                    st.push_back(atoi(operand.c_str()));
                } else {
                    st.push_back(get_variable_val(operand));
                }
                may_unary = false;
            }
        }
    }
    while (!op.empty())process_op(st, op.back()), op.pop_back();
    return st.back();
}

constexpr int K = 10;
constexpr int NAX = 1 << K;
pair<int, int> prior[K][K][2][2];

int main() {
    cin.tie(0)->sync_with_stdio(false);
    for (int i = 0; i < K; ++i) {
        for (int j = 0; j < K; ++j) {
            prior[i][j][0][0] =
            prior[i][j][0][1] =
            prior[i][j][1][0] =
            prior[i][j][1][1] = {-1, 0};
        }
    }
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        --a, --b;
        string exp;
        cin >> exp;
        int re;
        cin >> re;
        for (int x = 0; x < 2; ++x) {
            for (int y = 0; y < 2; ++y) {
                if (calc(exp, x, y)) {
                    if(prior[a][b][x][y].first == -1)prior[a][b][x][y] = {n - i, re};
                }
            }
        }
    }
    int de;
    cin >> de;
    const int nax = 1 << k;
    vector<bitset<NAX>> g(nax);
    for (int i = 0; i < nax; ++i) {
        for (int j = 0; j < nax; ++j) {
            pair<int, int> v = {0, de};
            for (int x = 0; x < k; ++x) {
                for (int y = 0; y < k; ++y) {
                    v = max(v, prior[x][y][(i >> x) & 1][(j >> y) & 1]);
                }
            }
            g[i][j] = v.second;
        }
    }
    ll a{0};
    for (int i = 0; i < nax; ++i) {
        a += g[i][i];
    }
    ll b{0};
    for (int i = 0; i < nax; ++i) {
        for (int j = 0; j < nax; ++j) {
            if (g[i][j] && g[j][i])++b;
        }
    }
    ll c{0};
    for (int i = 0; i < nax; ++i) {
        for (int j = 0; j < nax; ++j) {
            if (g[i][j]) {
                c += (~g[i] & g[j]).count();
            }
        }
    }
    cout << a << " " << b << " " << c << "\n";
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3556kb

input:

3 2
1 1 (x=0)&(y=1) 1
1 1 (x=1)&(y=(x^x)) 0
2 2 (x=1)|(y=0) 0
1

output:

0 0 0

result:

ok single line: '0 0 0'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3620kb

input:

4 3
2 1 x=0&(y=1) 1
1 2 !x^!!y 0
2 3 ((x|1)=y)&1&1 1
3 1 !x&!x&!x 0
1

output:

3 25 52

result:

ok single line: '3 25 52'

Test #3:

score: 0
Accepted
time: 50ms
memory: 3888kb

input:

1413 3
1 3 0 0
3 3 !x 0
2 2 x=0 1
1 2 !y^0 1
2 3 (x^1) 0
3 2 ((!0)) 1
1 1 !!1=(y) 0
2 2 !(1^x)&y 1
3 2 (y)&1|!!1 0
3 1 !x=(y&y=y) 0
2 1 (((!1)^!x)) 1
2 3 !0=(0&y)=1&y 0
1 2 ((((!0)))|!1) 0
3 1 !(y=!1=x|(!x)) 0
1 1 ((((y=!y)))&!0) 0
2 3 ((y=1)^!1^!!1|0) 1
2 3 1|(!x)&!x|1|(x=1) 1
2 3 !0^!!!!y&0=(!1&!0...

output:

4 16 0

result:

ok single line: '4 16 0'

Test #4:

score: 0
Accepted
time: 296ms
memory: 3844kb

input:

181737 10
5 2 1 1
1 10 !1=!x 0
10 1 (1^x) 0
2 4 !1 1
10 8 y=(!1)^1 1
6 2 !((x&!x)) 1
1 10 !!((!x)|x) 1
7 10 (((0))) 0
7 3 !(1)^!x 0
10 4 (!1)&x 0
7 7 !y&!0 1
8 8 !1=(x)|1^1 1
2 6 ((!!!x)) 0
7 2 1 1
2 2 y=1=0 0
6 3 (!0) 0
6 4 0 0
1 1 (!1) 1
1 8 y 1
3 5 !x|!x^!1 0
4 7 (!0) 0
3 4 !1&1&!1|!0 1
2 7 ((0|1...

output:

1024 1048576 0

result:

ok single line: '1024 1048576 0'

Test #5:

score: 0
Accepted
time: 15ms
memory: 6332kb

input:

1 3
1 1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!...

output:

4 16 0

result:

ok single line: '4 16 0'

Test #6:

score: 0
Accepted
time: 0ms
memory: 3864kb

input:

1 1
1 1 x^y|1 0
1

output:

1 1 0

result:

ok single line: '1 1 0'

Test #7:

score: 0
Accepted
time: 0ms
memory: 3556kb

input:

1 1
1 1 x&y|1 0
1

output:

0 0 0

result:

ok single line: '0 0 0'

Test #8:

score: 0
Accepted
time: 0ms
memory: 3620kb

input:

1 1
1 1 x=y|1 0
1

output:

0 0 0

result:

ok single line: '0 0 0'

Test #9:

score: 0
Accepted
time: 0ms
memory: 3816kb

input:

2 2
1 2 !x&!y 1
1 1 !x&y 0
1

output:

4 12 2

result:

ok single line: '4 12 2'

Test #10:

score: 0
Accepted
time: 121ms
memory: 3680kb

input:

2 10
9 8 ((((!((!x=1))^(!1&(x|x|!y))&((!y&!x=(x=y)))&!((((x=1))&(0=(y))^(!!(!!x^1=x)&(x)^y&1=!x&1=(((!0^(1)^1))^!(((((y))|x|!y))))^!!0=!y&(0)|(y=x&!y&y=x)))=((((!!y&!!0|!0^!0)=!x))))^0&(((!1=!(!x)))|(((((x=1|!y|y)=(!1^1^0^(0)))=!0^1)))=(!(!1^(((((!1)^!x^0))))=(1)^((((y=((x))|(0)|(!1^1)))|(!!!y))=((!...

output:

0 0 0

result:

ok single line: '0 0 0'

Test #11:

score: 0
Accepted
time: 0ms
memory: 3620kb

input:

4 3
1 1 !!!!!!x 0
2 1 !!y 1
1 2 !!!!!x 1
2 2 !!!x 0
1

output:

4 16 0

result:

ok single line: '4 16 0'

Test #12:

score: 0
Accepted
time: 2ms
memory: 3688kb

input:

1 1
1 1 ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((...

output:

1 1 0

result:

ok single line: '1 1 0'

Test #13:

score: 0
Accepted
time: 238ms
memory: 3660kb

input:

200000 10
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10...

output:

512 262144 134217728

result:

ok single line: '512 262144 134217728'

Test #14:

score: 0
Accepted
time: 0ms
memory: 3560kb

input:

10 3
3 1 (!x) 1
3 2 !1&x&1&!y 1
2 1 ((x)&y) 1
1 3 0 0
2 2 !1&0=y&0 1
3 3 (!y)^y 1
2 1 0|((!y)) 0
3 2 x 0
2 2 (y|1^x) 0
2 1 ((!0)|y) 0
1

output:

8 64 0

result:

ok single line: '8 64 0'

Test #15:

score: 0
Accepted
time: 0ms
memory: 3620kb

input:

0 3
1

output:

8 64 0

result:

ok single line: '8 64 0'