码迷,mamicode.com
首页 > 其他好文 > 详细

HDU1377_Counting Squares(扫描线/线段树)

时间:2014-08-16 21:10:01      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   os   io   

解题报告

题意:

矩形面积并。

思路:

扫描线+线段树

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
struct Seg
{
    int lx,rx,h,v;
    friend bool operator < (Seg a,Seg b)
    {
        return a.h<b.h;
    }
} seg[500000];
int lz[201000],sum[201000];
void push_up(int rt,int l,int r)
{
    if(lz[rt])
        sum[rt]=r+1-l;
    else if(l==r)sum[rt]=0;
    else sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void update(int rt,int l,int r,int ql,int qr,int v)
{
    if(ql>r||qr<l)return ;
    if(ql<=l&&r<=qr)
    {
        lz[rt]+=v;
        push_up(rt,l,r);
        return ;
    }
    int mid=(l+r)>>1;
    update(rt<<1,l,mid,ql,qr,v);
    update(rt<<1|1,mid+1,r,ql,qr,v);
    push_up(rt,l,r);
}
int main()
{
    int n,i,j,x1,y1,x2,y2,x3,y3,x4,y4;
    while(~scanf("%d%d%d%d",&x1,&y1,&x2,&y2))
    {
        int m=0;
        if(x1==y1&&x2==y2&&x1==x2&&x1==-2)break;
        memset(lz,0,sizeof(lz));
        memset(sum,0,sizeof(sum));
        seg[m].lx=min(x1,x2);seg[m].rx=max(x1,x2);seg[m].v=1;seg[m++].h=min(y1,y2);
        seg[m].lx=min(x1,x2);seg[m].rx=max(x1,x2);seg[m].v=-1;seg[m++].h=max(y1,y2);
        while(~scanf("%d%d%d%d",&x1,&y1,&x2,&y2))
        {
            if(x1==y1&&x2==y2&&x1==x2&&x1==-2)break;
            if(x1==y1&&x2==y2&&x1==x2&&x1==-1)break;
            seg[m].lx=min(x1,x2);seg[m].rx=max(x1,x2);seg[m].v=1;seg[m++].h=min(y1,y2);
            seg[m].lx=min(x1,x2);seg[m].rx=max(x1,x2);seg[m].v=-1;seg[m++].h=max(y1,y2);
        }
        sort(seg,seg+m);
        int ans=0;
        for(i=0; i<m-1; i++)
        {
            update(1,0,100-1,seg[i].lx,seg[i].rx-1,seg[i].v);
            ans+=sum[1]*(seg[i+1].h-seg[i].h);
        }
        printf("%d\n",ans);
    }
    return 0;
}

Counting Squares

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


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
 


HDU1377_Counting Squares(扫描线/线段树),布布扣,bubuko.com

HDU1377_Counting Squares(扫描线/线段树)

标签:des   style   blog   http   color   java   os   io   

原文地址:http://blog.csdn.net/juncoder/article/details/38616219

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!