题意:给定平面直角坐标系中的N个矩形,求它们的面积并。

题解:建立一个四元组(x,y1,y2,k).(假设y1<y2)用来储存每一条线,将每一条线按x坐标排序。记录所有的y坐标以后排序离散化。离散化之后线段树的第i个叶子节点储存的是y[i+1]-y[i].

这里的线段树用的是一个不用下传延迟标记的做法(仅限这一类题)。线段树的每一个节点维护length(这个节点的子节点覆盖的长度)和cnt(这个节点代表的线段[l,r]所覆盖的次数)。

任意一个区间都可以被线段树划分成O(logn)个子区间,我们把这些节点的cnt值加上1或减去1。

在修改任意一个节点的cnt时或者向上维护信息的时候,如果这个节点的cnt>0,则这个节点所代表的区间覆盖的长度是y[r+1]-y[l],否则是子节点的长度和。

代码:

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<map>
using namespace std;
map<double,int> mp;
const int maxn=200010;
struct ST{
int l,r,cnt;
double length;
}t[4*maxn];
struct node{
double x,y1,y2;
int k;
bool operator <(const node& rhs)const{
return x<rhs.x;
}
}a[maxn];
double b[maxn];
void build(int p,int l,int r){
t[p].l=l,t[p].r=r;
t[p].cnt=0;
t[p].length=0;
if(l==r){
return;
}
int mid=(l+r)/2;
build(p*2,l,mid);
build(p*2+1,mid+1,r);
}
void maintain(int p){
if(t[p].l==t[p].r)t[p].length=0;
else t[p].length=t[p*2].length+t[p*2+1].length;
}
void change(int p,int l,int r,int k){
if(l<=t[p].l&&r>=t[p].r){
t[p].cnt+=k;
if(t[p].cnt>0){
t[p].length=b[t[p].r+1]-b[t[p].l];
}
else
maintain(p);
return;
}
int mid=(t[p].l+t[p].r)/2;
if(mid>=l)change(p*2,l,r,k);
if(mid<r)change(p*2+1,l,r,k);
if(t[p].cnt>0)t[p].length=b[t[p].r+1]-b[t[p].l];
else maintain(p);
}
int main(){
int n,kase=0;
double x1,x2,y1,y2;
double ans=0;
while(~scanf("%d",&n)&&n){
ans=0;
mp.clear();
for(int i=1;i<=n;i++){
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
a[i]=(node){x1,y1,y2,1};
a[i+n]=(node){x2,y1,y2,-1};
b[i]=y1,b[i+n]=y2;
}
sort(a+1,a+1+2*n);
sort(b+1,b+1+2*n);
int m=unique(b+1,b+1+2*n)-(b+1);
for(int i=1;i<=m;i++)
mp[b[i]]=i;
build(1,1,m-1);
for(int i=1;i<2*n;i++){
int l=mp[a[i].y1],r=mp[a[i].y2]-1;
change(1,l,r,a[i].k);
ans+=(a[i+1].x-a[i].x)*(t[1].length);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n",++kase,ans);
}
return 0;
}

  

POJ 1151 扫描线 线段树的更多相关文章

  1. POJ 1151 - Atlantis 线段树+扫描线..

    离散化: 将所有的x轴坐标存在一个数组里..排序.当进入一条线段时..通过二分的方式确定其左右点对应的离散值... 扫描线..可以看成一根平行于x轴的直线..至y=0开始往上扫..直到扫出最后一条平行 ...

  2. POJ 1151 Atlantis 线段树求矩形面积并 方法详解

    第一次做线段树扫描法的题,网搜各种讲解,发现大多数都讲得太过简洁,不是太容易理解.所以自己打算写一个详细的.看完必会o(∩_∩)o 顾名思义,扫描法就是用一根想象中的线扫过所有矩形,在写代码的过程中, ...

  3. hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  4. POJ 1151 Atlantis 线段树+离散化+扫描线

    这次是求矩形面积并 /* Problem: 1151 User: 96655 Memory: 716K Time: 0MS Language: G++ Result: Accepted */ #inc ...

  5. POJ 1151Atlantis 扫描线+线段树求矩形面积并

    题目链接 #include <iostream> #include <vector> #include <cstdio> #include <cstring& ...

  6. HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)

    Description Ted has a new house with a huge window. In this big summer, Ted decides to decorate the ...

  7. HDU 3642 - Get The Treasury - [加强版扫描线+线段树]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3642 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  8. 【BZOJ3958】[WF2011]Mummy Madness 二分+扫描线+线段树

    [BZOJ3958][WF2011]Mummy Madness Description 在2011年ACM-ICPC World Finals上的一次游览中,你碰到了一个埃及古墓. 不幸的是,你打开了 ...

  9. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

随机推荐

  1. 解决:Eclipse安装Pydev插件报错: An error occurred while collecting items to be installed session context was:(profile=...

    今天在Elipse上安装Pydev插件时报错: An error occurred while collecting items to be installed session context was ...

  2. 解决:AttributeError: module 'requests' has no attribute 'get'”

    今天学习Requests库,当用pip install requests安装后,写了一段代码报错:AttributeError: module 'requests' has no attribute ...

  3. 设置SSH自动登陆(免密码,用户名)

    设置SSH自动登陆(免密码,用户名)   1.创建公钥.公钥  ssh-keygen -t rsa  无视它出来的任何提示,欢快的一路回车到底吧.  2.把公钥 id_rsa.pub 复制到远程机器的 ...

  4. POJ - 2079:Triangle (旋转卡壳,求最大三角形)

    Given n distinct points on a plane, your task is to find the triangle that have the maximum area, wh ...

  5. 【java规则引擎】之规则引擎解释

    转载:http://www.open-open.com/lib/view/open1417528754230.html 现实生活中,规则无处不在.法律.法规和各种制度均是:对于企业级应用来说,在IT技 ...

  6. Java处理乱码问题

    中文乱码分为GET乱码和POST乱码 GET乱码在Tomcat中配置编码 <Connector port="8080" protocol="HTTP/1.1&quo ...

  7. 如何打开 Windows 的热键提示

    如何打开 Windows 的热键提示 很早之前我记的 Windows 是有热键提示的,不知道什么时候开始默认不显示了,难道 微软是为是美观? 找了一下原来是有地方可以设置的.

  8. bzoj 1798 [Ahoi2009]Seq 维护序列seq ——线段树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1798 先乘后加,就可给加法标记乘上乘法标记. 注意可能有 *0 的操作,所以 pshd 时不 ...

  9. BZOJ1047:[HAOI2007]理想的正方形

    浅谈队列:https://www.cnblogs.com/AKMer/p/10314965.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?i ...

  10. TreeView 树节点的处理

    TreeView 树节点的处理 using System; using System.Collections.Generic; using System.ComponentModel; using S ...