zoukankan      html  css  js  c++  java
  • hdu1264 Counting Squares

    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

    分析:
    扫描线裸题

    tip

    All coordinates will be integers in the range 0 to 100.
    但是我用的int就是蜜汁RE,
    把所有和坐标有关的变量类型变成double之后
    就顺利A了

    哪位dalao帮我解答一下啊。。。

    还需要注意的一点是:
    这里写图片描述

    这里写代码片
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    
    using namespace std;
    
    const double eps=1e-8;
    const int N=310;
    struct node{
        int x,y,s;
        double ml,mr,len;
    };
    node t[N<<2];
    struct nd{
        double x,ya,yb;
        int ff;
    };
    nd li[N<<1];
    int n,tot;
    double yi[N<<1];
    
    int cmp(const nd &a,const nd &b)
    {
        return a.x-b.x<eps;
    }
    
    void build(int bh,int l,int r)
    {
        t[bh].x=l;
        t[bh].y=r;
        t[bh].ml=yi[l];
        t[bh].mr=yi[r];
        t[bh].s=t[bh].len=0;
        if (l==r-1) return;
        int mid=(l+r)>>1;
        build(bh<<1,l,mid);
        build(bh<<1|1,mid,r);
    }
    
    void update(int bh)
    {
        if (t[bh].s>0) t[bh].len=t[bh].mr-t[bh].ml;
        else if (t[bh].x==t[bh].y-1) t[bh].len=0;   ///
        else t[bh].len=t[bh<<1].len+t[bh<<1|1].len;
    }
    
    void add(int bh,nd p)
    {
        if (t[bh].ml==p.ya&&t[bh].mr==p.yb)
        {
            t[bh].s+=p.ff;
            update(bh);
            return;
        }
        if (p.yb<=t[bh<<1].mr) add(bh<<1,p);
        else if (p.ya>=t[bh<<1|1].ml) add(bh<<1|1,p);
        else 
        {
            nd temp;
            temp=p;
            temp.yb=t[bh<<1].mr;
            add(bh<<1,temp);
            temp=p;
            temp.ya=t[bh<<1|1].ml;
            add(bh<<1|1,temp);
        }
        update(bh);   //
    }
    
    int main()
    {
        double x=0,y=0,xx=0,yy=0;
        scanf("%lf%lf%lf%lf",&x,&y,&xx,&yy);
        while (x!=-2&&y!=-2&&xx!=-2&&yy!=-2)
        {
            tot=0;
            while (x!=-1&&y!=-1&&xx!=-1&&yy!=-1&&x!=-2&&y!=-2&&xx!=-2&&yy!=-2)
            {
                if (x>xx) swap(x,xx);
                if (y>yy) swap(y,yy);
                //给出的不一定是左下角和右上角 
                tot++;
                yi[tot]=y;
                li[tot].x=x;li[tot].ya=y;li[tot].yb=yy;li[tot].ff=1;
                tot++;
                yi[tot]=yy;
                li[tot].x=xx;li[tot].ya=y;li[tot].yb=yy;li[tot].ff=-1;
                scanf("%lf%lf%lf%lf",&x,&y,&xx,&yy);
            }
            sort(yi+1,yi+1+tot);
            sort(li+1,li+1+tot,cmp);
            build(1,1,tot);
            add(1,li[1]);
            double sum=0;
            for (int i=2;i<=tot;i++)
            {
                sum+=(t[1].len*(li[i].x-li[i-1].x));
                add(1,li[i]);
            }
            printf("%0.0lf
    ",sum);
            if (x==-2&&y==-2&&xx==-2&&yy==-2) break;
            scanf("%lf%lf%lf%lf",&x,&y,&xx,&yy);
        }
        return 0;
    }
  • 相关阅读:
    Java学习day2
    Java 学习day1
    const
    数组
    scanf、printf、gets、puts的应用及区别
    指针数组和数组指针
    指针函数和函数指针
    nginx Windows版使用说明
    windows平台上nginx部署web.py(转)
    python安装程序是报这样的错UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 1: ordinal not in range(128)
  • 原文地址:https://www.cnblogs.com/wutongtong3117/p/7673327.html
Copyright © 2011-2022 走看看