QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#506704 | #7623. Coloring Tape | ucup-team1198 | RE | 1ms | 6156kb | C++14 | 5.6kb | 2024-08-05 20:43:34 | 2024-08-05 20:43:34 |
Judging History
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;
/// if ((c + l + r + diff) % 5 == 4) continue;
--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);
}
for (int i = 1; i < (int)st.size(); ++i) {
assert(st[i][0] > st[i - 1][0] && st[i][1] > st[i - 1][1]);
}
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 || mask >= (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) {
assert(nmask >> n);
dp1[nmask][ptr] = add(dp1[nmask][ptr], dp0[mask][ptr]);
};
auto no_vert = [&](int nmask) {
assert((nmask >> n) == 0);
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;
}
/**
5 10 20
8 4 5 0
2 2 5 1
8 4 5 0
10 3 5 0
7 1 3 1
1 2 4 1
6 3 5 1
10 3 5 0
4 1 5 1
7 3 4 1
2 2 4 1
8 3 4 0
9 3 5 0
5 2 5 1
9 4 5 0
9 1 2 0
6 1 5 1
8 3 5 0
2 2 4 1
8 3 5 0
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 5872kb
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: 0
Accepted
time: 1ms
memory: 5936kb
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:
1514
result:
ok 1 number(s): "1514"
Test #3:
score: 0
Accepted
time: 1ms
memory: 6156kb
input:
5 10 20 8 4 5 0 2 2 5 1 8 4 5 0 10 3 5 0 7 1 3 1 1 2 4 1 6 3 5 1 10 3 5 0 4 1 5 1 7 3 4 1 2 2 4 1 8 3 4 0 9 3 5 0 5 2 5 1 9 4 5 0 9 1 2 0 6 1 5 1 8 3 5 0 2 2 4 1 8 3 5 0
output:
28131
result:
ok 1 number(s): "28131"
Test #4:
score: -100
Runtime Error
input:
10 100 200 95 5 7 0 7 4 6 1 62 9 10 0 32 5 8 1 31 2 6 1 75 7 9 1 1 4 7 1 18 7 10 1 75 1 8 1 87 6 9 1 44 7 8 1 68 6 9 1 95 4 6 0 34 1 2 1 70 1 6 1 31 5 9 1 15 6 10 1 48 5 8 1 51 3 7 1 39 5 9 1 23 2 3 1 7 8 9 1 84 7 10 1 13 4 9 1 18 3 6 1 59 9 10 0 31 8 10 1 6 7 9 1 76 3 10 1 41 5 6 0 33 3 4 1 96 1 10...