线段树扫描线的模板题,一个月前写的发现忘了一些还是要看看以前的博客呀!

/*
思路:数据小不用离散化处理,线段树叶子结点维护一个区间
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define maxn 20005
struct Seg{
int l,r,h,s;
Seg(){}
Seg(int a,int b,int c,int d):l(a),r(b),h(c),s(d){}
bool operator<(const Seg & a)const {
//if(h==a.h) return s>a.s;
return h<a.h;
}
}ss[maxn];
bool lbd[maxn<<],rbd[maxn<<];//被覆盖的区域是否和左右区间线重合
int numseg[maxn<<],cnt[maxn<<],len[maxn<<];
void pushup(int rt,int l,int r){
if(cnt[rt]){
lbd[rt]=rbd[rt]=;
len[rt]=r-l;
numseg[rt]=;
}
else if(l+==r)
len[rt]=numseg[rt]=rbd[rt]=lbd[rt]=;
else {
lbd[rt]=lbd[rt<<];
rbd[rt]=rbd[rt<<|];
len[rt]=len[rt<<]+len[rt<<|];
numseg[rt]=numseg[rt<<]+numseg[rt<<|];
if(lbd[rt<<|] && rbd[rt<<]) numseg[rt]-=;
}
}
#define lson l,m,rt<<1
#define rson m,r,rt<<1|1
void update(int L,int R,int c,int l,int r,int rt){
if(L<=l && R>=r){
cnt[rt]+=c;
pushup(rt,l,r);
return;
}
int m=l+r>>;
if(L<m) update(L,R,c,lson);
if(R>m) update(L,R,c,rson);
pushup(rt,l,r);
}
void init(){
memset(lbd,,sizeof lbd);
memset(rbd,,sizeof rbd);
memset(cnt,,sizeof cnt);
memset(len,,sizeof len);
memset(numseg,,sizeof numseg);
}
int main(){
int n;
while(scanf("%d",&n)==){
init();
int m=,x1,y1,x2,y2,lbd=,rbd=-,last=,ans=;
for(int i=;i<=n;i++){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
ss[m++]=Seg(x1,x2,y1,);
ss[m++]=Seg(x1,x2,y2,-);
lbd=min(lbd,x1),rbd=max(rbd,x2);
}
sort(ss,ss+m);
for(int i=;i<m;i++){
update(ss[i].l,ss[i].r,ss[i].s,lbd,rbd,);
ans+=abs(len[]-last);last=len[];
ans+=numseg[]*(ss[i+].h-ss[i].h);
}
printf("%d\n",ans);
}
return ;
}

poj1177 矩形周长并的更多相关文章

  1. 扫描线矩形周长的并 POJ1177

    //扫描线矩形周长的并 POJ1177 // 我是按x轴 #include <iostream> #include <cstdio> #include <cstdlib& ...

  2. 25.按要求编写一个Java应用程序: (1)编写一个矩形类Rect,包含: 两个属性:矩形的宽width;矩形的高height。 两个构造方法: 1.一个带有两个参数的构造方法,用于将width和height属性初化; 2.一个不带参数的构造方法,将矩形初始化为宽和高都为10。 两个方法: 求矩形面积的方法area() 求矩形周长的方法perimeter() (2)通过继承Rect类编写一个具有

    package zhongqiuzuoye; //自己写的方法 public class Rect { public double width; public double height; Rect( ...

  3. HDU 1828 / POJ 1177 Picture --线段树求矩形周长并

    题意:给n个矩形,求矩形周长并 解法:跟求矩形面积并差不多,不过线段树节点记录的为: len: 此区间线段长度 cover: 此区间是否被整个覆盖 lmark,rmark: 此区间左右端点是否被覆盖 ...

  4. HDU 1828 扫描线(矩形周长并)

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

  5. HDU 6362(求椭圆中矩形周长的期望 数学)

    题意是给定一个椭圆标准方程的a,b(椭圆的长半轴长和短半轴长),在[0,b]内取一个数,则过点(0,b)且平行于x轴的直线与椭圆交于两点,再将此两点关于x轴做对称点,顺次连接此四点构成矩形,求出这些矩 ...

  6. hdu 1828 Picture(线段树扫描线矩形周长并)

    线段树扫描线矩形周长并 #include <iostream> #include <cstdio> #include <algorithm> #include &l ...

  7. 51nod 1206 Picture 矩形周长求并 | 线段树 扫描线

    51nod 1206 Picture 矩形周长求并 | 线段树 扫描线 #include <cstdio> #include <cmath> #include <cstr ...

  8. P1856 [USACO5.5]矩形周长Picture

    P1856 [USACO5.5]矩形周长Picture $len$            $sum$              $num$             $flag\_l$ $flage\_ ...

  9. P1856 [USACO5.5]矩形周长Picture[扫描线]

    题目背景 墙上贴着许多形状相同的海报.照片.它们的边都是水平和垂直的.每个矩形图片可能部分或全部的覆盖了其他图片.所有矩形合并后的边长称为周长. 题目描述 编写一个程序计算周长. 如图1所示7个矩形. ...

随机推荐

  1. memcache、redis原理对比

    一.问题:     数据库表数据量极大(千万条),要求让服务器更加快速地响应用户的需求.   二.解决方案:      1.通过高速服务器Cache缓存数据库数据      2.内存数据库     ( ...

  2. nltk——文本分类

      sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003 ...

  3. 51job_selenium测试

    Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...

  4. Jenkins插件安装实战篇

    Jenkins插件安装实战篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 上篇博客我介绍了Jenkins是啥,以及持续集成,持续交付,持续部署的概念,那么问题来了:你知道CI和C ...

  5. HDU - 3973 AC's String(Hash+线段树)

    http://acm.hdu.edu.cn/showproblem.php?pid=3973 题意 给一个词典和一个主串.有两种操作,查询主串某个区间,问这主串区间中包含多少词典中的词语.修改主串某一 ...

  6. java元注解 @Retention注解使用

    @Retention定义了该Annotation被保留的时间长短: 1.某些Annotation仅出现在源代码中,而被编译器丢弃: 2.另一些却被编译在class文件中,注解保留在class文件中,在 ...

  7. currentColor

    http://www.zhangxinxu.com/wordpress/2014/10/currentcolor-css3-powerful-css-keyword/

  8. JS中的call()方法和apply()方法用法总结

    原文引自:https://blog.csdn.net/ganyingxie123456/article/details/70855586 最近又遇到了JacvaScript中的call()方法和app ...

  9. 01、@ConfigurationProperties 将属性文件里的值映射到JavaBean

    @ConfigurationProperties("person") //或是prefix属性 @Component //必须注册成容器中的bean被容器管理起来 public c ...

  10. 实现一个string类

    实现要实现:构造,析构,拷贝构造,赋值的功能 1. 提供构造函数 string(), string(const string & str),string(const  char * str), ...