HDU 4022 Bombing

来源:互联网 发布:乳液面霜推荐知乎 编辑:程序博客网 时间:2024/05/16 08:00
Problem Description
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.
 

Input
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.
 

Output
For each test case, output M lines, the ith line contains a single integer denoting the number of bases that were destroyed by the corresponding bomber in the input. Output a blank line after each test case.
 

Sample Input
3 21 21 32 30 11 30 0
 

Sample Output
21
题目大意:
派遣一个飞起,在一个二维分布的地图上轰炸,轰炸的范围是坐标点的横轴或者是纵轴,然后输出一行或一列所轰炸的基础设施的个数。
解题思路:
本题运用了map中的一对多的方式,再简单的一对一基础上用上了multiset,然后输入基础设施的个数n,飞机的个数m,然后在对应输入每个基础设施的坐标xy,随后输入C来判断轰炸行还是轰炸列,0代表列,1代表行,用输入的d来表示轰炸范围
#include<stdio.h>#include<iostream>#include<set>#include<map>#include<algorithm>using namespace std;typedef map<int,multiset<int> > line;//建立一个一对多的 map<int,multiset<int> >mx;//定义x坐标对应的mapmap<int,multiset<int> >my;//定义y坐标对应的mapint bomb(line &x,line &y,int pos){    int ret=x[pos].size();//得到x在上pos的大小     multiset<int>::iterator it;//迭代器     for(it=x[pos].begin();it!=x[pos].end();it++)//遍历     {    y[*it].erase(pos);//multiset 去除一个元素    }           x[pos].clear();//清空multiset    return ret;}int main(){int i;     int n,m;    int c,d;    int tx,ty;    while(scanf("%d%d",&n,&m)!=EOF&&m+n>0)//输入m和n     {        mx.clear();//mx清零         my.clear();//my清零         for(i=0;i<n;i++)//循环插入         {            scanf("%d%d",&tx,&ty);            mx[tx].insert(ty);            my[ty].insert(tx);        }        for(i=0;i<m;i++)        {            scanf("%d%d",&c,&d);            int ans;            if(c==0)//如果C等于0 {ans=bomb(mx,my,d);}             else             {            ans=bomb(my,mx,d);            }            printf("%d\n",ans);        }        printf("\n");    }    return 0;}


0 0