HDU-4666 Hyperspace 曼哈顿距离

  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4666

  题意:动态的增加或者删掉k维空间的点,求每次操作后剩下的点集中的最大的麦哈顿距离。

  如果是一维情况很好做,直接用个数据结构来维护就行了,那么多维情况怎么办?其实多维情况是可以降到一维情况的。考虑二维的情况:|xi-xj|+|yi-yj|,我们展开绝对值之后,就可以得到四个式子:(xi+yi)-(xj+yj), (-xi+yi)-(-xj+yj), (xi-yi)-(xj-yj), (-xi-yi)-(-xj-yj),根据不等式 |x|+|y|>=x+y,那么我们对所有的点求出(xi+yi),(xi-yi),(-xi+yi),(-xi-yi)这四个值,这里用状态s表示,总共有4种状态,00,01,10,11,0表示符号为正,1表示符号为负。那么分别对这四种状态求出最大值和最小值的差ans[i],那么最大的ans[i]就是答案了。多维情况类推。因为这里是动态的,所以我们维护一颗树就行了,堆,线段树。。。复杂度O(n*2^k*logn).

  1 //STATUS:C++_AC_5484MS_60452KB
  2 #include <functional>
  3 #include <algorithm>
  4 #include <iostream>
  5 //#include <ext/rope>
  6 #include <fstream>
  7 #include <sstream>
  8 #include <iomanip>
  9 #include <numeric>
 10 #include <cstring>
 11 #include <cassert>
 12 #include <cstdio>
 13 #include <string>
 14 #include <vector>
 15 #include <bitset>
 16 #include <queue>
 17 #include <stack>
 18 #include <cmath>
 19 #include <ctime>
 20 #include <list>
 21 #include <set>
 22 #include <map>
 23 using namespace std;
 24 //#pragma comment(linker,"/STACK:102400000,102400000")
 25 //using namespace __gnu_cxx;
 26 //define
 27 #define pii pair<int,int>
 28 #define mem(a,b) memset(a,b,sizeof(a))
 29 #define lson l,mid,rt<<1
 30 #define rson mid+1,r,rt<<1|1
 31 #define PI acos(-1.0)
 32 //typedef
 33 typedef __int64 LL;
 34 typedef unsigned __int64 ULL;
 35 //const
 36 const int N=60010;
 37 const int INF=0x3f3f3f3f;
 38 //const LL MOD=1000000007,STA=8000010;
 39 const LL LNF=1LL<<55;
 40 const double EPS=1e-9;
 41 const double OO=1e30;
 42 const int dx[4]={-1,0,1,0};
 43 const int dy[4]={0,1,0,-1};
 44 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
 45 //Daily Use ...
 46 inline int sign(double x){return (x>EPS)-(x<-EPS);}
 47 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
 48 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
 49 template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
 50 template<class T> inline T Min(T a,T b){return a<b?a:b;}
 51 template<class T> inline T Max(T a,T b){return a>b?a:b;}
 52 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
 53 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
 54 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
 55 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
 56 //End
 57 
 58 int x[5];
 59 int hig[32][N<<2];
 60 int low[32][N<<2];
 61 
 62 int n,k,tot,wi,wx,tar;
 63 
 64 void update_hig(int l,int r,int rt)
 65 {
 66     if(l==r){hig[wi][rt]=tar;return;}
 67     int mid=(l+r)>>1;
 68     if(wx<=mid)update_hig(lson);
 69     else update_hig(rson);
 70     hig[wi][rt]=Max(hig[wi][rt<<1],hig[wi][rt<<1|1]);
 71 }
 72 
 73 void update_low(int l,int r,int rt)
 74 {
 75     if(l==r){low[wi][rt]=tar;return;}
 76     int mid=(l+r)>>1;
 77     if(wx<=mid)update_low(lson);
 78     else update_low(rson);
 79     low[wi][rt]=Min(low[wi][rt<<1],low[wi][rt<<1|1]);
 80 }
 81 
 82 int main(){
 83  //   freopen("in.txt","r",stdin);
 84     int i,j,op,cnt,p,sum,ans;
 85     while(~scanf("%d%d",&n,&k))
 86     {
 87         tot=1<<k;
 88         cnt=0;
 89         for(i=0;i<tot;i++){
 90             mem(hig[i],-INF);
 91             mem(low[i],INF);
 92         }
 93         for(i=1;i<=n;i++){
 94             scanf("%d",&op);
 95             op?cnt--:cnt++;
 96             if(op){
 97                 scanf("%d",&wx);
 98                 for(wi=0;wi<tot;wi++){
 99                     tar=-INF;
100                     update_hig(1,n,1);
101                     tar=INF;
102                     update_low(1,n,1);
103                 }
104             }
105             else {
106                 for(j=0;j<k;j++)
107                     scanf("%d",&x[j]);
108                 wx=i;
109                 for(wi=0;wi<tot;wi++){
110                     for(sum=p=0;p<k;p++){
111                         if(wi&(1<<p))sum+=x[p];
112                         else sum-=x[p];
113                     }
114                     tar=sum;
115                     update_hig(1,n,1);
116                     update_low(1,n,1);
117                 }
118             }
119 
120             if(cnt<=1){
121                 puts("0");
122                 continue;
123             }
124             ans=-INF;
125             for(j=0;j<tot;j++){
126                 ans=Max(ans,hig[j][1]-low[j][1]);
127             }
128             printf("%d
",ans);
129         }
130     }
131     return 0;
132 }
原文地址:https://www.cnblogs.com/zhsl/p/3256112.html