您的位置:首页 > 其它

HDU 4022 Bombing STL/二分

2012-01-23 20:59 323 查看
用stl的set和map

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<iterator>
using namespace std;
typedef map<int,multiset<int> > def;

void pop(def &a,def &b,int k)
{
printf("%d\n",a[k].size());
for(multiset<int>::iterator i=a[k].begin();i!=a[k].end();i++)
b[*i].erase(k);
a[k].clear();
}
int main()
{
int n,m,x,y;
while(1)
{
scanf("%d%d",&n,&m);
if(n==0&&m==0)return 0;
def h;def l;
for(int i=0;i<n;i++)
{
scanf("%d%d",&x,&y);
h[x].insert(y);
l[y].insert(x);
}
for(int j=0;j<m;j++)
{
scanf("%d%d",&x,&y);
if(x==0)
pop(h,l,y);
else
pop(l,h,y);
}
printf("\n");
}
return 0;
}

或者二分

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=100010;
struct Node
{
int x,y;
}p
;
bool hv
;
int a
,b
;
bool cmp1(int k1,int k2)
{
return p[k1].x < p[k2].x;
}
bool cmp2(int k1,int k2)
{
return p[k1].y < p[k2].y;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m) && !(n==0 && m==0))
{
memset(hv,true,sizeof(hv));
for(int i=1;i<=n;i++) {
scanf("%d%d",&p[i].x,&p[i].y);
a[i]=b[i]=i;
}
sort(a+1,a+1+n,cmp1);
sort(b+1,b+1+n,cmp2);
while(m--)
{
int c,d;
scanf("%d%d",&c,&d);
int ans=0;
if(c==0)
{
int l=1,r=n,t;
while(l<r)
{
int mid=(l+r)/2;
if(p[a[mid]].x >= d) r=mid;
else l=mid+1;
}
t=l;
while(t<=n && p[a[t]].x == d) ans+=hv[a[t]],hv[a[t]]=false,t++;
}
else
{
int l=1,r=n,t;
while(l<r)
{
int mid=(l+r)/2;
if(p[b[mid]].y >= d) r=mid;
else l=mid+1;
}
t=l;
while(t<=n && p[b[t]].y == d) ans+=hv[b[t]],hv[b[t]]=false,t++;
}
printf("%d\n",ans);
}
puts("");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: