QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#420316 | #4887. Fast Bridges | Harry27182 | WA | 4ms | 27896kb | C++14 | 3.0kb | 2024-05-24 16:29:34 | 2024-05-24 16:29:34 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int inv6=166374059,mod=998244353;
int tot1,tot2,n,m,x[1005],y[1005],ans,e[1005][1005],siz[1005],val[1005],dp[2][1005][1005],lst[1005];
struct node{int x1,y1,x2,y2;}a[1005],b[1005];int v[1005][1005];vector<int>mp[1005][1005];
void Add(int &x,int y){x=(x+y>=mod?x+y-mod:x+y);}
bool cmp(node x,node y){return x.y2<y.y2;}
void solve(int m)
{
tot1=0,tot2=0;
sort(a+1,a+m+1,cmp);
for(int i=1;i<=m;i++)
{
x[++tot1]=a[i].x1+1;x[++tot1]=a[i].x2;
y[++tot2]=a[i].y1+1;y[++tot2]=a[i].y2;
}
x[++tot1]=1;x[++tot1]=n+1;y[++tot2]=1;y[++tot2]=n+1;
sort(x+1,x+tot1+1);sort(y+1,y+tot2+1);
tot1=unique(x+1,x+tot1+1)-x-1;tot2=unique(y+1,y+tot2+1)-y-1;
for(int i=1;i<=tot1;i++)for(int j=1;j<=tot2;j++)mp[i][j].clear();
for(int i=1;i<=m;i++)
{
a[i].x1=lower_bound(x+1,x+tot1+1,a[i].x1+1)-x;
a[i].x2=lower_bound(x+1,x+tot1+1,a[i].x2)-x;
a[i].y1=lower_bound(y+1,y+tot2+1,a[i].y1+1)-y;
a[i].y2=lower_bound(y+1,y+tot2+1,a[i].y2)-y;
mp[a[i].x1-1][a[i].y1-1].emplace_back(i);
}
for(int i=1;i<=m;i++)for(int j=1;j<=m;j++)e[i][j]=-0x3f3f3f3f;
for(int i=1;i<=m;i++)e[i][i]=0;
for(int i=1;i<=m;i++)
{
for(int j=1;j<=m;j++)
{
if(i==j)continue;
if(a[i].x2<a[j].x1&&a[i].y2<a[j].y1)e[i][j]=1;
}
}
for(int k=1;k<=m;k++)
{
for(int i=1;i<=m;i++)
{
for(int j=1;j<=m;j++)e[i][j]=max(e[i][j],e[i][k]+e[k][j]);
}
}
for(int i=1;i<=tot1;i++)for(int j=1;j<=m;j++)dp[0][i][j]=dp[1][i][j]=0;
int pos=m+1;
for(int i=tot2-1;i>=1;i--)
{
int p=i&1;
for(int j=tot1-1;j>=1;j--)
{
for(int k=1;k<=m;k++)dp[p][j][k]=max(dp[p][j+1][k],dp[p^1][j][k]);
for(int l=0;l<mp[i][j].size();l++)
{
int x=mp[i][j][l];
for(int k=1;k<=m;k++)
{
if(e[x][k]<0)continue;
dp[p][j][k]=max(dp[p][j][k],e[x][k]+1);
}
}
for(int k=1;k<=m;k++)lst[k]=0;
int res=0;
while(pos>1&&a[pos-1].y2>=i)pos--;
for(int k=i,l=pos;k<tot2;k++)
{
while(l<=m&&a[l].y2==k)
{
int len=x[tot1]-x[a[l].x2],w=dp[p][j][l];l++;
if(w<=0||lst[w]>=len)continue;
Add(res,mod-1ll*lst[w]*(y[tot2]-y[k])%mod);
Add(res,1ll*len*(y[tot2]-y[k])%mod);lst[w]=len;
}
}
Add(ans,1ll*res*(x[i+1]-x[i])%mod*(y[j+1]-y[j])%mod);
}
}
}
int main()
{
//freopen("line.in","r",stdin);
//freopen("line.out","w",stdout);
cin.tie(0)->sync_with_stdio(0);
cin>>m>>n;int flag=0;
for(int i=1;i<=m;i++)cin>>b[i].x1>>b[i].y1>>b[i].x2>>b[i].y2,flag&=(b[i].y1<b[i].y2);
for(int i=1;i<m;i++)flag&=(b[i].x2<=b[i+1].x1&&b[i].y2<=b[i+1].y1);
if(flag==1)
{
for(int i=1;i<=m;i++)Add(ans,1ll*b[i].x1*b[i].y1%mod*(n-b[i].x2+1)%mod*(n-b[i].y2+1)%mod);
}
else
{
int cnt=0;
for(int i=1;i<=m;i++)if(b[i].x1!=b[i].x2&&b[i].y1<b[i].y2)a[++cnt]=b[i];
solve(cnt);cnt=0;
for(int i=1;i<=m;i++)b[i].y1=n-b[i].y1+1,b[i].y2=n-b[i].y2+1;
for(int i=1;i<=m;i++)if(b[i].x1!=b[i].x2&&b[i].y1<b[i].y2)a[++cnt]=b[i];
solve(cnt);
}
int res=1ll*n*n%mod*(n-1)%mod*n%mod*(n+1)%mod*inv6%mod*2%mod;
res=(res-ans+mod)%mod;
cout<<res;
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 3ms
memory: 27332kb
input:
2 2 1 1 2 2 1 2 2 1
output:
6
result:
ok answer is '6'
Test #2:
score: 0
Accepted
time: 4ms
memory: 27300kb
input:
0 1000000000
output:
916520226
result:
ok answer is '916520226'
Test #3:
score: 0
Accepted
time: 4ms
memory: 27364kb
input:
5 5 1 1 3 3 3 3 5 1 3 3 4 5 3 3 5 4 1 5 3 3
output:
946
result:
ok answer is '946'
Test #4:
score: -100
Wrong Answer
time: 3ms
memory: 27896kb
input:
200 5 1 1 4 2 2 5 4 4 2 3 4 2 2 4 3 5 1 4 4 2 2 5 4 2 1 2 4 4 1 2 2 4 1 4 5 1 3 4 5 1 4 2 5 1 2 2 5 4 3 2 5 1 3 1 5 2 4 2 5 3 1 3 5 1 3 4 4 5 2 2 4 3 2 3 5 4 1 4 5 3 2 2 3 1 2 5 3 3 1 1 5 3 4 4 5 5 1 3 4 4 4 3 5 1 2 3 3 4 3 4 4 2 1 4 4 5 2 1 4 4 1 3 5 2 1 1 3 3 1 5 3 1 1 1 3 5 1 4 3 5 4 5 5 4 1 1 4 ...
output:
718
result:
wrong answer expected '708', found '718'