QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#420036 | #4887. Fast Bridges | Harry27182 | WA | 2ms | 7720kb | C++14 | 3.6kb | 2024-05-24 14:11:01 | 2024-05-24 14:11:01 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int inv6=166666668,mod=1000000007;
int tot1,tot2,n,m,x[1005],y[1005],ans,e[1005][1005],vis[1005],val[1005];
struct node{int x1,y1,x2,y2;}a[1005],b[1005];vector<int>v[1005];
void Add(int &x,int y){x=(x+y>=mod?x+y-mod:x+y);}
struct DS
{
set<int>s;int val[1005],ans,len;
void init(int x)
{
for(int i=1;i<=tot2;i++)val[i]=0;
s.clear();ans=0;len=x;
}
void upd(int k,int v)
{
if(v==0)return;
if(!s.size()){s.insert(k);val[k]=v;Add(ans,1ll*(y[tot2]-y[k])*v%mod*len%mod);return;}
auto it=s.upper_bound(k);
if(it!=s.begin()&&val[*prev(it)]>=v)return;
if(it!=s.begin())Add(ans,mod-1ll*val[*prev(it)]*((it==s.end()?y[tot2]:y[*it])-y[k])%mod*len%mod);
int flag=0,pos=0;
for(auto it2=it;it2!=s.end();it2=s.erase(it2))
{
if(val[*it2]>v){flag=1;pos=*it2;break;}
Add(ans,mod-1ll*val[*it2]*(next(it2)==s.end()?y[tot2]-y[*it2]:y[*next(it2)]-y[*it2])%mod*len%mod);
}
s.insert(k);val[k]=v;
if(flag==0)Add(ans,1ll*v*(y[tot2]-y[k])%mod*len%mod);
else Add(ans,1ll*v*(y[pos]-y[k])%mod*len%mod);
}
}ds[1005];
void solve(int m)
{
tot1=0,tot2=0;
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<=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;
}
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++)v[i].clear();
for(int i=1;i<=m;i++)v[a[i].x2].emplace_back(i);
for(int i=1;i<tot1;i++)
{
for(int j=1;j<tot2;j++)ds[j].init(y[j+1]-y[j]);
for(int j=1;j<=m;j++)vis[j]=0;
for(int j=i;j<tot1;j++)
{
for(int k=0;k<v[j].size();k++)
{
int x=v[j][k];
if(a[x].x1<=i)continue;
vis[x]=1;
for(int p=1;p<=a[x].y2;p++)val[p]=0;
for(int p=1;p<=m;p++)
{
if(!vis[p])continue;
int w=e[p][x]+1;
if(w<0)continue;
int l=a[p].y1-1,r=a[x].y2;
if(l>r||w==0)continue;
val[l]=max(val[l],w);
}
for(int p=a[x].y2-1;p>=1;p--)val[p]=max(val[p],val[p+1]);
for(int p=1;p<a[x].y2;p++)ds[p].upd(a[x].y2,val[p]);
}
int val=1ll*(x[j+1]-x[j])*(x[i+1]-x[i])%mod,res=0;
for(int p=1;p<tot2;p++)Add(res,ds[p].ans);
Add(ans,1ll*res*val%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=1;
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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 7720kb
input:
2 2 1 1 2 2 1 2 2 1
output:
6
result:
ok answer is '6'
Test #2:
score: -100
Wrong Answer
time: 2ms
memory: 7712kb
input:
0 1000000000
output:
999994519
result:
wrong answer expected '916520226', found '999994519'