扫描线矩形周长的并 POJ1177
//扫描线矩形周长的并 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的更多相关文章
- hdu 1828 Picture(线段树扫描线矩形周长并)
线段树扫描线矩形周长并 #include <iostream> #include <cstdio> #include <algorithm> #include &l ...
- hdu1828 Picture(线段树+扫描线+矩形周长)
看这篇博客前可以看一下扫描线求面积:线段树扫描线(一.Atlantis HDU - 1542(覆盖面积) 二.覆盖的面积 HDU - 1255(重叠两次的面积)) 解法一·:两次扫描线 如图我们可以 ...
- HDU 1828 扫描线(矩形周长并)
Picture Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- 51nod 1206 Picture 矩形周长求并 | 线段树 扫描线
51nod 1206 Picture 矩形周长求并 | 线段树 扫描线 #include <cstdio> #include <cmath> #include <cstr ...
- P1856 [USACO5.5]矩形周长Picture[扫描线]
题目背景 墙上贴着许多形状相同的海报.照片.它们的边都是水平和垂直的.每个矩形图片可能部分或全部的覆盖了其他图片.所有矩形合并后的边长称为周长. 题目描述 编写一个程序计算周长. 如图1所示7个矩形. ...
- HDU 1828“Picture”(线段树+扫描线求矩形周长并)
传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ...
- 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( ...
- HDU 1828 / POJ 1177 Picture --线段树求矩形周长并
题意:给n个矩形,求矩形周长并 解法:跟求矩形面积并差不多,不过线段树节点记录的为: len: 此区间线段长度 cover: 此区间是否被整个覆盖 lmark,rmark: 此区间左右端点是否被覆盖 ...
- HDU 6362(求椭圆中矩形周长的期望 数学)
题意是给定一个椭圆标准方程的a,b(椭圆的长半轴长和短半轴长),在[0,b]内取一个数,则过点(0,b)且平行于x轴的直线与椭圆交于两点,再将此两点关于x轴做对称点,顺次连接此四点构成矩形,求出这些矩 ...
随机推荐
- 学SpringBoot一篇就够了
1.SpringBoot概述 Spring 框架对于很多 Java 开发人员来说都不陌生.自从 2002 年发布以来,Spring 框架已经成为企业应用开发领域非常流行的基础框架.有大量的企业应用基于 ...
- linux及windows安装maven
一.linux安装maven 1.wget http://mirror.bit.edu.cn/apache/maven/maven-3/3.6.1/binaries/apache-maven-3.6. ...
- Python中死锁的形成示例及死锁情况的防止
死锁示例搞多线程的经常会遇到死锁的问题,学习操作系统的时候会讲到死锁相关的东西,我们用Python直观的演示一下.死锁的一个原因是互斥锁.假设银行系统中,用户a试图转账100块给用户b,与此同时用户b ...
- 关于underline
1, 有些系统可能看不到下划线的效果,这是不妨按着Alt键看看: 2, underline=#, 数字表示在哪个字母下显示下划线,从0开始
- 开源的DirectUI界面开发库DUILIB试用demo (Win32程序)
Demo 第三版源码VC2005工程(附全部.h,ansi/unicode lib,dll),下载地址:http://goo.gl/c0L7Q 开源项目地址:http://code.google. ...
- springboot与任务(异步任务)
描述:在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在Spring 3.x ...
- 深入浅出 Java Concurrency (26): 并发容器 part 11 Exchanger[转]
可以在对中对元素进行配对和交换的线程的同步点.每个线程将条目上的某个方法呈现给 exchange 方法,与伙伴线程进行匹配,并且在返回时接收其伙伴的对象.Exchanger 可能被视为 Synchro ...
- 渗透测试入门DVWA 环境搭建
DVWA是一款渗透测试的演练系统,在圈子里是很出名的.如果你需要入门,并且找不到合适的靶机,那我就推荐你用DVWA. 我们通常将演练系统称为靶机,下面请跟着我一起搭建DVWA测试环境.如果你有一定的基 ...
- 2019 西电ACM校赛网络赛 题解
今年题目难度有较大提升,总体与往年类似,数学题居多.以下为我通过的部分题解. 赛题链接:http://acm.xidian.edu.cn/contest.php?cid=1053 A - 上帝视角 我 ...
- Liunx常用命令行(Ubuntu)
关闭防火墙的命令行: 1. 永久性生效 开启:chkconfig iptables on 关闭:chkconfig iptables off 2. 即时生效,重启后失效 开启:service ipta ...