1232. 三体攻击
$5$页 $WA$ 换来的 $AC$
#include<bits/stdc++.h>
using namespace std;
int a,b,c,m;
const int N=2000010;
struct A{
int la,ra,lb,rb,lc,rc;
long long ht;
}q[N];
struct Node{
int i,j,k;
long long w;
}t[N],s[N],cs[N],f[N];
int turn2(int i,int j,int k,int ii,int jj,int kk){
if(i+ii==0||j+jj==0||k+kk==0)return 0;
if(i+ii>a||j+jj>b||k+kk>c)return 0;
return ((i+ii-1)*b+(j+jj-1))*c+kk+k;
}
int turn(struct Node w,int i,int j,int k){
if(w.i+i==0||w.j+j==0||w.k+k==0)return 0;
if(w.i+i>a||w.j+j>b||w.k+k>c)return 0;
return ((w.i+i-1)*b+(w.j+j-1))*c+w.k+k;
}
bool change(){
bool flag=false;
memcpy(f,cs,sizeof cs);
cs[0].w=0;
for(int i=1;i<=a*b*c;i++){
cs[i].w=cs[i].w
-cs[turn(cs[i],0,-1,-1)].w
+cs[turn(cs[i],-1,0,0)].w
-cs[turn(cs[i],-1,0,-1)].w
+cs[turn(cs[i],0,-1,0)].w
-cs[turn(cs[i],-1,-1,0)].w
+cs[turn(cs[i],0,0,-1)].w
+cs[turn(cs[i],-1,-1,-1)].w;
if(cs[i].w<0)flag=true;
}
if(flag==false)memcpy(s,f,sizeof f);
return flag;
}
bool check(int l,int r){
memcpy(cs,s,sizeof s);
for(int i=l;i<=r;i++){
cs[ turn2(q[i].la,q[i].lb,q[i].lc,0,0,0) ].w-=q[i].ht;
cs[ turn2(q[i].ra,q[i].rb,q[i].rc,1,1,1) ].w+=q[i].ht;
cs[ turn2(q[i].ra,q[i].lb,q[i].lc,1,0,0) ].w+=q[i].ht;
cs[ turn2(q[i].la,q[i].rb,q[i].lc,0,1,0) ].w+=q[i].ht;
cs[ turn2(q[i].la,q[i].lb,q[i].rc,0,0,1) ].w+=q[i].ht;
cs[ turn2(q[i].ra,q[i].rb,q[i].lc,1,1,0) ].w-=q[i].ht;
cs[ turn2(q[i].la,q[i].rb,q[i].rc,0,1,1) ].w-=q[i].ht;
cs[ turn2(q[i].ra,q[i].lb,q[i].rc,1,0,1) ].w-=q[i].ht;
}
return change();
}
int main(){
cin>>a>>b>>c>>m;
memset(s,0,sizeof s);
int n=1;
for(int i=1;i<=a;i++){
for(int j=1;j<=b;j++){
for(int k=1;k<=c;k++){
t[n]={i,j,k};
scanf("%lld",&t[n++].w);
}
}
}
for(int i=1;i<=a*b*c;i++){
s[i]={t[i].i,t[i].j,t[i].k};
s[i].w=t[i].w
-t[turn(t[i],0,0,-1)].w
+t[turn(t[i],-1,-1,0)].w
-t[turn(t[i],-1,0,0)].w
+t[turn(t[i],-1,0,-1)].w
-t[turn(t[i],0,-1,0)].w
+t[turn(t[i],0,-1,-1)].w
-t[turn(t[i],-1,-1,-1)].w;
}
int la,ra,lb,rb,lc,rc;
long long ht;
for(int i=1;i<=m;i++){
scanf("%d%d%d%d%d%d%lld", &la,&ra,&lb,&rb,&lc,&rc,&ht);
q[i]={la,ra,lb,rb,lc,rc,ht};
}
int l=1,r=m;
while(l<r){
int mid=r+l>>1;
if(!check(l,mid))l=mid+1;
else r=mid;
}
cout<<r<<endl;
return 0;
}