求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度。

我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可。因为最后的轮廓必定是由不重合的线段长度组成的,这样理论上是对的

要注意处理高度相同的线段,把底边优先处理(在代码里就是f标记为1的线段),因为若是一个矩形的底边和另一个矩形的上边重合,则这个轮廓肯定不能算

不过POJ和HDU的数据好像都比较弱,我没进行上面的细节处理也AC了,不过一个很简单的数据就会不对,所以还是要处理一下才是真正正确的代码

我之前敲代码的时候还想起在线段树里面的覆盖要涉及懒惰标记(因为是对区间进行覆盖嘛,没向子走)和向上更新。。。不过想了一下不用,这个线段树不是普通那种,因为总是先覆盖底边,然后由等长的上边来解除覆盖,所以区间总是相对不变的,没必要进行向上或者向下更新

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid,r
using namespace std;
const int N=10010;
struct node
{
int lx,ly,hx,hy;
}rec[N];
struct node2
{
int l,r,h,f;
bool operator <(const node2& rhs) const{
if (h==rhs.h){
return f>rhs.f;
}
else return h<rhs.h;
}
}seg[N*2];
int X[N*2];
int n;
int flag[N<<2];
int d[N<<2];
void build(int rt,int l,int r)
{
flag[rt]=0;
d[rt]=0;
if (r-l<=1) return;
int mid=(l+r)>>1;
build(lson);
build(rson);
}
void up(int rt,int l,int r)
{
if (flag[rt]>0){
d[rt]=X[r]-X[l];
}
else
{
if (r-l<=1) d[rt]=0;
else d[rt]=d[rt<<1]+d[rt<<1|1];
}
}
void cover(int L,int R,int v,int rt,int l,int r)
{
if (L<=l && r<=R)
{
flag[rt]+=v;
up(rt,l,r);
return;
}
if (r-l<=1) return ;
int mid=(l+r)>>1;
if (R<=mid) cover(L,R,v,lson);
else
if (L>mid) cover(L,R,v,rson);
else
{
cover(L,R,v,lson);
cover(L,R,v,rson);
}
up(rt,l,r);
}
int main()
{
while (scanf("%d",&n)!=EOF)
{
int cnt=0;
for (int i=0;i<n;i++){
scanf("%d%d%d%d",&rec[i].lx,&rec[i].ly,&rec[i].hx,&rec[i].hy);
X[++cnt]=rec[i].lx;
seg[cnt].l=rec[i].lx;
seg[cnt].r=rec[i].hx;
seg[cnt].h=rec[i].ly;
seg[cnt].f=1; X[++cnt]=rec[i].hx;
seg[cnt].l=rec[i].lx;
seg[cnt].r=rec[i].hx;
seg[cnt].h=rec[i].hy;
seg[cnt].f=-1;
}
int m=1;
sort(X+1,X+1+cnt);
sort(seg+1,seg+1+cnt);
for(int i=2;i<=cnt;i++){
if (X[i]!=X[i-1]){
X[++m]=X[i];
}
}
int ans=0;
build(1,1,m);
//for (int i=1;i<=m;i++) cout<<X[i]<<endl;
for (int i=1;i<=cnt;i++){
//cout<<seg[i].l<<" "<<seg[i].r<<" "<<seg[i].f<<endl;
int l=lower_bound(X+1,X+1+m,seg[i].l)-X;
int r=lower_bound(X+1,X+1+m,seg[i].r)-X;
int tmp=d[1];
//cout<<X[l]<<" xxx "<<X[r]<<endl;
// cout<<"bf: "<<d[1]<<endl;
cover(l,r,seg[i].f,1,1,m);
tmp=d[1]-tmp;
if (tmp<0) tmp=-tmp;
//cout<<"af: "<<d[1]<<endl;
//cout<<tmp<<endl;
ans+=tmp;
}
//cout<<ans<<endl;
cnt=0;
for (int i=0;i<n;i++){
X[++cnt]=rec[i].ly;
seg[cnt].l=rec[i].ly;
seg[cnt].r=rec[i].hy;
seg[cnt].h=rec[i].lx;
seg[cnt].f=1; X[++cnt]=rec[i].hy;
seg[cnt].l=rec[i].ly;
seg[cnt].r=rec[i].hy;
seg[cnt].h=rec[i].hx;
seg[cnt].f=-1;
}
sort(X+1,X+1+cnt);
sort(seg+1,seg+1+cnt);
m=1;
for (int i=2;i<=cnt;i++){
if (X[i]!=X[i-1]){
X[++m]=X[i];
}
}
build(1,1,m);
for (int i=1;i<=cnt;i++){
int l=lower_bound(X+1,X+1+m,seg[i].l)-X;
int r=lower_bound(X+1,X+1+m,seg[i].r)-X;
int tmp=d[1];
cover(l,r,seg[i].f,1,1,m);
tmp=d[1]-tmp;
if (tmp<0) tmp=-tmp;
ans+=tmp;
}
printf("%d\n",ans);
}
return 0;
}
/*
2
0 0 5 2
2 -2 4 3
*/

  

POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算的更多相关文章

  1. hdu 1828 Picture(线段树 || 普通hash标记)

    http://acm.hdu.edu.cn/showproblem.php?pid=1828 Picture Time Limit: 6000/2000 MS (Java/Others)    Mem ...

  2. POJ 1151Atlantis 矩形面积并[线段树 离散化 扫描线]

    Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21734   Accepted: 8179 Descrip ...

  3. HDU 1828 Picture (线段树+扫描线)(周长并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1828 给你n个矩形,让你求出总的周长. 类似面积并,面积并是扫描一次,周长并是扫描了两次,x轴一次,y ...

  4. hdu 1828 Picture(线段树轮廓线)

    Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  5. HDU 1828 Picture (线段树:扫描线周长)

    依然是扫描线,只不过是求所有矩形覆盖之后形成的图形的周长. 容易发现,扫描线中的某一条横边对答案的贡献. 其实就是 加上/去掉这条边之前的答案 和 加上/去掉这条边之后的答案 之差的绝对值 然后横着竖 ...

  6. POJ1177 Picture 线段树+离散化+扫描线

    求最终的覆盖图形周长,写这种代码应该短而精确,差的比较远 /* Problem: 1177 User: 96655 Memory: 348K Time: 32MS Language: C++ Resu ...

  7. 【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)

    [POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  8. hdu1542 矩形面积并(线段树+离散化+扫描线)

    题意: 给你n个矩形,输入每个矩形的左上角坐标和右下角坐标. 然后求矩形的总面积.(矩形可能相交). 题解: 前言: 先说说做这道题的感受: 刚看到这道题顿时就懵逼了,几何 烂的渣渣.后来从网上搜题解 ...

  9. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

随机推荐

  1. Java编程打开运行exe程序

    String path = "notepad.exe"; //(C:\Program Files\Tencent\QQ\Bin\qq.exe) try { Runtime runt ...

  2. SVM数学原理推导

    //2019.08.17 #支撑向量机SVM(Support Vector Machine)1.支撑向量机SVM是一种非常重要和广泛的机器学习算法,它的算法出发点是尽可能找到最优的决策边界,使得模型的 ...

  3. Melodic 使用URDF创建简单的机器人模型

    本人Linux版本:Ubuntu 18.04LTS ROS版本:Melodic URDF代码 <?xml version="1.0" ?> <robot name ...

  4. 格式化JSON插件

    参考:https://www.cnblogs.com/whycxb/p/7126116.html

  5. windows下用libevent 开发一个echo服务

    #include <stdio.h> #include <string.h> #include <errno.h> #include <iostream> ...

  6. 前端学习笔记系列一:6 一种新的css预编译器stylus

    stylus是 CSS 的预处理框架.CSS 预处理,顾名思义,预先处理 CSS.那 stylus 咋预先处理呢?stylus 给 CSS 添加了可编程的特性,也就是说,在 stylus 中可以使用变 ...

  7. rinetd 进行转发

    目前云数据库 Redis 版需要通过 ECS 进行内网连接访问.如果您本地需要通过公网访问云数据库 Redis,可以在 ECS Linux 云服务器中安装 rinetd 进行转发实现. 在云服务器 E ...

  8. 非阻塞多路IO

    socket.listen() rfds=[] wfds=[] while(select(rfds,wfds,timeout)){//事件循环 client=socket.accept(timeout ...

  9. Vue 中使用Ajax请求

    Vue 项目中常用的 2 个 ajax 库 (一)vue-resource vue 插件, 非官方库,vue1.x 使用广泛 vue-resource 的使用 在线文档   https://githu ...

  10. Jumpserver docker-compose 随手记

    wget  或  git clone   docker  build  -t   jumpserver:v1   .     #构建镜像   docker images vim  jumpserver ...