Counting Squares

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

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

题目链接:HDU 1264

连离散化都不用的水题,有一个坑点就是题目给的两个对角线坐标不一定是左下、右上这样一个顺序,或者也可能是副对角线上的点,需要判断一下

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=1e3+7;
struct seg
{
int l,mid,r;
int cnt,len;
};
struct Line
{
int l,r,h,flag;
bool operator<(const Line &t)const
{
return h<t.h;
}
};
seg T[N<<3];
Line xline[N<<1]; inline void pushup(int k)
{
if(T[k].cnt>0)
T[k].len=T[k].r-T[k].l+1;
else
{
if(T[k].l==T[k].r)
T[k].len=0;
else
T[k].len=T[LC(k)].len+T[RC(k)].len;
}
}
void build(int k,int l,int r)
{
T[k].l=l;
T[k].r=r;
T[k].mid=MID(l,r);
T[k].len=T[k].cnt=0;
if(l==r)
return ;
build(LC(k),l,T[k].mid);
build(RC(k),T[k].mid+1,r);
pushup(k);
}
void update(int k,int l,int r,int flag)
{
if(l<=T[k].l&&T[k].r<=r)
{
T[k].cnt+=flag;
pushup(k);
}
else
{
if(r<=T[k].mid)
update(LC(k),l,r,flag);
else if(l>T[k].mid)
update(RC(k),l,r,flag);
else
update(LC(k),l,T[k].mid,flag),update(RC(k),T[k].mid+1,r,flag);
pushup(k);
}
}
int main(void)
{
int n,i;
int xa,ya,xb,yb;
int cnt=0;
while (scanf("%d%d%d%d",&xa,&ya,&xb,&yb))
{
if(xa==-1&&xb==-1&&ya==-1&&yb==-1)
{
int ans=0;
build(1,0,N);
sort(xline,xline+cnt);
for (i=0; i<cnt-1; ++i)
{
update(1,xline[i].l,xline[i].r-1,xline[i].flag);
ans=ans+(xline[i+1].h-xline[i].h)*T[1].len;
}
printf("%d\n",ans);
cnt=0;
}
else if(xa==-2&&xb==-2&&ya==-2&&yb==-2)
{
int ans=0;
build(1,0,N);
sort(xline,xline+cnt);
for (i=0; i<cnt-1; ++i)
{
update(1,xline[i].l,xline[i].r-1,xline[i].flag);
int dh=(xline[i+1].h-xline[i].h);
ans=ans+dh*T[1].len;
}
printf("%d\n",ans);
cnt=0;
break;
}
else
{
if(xa>xb)
swap(xa,xb);
if(ya>yb)
swap(ya,yb);
xline[cnt++]=(Line){xa,xb,ya,1};
xline[cnt++]=(Line){xa,xb,yb,-1};
} }
return 0;
}

HDU 1264 Counting Squares(线段树求面积的并)的更多相关文章

  1. HDU - 1542 Atlantis(线段树求面积并)

    https://cn.vjudge.net/problem/HDU-1542 题意 求矩形的面积并 分析 点为浮点数,需要离散化处理. 给定一个矩形的左下角坐标和右上角坐标分别为:(x1,y1).(x ...

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

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

  3. HDU - 1255 覆盖的面积 (线段树求面积交)

    https://cn.vjudge.net/problem/HDU-1255 题意 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. 分析 求面积并的题:https://www.cnbl ...

  4. hdu-1255(线段树求面积并)模板

    题目链接:传送门 思路: (1)建立线段的信息,每个线段存储l到r的线段的x位置和y的起始点与终点. 建立线段树的节点信息,每个节点代表一个区间的信息,x表示区间的横坐标的位置,l,r表示纵坐标的范围 ...

  5. hdu 3265 Posters(线段树+扫描线+面积并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3265 题意:给你一张挖了洞的墙纸贴在墙上,问你总面积有多少. 挖了洞后其实就是多了几个矩形墙纸,一张墙 ...

  6. HDU 1264 Counting Squares(模拟)

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

  7. poj-1151-Atlantis-线段树求面积并

    非常裸的线段树求面积并. 坐标须要离散化一下. #include<stdio.h> #include<iostream> #include<stdlib.h> #i ...

  8. hdu-3642--Get The Treasury-线段树求面积并

    求空间中叠加3次及3次以上的体积. 由于|z|<=500.所以直接把z轴剥离出来1000层. 然后对于每一层进行线段树求面积并. #include<stdio.h> #include ...

  9. HDU - 1255 覆盖的面积(线段树求矩形面积交 扫描线+离散化)

    链接:线段树求矩形面积并 扫描线+离散化 1.给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. 2.看完线段树求矩形面积并 的方法后,再看这题,求的是矩形面积交,类同. 求面积时,用被覆 ...

随机推荐

  1. express-8 Handlebars模板引擎(1)

    简介 使用JavaScript生成一些HTML document.write('<h1>Please Don\'t Do This</h1>'); document.write ...

  2. Ubantu下面命令听歌(豆瓣fm)

    在Linux下一直是不太方便的事情,下面推荐一个方法: 终端中输入以下命令安装豆瓣fm: >> sudo pip install douban.fm >> sudo apt-g ...

  3. MapReduce应用案例--简单排序

    1. 设计思路 在MapReduce过程中自带有排序,可以使用这个默认的排序达到我们的目的. MapReduce 是按照key值进行排序的,我们在Map过程中将读入的数据转化成IntWritable类 ...

  4. oracle性能优化----处理大数据量数据

     场景:对2千万个数据,修改他们的名字加上后缀“生日”. 普通sql:   and not regexp_like(title, '生日'); 优化sql: declare type rid_Arra ...

  5. 【SAP BO】【WEBI】【转】Webi实现动态选择度量

    我们都知道Web Intelligence具有高级的分析功能,是一个非常灵活的报表工具.在这篇文章里,我会演示一个使用Webi实现动态选择度量对象的方案.首先解释一下什么是”动态选择度量”:例如我们有 ...

  6. ai seek

    原文地址链接:http://gamedevelopment.tutsplus.com/tutorials/understanding-steering-behaviors-seek--gamedev- ...

  7. HDU 2852 (树状数组+无序第K小)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意:操作①:往盒子里放一个数.操作②:从盒子里扔掉一个数.操作③:查询盒子里大于a的第K小 ...

  8. 3分钟wamp中php安装 pear 然而并没有用 并没能借此安装phpunit 不得不借用了其他的方式安装phpunit

    15:42 2015/11/233分钟wamp中php安装 pear环境介绍:windows10,wamp2.5(推荐博客的博主是win7,所以系统应该不是问题)注意:在过程中要输入一次 yes,不要 ...

  9. 【BZOJ】3028: 食物

    http://www.lydsy.com/JudgeOnline/problem.php?id=3028 题意: 每种食物的限制如下:汉堡:偶数个:可乐:0个或1个鸡腿:0个,1个或2个蜜桃:奇数个鸡 ...

  10. URAL 1117. Hierarchy(DP)

    题目链接 这破题,根本看不懂题意啊...题意:一棵中序遍历是1 2 3 4 5...的满二叉树,从a a+1 a+2 a+3 b,总共多少步.x到y的距离为中间有多少个点.a > b没注意2Y. ...