QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#420263#4887. Fast BridgesHarry27182WA 579ms36616kbC++143.9kb2024-05-24 15:56:482024-05-24 15:56:52

Judging History

你现在查看的是最新测评结果

  • [2024-05-24 15:56:52]
  • 评测
  • 测评结果:WA
  • 用时:579ms
  • 内存:36616kb
  • [2024-05-24 15:56:48]
  • 提交

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],vis[1005],val[1005],dp[2][1005][1005],lst[1005];
struct node{int x1,y1,x2,y2;}a[1005],b[1005];vector<pair<int,int> >v[1005];vector<int>mp[1005][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<=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;
	for(int i=tot2-1;i>=1;i--)
	{
		int p=i&1;
		for(int j=1;j<tot1;j++)for(int k=1;k<=m;k++)dp[p][j][k]=0;
		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];
					dp[p][j][k]=max(dp[p][j][k],e[x][k]+1);
				}
			}
			for(int k=1;k<tot2;k++)v[k].clear();
			for(int k=1;k<=m;k++)
			{
				if(dp[p][j][k]>0)v[a[k].y2].emplace_back(make_pair(a[k].x2,dp[p][j][k]));
			}
			for(int k=1;k<=m;k++)lst[k]=0;
			int res=0;
			for(int k=1;k<tot2;k++)
			{
				for(int j=0;j<v[k].size();j++)
				{
					int len=x[tot1]-x[v[k][j].first],w=v[k][j].second;
					if(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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 32592kb

input:

2 2
1 1 2 2
1 2 2 1

output:

6

result:

ok answer is '6'

Test #2:

score: 0
Accepted
time: 0ms
memory: 32212kb

input:

0 1000000000

output:

916520226

result:

ok answer is '916520226'

Test #3:

score: 0
Accepted
time: 0ms
memory: 32212kb

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: 0
Accepted
time: 7ms
memory: 32120kb

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:

708

result:

ok answer is '708'

Test #5:

score: 0
Accepted
time: 30ms
memory: 34248kb

input:

500 10
5 6 7 10
1 3 8 10
3 3 4 9
2 10 10 2
9 4 10 10
5 4 7 8
7 1 10 7
3 1 7 10
5 2 8 9
6 3 7 10
3 10 7 9
4 9 5 1
2 5 3 3
7 10 8 2
7 7 9 8
6 6 8 3
5 10 8 8
1 1 5 5
3 3 10 5
5 5 7 6
3 8 4 7
6 7 7 5
7 3 10 9
5 3 9 4
4 6 10 5
1 5 9 10
5 6 9 7
3 10 10 3
1 2 5 7
4 6 5 1
3 1 8 5
5 8 8 9
1 8 4 3
6 4 7 10
7 ...

output:

27373

result:

ok answer is '27373'

Test #6:

score: 0
Accepted
time: 31ms
memory: 34544kb

input:

500 30
3 13 20 29
14 5 16 25
2 29 9 15
23 30 24 9
1 18 24 28
4 16 5 2
3 29 30 25
4 8 24 19
8 26 10 24
20 14 26 25
15 8 25 25
5 13 18 28
3 30 29 10
14 26 25 11
11 19 16 4
9 4 29 30
15 10 16 8
2 29 12 2
11 22 20 28
4 10 28 1
24 17 30 1
8 26 27 9
15 25 30 14
16 20 24 17
9 23 12 13
9 16 25 28
2 15 8 16
...

output:

7717993

result:

ok answer is '7717993'

Test #7:

score: 0
Accepted
time: 35ms
memory: 34328kb

input:

500 100
25 55 55 43
14 22 84 5
57 7 79 15
63 9 86 23
22 3 53 97
2 22 64 65
32 52 66 30
76 37 79 22
46 100 76 22
21 78 78 44
29 41 92 55
43 14 46 3
14 97 42 1
16 7 35 64
15 27 29 3
11 92 92 70
4 13 66 2
3 38 55 82
41 94 83 44
52 90 100 82
6 100 99 70
18 38 24 22
74 17 98 20
17 94 44 82
33 97 48 19
12...

output:

291628571

result:

ok answer is '291628571'

Test #8:

score: 0
Accepted
time: 88ms
memory: 33756kb

input:

500 8
2 4 8 2
3 7 5 4
2 6 8 1
4 8 5 5
6 6 7 5
2 6 5 5
1 6 8 5
6 5 7 3
4 8 5 7
5 7 6 5
1 6 4 5
2 3 4 2
2 8 8 6
3 8 4 3
5 6 7 2
7 8 8 3
1 8 4 7
1 6 6 1
1 8 7 1
1 4 3 3
2 3 3 1
1 4 5 1
1 8 5 4
7 7 8 5
2 7 4 1
3 7 4 3
2 3 5 1
3 7 8 1
4 7 5 5
6 6 8 3
2 7 5 1
2 5 4 3
5 4 8 2
4 5 8 3
2 3 4 1
2 8 3 2
5 6 8 ...

output:

9321

result:

ok answer is '9321'

Test #9:

score: -100
Wrong Answer
time: 579ms
memory: 36616kb

input:

500 1000000000
228604634 522874974 789854111 585676486
340802063 175643637 661594207 749079321
490078806 844144515 583746323 707696611
833939453 901516824 867397264 848066012
553537526 886003963 679012061 187030606
351500555 847099665 751201742 855105070
169763646 729114554 248951243 211939611
17040...

output:

258787181

result:

wrong answer expected '230090667', found '258787181'