QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#429307#8770. Comparatorjames1BadCreeperAC ✓965ms20348kbC++175.0kb2024-06-02 11:12:032024-06-02 11:12:03

Judging History

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

  • [2024-06-02 11:12:03]
  • 评测
  • 测评结果:AC
  • 用时:965ms
  • 内存:20348kb
  • [2024-06-02 11:12:03]
  • 提交

answer

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


class Solution {
public:
    inline bool delim(char c) { return c == ' '; }
    inline bool is_op(char c) { return c == '&' || c == '|' || c == '^' || c == '=' || c == '!'; }
    inline bool is_unary(char c) { return c == '!'; }
    inline int priority(char op) {
        if (op < 0) return 5; 
        if (op == '=') return 4; 
        if (op == '&') return 3; 
        if (op == '|') return 2; 
        if (op == '^') return 1; 
        return -1; 
    }

    void process_op(stack<int> &st, char op) {
        if (op < 0) {
            int l = st.top();
            st.pop();
            st.push(l ^ 1); 
        } else { 
            int r = st.top(); st.pop();
            int l = st.top(); st.pop(); 
            switch (op) {
            case '&':
                st.push(l & r);
                break;
            case '|':
                st.push(l | r);
                break;
            case '^':
                st.push(l ^ r);
                break;
            case '=':
                st.push(l == r);
                break;
            }
        }
    }

    int evaluate(string &s, int xval, int yval) {
        stack<int> st;
        stack<char> op;
        bool may_be_unary = true;
        for (int i = 0; i < int(s.size()); ++i) {
            if (delim(s[i])) continue;

            if (s[i] == '(') {
                op.push('('); // 2. 如果遇到左括号,那么将其放在运算符栈上
                may_be_unary = true;
            } else if (s[i] == ')') { // 3. 如果遇到右括号,执行一对括号内的所有运算符
                while (op.top() != '(') {
                    process_op(st, op.top());
                    op.pop(); // 不断输出栈顶元素,直至遇到左括号
                }
                op.pop(); // 左括号出栈
                may_be_unary = false;
            } else if (is_op(s[i])) { // 4. 如果遇到其他运算符
                char cur_op = s[i];
                if (may_be_unary && is_unary(cur_op))
                    cur_op = -cur_op;
                while (!op.empty() &&
                       ((cur_op >= 0 && priority(op.top()) >= priority(cur_op)) ||
                        (cur_op < 0 && priority(op.top()) > priority(cur_op)))) {
                    process_op(st, op.top());
                    op.pop(); // 不断输出所有运算优先级大于等于当前运算符的运算符
                }
                op.push(cur_op); // 新的运算符入运算符栈
                may_be_unary = true;
            } else { // 1. 如果遇到数字,直接输出该数字
                int number = 0;
                if (s[i] == 'x') number = xval; 
                else if (s[i] == 'y') number = yval; 
                else assert(isdigit(s[i])), number = s[i] - '0'; 
                st.push(number);
                may_be_unary = false;
            }
        }

        while (!op.empty()) {
            process_op(st, op.top());
            op.pop();
        }
        return st.top();
    }
} whiteqwq; 

int n, m, k; 
int ans[1024][1024]; 

struct Node {
    int x, y, val; 
    set<pair<int, int>> st; 
} a[200005]; 
int atval; 
vector<Node> t; 

inline int calc(int x, int y) {
    for (int i = 1; i <= m; ++i)
        if (a[i].st.find({x >> a[i].x & 1, y >> a[i].y & 1}) != a[i].st.end())
            return a[i].val; 
    return atval; 
}

set<tuple<int, int, int, int>> clc; 

int main(void) {
    ios::sync_with_stdio(0); 
    cin >> n >> k; 
    for (int i = 1; i <= n; ++i) {
        Node t; cin >> t.x >> t.y; --t.x; --t.y; 
        string s; cin >> s; 
        cin >> t.val; 
        if (whiteqwq.evaluate(s, 0, 0) && clc.find({t.x, t.y, 0, 0}) == clc.end()) t.st.emplace(0, 0), clc.emplace(t.x, t.y, 0, 0); 
        if (whiteqwq.evaluate(s, 0, 1) && clc.find({t.x, t.y, 0, 1}) == clc.end()) t.st.emplace(0, 1), clc.emplace(t.x, t.y, 0, 1); 
        if (whiteqwq.evaluate(s, 1, 0) && clc.find({t.x, t.y, 1, 0}) == clc.end()) t.st.emplace(1, 0), clc.emplace(t.x, t.y, 1, 0); 
        if (whiteqwq.evaluate(s, 1, 1) && clc.find({t.x, t.y, 1, 1}) == clc.end()) t.st.emplace(1, 1), clc.emplace(t.x, t.y, 1, 1); 
        if (t.st.size()) a[++m] = t; 
        cerr << i << "\n"; 
        for (auto [x, y] : t.st) cerr << x << " " << y << "\n"; 
        cerr << "----------\n"; 
    }
    cin >> atval; 

    for (int i = 0; i < 1 << k; ++i)
        for (int j = 0; j < 1 << k; ++j)
            ans[i][j] = calc(i, j); 
    cerr << calc(2, 2) << "\n"; 
    
    int ans1 = 0, ans2 = 0, ans3 = 0; 
    for (int i = 0; i < 1 << k; ++i) 
        if (ans[i][i] != 0) ++ans1; 
    for (int x = 0; x < 1 << k; ++x)
        for (int y = 0; y < 1 << k; ++y) {
            if (ans[x][y] == 1 && ans[y][x] != 0) ++ans2; 
            if (ans[x][y])
                for (int z = 0; z < 1 << k; ++z)
                    if (ans[y][z] && ans[x][z] == 0) ++ans3; 
        }
    cout << ans1 << " " << ans2 << " " << ans3 << "\n"; 
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 16436kb

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: 16680kb

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: 43ms
memory: 17156kb

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: 965ms
memory: 20308kb

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: 29ms
memory: 19208kb

input:

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

output:

4 16 0

result:

ok single line: '4 16 0'

Test #6:

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

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: 17856kb

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: 16616kb

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: 4ms
memory: 16640kb

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: 16ms
memory: 20348kb

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: 4ms
memory: 18012kb

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: 3ms
memory: 16732kb

input:

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

output:

1 1 0

result:

ok single line: '1 1 0'

Test #13:

score: 0
Accepted
time: 544ms
memory: 20216kb

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: 16968kb

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: 16572kb

input:

0 3
1

output:

8 64 0

result:

ok single line: '8 64 0'