QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#107805 | #1286. Ternary String Counting | lonytree | TL | 49ms | 101264kb | C++14 | 2.4kb | 2023-05-22 21:01:06 | 2023-05-22 21:01:09 |
Judging History
answer
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N = 5005, mod = 1e9 + 7;
int T, n, m;
int a[N], b[N], c[N], d[N];
int f[N][N], sum1[N], sum2[N], tsum1[N], tsum2[N];
int solve(){
cin >> n >> m;
for (int i = 1; i <= n; i++){
a[i] = c[i] = 0;
b[i] = d[i] = n + 1;
}
for (int i = 1; i <= m; i++){
int l, r, x;
cin >> l >> r >> x;
if (x == 3) a[r] = max(l, a[r]);
else if (x == 2){
b[r] = min(l, b[r]);
c[r] = max(l, c[r]);
}else d[r] = min(l, d[r]);
}
for (int i = 1; i <= n; i++)
if (a[i] >= b[i] || c[i] >= d[i])
return 0;
memset(f, 0, sizeof(f));
memset(sum1, 0, sizeof(sum1));
memset(sum2, 0, sizeof(sum2));
f[0][0] = 1;
sum1[0] = sum2[0] = 1;
int l1 = 0, l2 = 0, r1 = n, r2 = n;
for (int i = 1; i <= n; i++){
for (int j = 0; j <= n; j++){
tsum1[j] = sum1[j];
tsum2[j] = sum2[j];
}
for (int j = 0; j < i; j++){
f[j][i - 1] = (f[j][i - 1] + tsum1[j]) % mod;
sum1[j] = (sum1[j] + tsum1[j]) % mod;
sum2[i - 1] = (sum2[i - 1] + tsum1[j]) % mod;
}
for (int k = 0; k < i; k++){
f[k][i - 1] = (f[k][i - 1] + tsum2[k]) % mod;
sum1[k] = (sum1[k] + tsum2[k]) % mod;
sum2[i - 1] = (sum2[i - 1] + tsum2[k]) % mod;
}
int nl1 = max(l1, a[i]), nl2 = max(l2, c[i]), nr1 = min(r1, b[i] - 1), nr2 = min(r2, d[i] - 1);
if (nl1 > nr1 || nl2 > nr2) return 0;
auto clear = [&](int x, int y){
sum1[x] = (sum1[x] - f[x][y]) % mod;
sum2[y] = (sum2[y] - f[x][y]) % mod;
f[x][y] = 0;
};
for (int x = l1; x < nl1; x++)
for (int y = l2; y <= r2; y++)
clear(x, y);
for (int x = nr1 + 1; x <= r1; x++)
for (int y = l2; y <= r2; y++)
clear(x, y);
for (int x = nl1; x <= nr1; x++){
for (int y = l2; y < nl2; y++)
clear(x, y);
for (int y = nr2 + 1; y < r2; y++)
clear(x, y);
}
// cout << i << ' ' << l1 << ' ' << r1 << ' ' << l2 << ' ' << r2 << endl;
l1 = nl1, r1 = nr1, l2 = nl2, r2 = nr2;
// cout << i << ' ' << l1 << ' ' << r1 << ' ' << l2 << ' ' << r2 << endl;
}
int ans = 0;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
ans = (ans + f[i][j]) % mod;
return (ans + mod) % mod;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
cin >> T;
while (T--) cout << solve() << endl;
return 0;
}
/*
a <= j < b
c <= k < d
a < b <= c < d
*/
詳細信息
Test #1:
score: 100
Accepted
time: 49ms
memory: 101264kb
input:
4 1 0 2 0 3 0 5 2 1 3 3 4 5 1
output:
3 9 27 18
result:
ok 4 tokens
Test #2:
score: -100
Time Limit Exceeded
input:
741 5 3 1 5 3 3 4 2 1 4 3 4 3 2 4 2 1 4 1 2 3 3 10 3 9 10 2 3 6 3 1 9 1 3 4 2 3 2 1 3 2 2 3 3 1 3 3 10 4 6 6 1 9 10 2 4 8 3 4 10 3 6 3 1 4 3 2 4 2 2 2 2 4 3 1 4 1 1 1 2 2 3 1 5 3 4 5 2 4 5 1 1 4 3 9 3 2 3 2 1 9 2 2 4 2 4 3 1 3 3 2 3 2 1 2 3 8 4 5 8 1 4 8 1 3 5 3 1 3 3 9 3 4 5 1 1 5 3 3 8 2 8 3 5 7 2...