HDU4022 Bombing [离散化统计]

  一开始想的很复杂,其实只要离散化一下就行了,炸x的时候将对应的y都-1,然后统计对应的y的时候将对应的已经炸掉的点减去即可。

   

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define MAXN 100005

struct pnt{
    int x,y;
}px[MAXN],py[MAXN];
bool cmpx(const pnt& a,const pnt& b){
    return a.x<b.x||a.x==b.x&&a.y<b.y;
}
bool cmpy(const pnt& a,const pnt& b){
    return a.y<b.y||a.y==b.y&&a.x<b.x;
}
int tx,ty,n,m;
int dx[MAXN],dy[MAXN],dxs,dys;
int stx[MAXN],sty[MAXN],totx[MAXN],toty[MAXN],calx[MAXN],caly[MAXN];
int binser(int num,int h,int arr[]){
    int low=0,high=h,mid;
    for(;;){
        mid=(low+high)/2;
        if(low==mid)break;
        if(arr[mid]<=num)low=mid;
        else high=mid;
    }
    return arr[mid]==num?mid:-1;
}
int main(){
//freopen("test.in","r",stdin);
    while(scanf("%d%d",&n,&m),n||m){
        for(int i=0;i<n;i++){
            scanf("%d%d",&px[i].x,&px[i].y);
            py[i]=px[i];
        }
        std::sort(px,px+n,cmpx);
        std::sort(py,py+n,cmpy);
        dxs=dys=1,dx[0]=px[0].x,dy[0]=py[0].y;
        //离散化并且记录每个x开始的位置
        for(int i=1;i<n;i++){
            if(px[i].x!=px[i-1].x)stx[dxs]=i,dx[dxs++]=px[i].x;
            if(py[i].y!=py[i-1].y)sty[dys]=i,dy[dys++]=py[i].y;
        }
        stx[dxs]=n,sty[dys]=n;
        memset(calx,0,dxs*4);memset(caly,0,dys*4);
        memset(totx,0,dxs*4); memset(toty,0,dys*4);
        while(m--){
            scanf("%d%d",&tx,&ty);
            if(tx==0){
                ty=binser(ty,dxs,dx);
                if(ty==-1||calx[ty]){printf("0\n");continue;}
                calx[ty]=1;
                int ans=0;
                for(int i=stx[ty];i<stx[ty+1];i++,ans++)
                    toty[binser(px[i].y,dys,dy)]++;
                printf("%d\n",ans-totx[ty]);
            }else{
                ty=binser(ty,dys,dy);
                if(ty==-1||caly[ty]){printf("0\n");continue;}
                caly[ty]=1;
                int ans=0;
                for(int i=sty[ty];i<sty[ty+1];i++,ans++)
                    totx[binser(py[i].x,dxs,dx)]++;
                printf("%d\n",ans-toty[ty]);
            }
        }
        printf("\n");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/swm8023/p/2659311.html