QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#108271 | #1286. Ternary String Counting | Zeardoe | ML | 136ms | 79384kb | C++20 | 4.7kb | 2023-05-24 09:58:08 | 2023-05-24 09:58:13 |
Judging History
answer
/*
[templates]:
duipai
spjdp
compre
addhis
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
//use ll instead of int.
#define f(i, a, b) for(int i = (a); i <= (b); i++)
#define cl(i, n) i.clear(),i.resize(n);
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 1e9;
//#define cerr if(false)cerr
//#define freopen if(false)freopen
mt19937 rng(time(0));
int rnd(int l, int r) {return rng() % (r-l+1) + l; }
#define watch(x) cerr << (#x) << ' '<<'i'<<'s'<<' ' << x << endl
void pofe(int number, int bitnum) {
string s; f(i, 0, bitnum) {s += char(number & 1) + '0'; number >>= 1; }
reverse(s.begin(), s.end()); cerr << s << endl;
return;
}
template <typename TYP> void cmax(TYP &x, TYP y) {if(x < y) x = y;}
template <typename TYP> void cmin(TYP &x, TYP y) {if(x > y) x = y;}
//调不出来给我对拍!
//use std::array.
const int mod = 1e9 + 7;
struct con {
int jl, jr; int kl, kr;
con() {}
con(int x) {jl = 0; jr = x; kl = 0; kr = x; }
con(int _jl, int _jr, int _kl, int _kr):jl(_jl),jr(_jr),kl(_kl),kr(_kr){}
};
con merge(con th, con op) {
return con(max(th.jl, op.jl), min(th.jr, op.jr), max(th.kl, op.kl), min(th.kr, op.kr));
}
con calc(int x, int y, int z) {
if(z == 1) return con(0, x - 1, 0, x - 1);
else if(z == 2) return con(x, y - 1, 0, x - 1);
else return con(x, y - 1, x, y - 1);
}
con v[5050];
bool emp(con x) {return (x.jr < x.jl || x.kr < x.kl); }
bool buhan(int x, int y, int z) {return z < x || z > y; }
deque<int> hang[5050], lie[5050]; int shang[5050], slie[5050]; int dp[5050][5050];
signed main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
//freopen();
//freopen();
//time_t start = clock();
//think twice,code once.
//think once,debug forever.
int T; cin >> T;
while(T--) {
int n, m; cin >> n >> m;
f(i, 1, n) v[i] = con(i);
f(i, 0, n - 1) f(j, 0, n - 1) dp[i][j] = 0;
f(i, 0, n - 1) slie[i] = shang[i] = 0;
f(i, 0, n - 1) { lie[i].clear(); hang[i].clear(); }
f(i, 1, m) {
int x, y, z; cin >> x >> y >> z;
v[y] = merge(v[y], calc(x, y, z));
}
// f(i, 1, n) cerr << v[i].jl << " " << v[i].jr<<" "<<v[i].kl<<" "<<v[i].kr<<endl;
// cerr << endl;
if(emp(v[1])) {cout << 0 << endl; continue; }
else {shang[0] = slie[0] = dp[0][0] = 3; hang[0].push_back(0); lie[0].push_back(0); }
f(i, 1, n) {
if(i != 1) {
f(j, 0, i - 2) {
//dp_{i-1,j} = hang_j + lie_j
dp[i-1][j] = shang[j] + slie[j]; dp[i-1][j] %= mod;
shang[i-1] += shang[j] + slie[j]; shang[i-1] %= mod;
slie[j] += shang[j] + slie[j]; slie[j] %= mod;
hang[i-1].push_back(j); lie[j].push_back(i-1);
}
}
f(j, 0, i - 1) {
if(buhan(v[i].jl, v[i].jr, j)) {
shang[j] = 0; hang[j].clear();
}
else {
while(!hang[j].empty() && buhan(v[i].kl, v[i].kr, hang[j].front())) {
shang[j] = shang[j] + mod - dp[j][hang[j].front()];
shang[j] %= mod; hang[j].pop_front();
}
while(!hang[j].empty() && buhan(v[i].kl, v[i].kr, hang[j].back())) {
shang[j] = shang[j] + mod - dp[j][hang[j].back()];
shang[j] %= mod; hang[j].pop_back();
}
}
}
f(j, 0, i - 1) {
if(buhan(v[i].kl, v[i].kr, j)) {
slie[j] = 0; lie[j].clear();
}
else {
while(!lie[j].empty() && buhan(v[i].jl, v[i].jr, lie[j].front())) {
slie[j] = slie[j] + mod - dp[lie[j].front()][j];
slie[j] %= mod; lie[j].pop_front();
}
while(!lie[j].empty() && buhan(v[i].jl, v[i].jr, lie[j].back())) {
slie[j] = slie[j] + mod - dp[lie[j].back()][j];
slie[j] %= mod; lie[j].pop_back();
}
}
}
// f(x, 0, i - 1) f(y, 0, i - 1) cout << dp[x][y] << " \n"[y == i - 1];
}
int ans = 0;
f(i, 0, n - 1) {ans = (ans + slie[i]) % mod;}
cout << ans << endl;
}
//time_t finish = clock();
//cout << "time used:" << (finish-start) * 1.0 / CLOCKS_PER_SEC <<"s"<< endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 10332kb
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: 0
Accepted
time: 0ms
memory: 10416kb
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...
output:
90 0 0 0 24300 0 0 0 768 0 0 972 2916 0 6480 864 0 0 0 0 0 0 0 0 6 0 0 0 0 0 96 0 6 0 0 0 0 0 0 0 108 0 0 0 2916 0 0 0 0 0 0 0 0 0 0 324 8748 0 0 0 0 0 0 4320 0 0 0 18 0 0 0 0 0 0 0 0 0 0 0 1992 0 0 450 0 162 1656 0 0 0 0 0 54 0 18 0 0 0 0 744 0 1620 324 0 0 36 36 0 0 60 6 54 0 0 0 0 0 0 0 0 243 810...
result:
ok 741 tokens
Test #3:
score: 0
Accepted
time: 8ms
memory: 10404kb
input:
332 17 5 4 9 1 4 6 1 7 17 1 5 5 1 5 6 3 13 5 10 12 3 8 13 2 1 10 3 2 9 1 9 10 1 19 5 11 13 1 9 17 2 1 7 1 4 4 2 3 9 1 15 4 8 11 2 9 14 3 10 12 2 4 5 2 12 4 3 9 2 7 8 1 1 7 3 6 7 3 16 5 4 14 3 1 5 1 2 10 1 2 12 3 6 9 3 20 4 10 17 3 4 20 3 17 19 3 8 17 1 20 5 11 11 1 9 13 1 1 5 3 17 17 3 3 18 3 17 5 1...
output:
0 0 0 2047032 0 0 0 0 0 839808 0 0 0 0 0 0 0 0 0 0 0 0 44641044 0 20412 0 0 0 0 0 0 0 6 0 0 0 0 81 0 0 0 0 0 0 0 0 0 0 0 313155072 0 0 0 0 0 0 0 0 0 5400 0 10368 0 0 0 0 14580 0 0 0 217728 40310784 0 0 218700 0 0 0 0 1620 0 0 0 0 0 108 146304 0 0 0 466560 288 0 0 0 0 0 0 0 276138 0 0 0 0 0 0 0 52488...
result:
ok 332 tokens
Test #4:
score: 0
Accepted
time: 65ms
memory: 30076kb
input:
5 1000 0 1000 5 779 779 2 543 840 3 30 477 1 10 295 3 463 741 1 1000 6 379 569 1 249 826 2 194 819 3 90 400 1 614 808 2 102 868 2 1000 8 463 981 1 680 857 3 560 623 1 209 659 1 55 957 2 244 327 1 321 888 3 37 204 3 1000 5 336 851 3 410 676 1 522 911 2 165 962 1 258 330 3
output:
56888193 0 0 0 0
result:
ok 5 tokens
Test #5:
score: 0
Accepted
time: 67ms
memory: 28308kb
input:
5 1000 9 190 765 1 212 745 3 437 798 3 682 814 3 122 289 1 44 821 1 115 448 3 400 936 2 562 639 3 1000 5 561 808 3 23 160 2 57 915 2 211 943 3 125 596 2 1000 1 398 739 2 1000 3 629 973 2 7 721 2 591 815 2 1000 10 536 708 1 217 256 2 690 913 3 418 871 2 302 325 2 334 830 2 496 573 2 396 865 1 240 837...
output:
0 0 722271060 85306976 0
result:
ok 5 tokens
Test #6:
score: 0
Accepted
time: 136ms
memory: 72868kb
input:
2 2000 1 1501 1703 2 2000 5 1230 1521 3 1022 1939 3 1241 1283 3 767 1944 2 303 1163 3
output:
841960641 0
result:
ok 2 tokens
Test #7:
score: 0
Accepted
time: 133ms
memory: 79384kb
input:
2 2000 2 640 1942 1 1473 1917 3 2000 4 490 887 1 670 1163 3 824 1434 2 1221 1746 1
output:
0 0
result:
ok 2 tokens
Test #8:
score: -100
Memory Limit Exceeded
input:
1 5000 9 2821 3377 2 798 1535 2 3563 4161 3 2205 2946 3 1320 1594 3 278 1624 1 1243 2759 3 2882 4939 1 2639 3559 3