D Fenwick tree violence seek Manhattan distance most value - cattle off more school eighth-field D

It involved with a lot of knowledge, but mostly routine

1. seeking the most value of the Manhattan distance discussed generally in all cases

2. The three-dimensional array is used to request the prefix tree maximum

/ * 
There is a three dimensional coordinate system (x, y, z), in the range [1, n], [1 , m], [1, h], has two operating 
1. Update on a three dimensional coordinate system point (X1, Y1, Z1) 
2. given a point (x2, y2, z2), the point Q from the point of minimum Manhattan distance on the coordinate system 
    that is the smallest | x2-x1 | + | y2 -y1 | + | z2-z1 | 
order f = | x2-x1 | + | y2-y1 | + | z2-z1 |, then the absolute value of f to be discussed after eight situation 
f0 = (x2 + y2 + z2 ) - (x1 + Y1 + Z1), X2> = X1, Y2> = Y1, Z2> = Z1 
F1 = (X2 + Y2-Z2) - (X1 + Y1-Z1), X2> = X1, Y2> = Y1, X2 < X1 
... 
    consider how to find the minimum value in each case 
    because the value of x2 + y2 + z2 are fixed, the maximum required only qualified x1 + y1 + z1 can be found x1 <= x2 && y1 < = y2 && z1 <= z2 just this condition can be maintained by an array of three-dimensional tree (maximum seek prefix, a single point of update) 
    the same eight kinds of eight cases can be used to maintain the three-dimensional tree-like array of 
    additional conditions need to be considered f1 converting x1> x2 to + 1 <= n-x2 + 1 is then updated n-x1 x1 + y1-z1 , the query result is the greatest x1 + y1-z1 
    otherwise Similarly 
    (since n * m * h <= 1e5, so that a turn with a three-dimensional way to store) 
* / 
#include <bits / STDC ++. H>
using namespace std;
#define maxn 300005
void update(int &a,int b){a=min(a,b);}
void Max(int &a,int b){a=max(a,b);}
int n,m,h,q;
struct Bit{
    int b[maxn];
    void init(){memset(b,-0x3f,sizeof b);}
    inline int id(int x,int y,int z){return x*m*h+y*h+z;} 
    inline int lowbit(int x){return x&-x;}
    void update(int x,int y,int z,int val){//在[x,y,z]出更新值val 
        for(int i=x;i<=n;i+=lowbit(i))
            for(int j=y;j<=m;j+=lowbit(j))
                for(int k=z;k<=h;k+=lowbit(k))
                    Max(b[id(i,j,k)],val);
    }
    int query(int x,int y,int z){//查询<=x,<=y,<=z的最大值 
        int res=-0x3f3f3f3f;
        for(int i=x;i;i-=lowbit(i))
            for(int j=y;j;j-=lowbit(j))
                for(int k=z;k;k-=lowbit(k))
                    Max(res,b[id(i,j,k)]);
        return res;        
    }
}bit[10];

int main(){
    for(int i=0;i<10;i++)
        bit[i].init();
    cin>>n>>m>>h>>q;
    int x,y,z,op;
    while(q--){
        scanf("%d%d%d%d",&op,&x,&y,&z);
        if(op==1){//更新 
            bit[0].update(x,y,z,x+y+z);
            bit[1].update(x,y,h-z+1,x+y-z);
            bit[2].update(x,m-y+1,z,x-y+z);
            bit[3].update(n-x+1,y,z,-x+y+z);
            bit[4].update(x,m-y+1,h-z+1,x-y-z);
            bit[5].update(n-x+1,y,h-z+1,-x+y-z);
            bit[6].update(n-x+1,m-y+1,z,-x-y+z);
            bit[7].update(n-x+1,m-y+1,h-z+1,-x-y-z); 
        }
        else {
            int ans=0x3f3f3f3f;
            update(ans,x+y+z-bit[0].query(x,y,z));
            update(ans,x+y-z-bit[1].query(x,y,h-z+1));
            update(ans,x-y+z-bit[2].query(x,m-y+1,z));
            update(ans,-x+y+z-bit[3].query(n-x+1,y,z));
            update(ans,x-y-z-bit[4].query(x,m-y+1,h-z+1));
            update(ans,-x+y-z-bit[5].query(n-x+1,y,h-z+1));
            update(ans,-x-y+z-bit[6].query(n-x+1,m-y+1,z));
            update(ans,-x-y-z-bit[7].query(n-x+1,m-y+1,h-z+1));
            cout<<ans<<'\n';
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/zsben991126/p/11334954.html