Counting Squares

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

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

漂浮法的简单应用

#include<stdio.h>
int l[],r[],u[],d[],N,ans;
void dfs(int ll,int rr,int uu,int dd,int dep)
{
if (dep==N)
{
ans+=(rr-ll)*(uu-dd);
return;
}
if (ll==rr || uu==dd) return;
if (rr<=l[dep+] || r[dep+]<=ll || u[dep+]<=dd || uu<=d[dep+]) dfs(ll,rr,uu,dd,dep+);
else
{
if (ll<l[dep+] && l[dep+]<rr)
{
dfs(ll,l[dep+],uu,dd,dep+);
ll=l[dep+];
}
if (ll<r[dep+] && r[dep+]<rr)
{
dfs(r[dep+],rr,uu,dd,dep+);
rr=r[dep+];
}
if (dd<u[dep+] && u[dep+]<uu)
{
dfs(ll,rr,uu,u[dep+],dep+);
uu=u[dep+];
}
if (dd<d[dep+] && d[dep+]<uu)
{
dfs(ll,rr,d[dep+],dd,dep+);
dd=d[dep+];
}
}
}
int main()
{
while (true)
{
N=;
int xx1,xx2,yy1,yy2,i;
while (scanf("%d%d%d%d",&xx1,&yy1,&xx2,&yy2)!=EOF)
{
N++;
if (xx1<xx2)
{
l[N]=xx1;
r[N]=xx2;
}
else
{
l[N]=xx2;
r[N]=xx1;
}
if (yy1<yy2)
{
u[N]=yy2;
d[N]=yy1;
}
else
{
u[N]=yy1;
d[N]=yy2;
}
if (xx1<)
{
N--;
break;
}
}
ans=;
for (i=;i<=N;i++) dfs(l[i],r[i],u[i],d[i],i);
printf("%d\n",ans);
if (xx1==-) return ;
}
return ;
}

Counting Squares[HDU1264]的更多相关文章

  1. HDU 1264 Counting Squares(线段树求面积的并)

    Counting Squares Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. HDU 1264 Counting Squares (线段树-扫描线-矩形面积并)

    版权声明:欢迎关注我的博客.本文为博主[炒饭君]原创文章,未经博主同意不得转载 https://blog.csdn.net/a1061747415/article/details/25471349 P ...

  3. HDU 1264 Counting Squares(模拟)

    题目链接 Problem Description Your input is a series of rectangles, one per line. Each rectangle is speci ...

  4. D - Counting Squares

    Your input is a series of rectangles, one per line. Each rectangle is specified as two points(X,Y) t ...

  5. Counting Squares_hdu_1264(矩阵).java

    Counting Squares Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. tc 146 2 RectangularGrid(数学推导)

    SRM 146 2 500RectangularGrid Problem Statement Given the width and height of a rectangular grid, ret ...

  7. UVaLive 6602 Counting Lattice Squares (找规律)

    题意:给定一个n*m的矩阵,问你里面有几面积为奇数的正方形. 析:首先能知道的是,大的矩阵是包括小的矩阵的,而且面积为奇数,我们只要考虑恰好在边界上的正方形即可,画几个看看就知道了,如果是3*3的有3 ...

  8. UVALive 6602 Counting Lattice Squares

    给定一个n*m的网格,求面积为奇数的正方形有多少个. 首先是n*m个面积为1的,然后剩下的要么是边长为奇数,要么被这样一个奇数边长所包围. 原因如下: 对于一个边长不平行于坐标抽的正方形,其边长一定是 ...

  9. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

随机推荐

  1. ios流媒体

    http://my.oschina.net/CgShare/blog/302303 渐进式下载(伪流媒体) 介于下载本地播放与实时流媒体之间的一种播放形式,下载本地播放必须全部将文件下载完成后才能播放 ...

  2. vim常用指令及快捷键(持续更新)

    (这些文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) 发现了个非常赞的网站  http://openvim.com/ 以下很多操作都是安装好vund ...

  3. puppet 安装

    yum源配置 1. wget http://ftp.kaist.ac.kr/fedora//epel/6/i386/epel-release-6-8.noarch.rpm 2. yum list | ...

  4. python 的编码问题

    老是碰到这个问题,决定好好给整理一番思路. 翻阅资料和实践证明,以下论述为真理: 字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将 ...

  5. SQL常见笔试面试题

    sql理论题 1.触发器的作用? 答:触发器是一中特殊的存储过程,主要是通过事件来触发而被执行的.它可以强化约束,来维护数据的完整性和一致性,可以跟踪数据库内的操作从而不允许未经许可的更新和变化.可以 ...

  6. Android 中“TabBar”的背景拉伸问题

    在最近的一个工程中,要求有一个在上方了tabbar,上面有并排的3个方形按钮,每个按钮都有背景图.问题来了,如何让图片在不同尺寸的屏幕上不失真呢?(由于我们的项目比较小,工时很短,不能为每一个屏幕尺寸 ...

  7. iOS7 中的statusbar的隐藏和样式更改

    ios7以前,如果想要隐藏statusbar,需要用到[UIApplicationsharedApplication].statusBarHidden = YES; 或者在plist文件中设定Stat ...

  8. Linux卸载系统自带的JDK

    安装Linux后,一般系统都会自带openjdk,我们开发中都需要自己安装,所以需要卸载之前的,以CentOS为例,卸载方法如下: 首先执行命令查看存在哪些已安装的包 rpm -qa | grep j ...

  9. 利用FFmpeg生成视频缩略图 2.3.1

    1.下载FFmpeg文件包,解压包里的\bin\下的文件解压到 D:\ffmpeg\ 目录下. 下载地址 http://ffmpeg.zeranoe.com/builds/win32/static/ ...

  10. July 29th, Week 31st Friday, 2016

    I am a slow walker, but I never walk backwards. 我走得很慢,但我从来不会后退. I had run very fast, and I had once ...