求最终的覆盖图形周长,写这种代码应该短而精确,差的比较远

/*
Problem: 1177        User: 96655
Memory: 348K        Time: 32MS
Language: C++        Result: Accepted
*/
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include <algorithm>
using namespace std;
const int maxn=10010;
struct Node
{int s,t,num,len,cover;bool lb,rb;void change(int o){cover+=o;if(cover==0)len=lb=rb=num=0;else len=t-s,lb=1,rb=1,num=1;}
} node[maxn<<2];
struct Line
{int x,y1,y2,flag;void fun(int a,int b,int c,int d){x=a,y1=b,y2=c,flag=d;}bool operator<(const Line &e)const{if(x==e.x)return flag>e.flag;return x<e.x;}
} line[maxn];
int y[maxn];
void build(int rt,int l,int r)
{node[rt].s=y[l];node[rt].t=y[r];node[rt].num=node[rt].len=node[rt].cover=0;if(l+1==r)return;int m=(l+r)>>1;build(rt*2,l,m);build(rt*2+1,m,r);
}
void update_line(int rt)
{node[rt].lb=node[rt*2].lb;node[rt].rb=node[rt*2+1].rb;node[rt].num=node[rt*2].num+node[rt*2+1].num-node[rt*2].rb*node[rt*2+1].lb;
}
void update_len(int rt)
{node[rt].len=node[rt*2].len+node[rt*2+1].len;
}
void update(int rt,int l,int r,Line e)
{if(l+1==r){node[rt].change(e.flag);return;}int m=(l+r)>>1;if(e.y1<node[rt*2].t)update(rt*2,l,m,e);if(e.y2>node[rt*2+1].s)update(rt*2+1,m,r,e);update_len(rt);update_line(rt);
}
int main()
{int n,x1,x2,y1,y2,cnt=0,d=1;scanf("%d",&n);for(int i=1; i<=n; i++){scanf("%d%d%d%d",&x1,&y1,&x2,&y2);line[++cnt].fun(x1,y1,y2,1);y[cnt]=y1;line[++cnt].fun(x2,y1,y2,-1);y[cnt]=y2;}sort(y+1,y+1+cnt);sort(line+1,line+1+cnt);for(int i=2; i<=cnt; ++i)if(y[i]!=y[i-1])y[++d]=y[i];build(1,1,d);int perimeter=0;int now_len=0;int now_num=0;for(int i=1; i<=cnt; ++i){update(1,1,d,line[i]);if(i>1)perimeter+=2*now_num*(line[i].x-line[i-1].x);perimeter+=abs(node[1].len-now_len);now_num=node[1].num;now_len=node[1].len;}printf("%d\n",perimeter);return 0;
}

View Code

转载于:https://www.cnblogs.com/shuguangzw/p/4956291.html

POJ1177 Picture 线段树+离散化+扫描线相关推荐

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

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

  2. poj 1177 线段树+离散化+扫描线 求矩形并的轮廓长

    Picture 题意:求矩形面积并额轮廓长 解法:扫描线+离散化+线段树 做法:等于更新操作前后的tree[1].len差,做法这么巧妙实际我也不知道为什么.差不多的意思就是更新操作的cover变化长 ...

  3. HDU 1542 Atlantis 线段树+离散化+扫描线

    题意:给出一些矩形的最上角坐标和右下角坐标,求这些矩形的面积并. NotOnlySuccess 线段树专辑中扫描线模板题,弱智的我对着大大的代码看了一下午才搞懂. 具体见思路见注释=.= #inclu ...

  4. poj--1177(线段树+离散化+扫描线)

    题目:http://poj.org/problem?id=1177 A number of rectangular posters, photographs and other pictures of ...

  5. 线段树辅助——扫描线法计算矩形面积并

    线段树辅助--扫描线法计算矩形面积并 本篇文章转自:传送门 分析: 1.矩形比较多,坐标也很大,所以横坐标需要离散化(纵坐标不需要),熟悉离散化后这个步骤不难,所以这里不详细讲解了,不明白的还请百度 ...

  6. HDU 1828 线段树之扫描线之周长并

    点击打开链接 题意:给n个矩形,求它们重叠后的周长 思路:用线段树的扫描线从下到上扫一遍,与面积并思想有些相似面积并,下面重边的处理相似,但是周长的并需要求的是竖边的个数然后乘以高度,而面积并求的是底 ...

  7. poj2528贴海报(线段树离散化)

    //poj2528贴海报(线段树离散化) #include<cstring> #include<iostream> #include<cstdio> #includ ...

  8. poj 2528 Mayor's posters(线段树+离散化)

    1 /* 2 poj 2528 Mayor's posters 3 线段树 + 离散化 4 5 离散化的理解: 6 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用 ...

  9. HDOJ 2492 Ping pong 线段树+离散化

    //2492 Ping pong 线段树+离散化 /* 题意: 有一陀人从左到右排成一排,每个人有一个唯一的技能值,每个人都找其他人比赛, 比赛前要再找一个人做裁判,裁判的技能值不能比这两个人都高,也 ...

最新文章

  1. 一次gitlab无法使用ssh登录的问题解决
  2. ❀❀ selenium 学习网站 ★★★★★
  3. Mysql大数据备份和增量备份及还原
  4. vba 自动换行_Excel中quot;强制换行quot;quot;快速求和quot;等操作,这9个AlT键技巧全都包含了...
  5. 越优秀的人越努力,越努力的人越幸运!
  6. Spring 处理请求和响应相关的注解
  7. Linus 07年在 Google讲座介绍Git的特点和设计思路
  8. java实现kotlin接口_Kotlin 接口与 Java8 新特性接口详解
  9. ts视频文件的爬取与合并
  10. 【20220912】电商业务的核心流程
  11. 电驴服务器更新的作用,怎样更新eMule(电驴)服务器列表,有什么好处?
  12. 实验:Android案例——小宝宝装备选择
  13. 一个程序员应该怎样去学习和掌握计算机英语
  14. Structure of Heap
  15. 基于微信小程序电子产品商城系统(springboot+ mybatis-plus+mysql+原生微信小程序)
  16. 学习率设置的学问(如何设置学习率)
  17. 跨境电商必读:什么是社交媒体营销?
  18. CobaltStrike 部署
  19. 信息论与编码 python实现 费诺编码 代码详解
  20. Vue中ref的作用???

热门文章

  1. 3dmax导出到unity3d下分割动画
  2. 作为一名前端开发工程师,你必须掌握的WEB模板引擎:Handlebars
  3. 密码学基本概念(一)
  4. 再利用Chakra引擎绕过CFG
  5. Cocos2D将v1.0的tileMap游戏转换到v3.4中一例(八)
  6. .NET4.0 之 Dynamic VS Refle“.NET研究”ction 效率
  7. 用户体验可视化指南pdf_R中增强可视化的初学者指南
  8. 山师计算机专业研究生怎么样,山东师范大学有计算机专业硕士吗?
  9. linux中gradle编译慢,【Linux】解决linux下android studio用gradle构建从jcenter或maven下载依赖太慢...
  10. 在Java里重写equals和hashCode要注意什么问题