HDU 1828 扫描线(矩形周长并)
Picture
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3310 Accepted Submission(s): 1723
Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1.

The corresponding boundary is the whole set of line segments drawn in Figure 2.

The vertices of all rectangles have integer coordinates.
0 <= number of rectangles < 5000
All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.
Please process to the end of file.
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 5005
#define ll root<<1
#define rr root<<1|1
#define mid (a[root].l+a[root].r)/2 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} struct node{
int l, r;
int sum;
int ysum;
int val;
bool lv, rv;
}a[N*]; struct Line{
int x1, x2, y;
int val;
Line(){}
Line(int a,int b,int c,int d){
x1=a;
x2=b;
y=c;
val=d;
}
}line[N*]; bool cmp(Line a,Line b){
return a.y<b.y;
}
int n, m;
int xx[N*]; int b_s(int key){
int l=, r=m;
while(l<=r){
int mm=(l+r)/;
if(xx[mm]==key) return mm;
else if(xx[mm]>key) r=mm-;
else if(xx[mm]<key) l=mm+;
}
} void build(int l,int r,int root){
a[root].l=l;
a[root].r=r;
a[root].ysum=a[root].sum=a[root].val=;
a[root].lv=a[root].rv=false;
if(l==r) return;
build(l,mid,ll);
build(mid+,r,rr);
} void up(int root){
if(a[root].val){
a[root].sum=xx[a[root].r+]-xx[a[root].l];
a[root].lv=a[root].rv=true;
a[root].ysum=;
}
else if(a[root].l==a[root].r) {
a[root].lv=a[root].rv=false;
a[root].ysum=a[root].sum=;
}
else{
a[root].sum=a[ll].sum+a[rr].sum;
a[root].lv=a[ll].lv;
a[root].rv=a[rr].rv;
a[root].ysum=a[ll].ysum+a[rr].ysum;
if(a[rr].lv&&a[ll].rv) a[root].ysum-=;
}
} void update(int l,int r,int val,int root){
if(a[root].l==l&&a[root].r==r){
a[root].val+=val;
up(root);
return;
}
if(l>=a[rr].l) update(l,r,val,rr);
else if(r<=a[ll].r) update(l,r,val,ll);
else{
update(l,mid,val,ll);
update(mid+,r,val,rr);
}
up(root);
} main()
{
int i, j, k;
int x1, y1, x2, y2;
int kase=;
while(scanf("%d",&n)==&&n){
m=;k=;
for(i=;i<n;i++){
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
line[k++]=Line(x1,x2,y1,);
line[k++]=Line(x1,x2,y2,-);
xx[m++]=x1;xx[m++]=x2;
}
sort(xx+,xx+m);
m=unique(xx+,xx+m)-xx-;
sort(line,line+k,cmp);
build(,m,);
int ans=;
int pre=;
for(i=;i<k-;i++){
update(b_s(line[i].x1),b_s(line[i].x2)-,line[i].val,);
ans+=abs(a[].sum-pre)+(line[i+].y-line[i].y)*a[].ysum;
pre=a[].sum;
}
update(b_s(line[k-].x1),b_s(line[k-].x2)-,line[k-].val,);
ans+=abs(a[].sum-pre);
printf("%d\n",ans);
}
}
HDU 1828 扫描线(矩形周长并)的更多相关文章
- hdu 1828 Picture(线段树扫描线矩形周长并)
线段树扫描线矩形周长并 #include <iostream> #include <cstdio> #include <algorithm> #include &l ...
- 扫描线矩形周长的并 POJ1177
//扫描线矩形周长的并 POJ1177 // 我是按x轴 #include <iostream> #include <cstdio> #include <cstdlib& ...
- hdu1828 Picture(线段树+扫描线+矩形周长)
看这篇博客前可以看一下扫描线求面积:线段树扫描线(一.Atlantis HDU - 1542(覆盖面积) 二.覆盖的面积 HDU - 1255(重叠两次的面积)) 解法一·:两次扫描线 如图我们可以 ...
- HDU 1828 Picture(线段树扫描线求周长)
Picture Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- HDU 1828“Picture”(线段树+扫描线求矩形周长并)
传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ...
- HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)
做这道题之前,建议先做POJ 1151 Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...
- hdu 1828 线段树扫描线(周长)
Picture Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU 1828 / POJ 1177 Picture --线段树求矩形周长并
题意:给n个矩形,求矩形周长并 解法:跟求矩形面积并差不多,不过线段树节点记录的为: len: 此区间线段长度 cover: 此区间是否被整个覆盖 lmark,rmark: 此区间左右端点是否被覆盖 ...
- HDU 1828 Picture(长方形的周长和)
HDU 1828 Picture 题目链接 题意:给定n个矩形,输出矩形周长并 思路:利用线段树去维护,分别从4个方向扫一次,每次多一段的时候,就查询该段未被覆盖的区间长度,然后周长就加上这个长度,4 ...
随机推荐
- Mysql 允许null 与 default值
分为下面4种情况: 1.允许null, 指定default值. 2.允许null, 不指定default,这个时候可认为default值就是null 3.不允许null,指定default值,不能指定 ...
- Single Number III
Description: Given an array of numbers nums, in which exactly two elements appear only once and all ...
- 转:如何学习SQL(第二部分:从关系角度理解SQL)
转自:http://blog.163.com/mig3719@126/blog/static/285720652010950825538/ 6. 从关系角度理解SQL 6.1. 关系和表 众所周知,我 ...
- 工作流学习——Activiti流程定义管理三步曲 (zhuan)
http://blog.csdn.net/zwk626542417/article/details/46602419 ***************************************** ...
- KD-tree
就是K维空间上的二叉查找树. 每个node对应k维空间的超矩形区域 在方差最大的维度上,比较数据与kd-tree的根节点.中间节点,在该维度上的中值处划分,得到新的子空间,直到不能再分. 用于最近邻查 ...
- 机器学习与R语言
此书网上有英文电子版:Machine Learning with R - Second Edition [eBook].pdf(附带源码) 评价本书:入门级的好书,介绍了多种机器学习方法,全部用R相关 ...
- for( unsigned int i=heapSize/2-1; i>=0; --i)
unsigned int的表示 今天在写堆排序的时候遇到一个BUG void builMaxHeap( int *arr,unsigned int heapSize){ unsigned int i; ...
- MySQL高可用性分析
版权声明:本文由易固武原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/203 来源:腾云阁 https://www.qclo ...
- Epplus使用教程1(基本介绍)
1.前言 目前Epplus的介绍中文资料很少,我也一直在摸索中使用它,以下是我在使用过程中得到的经验,写出来供大家参考.本系列共4章: 导出Excel之Epplus使用教程1(基本介绍) 导出Exce ...
- jmeter笔记3
1. 使用JMeter做性能测试(Windows) 1.1. 启动JMeter 下载JMeter的安装包,点击安装包\jakarta-jmeter-2.3RC4\bin下的jmeter.bat文件即 ...