题意:求矩形面积的并

思路:

注意是[l,mid][mid,r] 这是真正的线段了

就当扫描线模板使吧~

//By SiriusRen
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define eps 1e-5
int N,tot,n,cases;
double y[666],ans=0;
struct Node{double x,y1,y2;int cover;}node[666];
struct Tree{double l,r,len;int cover;}tree[66666];
bool cmp(Node a,Node b){return a.x<b.x;}
void build(int l,int r,int pos){
tree[pos].l=y[l],tree[pos].r=y[r],tree[pos].len=tree[pos].cover=0;
if(l+1==r)return;
int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
build(l,mid,lson),build(mid,r,rson);
}
void push_up(int l,int r,int pos){
if(tree[pos].cover>0)
tree[pos].len=tree[pos].r-tree[pos].l;
else if(l+1==r)tree[pos].len=0;
else tree[pos].len=tree[pos<<1].len+tree[pos<<1|1].len;
}
void update(int l,int r,int pos,Node jy){
if(tree[pos].l>=jy.y1&&tree[pos].r<=jy.y2){
tree[pos].cover+=jy.cover;push_up(l,r,pos);
return;
}
int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
if(tree[lson].r>=jy.y2)update(l,mid,lson,jy);
else if(tree[rson].l<=jy.y1)update(mid,r,rson,jy);
else update(l,mid,lson,jy),update(mid,r,rson,jy);
push_up(l,r,pos);
}
int main(){
while(scanf("%d",&N)&&N){
ans=tot=0;
for(int i=1;i<=N;i++){
double X1,Y1,X2,Y2;
scanf("%lf%lf%lf%lf",&X1,&Y1,&X2,&Y2);
node[++tot].x=X1,node[tot].y1=Y1,node[tot].y2=Y2;node[tot].cover=1;y[tot]=Y1;
node[++tot].x=X2,node[tot].y1=Y1,node[tot].y2=Y2;node[tot].cover=-1;y[tot]=Y2;
}
sort(node+1,node+1+tot,cmp),sort(y+1,y+1+tot);
n=unique(y+1,y+1+tot)-y-1;
build(1,n,1),update(1,n,1,node[1]);
for(int i=2;i<=tot;i++){
ans+=tree[1].len*(node[i].x-node[i-1].x);
update(1,n,1,node[i]);
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n",++cases,ans);
}
}

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

  1. POJ 1151 线段树+扫描线(计算矩形面积并)

    前一篇博客有了讲解就不再叙述了 #include<cstdio> #include<cstring> #include<cmath> #include<ios ...

  2. POJ 3277 线段树+扫描线

    题意: 思路: 线段树求矩形面积的并...同 POJ 1151 //By SiriusRen #include <cstdio> #include <algorithm> us ...

  3. 线段树 扫描线 L - Atlantis HDU - 1542 M - City Horizon POJ - 3277 N - Paint the Wall HDU - 1543

    学习博客推荐——线段树+扫描线(有关扫描线的理解) 我觉得要注意的几点 1 我的模板线段树的叶子节点存的都是 x[L]~x[L+1] 2 如果没有必要这个lazy 标志是可以不下传的 也就省了一个pu ...

  4. 【POJ-2482】Stars in your window 线段树 + 扫描线

    Stars in Your Window Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11706   Accepted:  ...

  5. POJ-1151-Atlantis(线段树+扫描线+离散化)[矩形面积并]

    题意:求矩形面积并 分析:使用线段树+扫描线...因为坐标是浮点数的,因此还需要离散化! 把矩形分成两条边,上边和下边,对横轴建树,然后从下到上扫描上去,用col表示该区间有多少个下边,sum代表该区 ...

  6. 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)

    D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...

  7. Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)

    题目链接:http://codeforces.com/contest/522/problem/D 题目大意:  给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...

  8. HDU 4419 Colourful Rectangle --离散化+线段树扫描线

    题意: 有三种颜色的矩形n个,不同颜色的矩形重叠会生成不同的颜色,总共有R,G,B,RG,RB,GB,RGB 7种颜色,问7种颜色每种颜色的面积. 解法: 很容易想到线段树扫描线求矩形面积并,但是如何 ...

  9. BZOJ-3228 棋盘控制 线段树+扫描线+鬼畜毒瘤

    3228: [Sdoi2008]棋盘控制 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 23 Solved: 9 [Submit][Status][D ...

随机推荐

  1. sizeof()函数的使用——————【Badboy】

    1.sizeof的使用:sizeof操作符以字节形式给出了其操作数的存储大小. sizeof操作符不能用于函数类型,不全然类型或位字段.不全然类型指具有未知存储大小的数据类型,如未知存储大小的数组类型 ...

  2. Python 获取Google+特定用户最新动态

    CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-28 @author: guaguastd @name: l ...

  3. spring boot系统学习(知识点笔记)

    一.http的注解配置 1.@SpringBootAplication=@SpringBootConfiguration(其实就是个@Configuration)+@EnableAutoConfigu ...

  4. 【LeetCode-面试算法经典-Java实现】【145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)】

    [145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bin ...

  5. Git-删除本地文件夹的repository(本地仓库)

    安装git软件后.有些文件夹里会出现带有?的图标,右键菜单上会有"Git-Sync"或者"Git-Commit"等命令:正常的应该是"Git-Clon ...

  6. keras安装及使用

    安装全称参考https://keras-cn.readthedocs.io/en/latest/for_beginners/keras_linux/ 环境中已配置cuda8.0.cudnn5.0,ub ...

  7. 19.boost A*算法

    #include <iostream> #include <string> #include <utility> #include <vector> # ...

  8. 页面的URL分析----window.location.href

    window.location是页面的位置对象window.location.href是 location的一个属性值,并且它是location的默认属性. window.location直接赋值一个 ...

  9. 新版Eclipse找不到Java EE Module Dependencies选项

    在 Eclipse Galileo (3.5) 版本或Ganymede (3.4) 等更老的版本中, 你可以使用Java EE Module Dependencies 选项来组织你的项目结构,确保依赖 ...

  10. STM8S103之时钟设置

    最大时钟(指的是system clock):外部晶振24MHz,内部高速RC16MHz 三个时钟源:外部晶振.内部高速RC(上电默认) +内部低速RC 几个时钟:master clock(即sytem ...