Bombing
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1884 Accepted Submission(s): 703
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 10
9, 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 2
1 2
1 3
2 3
0 1
1 3
0 0
Sample Output
Source
The 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>
#define maxx 100005
using namespace std;
int n,m;
struct node
{
int x;
int y;
int index; //每个点有自己的标记
}mq;
node a[maxx],b[maxx];
int visi[maxx];
//a按照x排序 b按照y排序
//visi记录每个基地是否被炸
bool cmp1(node m1,node m2) //按照x排序
{
return m1.x<=m2.x;
}
bool cmp2(node m1,node m2) //按照x排序
{
return m1.y<=m2.y;
}
int cal1(int xx) //计算x相等的数目
{
int l,r,mid,ans;
l=0,r=n-1,mid=(l+r)>>1;
ans=0;
while(l<=r)
{
if(a[mid].x<xx) l=mid+1;
else r=mid-1;
mid=(l+r)>>1;
}
for(mid=l;mid<n;mid++)
{
if(a[mid].x!=xx) break;
if(visi[a[mid].index]) continue;
visi[a[mid].index]=1; //标记这个点的x坐标被炸
ans++;
}
return ans;
}
int cal2(int yy) //计算y相等的数目
{
int l,r,mid,ans;
l=0,r=n-1,mid=(l+r)>>1;
ans=0;
while(l<=r)
{
if(b[mid].y<yy) l=mid+1;
else r=mid-1;
mid=(l+r)>>1;
}
//cout<<l<<" "<<r<<" "<<mid<<endl;
for(mid=l;mid<n;mid++)
{
if(b[mid].y!=yy) break;
if(visi[b[mid].index]) continue;
visi[b[mid].index]=1; //标记这个点的x坐标被炸
ans++;
}
return ans;
}
int main()
{
int i,tmp,p;
while(scanf("%d%d",&n,&m))
{
if(n==0&&m==0)
break;
for(i=0;i<n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
a[i].index=i;
b[i]=a[i];
}
memset(visi,0,sizeof(visi));
sort(a,a+n,cmp1);
sort(b,b+n,cmp2);
for(i=0;i<m;i++)
{
scanf("%d%d",&tmp,&p);
if(tmp==0) printf("%d\n",cal1(p));
else printf("%d\n",cal2(p));
}
puts("");
}
return 0;
}
//484MS
在网上搜了一下,很多都是用STL过的。
首先建立两个map<int,multiset<int> > mx,注意后面两个尖括号中间要有个空格。否则编译错误,第一个map是x,第一维是x,第二维便是这个x对应的y。然后每次如果看
能炸毁多少个基地很容易知道,直接mx[p].size(),即可求出,主要是找到个数之后要把基地炸毁,把x对应的y删掉,把y对应的x也要删掉,具体实现见代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#define maxx 100005
using 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