//扫描线矩形周长的并 POJ1177
// 我是按x轴 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstring>
// #include <memory.h>
// #include <bits/stdc++.h>
using namespace std;
#define LL long long
typedef pair<int,int> pii;
const LL inf = 0x3f3f3f3f;
const LL MOD =100000000LL;
const int N = ;
const double eps = 1e-;
void fre() {freopen("in.txt","r",stdin);}
void freout() {freopen("out.txt","w",stdout);}
inline int read() {int x=,f=;char ch=getchar();while(ch>''||ch<'') {if(ch=='-') f=-; ch=getchar();}while(ch>=''&&ch<='') {x=x*+ch-'';ch=getchar();}return x*f;} struct node{
int x1,x2,y;
int flag;
bool operator < (const node &a) const{
return y<a.y||(y==a.y&&flag>a.flag);
}
}e[N<<]; int color[N<<];
int sum[N<<],hashh[N<<];
int cnt[N<<],pl[N<<],pr[N<<];
void pushup(int rt,int l,int r){
if(color[rt]) {
sum[rt]=hashh[r+]-hashh[l];
cnt[rt]=;
pl[rt]=pr[rt]=;
}
else if(l!=r) {
sum[rt]=sum[rt<<]+sum[rt<<|];
cnt[rt]=cnt[rt<<]+cnt[rt<<|]-(pr[rt<<]&&pl[rt<<|]);
pr[rt]=pr[rt<<|];
pl[rt]=pl[rt<<];
}
else sum[rt]=cnt[rt]=pl[rt]=pr[rt]=;
}
void update(int l,int r,int L,int R,int rt,int f){
if(l<=L&&R<=r){
color[rt]+=f;
pushup(rt,L,R);
return;
}
int mid=(L+R)>>;
if(l<=mid) update(l,r,L,mid,rt<<,f);
if(r>mid) update(l,r,mid+,R,rt<<|,f);
pushup(rt,L,R);
} int main(){
// fre();
int n;
scanf("%d",&n);
memset(color,,sizeof(color));
memset(sum,,sizeof(sum));
memset(cnt,,sizeof(cnt));
memset(pr,,sizeof(pr));
memset(pl,,sizeof(pl));
int x1,x2,y1,y2;
for(int i=;i<=n;i++){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
e[i*-].x1=e[i*].x1=x1;
e[i*-].x2=e[i*].x2=x2;
e[i*-].y=y1,e[i*].y=y2;
e[i*-].flag=,e[i*].flag=-;
hashh[i*-]=x1,hashh[i*]=x2;
}
sort(e+,e++*n);
sort(hashh+,hashh++*n);
int ans=;
int lastsum=;
e[].y=e[].y;
for(int i=;i<=*n;i++){
ans+=(e[i].y-e[i-].y)**cnt[];
int l=lower_bound(hashh+,hashh++*n,e[i].x1)-hashh;
int r=lower_bound(hashh+,hashh++*n,e[i].x2)-hashh-;
if(l<=r) update(l,r,,*n,,e[i].flag);
ans+=abs(sum[]-lastsum);
lastsum=sum[];
}
printf("%d\n",ans);
return ;
}

扫描线矩形周长的并 POJ1177的更多相关文章

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

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

  2. hdu1828 Picture(线段树+扫描线+矩形周长)

    看这篇博客前可以看一下扫描线求面积:线段树扫描线(一.Atlantis HDU - 1542(覆盖面积) 二.覆盖的面积 HDU - 1255(重叠两次的面积))  解法一·:两次扫描线 如图我们可以 ...

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

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

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

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

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

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

  6. HDU 1828“Picture”(线段树+扫描线求矩形周长并)

    传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ...

  7. 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( ...

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

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

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

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

随机推荐

  1. 20.multi_case06

    # coding:utf-8 import asyncio # 通过create_task()方法 async def a(t): print('-->', t) await asyncio.s ...

  2. Java中循环体的初步了解以及另一种随机数的获取方法

    Math中的相关操作 随机数 Java中除了可以直接导入Random类,获取随机数,还可以通过本身自带的Math方法去获取随机数.Math.random()可以产生随机小数,区间范围为[0.0,1.0 ...

  3. 13_springmvc拦截器应用

    一.实现登录认证 1.需求: 用户请求url,拦截器进行拦截校验 如果请求的url是公开地址(无需登陆即可访问的url),让放行 如果用户session 不存在跳转到登陆页面 如果用户session存 ...

  4. 数据可视化(matplotilb)

    一,matplotilb库(数学绘图库) mat数学 plot绘图  lib库 matplotlib.pyplot(缩写mp)->python 最常用接口 mp.plot(水平坐标,垂直坐标数组 ...

  5. Spring 事务传播行为(12)

    事务传播行为 指定是Spring中一个事务方法调用另一个事务方法时.处理的行为 使用方式: @Transactional(propagation=Propagation.REQUIRED) 事务的使用 ...

  6. WPF 字体描边的实现方式

    原文:WPF 字体描边的实现方式   <local:TextPath x:Name="PathEdge" Fill="Red" Stroke=" ...

  7. JS数组 组团(如何创建数组)var mychar = new Array( )

    组团,并给团取个名(如何创建数组) 使用数组之前首先要创建,而且需要把数组本身赋至一个变量.好比我们出游,要组团,并给团定个名字"云南之旅". 创建数组语法: var myarra ...

  8. C++函数模板&类模板

    函数模板 模板概念及语法 主要目的,简化代码,减少重复代码.基本语法格式:  template<class T> 或者 template<typename T> //末尾不加分 ...

  9. java定时器demo

    package cn.threadtest.thread; import java.util.Date; import java.util.Timer; import java.util.TimerT ...

  10. FreeMarker 自定义 TemplateDirectiveModel(二)

    FreeMarker 是一个用 Java 语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker 与 Web 容器无关,即在 Web 运行时,它并不知道 Servlet 或 HTTP.它不 ...