QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#571838 | #1286. Ternary String Counting | SFlyer | WA | 1ms | 5700kb | C++14 | 2.2kb | 2024-09-18 09:20:03 | 2024-09-18 09:20:03 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 5e3+3;
const ll mod = 1e9+7;
int n,m,yl[N],yr[N];
ll sx[N],sy[N],dp[N][N];
int mn[N][4],mx[N][4];
void upd(int y,int l,int r){
if (yl[y]<yr[y]){
l=max(l,yl[y]),r=min(r,yr[y]);
if (l<=r){
for (int x=yl[y]; x<l; x++){
sx[x]=(sx[x]-dp[y][x]+mod)%mod;
sy[y]=(sy[y]-dp[y][x]+mod)%mod;
}
for (int x=r+1; x<=yr[y]; x++){
sx[x]=(sx[x]-dp[y][x]+mod)%mod;
sy[y]=(sy[y]-dp[y][x]+mod)%mod;
}
}
else{
for (int x=yl[y]; x<=yr[y]; x++){
sx[x]=(sx[x]-dp[y][x]+mod)%mod;
sy[y]=(sy[y]-dp[y][x]+mod)%mod;
}
}
}
yl[y]=l,yr[y]=r;
}
void solve(){
cin>>n>>m;
for (int i=0; i<=n; i++){
yl[i]=0,yr[i]=i,sx[i]=sy[i]=0;
for (int j=0; j<4; j++)
mn[i][j]=i,mx[i][j]=0;
for (int j=0; j<=n; j++)
dp[i][j]=0;
}
for (int i=1; i<=m; i++){
int l,r,x;
cin>>l>>r>>x;
mn[r][x]=min(mn[r][x],l);
mx[r][x]=max(mx[r][x],l);
}
dp[0][0]=3,sx[0]=sy[0]=3;
ll ans=0;
for (int i=1; i<=n; i++){
int x_l=mx[i][3],x_r=mn[i][2],y_l=mx[i][2],y_r=mn[i][1];
for (int y=0; y<y_l; y++){
upd(y,i,i);
}
for (int y=y_l; y<y_r; y++){
upd(y,x_l,x_r-1);
}
for (int y=y_r; y<i; y++){
upd(y,i,i);
}
if (i==n){
for (int x=x_l; x<x_r; x++){
ans=(ans+sx[x])%mod;
}
break;
}
for (int y=y_l; y<y_r; y++){
dp[i][y]=(dp[i][y]+sy[y])%mod;
}
for (int x=x_l; x<x_r; x++){
dp[i][x]=(dp[i][x]+sx[x])%mod;
}
for (int j=0; j<=i; j++){
sx[j]=(sx[j]+dp[i][j])%mod;
sy[i]=(sy[i]+dp[i][j])%mod;
}
}
cout<<ans<<"\n";
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
while (t--){
solve();
}
return 0;
}
// TRY! TRY! TRY!
/*
Think twice before coding. Have you overkilled?
Think twice before submitting.
Check on the samples and constraints carefully.
*/
/*
Be brave to guess.
Is your former/first approach correct?
Follow your intuition.
Use a notebook to note down your ideas and check whether they are correct.
*/
/*
A simple brute force may work? There is some limit on the answer?
Try to find patterns.
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5700kb
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
Wrong Answer
time: 1ms
memory: 5692kb
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 999999848 0 24300 999999989 3 0 765 3 999999998 846 2916 0 6345 837 0 0 0 0 0 0 0 0 6 999999965 0 0 0 0 87 0 6 0 999999773 6 2916 0 999999863 216 153 0 999999845 999999929 2898 0 0 999999995 999999956 999999872 999999422 0 999999974 0 999999251 243 8730 0 0 0 0 0 999999602 3888 0 0 0 36 12 0 0 ...
result:
wrong answer 3rd words differ - expected: '0', found: '999999848'