HDU 3490 推公式

Manhattan Distance

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)



Problem Description
The kingdom of Henryy is the most civilized country in the world. Meanwhile, the capital city H is as well the most beautiful city on the earth. Currently, the city is to build up database to maintain the statistics of the buildings in the city.
You are a little P in the company that takes this project. Your task is to calculate the maximum distance between two buildings in the city.
The Manhattan Distance is defined as: for two points P1(x1, y1) and P2(x2, y2) on a two dimension Cartesian plane, the Manhattan Distance D(P1, P2) = |x1-x2|+|y1-y2|.
Your database should support this query: after updating a data that a building has built or demolished, it should return the maximum distance between existed buildings:

HDU 3490 推公式_第1张图片


N is the current number of buildings, and Pi is the coordinates of the ith building.
 

Input
The first line contains an integer T (T<=10), indicating the number of the test cases.
For each test case, the first line contains an integer M (M<=100000), the number of records of the changes of buildings.
Then M lines followed, each line starting with a positive integer: 1 for building that has built, and 2 for demolished. A string with less than 9 characters follows, indicating the name of the building. If this building is just built, two integers of the coordinates of this building are given afterwards.
Notice that no two buildings share the same name, even when one of these buildings has been demolished, but might share the same coordinates. It is also guaranteed that the building exists when a demolishing record is given.
A blank line is followed after each test case.
 

Output
For each record, output an integer in a single line, indicating the maximum Manhattan Distance among existing buildings.
Print a blank line after each test case.
 

Sample Input
 
   
1 8 1 mgj -2 1 2 mgj 1 kmoaktmr 4 -4 1 mauxizu 3 -2 2 kmoaktmr 1 md -1 4 2 md 1 umos -5 0
 Sample Output
 
   
0 -1 0 3 0 10 0 10

题意:给你n个操作,1代表在(x,y)这个坐标修建一个城市,2代表拆除一个城市,每次操作后,输出图中城市间的最大曼哈顿距离,如果没有城市,输出-1.


题解:对于(x1-x2)+(y1-y2),最多有两种可能,两者同号或者异号。
当同号时|x1-x2|+|y1-y2|=(x1+y1)-(x2+y2)。
当异号时|x1-x2|+|y1-y2|=(x1-y1)-(x2-y2)。
所以我们可以用map保存城市的坐标,用两个multiset保存每个城市的x-y和x+y,对于每次操作后,输出两个multiset中最小元素和最大元素的差的最大值即可。代码是实现思想的,很容易理解,就不加注释了。
注意题目要每个测试样例输出一行空行。



#include
#include
#include
#include
#include
#include
using namespace std;
multisetsp1,sp2;
struct node{
	int l,r;
}e[100005];
int cnt;
mapspp;
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		cnt=0;
		spp.clear();
		sp1.clear();
		sp2.clear();
		int x,n,i;
		char c[11];
		scanf("%d",&n);
		for(i=1;i<=n;i++){
			scanf("%d%s",&x,c);
			multiset::iterator it;
			if(x==1){
				int l,r;
				scanf("%d%d",&l,&r);
				spp[c]=++cnt;
				e[cnt].l=l;
				e[cnt].r=r;
				sp1.insert(l-r);
				sp2.insert(l+r);
			}
			else{
				int lab=spp[c];
				int l=e[lab].l,r=e[lab].r;
				it=sp1.find(l-r);
				sp1.erase(it);
				it=sp2.find(l+r);
				sp2.erase(it);
			}
			if(sp1.empty()||sp2.empty()){
				printf("-1\n");
				continue;
			}
			it=sp1.begin();
			int mu1=*it;
			it=sp1.end();
			it--;
			int mu2=*it;
			it=sp2.begin();
			int mu3=*it;
			it=sp2.end();
			it--;
			int mu4=*it;
			printf("%d\n",max(mu2-mu1,mu4-mu3));
		}
		printf("\n"); 
	}
	return 0;
}



你可能感兴趣的:(推公式)