Bombing
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 4834 Accepted Submission(s): 1813
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.
题意:
在二维坐标轴上有N个坐标,有M次操作,每次给出d,x当d=0时,删除x轴上坐标为x的所有坐标并输出个数,d=1时,删除y轴上坐标为x的所有坐标输出个数。
思路:
因为只有1e5个坐标,也就是x和y轴上出现的坐标最多也只有1e5,而数可以达到1e9,需要离散化。
所以用map+multiset来解决,设置两个一个存x轴上的每个坐标对应的y坐标,同理一个存y轴的。
每次更新的时候都需要将两个map来更新。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
map <int,multiset<int> > mpx;
map <int,multiset<int> > mpy;
multiset<int> :: iterator it;
int main()
{
int n,m,x,y;
while(~scanf("%d%d",&n,&m)&&n&&m)
{
mpx.clear();
mpy.clear();
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
mpx[x].insert(y);
mpy[y].insert(x);
}
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
if(x==0)
{
printf("%d\n",mpx[y].size());
for(it=mpx[y].begin();it!=mpx[y].end();it++)
{
mpy[*it].erase(mpy[*it].find(y));
}
mpx[y].clear();
}
else
{
printf("%d\n",mpy[y].size());
for(it=mpy[y].begin();it!=mpy[y].end();it++)
{
mpx[*it].erase(mpx[*it].find(y));
}
mpy[y].clear();
}
}
printf("\n");
}
return 0;
}
546



被折叠的 条评论
为什么被折叠?



