题意:求矩形面积的并

思路:

注意是[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. [Python Test] Use pytest fixtures to reduce duplicated code across unit tests

    In this lesson, you will learn how to implement pytest fixtures. Many unit tests have the same resou ...

  2. Swift基础--定位

    // // ViewController.swift // JieCoreLocation // // Created by jiezhang on 14-10-4. // Copyright (c) ...

  3. C++模板中的静态

    #include <iostream> #include <stdlib.h> using namespace std; template<class T> cla ...

  4. Maximum Bipartite Matching

    算法旨在用尽可能简单的思路解决这个问题.理解算法也应该是一个越看越简单的过程,当你看到算法里的一串概念,或者一大坨代码,第一感觉是复杂,此时最好还是从样例入手.通过一个简单的样例,并编程实现,这个过程 ...

  5. bzoj5105: [CodePlus2017]晨跑(LCM)

    5105: [CodePlus2017]晨跑 题目:传送门 题解: 没有很懂Code Puls 的操作...一道签到的三个数的LCM??? 代码: #include<cstdio> #in ...

  6. VC++基于CXImage库实现缩略图

    一般的图像处理软件都对读入程序的图像文件建一个缩略图的列表,像ACDSee那样.笔者最近在做一个图像处理的项目,处理的原始数据就是图像文件.从项目一开始就想做一个缩略图,但一直苦于技术水平有限,且时间 ...

  7. BZOJ 2190 欧拉函数

    思路: 递推出来欧拉函数 搞个前缀和 sum[n-1]*2+3就是答案 假设仪仗队是从零开始的 视线能看见的地方就是gcd(x,y)=1的地方 倒过来一样 刨掉(1,1) 就是ans*2+1 再加一下 ...

  8. ios各个屏幕

    func currentDeviceScreenMeasurement() -> CGFloat { var deviceScree: CGFloat = 3.5 if ((568 == SCR ...

  9. springMVC接受对象实体并且对象实体里面又有对象集合方式

    springMVC接受对象实体并且对象实体里面又有对象集合方式: Ajax: function add(){ var orders = [ { orderNo : "H222255" ...

  10. js 40 个技巧

    1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键<table border oncontextmenu= ...