QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#385365 | #6846. Wiring Engineering | schrodingerstom | WA | 20ms | 14648kb | C++14 | 8.6kb | 2024-04-10 18:04:19 | 2024-04-10 18:04:19 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
bool memBeg;
template<typename T,typename TT> void chkmin(T &x,TT y) {if(x>y) x=y;}
template<typename T,typename TT> void chkmax(T &x,TT y) {if(x<y) x=y;}
constexpr int mod=998244353;
void inc(int &x,int y) {x+=y; x-=(x>=mod)*mod;}
void dec(int &x,int y) {x-=y; x+=(x<0)*mod;}
constexpr int add(int x,int y) {return (x+y>=mod)?x+y-mod:x+y;}
constexpr int sub(int x,int y) {return (x<y)?x-y+mod:x-y;}
constexpr int quick_pow(int x,ll times,int ret=1) {
for(;times;times>>=1,x=1ll*x*x%mod) if(times&1) ret=1ll*ret*x%mod;
return ret;
}
constexpr int maxn=505;
constexpr int maxQ=3e5+5;
constexpr int inf=0x3f3f3f3f;
int n,Q,c1[maxn],c2[maxn],mat[maxn][maxn],qa[maxQ],qb[maxQ],qc[maxQ],qd[maxQ],ret[maxQ];
int lhs[maxn][maxn][2][2],rhs[maxn][maxn][2][2],qry[maxQ];
void clhs(int l1,int r1,int l2,int r2,int tp) {
for(int i=l1;i<=r1+1;i++) {
lhs[i][r2+1][0][0]=lhs[i][r2+1][0][1]=i<=r1&&tp==1?-inf:0;
lhs[i][r2+1][1][0]=lhs[i][r2+1][1][1]=i<=r1?-c1[i]:0;
}
for(int i=l2;i<=r2+1;i++) {
lhs[r1+1][i][0][0]=lhs[r1+1][i][1][0]=i<=r2&&tp==2?-inf:0;
lhs[r1+1][i][0][1]=lhs[r1+1][i][1][1]=i<=r2?-c2[i]:0;
}
// lhs[r1+1][r2+1][0][0]=lhs[r1+1][r2+1][0][1]=0;
// lhs[r1+1][r2+1][1][0]=lhs[r1+1][r2+1][1][1]=0;
// lhs[r1+1][r2][1][1]=lhs[r1+1][r2][0][1]=-c2[r2];
// lhs[r1+1][r2][1][0]=lhs[r1+1][r2][0][0]=-(tp==2)*inf;
// lhs[r1][r2+1][0][0]=lhs[r1][r2+1][0][1]=-(tp==1)*inf;
// lhs[r1][r2+1][1][0]=lhs[r1][r2+1][1][1]=-c1[r1];
// printf("l1 = %d, r1 = %d, l2 = %d, r2 = %d, tp = %d\n",l1,r1,l2,r2,tp);
for(int i=r1;i>=l1;i--) {
for(int j=r2;j>=l2;j--) {
for(int a=1;a>=0;a--) {
for(int b=1;b>=0;b--) {
lhs[i][j][a][b]=-inf;
if(!a) chkmax(lhs[i][j][a][b],lhs[i][j][1][b]);
if(!b) chkmax(lhs[i][j][a][b],lhs[i][j][a][1]);
// printf("lhs[i = %d][j = %d][a = %d][b = %d] = %d\n",i,j,a,b,lhs[i][j][a][b]);
if(tp==1&&i==r1&&!a) continue;
if(tp==2&&j==r2&&!b) continue;
if(!a) chkmax(lhs[i][j][a][b],lhs[i+1][j][0][b]);
if(!b) chkmax(lhs[i][j][a][b],lhs[i][j+1][a][0]);
// printf("lhs[i = %d][j = %d][a = %d][b = %d] = %d\n",i,j,a,b,lhs[i][j][a][b]);
if(a&&b) {
chkmax(lhs[i][j][a][b],lhs[i+1][j][0][1]+mat[i][j]-c1[i]);
// printf("lhs[i = %d][j = %d][a = %d][b = %d] = %d\n",i,j,a,b,lhs[i][j][a][b]);
chkmax(lhs[i][j][a][b],lhs[i][j+1][1][0]+mat[i][j]-c2[j]);
chkmax(lhs[i][j][a][b],lhs[i+1][j+1][0][0]+mat[i][j]-c1[i]-c2[j]);
}
// printf("lhs[i = %d][j = %d][a = %d][b = %d] = %d\n",i,j,a,b,lhs[i][j][a][b]);
}
}
}
}
}
void crhs(int l1,int r1,int l2,int r2,int tp) {
int t=-1;
if(tp==1) t=c1[l1],c1[l1]=0;
if(tp==2) t=c2[l2],c2[l2]=0;
for(int i=l1-1;i<=r1;i++) {
rhs[i][l2-1][0][0]=rhs[i][l2-1][0][1]=0;
rhs[i][l2-1][1][0]=rhs[i][l2-1][1][1]=i>=l1?-c1[i]:0;
}
for(int i=l2-1;i<=r2;i++) {
rhs[l1-1][i][0][0]=rhs[l1-1][i][1][0]=0;
rhs[l1-1][i][0][1]=rhs[l1-1][i][1][1]=i>=l2?-c2[i]:0;
}
// printf("rhs[0][4][0][1] = %d\n",rhs[0][4][0][1]);
// rhs[l1-1][l2-1][0][0]=rhs[l1-1][l2-1][0][1]=0;
// rhs[l1-1][l2-1][1][0]=rhs[l1-1][l2-1][1][1]=0;
// rhs[l1][l2-1][0][0]=rhs[l1][l2-1][0][1]=0;
// rhs[l1][l2-1][1][0]=rhs[l1][l2-1][1][1]=-c1[l1];
// rhs[l1-1][l2][0][0]=rhs[l1-1][l2][1][0]=0;
// rhs[l1-1][l2][0][1]=rhs[l1-1][l2][1][1]=-c2[l2];
// printf("l1 = %d, r1 = %d, l2 = %d, r2 = %d, tp = %d\n",l1,r1,l2,r2,tp);
for(int i=l1;i<=r1;i++) {
for(int j=l2;j<=r2;j++) {
for(int a=1;a>=0;a--) {
for(int b=1;b>=0;b--) {
rhs[i][j][a][b]=-inf;
if(!a) {
chkmax(rhs[i][j][a][b],rhs[i-1][j][0][b]);
chkmax(rhs[i][j][a][b],rhs[i][j][1][b]);
}
if(!b) {
chkmax(rhs[i][j][a][b],rhs[i][j-1][a][0]);
chkmax(rhs[i][j][a][b],rhs[i][j][a][1]);
}
if(a&&b) {
chkmax(rhs[i][j][a][b],rhs[i-1][j][0][1]+mat[i][j]-c1[i]);
// printf("rhs[i = %d][j = %d][a = %d][b = %d] = %d\n",i,j,a,b,rhs[i][j][a][b]);
chkmax(rhs[i][j][a][b],rhs[i][j-1][1][0]+mat[i][j]-c2[j]);
chkmax(rhs[i][j][a][b],rhs[i-1][j-1][0][0]+mat[i][j]-c1[i]-c2[j]);
}
// printf("rhs[i = %d][j = %d][a = %d][b = %d] = %d\n",i,j,a,b,rhs[i][j][a][b]);
}
}
}
}
if(tp==1) c1[l1]=t;
if(tp==2) c2[l2]=t;
}
void solve(int l1,int r1,int l2,int r2,int *stk,int top) {
if(!top) return;
if(l1==r1&&l2==r2) {
int mx=max(0,-c1[l1]-c2[l2]+mat[l1][l2]);
for(int i=1;i<=top;i++) ret[stk[i]]=mx;
return;
}
if(r1-l1<r2-l2) {
int mid=(l2+r2)>>1,ltop=0,rtop=0,key=0;
for(int i=1;i<=top;i++) {
if(qc[stk[i]]>mid) rtop++;
else if(qd[stk[i]]<=mid) ltop++;
else key++;
}
nth_element(stk+1,stk+ltop+1,stk+top+1,[&](int x,int y) {return qd[x]<qd[y];});
nth_element(stk+ltop+1,stk+ltop+key+1,stk+top+1,[&](int x,int y) {return qc[x]<qc[y];});
solve(l1,r1,l2,mid,stk,ltop);
solve(l1,r1,mid+1,r2,stk+ltop+key,rtop);
// printf("line = %d, l1 = %d, r1 = %d, l2 = %d, r2 = %d\n",__LINE__,l1,r1,l2,r2);
int st=ltop+1,ed=ltop+key;
for(int i=l1;i<=r1;i++) {
clhs(l1,i,l2,mid,1); crhs(i,r1,mid+1,r2,1);
for(int j=st;j<=ed;j++) if(qa[stk[j]]<=i&&i<=qb[stk[j]]) {
chkmax(ret[stk[j]],lhs[qa[stk[j]]][qc[stk[j]]][0][0]+rhs[qb[stk[j]]][qd[stk[j]]][0][0]);
// printf("line = %d, i = %d, ret = %d\n",__LINE__,i,ret[stk[j]]);
chkmax(ret[stk[j]],rhs[qb[stk[j]]][qd[stk[j]]][0][0]-c1[i]);
// printf("line = %d, i = %d, ret = %d\n",__LINE__,i,ret[stk[j]]);
}
}
} else {
int mid=(l1+r1)>>1,ltop=0,rtop=0,key=0;
for(int i=1;i<=top;i++) {
if(qa[stk[i]]>mid) rtop++;
else if(qb[stk[i]]<=mid) ltop++;
else key++;
}
nth_element(stk+1,stk+ltop+1,stk+top+1,[&](int x,int y) {return qb[x]<qb[y];});
nth_element(stk+ltop+1,stk+ltop+key+1,stk+top+1,[&](int x,int y) {return qa[x]<qa[y];});
solve(l1,mid,l2,r2,stk,ltop);
solve(mid+1,r1,l2,r2,stk+ltop+key,rtop);
// printf("line = %d, l1 = %d, r1 = %d, l2 = %d, r2 = %d\n",__LINE__,l1,r1,l2,r2);
int st=ltop+1,ed=ltop+key;
for(int i=l2;i<=r2;i++) {
clhs(l1,mid,l2,i,2); crhs(mid+1,r1,i,r2,2);
for(int j=st;j<=ed;j++) if(qc[stk[j]]<=i&&i<=qd[stk[j]]) {
chkmax(ret[stk[j]],lhs[qa[stk[j]]][qc[stk[j]]][0][0]+rhs[qb[stk[j]]][qd[stk[j]]][0][0]);
// printf("lhs = %d\n",lhs[qa[stk[j]]][qc[stk[j]]][0][0]);
// printf("line = %d, i = %d, ret = %d\n",__LINE__,i,ret[stk[j]]);
chkmax(ret[stk[j]],rhs[qb[stk[j]]][qd[stk[j]]][0][0]-c2[i]);
// printf("line = %d, i = %d, ret = %d\n",__LINE__,i,ret[stk[j]]);
}
}
}
}
bool memEn;
void fl() {
freopen(".in","r",stdin);
freopen(".out","w",stdout);
}
int main() {
fprintf(stderr,"%.24lf\n",fabs(&memEn-&memBeg)/1024.0/1024.0);
// fl();
scanf("%d%d",&n,&Q);
for(int i=1;i<=n;i++) scanf("%d",&c1[i]);
for(int i=1;i<=n;i++) scanf("%d",&c2[i]);
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&mat[i][j]);
for(int i=1;i<=Q;i++) scanf("%d%d%d%d",&qa[i],&qb[i],&qc[i],&qd[i]);
iota(qry+1,qry+Q+1,1); solve(1,n,1,n,qry,Q);
// if(Q>1000) {
// printf("%d %d %d %d\n",qa[302],qb[302],qc[302],qd[302]);
// for(int i=1;i<=5;i++) printf("%d ",c1[i]); puts("");
// for(int i=1;i<=5;i++) printf("%d ",c2[i]); puts("");
// for(int i=1;i<=5;i++) {
// for(int j=1;j<=5;j++) {
// printf("%d ",mat[i][j]);
// }
// puts("");
// }
// }
for(int i=1;i<=Q;i++) printf("%d\n",ret[i]);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 10128kb
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: 20ms
memory: 14648kb
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 0 0 0 734 8060 10799 10799 14772 14772 14772 14772 14772 20708 24243 25895 27159 33403 33403 33403 33469 33469 0 0 0 0 0 734 8060 10799 10799 14772 14772 14772 14772 14772 20708 24243 25895 27159 33403 33403 33403 33469 33469 0 0 0 0 402 7728 10467 10467 14440 14440 14440 14440 14440 20376 239...
result:
wrong answer 433rd lines differ - expected: '10345', found: '13462'