HDH 1264 Counting Squares (线段树+扫描线|暴力)

本篇介绍了一种计算多个矩形覆盖的单位正方形数量的方法,包括两种实现方案:一种采用线段树的数据结构优化计算过程,另一种通过直接遍历坐标系进行暴力求解。

Counting Squares

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


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
 
题意:
给你几个矩形,算面积并。
POINT:
注意输入和输出方式,没有规定先输入左下和右上,都有可能,被这个坑的WA了好久。别的就和 HDU 1542一样。
还不需要离散化。
法2:
因为数据很小而且是整数,可以暴力。就是遍历一个坐标系,才100*100;


法1:
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <map>
#include <string.h>
#include <algorithm>
#include <vector>
#include <fstream>
using namespace std;
#define lt 2*x
#define rt 2*x+1
#define LL long long
const int N = 101*100;
int num=0;
int sum[N];
int cnt[N];
struct node
{
    int l,r;
    int h;
    int f;
}len[N];
bool cmd(node a,node b)
{
    return a.h<b.h;
}
void init()
{
    num=0;
    memset(sum,0,sizeof sum);
    memset(cnt,0,sizeof cnt);
    memset(len,0,sizeof len);
}
void pushup(int x,int l,int r)
{
    /* if(l==r&&cnt[x]) sum[x]=1;
     else if(cnt[x]) sum[x]=r-l;
     else
     sum[x]=sum[rt]+sum[lt];*/
    if(cnt[x]) sum[x]=r+1-l;
    else sum[x]=sum[lt]+sum[rt];
    
}
void add(int x,int l,int r,int ll,int rr,int f)
{
    if(ll<=l&&rr>=r)
    {
        cnt[x]+=f;
    }
    else
    {
        int mid=(l+r)>>1;
        if(ll<=mid) add(lt,l,mid,ll,rr,f);
        if(mid<rr) add(rt,mid+1,r,ll,rr,f);
    }
    pushup(x,l,r);
}
void gao()
{
    sort(len+1,len+1+num,cmd);
    int ans=0;
    for(int i=1;i<=num;i++)
    {
        add(1,0,1000,len[i].l,len[i].r-1,len[i].f);
        ans+=sum[1]*(len[i+1].h-len[i].h);
        
    }
    printf("%d\n",ans);
}
int main()
{
    int x1,x2,y1,y2;
    while(~scanf("%d %d %d %d",&x1,&y1,&x2,&y2))
    {
        if(x1==-1&&x2==-1&&y1==-1&&y2==-1)
        {
            gao();
            init();
        }
        else if(x1==-2&&x2==-2&&y1==-2&&y2==-2)
        {
            gao();
            break;
        }
        else
        {
            if(x1>x2) swap(x1, x2);
            if(y1>y2) swap(y1, y2);
            num++;
            len[num].f=1,len[num].l=x1,len[num].r=x2,len[num].h=y1;
            num++;
            len[num].f=-1,len[num].l=x1,len[num].r=x2,len[num].h=y2;
        }
    }
    
    return 0;
}

法2:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
int ans=0;
int flag[120][120];
int main()
{
    int x1,x2,y1,y2;
    while(~scanf("%d %d %d %d",&x1,&y1,&x2,&y2))
    {
        if(x1==-1&&x2==-1&&y1==-1&&y2==-1)
        {
            printf("%d\n",ans);
            memset(flag,0,sizeof flag);
            ans=0;
        }
        else if(x1==-2&&x2==-2&&y1==-2&&y2==-2)
        {
            printf("%d\n",ans);
            memset(flag,0,sizeof flag);
            ans=0;
            break;
        }
        else
        {
            for(int i=min(x1,x2);i<max(x2,x1);i++)
            {
                for(int j=min(y1,y2);j<max(y1,y2);j++)
                {
                    if(!flag[i][j])
                    {
                        ans++;
                        flag[i][j]=1;
                    }
                }
            }
        }
    }
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值