HDU 4022Bombing 2011网络赛(2分 或 STL)
HDU 4022Bombing 2011网络赛(二分 或 STL)BombingTime Limit: 4000/2000 MS (Java/Others)Memory Limit: 6
HDU 4022Bombing 2011网络赛(二分 或 STL)
BombingTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1884 Accepted Submission(s): 703
Problem DescriptionIt’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.
InputMultiple 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.
OutputFor 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 Input3 21 21 32 30 11 30 0
Sample Output21
SourceThe 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest
题目大意:题目意思说给你n个基地的坐标位置,然后有m个炸弹,炸弹有两种,0号炸弹可以炸毁x相同的基地,1号炸弹可以炸毁y相同的基地。问你每颗炸弹能炸毁的基地数目,如果这个基地已经被先前的炸弹炸毁了,就不能再被炸了。
解题思路:开始上来就直接二分,没有考虑到坐标的范围,那样的话是需要将坐标位置离散化的。但是考虑到另一个问题,离散化将x坐标离散化,但是把x炸掉之后,那所有x的基地都将被炸掉,所以最后在每一个点先设置一个索引index,然后index是多少,就表示哪个点,开始设置visi都为0,具体实现见代码:
题目地址:Bombing
二分AC代码:#include<iostream>#include<cstring>#include<string>#include<cstdio>#include<algorithm>#include<map>#include<set>#define maxx 100005using namespace std;typedef map<int,multiset<int> > node;node mx;node my;int cal(node &x,node &y,int p){ int res=x[p].size(); multiset<int>::iterator it; for(it=x[p].begin();it!=x[p].end();it++) { //x[p].erase(*it); //不能这样删除,这样删之后it无效 y[*it].erase(p); } x[p].clear(); //直接一次清空 return res;}int main(){ int n,m,x,y,q,p; while(scanf("%d%d",&n,&m)) { if(n==0&&m==0) break; mx.clear(); my.clear(); while(n--) { scanf("%d%d",&x,&y); mx[x].insert(y); my[y].insert(x); } while(m--) { scanf("%d%d",&q,&p); if(q==0) printf("%d\n",cal(mx,my,p)); else printf("%d\n",cal(my,mx,p)); } puts(""); } return 0;}//1062MS