hdu 4022 map的一对多实现 map和set的结合应用

来源:互联网 发布:双系统ubuntu引导恢复 编辑:程序博客网 时间:2024/05/14 15:29

Bombing

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1471    Accepted Submission(s): 551


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
//题目大意:在二维平面上,给一些点。有两个操纵:输入flag 和一个d,flag=0把所有x为d的点去掉,否则把所有y为d的点去掉,问每次去掉的点。
 
//map的一对多#include<stdio.h>#include<map>#include<set>using namespace std;int main(){int i,j,n,m;while(scanf("%d %d",&n,&m)!=EOF){map<int,multiset<int> >xs,ys;for(i=0;i<n;i++){int x,y;scanf("%d %d",&x,&y);xs[x].insert(y);ys[y].insert(x);}for(i=0;i<m;i++){int flag,k;scanf("%d %d",&flag,&k);if(flag){printf("%d\n",ys[k].size());multiset<int>::iterator it;for(it=ys[k].begin();it!=ys[k].end();it++)     xs[*it].erase(k);ys[k].clear();}else{printf("%d\n",xs[k].size());multiset<int>::iterator it;for(it=xs[k].begin();it!=xs[k].end();it++)     ys[*it].erase(k);xs[k].clear();}}if(!n&&!m) break;else        printf("\n");}return 0;}

原创粉丝点击