HDOJ 题目1264 Counting Squares(线段树+扫描线)

本文介绍了一种算法,用于计算一系列矩形覆盖的单元格数量。输入为多个指定矩形的对角坐标,输出为所有矩形覆盖的独特1*1单元格总数。通过构建数据结构并使用区间更新和离散化技术来高效解决该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Counting Squares

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


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
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:   1280  1543  3303  2600  1156 
ac带吗
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define N 100010
#define ll long long
using namespace std;
struct s
{
    int x1,x2,y;
    int flag;
}a[N<<2];
int __hash[N<<3],k,col[N<<3];
ll sum[N<<3];
int cmp(s a ,s b)
{
    return a.y<b.y;
}
void add(int x1,int y1,int x2,int y2)
{
    a[k].x1=x1;
    a[k].x2=x2;
    a[k].y=y1;
    a[k].flag=1;
    __hash[k++]=x1;
    a[k].x1=x1;
    a[k].x2=x2;
    a[k].y=y2;
    a[k].flag=-1;
    __hash[k++]=x2;
}
int bseach(int key,int n)
{
    int l=1,r=n;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(__hash[mid]==key)
            return mid;
        if(__hash[mid]<key)
            l=mid+1;
        else
            r=mid-1;
    }
    return l;
}
void pushup(int tr,int l,int r)
{
    if(col[tr])
    {
        sum[tr]=__hash[r+1]-__hash[l];
    }
    else
        if(l==r)
            sum[tr]=0;
        else
            sum[tr]=sum[tr<<1]+sum[tr<<1|1];
}
void update(int L,int R,int l,int r,int tr,int flag)
{
    if(L<=l&&r<=R)
    {
        col[tr]+=flag;
        pushup(tr,l,r);
        return ;
    }
    int mid=(l+r)>>1;
    if(L<=mid)
        update(L,R,l,mid,tr<<1,flag);
    if(R>mid)
        update(L,R,mid+1,r,tr<<1|1,flag);
    pushup(tr,l,r);
}
int main()
{
    int x1,x2;
    int y1,y2;
    k=1;
    int w=0;
    while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF)
    {
        int i,j;
        if(x1>x2)swap(x1,x2);
        if(y1>y2)swap(y1,y2);
        if(x1==-2&&x2==-2&&y1==-2&&y2==-2)
            break;
        if(x1==-1&&x2==-1&&y1==-1&&y2==-1)
        {
            k=1;
            continue;
        }
        memset(col,0,sizeof(col));
        memset(sum,0,sizeof(sum));
        add(x1,y1,x2,y2);
        while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF)
        {
            if(x1>x2)swap(x1,x2);
            if(y1>y2)swap(y1,y2);
            if(x1==-1&&x2==-1&&y1==-1&&y2==-1)
                break;
            if(x1==-2&&x2==-2&&y1==-2&&y2==-2)
            {
                w=1;
                break;
            }
            add(x1,y1,x2,y2);
        }
        sort(a+1,a+k,cmp);
        sort(__hash+1,__hash+k);
        ll ans=0;
        for(i=1;i<k-1;i++)
        {
            int x,y;
            x=bseach(a[i].x1,k-1);
            y=bseach(a[i].x2,k-1)-1;
            if(x<=y)
                update(x,y,1,k-1,1,a[i].flag);
            ans+=sum[1]*(ll)(a[i+1].y-a[i].y);
        }
        printf("%lld\n",ans);
        k=1;
        if(w)
            break;
    }
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值