zoukankan      html  css  js  c++  java
  • HDU 1264 Counting Squares(线段树求面积的并)

    Counting Squares

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 1885    Accepted Submission(s): 946

    Problem Description
    Your input is a series of rectangles, one per line. Each rectangle is specified as two points(X,Y) that specify the opposite corners of a rectangle. All coordinates will be integers in the range 0 to 100. For example, the line
    5 8 7 10
    specifies the rectangle who's corners are(5,8),(7,8),(7,10),(5,10).
    If drawn on graph paper, that rectangle would cover four squares. Your job is to count the number of unit(i.e.,1*1) squares that are covered by any one of the rectangles given as input. Any square covered by more than one rectangle should only be counted once.
     
    Input
    The input format is a series of lines, each containing 4 integers. Four -1's are used to separate problems, and four -2's are used to end the last problem. Otherwise, the numbers are the x-ycoordinates of two points that are opposite corners of a rectangle.
     
    Output
    Your output should be the number of squares covered by each set of rectangles. Each number should be printed on a separate line.
     
    Sample Input
    5 8 7 10
    6 9 7 8
    6 8 8 11
    -1 -1 -1 -1
    0 0 100 100
    50 75 12 90
    39 42 57 73
    -2 -2 -2 -2
     
    Sample Output
    8
    10000

    题目链接:HDU 1264

    连离散化都不用的水题,有一个坑点就是题目给的两个对角线坐标不一定是左下、右上这样一个顺序,或者也可能是副对角线上的点,需要判断一下

    代码:

    #include <stdio.h>
    #include <bits/stdc++.h>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define CLR(arr,val) memset(arr,val,sizeof(arr))
    #define LC(x) (x<<1)
    #define RC(x) ((x<<1)+1)
    #define MID(x,y) ((x+y)>>1)
    typedef pair<int,int> pii;
    typedef long long LL;
    const double PI=acos(-1.0);
    const int N=1e3+7;
    struct seg
    {
        int l,mid,r;
        int cnt,len;
    };
    struct Line
    {
        int l,r,h,flag;
        bool operator<(const Line &t)const
        {
            return h<t.h;
        }
    };
    seg T[N<<3];
    Line xline[N<<1];
    
    inline void pushup(int k)
    {
        if(T[k].cnt>0)
            T[k].len=T[k].r-T[k].l+1;
        else
        {
            if(T[k].l==T[k].r)
                T[k].len=0;
            else
                T[k].len=T[LC(k)].len+T[RC(k)].len;
        }
    }
    void build(int k,int l,int r)
    {
        T[k].l=l;
        T[k].r=r;
        T[k].mid=MID(l,r);
        T[k].len=T[k].cnt=0;
        if(l==r)
            return ;
        build(LC(k),l,T[k].mid);
        build(RC(k),T[k].mid+1,r);
        pushup(k);
    }
    void update(int k,int l,int r,int flag)
    {
        if(l<=T[k].l&&T[k].r<=r)
        {
            T[k].cnt+=flag;
            pushup(k);
        }
        else
        {
            if(r<=T[k].mid)
                update(LC(k),l,r,flag);
            else if(l>T[k].mid)
                update(RC(k),l,r,flag);
            else
                update(LC(k),l,T[k].mid,flag),update(RC(k),T[k].mid+1,r,flag);
            pushup(k);
        }
    }
    int main(void)
    {
        int n,i;
        int xa,ya,xb,yb;
        int cnt=0;
        while (scanf("%d%d%d%d",&xa,&ya,&xb,&yb))
        {
            if(xa==-1&&xb==-1&&ya==-1&&yb==-1)
            {
                int ans=0;
                build(1,0,N);
                sort(xline,xline+cnt);
                for (i=0; i<cnt-1; ++i)
                {
                    update(1,xline[i].l,xline[i].r-1,xline[i].flag);
                    ans=ans+(xline[i+1].h-xline[i].h)*T[1].len;
                }
                printf("%d
    ",ans);
                cnt=0;
            }
            else if(xa==-2&&xb==-2&&ya==-2&&yb==-2)
            {
                int ans=0;
                build(1,0,N);
                sort(xline,xline+cnt);
                for (i=0; i<cnt-1; ++i)
                {
                    update(1,xline[i].l,xline[i].r-1,xline[i].flag);
                    int dh=(xline[i+1].h-xline[i].h);
                    ans=ans+dh*T[1].len;
                }
                printf("%d
    ",ans);
                cnt=0;
                break;
            }
            else
            {
                if(xa>xb)
                    swap(xa,xb);
                if(ya>yb)
                    swap(ya,yb);
                xline[cnt++]=(Line){xa,xb,ya,1};
                xline[cnt++]=(Line){xa,xb,yb,-1};
            }
    
        }
        return 0;
    }
  • 相关阅读:
    2013-10-31 《问题儿童居然一天两更!?》
    2013-10-31 《October 31st, 2013》
    2013-10-31 《三天里什么都没干……总之把目前为止的代码发了吧……》
    日怎么没人告诉我这博客可以改博文界面的显示宽度的
    俗话说打脸哦不打铁要趁热所以记录下替换图片的方法
    GUI好看码难写不是难写是难看我是说码难看不是GUI
    虽然保持了连续代码生产量但是仔细想想也没什么必要
    重写了电话本代码全面更新居然连续三天每天一个程序
    专注写字典三十年问你怕未又被编码卡了简直难以置信
    我就写个字典居然卡了两天重申一遍文字编码日你大爷
  • 原文地址:https://www.cnblogs.com/Blackops/p/6028711.html
Copyright © 2011-2022 走看看