首页 > ACM题库 > HDU-杭电 > HDU 4022-Bombing-分治-[解题报告]HOJ
2015
04-15

HDU 4022-Bombing-分治-[解题报告]HOJ

Bombing

问题描述 :

It’s a cruel war which killed millions of people and ruined series of cities. In order to stop it, let’s bomb the opponent’s base.
It seems not to be a hard work in circumstances of street battles, however, you’ll be encountered a much more difficult instance: recounting exploits of the military. In the bombing action, the commander will dispatch a group of bombers with weapons having the huge destructive power to destroy all the targets in a line. Thanks to the outstanding work of our spy, the positions of all opponents’ bases had been detected and marked on the map, consequently, the bombing plan will be sent to you.
Specifically, the map is expressed as a 2D-plane with some positions of enemy’s bases marked on. The bombers are dispatched orderly and each of them will bomb a vertical or horizontal line on the map. Then your commanded wants you to report that how many bases will be destroyed by each bomber. Notice that a ruined base will not be taken into account when calculating the exploits of later bombers.

输入:

Multiple test cases and each test cases starts with two non-negative integer N (N<=100,000) and M (M<=100,000) denoting the number of target bases and the number of scheduled bombers respectively. In the following N line, there is a pair of integers x and y separated by single space indicating the coordinate of position of each opponent’s base. The following M lines describe the bombers, each of them contains two integers c and d where c is 0 or 1 and d is an integer with absolute value no more than 109, if c = 0, then this bomber will bomb the line x = d, otherwise y = d. The input will end when N = M = 0 and the number of test cases is no more than 50.

输出:

Multiple test cases and each test cases starts with two non-negative integer N (N<=100,000) and M (M<=100,000) denoting the number of target bases and the number of scheduled bombers respectively. In the following N line, there is a pair of integers x and y separated by single space indicating the coordinate of position of each opponent’s base. The following M lines describe the bombers, each of them contains two integers c and d where c is 0 or 1 and d is an integer with absolute value no more than 109, if c = 0, then this bomber will bomb the line x = d, otherwise y = d. The input will end when N = M = 0 and the number of test cases is no more than 50.

样例输入:

3 2
1 2
1 3
2 3
0 1
1 3
0 0

样例输出:

2
1

/*
分析:
    快排+二分。

                   2012-10-13
*/

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
struct A
{
	int x,y,index;
}x[100011],y[100011];
int hash[100011];
int cmp_x(const void *a,const void *b)
{
	struct A *c,*d;
	c=(struct A *)a;
	d=(struct A *)b;
	return c->x-d->x;
}
int cmp_y(const void *a,const void *b)
{
	struct A *c,*d;
	c=(struct A *)a;
	d=(struct A *)b;
	return c->y-d->y;
}
int main()
{
	int n,m;
	int i,l;
	int low,mid,up;
	int a,b,c,d;
	int ans;
	while(scanf("%d%d",&n,&m),n||m)
	{
		for(i=0;i<n;i++)
		{
			scanf("%d%d",&a,&b);
			x[i].x=a;
			y[i].y=b;
			x[i].index=y[i].index=i;
		}
		qsort(x,n,sizeof(x[0]),cmp_x);
		qsort(y,n,sizeof(y[0]),cmp_y);

		memset(hash,0,sizeof(hash));
		for(i=0;i<m;i++)
		{
			ans=0;
			scanf("%d%d",&c,&d);
			low=0;up=n-1;mid=(low+up)>>1;
			if(c==0)
			{
				while(low<=up)
				{
					if(x[mid].x<d)	low=mid+1;
					else			up=mid-1;
					mid=(low+up)>>1;
				}
				for(l=low;l<n;l++)
				{
					if(x[l].x!=d)	break;
					if(hash[x[l].index])	continue;
					hash[x[l].index]=1;
					ans++;
				}
			}
			else
			{
				while(low<=up)
				{
					if(y[mid].y<d)	low=mid+1;
					else			up=mid-1;
					mid=(low+up)>>1;
				}
				for(l=low;l<n;l++)
				{
					if(y[l].y!=d)	break;
					if(hash[y[l].index])	continue;
					hash[y[l].index]=1;
					ans++;
				}
			}
			printf("%d\n",ans);
		}

		printf("\n");
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/ice_crazy/article/details/8067906