QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#865410#6846. Wiring Engineering2020zymWA 21ms20508kbC++144.2kb2025-01-21 18:05:202025-01-21 18:05:20

Judging History

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

  • [2025-01-21 18:05:20]
  • 评测
  • 测评结果:WA
  • 用时:21ms
  • 内存:20508kb
  • [2025-01-21 18:05:20]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int n,Q,A1[310],A2[310],w[310][310],tot,h1[500010],h2[500010],vis[500010],cnt1,cnt2,fl,deg[500010],bian[500010];
int f[310][310],g[310][310],H[310][310];
long long dis1[500010],dis2[500010],Ans[100010];
struct node2{int s,t,w;}a1[10000010],a2[10000010];
void add1(int x,int y,int v){a1[++cnt1].s=h1[x],a1[cnt1].t=y,a1[cnt1].w=v,h1[x]=cnt1;}
void add2(int x,int y,int v){a2[++cnt2].s=h2[x],a2[cnt2].t=y,a2[cnt2].w=v,h2[x]=cnt2;}
void Add(int x,int y,int v){
	if(!x||!y)return;
	add1(x,y,v),add2(y,x,v);
}
struct node{int l1,r1,l2,r2,pos;};
void dfs1(int x){
	dis1[x]=-1e18,bian[x]=1;
	for(int i=h1[x];i;i=a1[i].s)
		if(vis[a1[i].t]){
			deg[a1[i].t]++;
			if(!bian[a1[i].t])
				dfs1(a1[i].t);
		}
}
void dfs2(int x){
	dis2[x]=-1e18,bian[x]=1;
	for(int i=h2[x];i;i=a2[i].s){
		if(vis[a2[i].t]){
			deg[a2[i].t]++;
			if(!bian[a2[i].t])
				dfs2(a2[i].t);
		}
	}
}
queue<int>q;
void work1(int st){
	dfs1(st),q.push(st);
	dis1[st]=0;
	while(!q.empty()){
		int x=q.front();
		q.pop(),bian[x]=0;
		for(int i=h1[x];i;i=a1[i].s)
			if(vis[a1[i].t]){
				deg[a1[i].t]--;
				dis1[a1[i].t]=max(dis1[a1[i].t],dis1[x]+a1[i].w);
				if(deg[a1[i].t]==0)q.push(a1[i].t);
			}
	}
}
void work2(int st){
	dfs2(st);
	q.push(st);
	dis2[st]=0;
	while(!q.empty()){
		int x=q.front();
		q.pop(),bian[x]=0;
		for(int i=h2[x];i;i=a2[i].s)
			if(vis[a2[i].t]){
				deg[a2[i].t]--;
				dis2[a2[i].t]=max(dis2[a2[i].t],dis2[x]+a2[i].w);
				if(deg[a2[i].t]==0)q.push(a2[i].t);
			}
	}
}
void solve(int l1,int r1,int l2,int r2,vector<node>z){
	if((int)z.size()==0)return;
	if(l1==r1){
		for(node i:z){
			long long op=-A1[l1];
			for(int j=i.l2;j<=i.r2;j++)op+=max(0,w[l1][j]-A2[j]);
			Ans[i.pos]=max(Ans[i.pos],op);
		}
		return;
	}
	if(l2==r2){
		for(node i:z){
			long long op=-A2[l2];
			for(int j=i.l1;j<=i.r1;j++)op+=max(0,w[j][l2]-A1[j]);
			Ans[i.pos]=max(Ans[i.pos],op);
		}
		return;
	}
	vector<node>z1,z2,z3,z4,z5,z6;
	int mid1=(l1+r1)>>1,mid2=(l2+r2)>>1;
	for(node i:z){
		if(i.l1<=mid1&&mid1+1<=i.r1)z5.push_back(i);
		else if(i.l2<=mid2&&mid2+1<=i.r2)z6.push_back(i);
		else{
			if(i.r1<=mid1&&i.r2<=mid2)z1.push_back(i);
			if(i.r1<=mid1&&i.l2>mid2)z2.push_back(i);
			if(i.l1>mid1&&i.r2<=mid2)z3.push_back(i);
			if(i.l1>mid1&&i.r2>mid2)z4.push_back(i);
		}
	}
	for(int i=l1;i<=r1+1;i++)
		for(int j=l2;j<=r2+1;j++)vis[f[i][j]]=vis[g[i][j]]=vis[H[i][j]]=1;
	if((int)z5.size()>0){
		for(int i=l2;i<=r2;i++){
			int d=H[mid1][i];
			work1(d);
			work2(d);
			for(node j:z5){
				Ans[j.pos]=max(Ans[j.pos],dis2[f[j.l1][j.l2]]+dis1[f[j.r1+1][j.r2+1]]);
			}
		}	
	}	
	if((int)z6.size()>0){
		for(int i=l1;i<=r1;i++){
			int d=g[i][mid2];
			work1(d),work2(d);
			for(node j:z6)Ans[j.pos]=max(Ans[j.pos],dis2[f[j.l1][j.l2]]+dis1[f[j.r1+1][j.r2+1]]);
		}
	}
	for(int i=l1;i<=r1+1;i++)
		for(int j=l2;j<=r2+1;j++)vis[f[i][j]]=vis[g[i][j]]=vis[H[i][j]]=0;
	solve(l1,mid1,l2,mid2,z1);
	solve(l1,mid1,mid2+1,r2,z2);
	solve(mid1+1,r1,l2,mid2,z3);
	solve(mid1+1,r1,mid2+1,r2,z4);
	vector<node>().swap(z1);
	vector<node>().swap(z2);
	vector<node>().swap(z3);
	vector<node>().swap(z4);
	vector<node>().swap(z5);
	vector<node>().swap(z6);
}
int main(){
//	freopen("tower.in","r",stdin);
//	freopen("tower.out","w",stdout);
	scanf("%d%d",&n,&Q);
	for(int i=1;i<=n;i++)scanf("%d",&A1[i]);
	for(int i=1;i<=n;i++)scanf("%d",&A2[i]);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)scanf("%d",&w[i][j]);
	for(int i=1;i<=n+1;i++)
		for(int j=1;j<=n+1;j++)
			f[i][j]=++tot,g[i][j]=++tot,H[i][j]=++tot; 
	for(int i=1;i<=n+1;i++){
		for(int j=1;j<=n+1;j++){
			Add(f[i-1][j],f[i][j],0);
			Add(f[i][j-1],f[i][j],0);
			Add(g[i-1][j],f[i][j],0);
			Add(H[i][j-1],f[i][j],0);
			if(i<=n){
				Add(f[i][j],g[i][j],-A1[i]);
				Add(g[i][j-1],g[i][j],max(w[i][j-1]-A2[j-1],0));
				Add(H[i][j-1],g[i][j],w[i][j-1]-A1[i]);
			}
			if(j<=n){
				Add(f[i][j],H[i][j],-A2[j]);
				Add(H[i-1][j],H[i][j],max(w[i-1][j]-A1[i-1],0));
				Add(g[i-1][j],H[i][j],w[i-1][j]-A2[j]);
			}
		}
	}
	vector<node>z;
	for(int i=1;i<=Q;i++){
		int l1,r1,l2,r2;
		scanf("%d%d%d%d",&l1,&r1,&l2,&r2);
		z.push_back((node){l1,r1,l2,r2,i});
	}
	solve(1,n,1,n,z);
	for(int i=1;i<=Q;i++)printf("%lld\n",Ans[i]);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 14172kb

input:

3 4
1 2 1
2 1 2
1 2 3
4 5 6
3 2 1
1 3 1 3
2 3 1 2
1 1 2 3
1 2 2 3

output:

8
5
1
7

result:

ok 4 lines

Test #2:

score: -100
Wrong Answer
time: 21ms
memory: 20508kb

input:

24 90000
8793 8115 9643 2814 6394 7070 3822 4788 6737 6506 2901 4772 5347 5050 3493 2803 584 2544 3834 678 9891 2958 5475 522
9057 3674 3163 6433 5937 8480 4815 1201 5509 1303 4151 8190 6229 9339 9765 3011 2256 3682 8442 3641 2268 5609 4948 9632
5872 4006 7690 2611 5381 6184 9483 8527 8248 960 8124 ...

output:

0
0
0
5369
5369
5369
36650
43976
46715
46715
50688
50688
109829
109829
109829
115765
119300
120952
122216
128460
128460
128460
128526
128526
0
0
5369
5369
5369
36650
43976
46715
46715
50688
50688
109829
109829
109829
115765
119300
120952
122216
128460
128460
128460
128526
128526
0
1713
1713
1713
315...

result:

wrong answer 4th lines differ - expected: '0', found: '5369'