HDU - 1264 - Counting Squares (扫描线)

Counting Squares

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


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
 

Source
 

题意:给许多矩形,求所有矩形框住的总面积,重合部分只算一次。


求总面积,被覆盖多次的只按一次来计算,就是线段树扫描线的用法,从左往右扫,每次遇到矩形边界进行判断,若该点为矩形左边界,则接下来又有一段线段覆盖的次数+1,若为矩形右边界,则覆盖次数-1,然后计算面积,S +=(覆盖的线段长度)*(该点水平位置-上一点水平位置)。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int x1,x2,y1,y2,sum[450],cover[150],len,x,ans,cnt;
struct node {int x,l,r,in;}line[1010];
bool cmp(node a,node b) {return a.x<b.x;}
void Cover(int L,int R,int l,int r,int x,int C)
{
    if(l==r)
    {
        cover[l]+=C;
        sum[x] = min(1,cover[l]);
        return ;
    }
    int m = (l+r)>>1;
    if(L<=m) Cover(L, R, l, m, x<<1, C);
    if(R>m) Cover(L, R, m+1, r, x<<1|1, C);
    sum[x] = sum[x<<1] + sum[x<<1|1];
}
void init()
{
    ans = cnt = len = x = 0;
    memset(cover,0,sizeof cover);
    memset(sum,0,sizeof sum);
    memset(line,0,sizeof line);
}
int main()
{
    init();
    while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF)
    {
        if(x1+x2+y1+y2<0)
        {
            sort(line, line+cnt, cmp);
            for(int i=0;i<cnt;i++)
            {
                ans += len*(line[i].x-x);
                x = line[i].x;
                Cover(line[i].l+1, line[i].r, 0, 100, 1, line[i].in);
                len = sum[1];
            }
            printf("%d\n",ans);
            init();
            if(x1+x2+y1+y2==-4) continue;
            else break;
        }
       
        if(x1>x2) swap(x1,x2);
        if(y1>y2) swap(y1,y2);
        line[cnt].x = x1, line[cnt].l = y1, line[cnt].r = y2, line[cnt++].in = 1;
        line[cnt].x = x2, line[cnt].l = y1, line[cnt].r = y2, line[cnt++].in = -1;
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值