zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    IE9的css hack
    ie6 插入图片img png24 阴影
    clear:both; overflow:hidden
    ie6 背景图片 png24 阴影
    html 页面定位
    IE无法显示PNG
    行内元素 & 块元素
    div+css布局时的浏览器兼容问题
    删除源代码的管理信息(VSS)
    string path;
  • 原文地址:https://www.cnblogs.com/swm8023/p/2659311.html
Copyright © 2011-2022 走看看