QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#385303 | #6846. Wiring Engineering | schrodingerstom | WA | 20ms | 14636kb | C++14 | 6.9kb | 2024-04-10 17:27:01 | 2024-04-10 17:27:01 |
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) {
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][0][0]=0; lhs[r1+1][r2][0][1]=-inf;
lhs[r1+1][r2][1][0]=0; lhs[r1+1][r2][1][1]=-inf;
lhs[r1][r2+1][0][0]=lhs[r1][r2+1][0][1]=0;
lhs[r1][r2+1][1][0]=lhs[r1][r2+1][1][1]=-inf;
// 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]);
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]);
if(a&&b) {
chkmax(lhs[i][j][a][b],lhs[i+1][j][0][1]+mat[i][j]-c1[i]);
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) {
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]=-inf;
rhs[l1-1][l2][0][0]=0; rhs[l1-1][l2][0][1]=-inf;
rhs[l1-1][l2][1][0]=0; rhs[l1-1][l2][1][1]=-inf;
// printf("l1 = %d, r1 = %d, l2 = %d, r2 = %d, tp = %d\n",l1,r1,l2,r2,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;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]);
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]);
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);
for(int i=1;i<=Q;i++) printf("%d\n",ret[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 12144kb
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: 14636kb
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 3649 3649 4537 11863 14602 14602 18575 18575 22989 29538 29538 35474 39009 40661 41925 48169 48169 48169 48235 48235 0 0 0 3649 3649 4537 11863 14602 14602 18575 18575 22989 29538 29538 35474 39009 40661 41925 48169 48169 48169 48235 48235 0 0 1115 1115 3088 10414 13153 13153 17126 17126 215...
result:
wrong answer 5th lines differ - expected: '0', found: '3649'