QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#506667#7623. Coloring Tapeucup-team1198RE 1ms5864kbC++145.2kb2024-08-05 20:32:402024-08-05 20:32:40

Judging History

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

  • [2024-08-05 20:32:40]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:5864kb
  • [2024-08-05 20:32:40]
  • 提交

answer

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

const int MOD = 998244353;

int add(int a, int b) {
    return a + b >= MOD ? a + b - MOD : a + b;
}

const int MAXN = 14, MAXM = 510;
int need[MAXN][MAXM];
int nptr[MAXN][MAXM];
vector<array<int, 2>> restr[MAXM];
vector<int> dp0[3 << MAXN];
vector<int> dp1[3 << MAXN];

void solve_() {
    int n, m, r;
    cin >> n >> m >> r;
    bool bad = false;
    while (r--) {
        int c, l, r, diff;
        cin >> c >> l >> r >> diff;
        --c;
        if (c == 0) {
            if (diff == 0) bad = true;
            continue;
        }
        if (diff == 0) {
            for (int i = l; i < r; ++i) {
                need[i][c] = true;
            }
        } else {
            --l; --r;
            restr[c].push_back({l, r});
        }
    }
    if (bad) {
        cout << "0\n";
        return;
    }
    for (int c = 0; c < m; ++c) {
        sort(restr[c].begin(), restr[c].end());
        vector<array<int, 2>> st;
        for (auto elem : restr[c]) {
            if (!st.empty() && st.back()[1] >= elem[1]) st.pop_back();
            if (!st.empty() && st.back()[0] == elem[0]) continue;
            st.push_back(elem);
        }
        restr[c] = st;
        for (int i = 0; i < n; ++i) {
            nptr[i][c] = st.size();
        }
        for (int i = (int)st.size() - 1; i >= 0; --i) {
            for (int r = st[i][0] - 1; r >= 0; --r) {
                nptr[r][c] = i;
            }
        }
    }
    fill(dp0, dp0 + (3 << n), vector<int>(1, 0));
    dp0[(1 << n) - 1][0] = 1;
    for (int cell = 0; cell < n * m; ++cell) {
        int r = cell % n;
        int c = cell / n;
        /// cerr << r << " " << c << " " << nptr[r][c] << endl;
        if (r == 0) {
            for (int mask = 0; mask < (3 << n); ++mask) {
                int val = dp0[mask].back();
                if (mask >= (1 << n)) val = 0;
                dp0[mask].resize(restr[c].size() + 1);
                fill(dp0[mask].begin(), dp0[mask].end(), 0);
                dp0[mask][0] = val;
            }
        }
        if (need[r][c]) {
            for (int mask = 0; mask < (1 << n); ++mask) {
                fill(dp0[mask].begin(), dp0[mask].end(), 0);
            }
        }
        for (int mask = 0; mask < (3 << n); ++mask) {
            for (int ptr = 0; ptr < (int)restr[c].size(); ++ptr) {
                if (r >= restr[c][ptr][1]) {
                    assert(dp0[mask][ptr] == 0 || ptr >= (1 << n));
                    dp0[mask][ptr] = 0;
                }
            }
        }
        for (int mask = 0; mask < (3 << n); ++mask) {
            dp1[mask].resize(restr[c].size() + 1);
            fill(dp1[mask].begin(), dp1[mask].end(), 0);
        }
        /// cerr << "prep done" << endl;
        for (int mask = 0; mask < (3 << n); ++mask) {
            int tp = (mask >> n);
            int to_left = (mask >> r) & 1;
            for (int ptr = 0; ptr <= (int)restr[c].size(); ++ptr) {
                if (dp0[mask][ptr] == 0) continue;
                /// cerr << mask << "-" << ptr << endl;

                auto vert = [&](int nmask) {
                    dp1[nmask][ptr] = add(dp1[nmask][ptr], dp0[mask][ptr]);
                };

                auto no_vert = [&](int nmask) {
                    dp1[nmask][nptr[r][c]] = add(dp1[nmask][nptr[r][c]], dp0[mask][ptr]);
                };

                if (to_left == 0) {
                    if (tp == 0) {
                        int nmask = mask | (1 << n);
                        vert(nmask);
                        nmask |= (1 << r);
                        vert(nmask);
                    } else if (tp == 1) {
                        int nmask = mask;
                        vert(nmask);
                    } else {
                        int nmask = mask;
                        vert(nmask);
                        nmask ^= (2 << n);
                        /// cerr << "->" << nmask << endl;
                        no_vert(nmask);
                        /// cerr << dp1[nmask][0] << endl;
                        nmask ^= (1 << r);
                        no_vert(nmask);
                    }
                } else {
                    if (tp == 0) {
                        int nmask = mask;
                        no_vert(nmask);
                        nmask ^= (1 << r);
                        no_vert(nmask);
                        nmask ^= (2 << n);
                        vert(nmask);
                    } else if (tp == 1) {
                        int nmask = mask ^ (1 << r) ^ (1 << n);
                        /// cerr << "->" << nmask << endl;
                        no_vert(nmask);
                        /// cerr << dp1[nmask][0] << endl;
                    }
                }
            }
        }
        for (int i = 0; i < (3 << n); ++i) {
            swap(dp0[i], dp1[i]);
        }
    }
    int ans = dp0[0].back();
    cout << ans << "\n";
}

/// #define MULTITEST

main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int tst = 1;
#ifdef MULTITEST
    cin >> tst;
#endif // MULTITEST
    while (tst--) {
        solve_();
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 5864kb

input:

3 5 3
3 2 3 0
4 1 2 0
5 1 3 0

output:

19

result:

ok 1 number(s): "19"

Test #2:

score: -100
Runtime Error

input:

5 10 10
9 3 4 1
2 4 5 0
7 2 3 0
9 2 3 0
6 3 5 0
6 2 4 1
2 4 5 0
1 1 3 1
7 2 4 0
10 2 3 0

output:


result: