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. ios5之后arc的问题

    原创: 自从ios5以后, apple官方引进了ARC (automaically reference count 自动索引计数) 这个新特性, 简单的说就是对象设置为nil(空引用)之后, 当自动索 ...

  2. [BZOJ4632]树的编码

    [BZOJ4632]树的编码 试题描述 SHUXK 正在对一棵N个结点的有根树进行研究,首要的一件事就是对这棵树进行编码. lz 说:“这还不容易吗?我令根节点的编号为 1,然后保证每个结点的编号都比 ...

  3. web api 解决跨域的问题

    web api 总是会遇到跨域的问题,今天我找到了如下方法解决跨域: 1: a:在配置文件中的 加上如下代码 <system.webServer> <httpProtocol> ...

  4. ubuntu 桌面版性能调优

    http://www.howtogeek.com/115797/6-ways-to-speed-up-ubuntu/

  5. ORM框架是什么

    ORM框架是什么 对象关系映射,目前数据库是关系型数据库  ORM 主要是把数据库中的关系数据映射称为程序中的对象 目前集中常见的ORM框架1  Nhibernate原因:用的比较多,资料也比较好找. ...

  6. 《ASP.NET1200例》ListView 控件与DataPager控件的结合<二>

    ASP.NET使用ListView数据绑定控件和DataPager实现数据分页显示 为什么使用ListView+DataPager的方式实现分页显示? .net提供的诸多数据绑定控件,每一种都有它自己 ...

  7. iOS NSURLConnection 和 dispatch_async 错误的使用方法,导致回调方法无法调用

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ), ^{ NSMutableURLRequest ...

  8. DP:Sumsets(POJ 2229)

     数的集合问题 题目大意:给定你一个整数m,你只能用2的k次幂来组合这个数,问你有多少种组合方式? 这一题一看,天啦太简单了,完全背包?是不是? 不过的确这一题可以用完全背包来想,但是交题绝对是TLE ...

  9. 在一个JSP页面中包含另一个JSP页面的三种方式

    转载自://http://blog.163.com/neu_lxb/blog/static/179417010201121343132918/ (1)include指令          includ ...

  10. JDK安装和配置

    一.Windows下的JDK环境变量配置 在java 中需要设置三个环境变量(1.5之后不用再设置classpath了,但个人强烈建议继续设置以保证向下兼用问题) JDK安装完成之后我们来设置环境变量 ...