3044 矩形面积求并

http://hzwer.com/879.html

扫描线

 // #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string>
#include <algorithm>
#include <list>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
// #include <conio.h>
using namespace std;
#define clc(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int N = ;
const int MOD = 1e9+;
#define LL long long
#define mi() (l+r)>>1
double const pi = acos(-); // inline int r() {
// int x=0,f=1;char ch=getchar();
// while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}
// while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f;
// }
int n;
double hashh[N<<];
double sum[N<<];
int color[N<<];
struct Edge{
double x1,x2,y;
int flag;
bool operator < (const Edge &a) const{
return y<a.y;
}
}e[N<<]; int b_s(double x){
int l=,r=*n;
int mid;
while(l<=r){
mid=(l+r)>>;
if(hashh[mid]==x) return mid;
else if(hashh[mid]<x) l=mid+;
else r=mid-;
}
} void pushdown(int rt,int l,int r){
if(color[rt]) sum[rt]=hashh[r+]-hashh[l];
else if(l==r) sum[rt]=;
else sum[rt]=sum[rt<<]+sum[rt<<|];
} void update(int l,int r,int rt,int x,int y,int f){
if(x==l&&y==r){
color[rt]+=f;
pushdown(rt,l,r);
return;
}
int mid=mi();
if(y<=mid) update(l,mid,rt<<,x,y,f);
else if(x>mid) update(mid+,r,rt<<|,x,y,f);
else {
update(l,mid,rt<<,x,mid,f);
update(mid+,r,rt<<|,mid+,y,f);
}
pushdown(rt,l,r);
}
int main(){
// int n;
while(scanf("%d",&n)){
if(n==)
break;
clc(color,);
clc(sum,);
double x1,x2,y1,y2;
for(int i=;i<=n;i++){
scanf("%lf%lf%lf%lf",&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);
double ans=;
for(int i=;i<=*n;i++){
int l=b_s(e[i].x1),r=b_s(e[i].x2)-;
// cout<<"l:"<<l<<" "<<"r:"<<r<<endl;
if(l<=r) update(,*n,,l,r,e[i].flag);
// cout<<"sum[1]"<<sum[1]<<endl;
ans+=sum[]*(e[i+].y-e[i].y);
}
printf("%.2f\n",ans);
}
return ;
}

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

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

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

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

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

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

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

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

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

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

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

  6. BZOJ-3225 立方体覆盖 线段树+扫描线+乱搞

    看数据范围像是个暴力,而且理论复杂度似乎可行,然后被卡了两个点...然后来了个乱搞的线段树+扫描线.. 3225: [Sdoi2008]立方体覆盖 Time Limit: 2 Sec Memory L ...

  7. hdu 5091(线段树+扫描线)

    上海邀请赛的一道题目,看比赛时很多队伍水过去了,当时还想了好久却没有发现这题有什么水题的性质,原来是道成题. 最近学习了下线段树扫描线才发现确实是挺水的一道题. hdu5091 #include &l ...

  8. POJ1151+线段树+扫描线

    /* 线段树+扫描线+离散化 求多个矩形的面积 */ #include<stdio.h> #include<string.h> #include<stdlib.h> ...

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

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

随机推荐

  1. Orcle数据库编程:一

    1.PL/SQL是一种块结构的语言,一个PL/SQL程序包含了一个或者多个逻辑块,逻辑块中可以声明变量,变量在使用之前必须先声明. declare mstu student%ROWTYPE;--定义参 ...

  2. QTP之delphi试用感想一(自动化测试)

    这两天一直在琢磨自动化测试,自动化测试,其实与单元测试有一些相同之处,单元测试的目的也是可以一次写,多次运行,对于测试驱动及后期维护真是有非常多的好处,用自动化测试工具也是如何,主要目的是为了回归测试 ...

  3. wxpython ItemContainer

    ItemContainer 是 很多可以添加string item的部件的父类,封装很多有用的方法,可以用来获取部件的被选中item 的string 如wx.ListBox ,wx.CheckList ...

  4. Python基本程序结构

    条件判断: 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断.比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现:

  5. KMP入门题目[不定期更新]

    HDU 1711 Number Sequence(模板题) #include <cstdio> ; ; int N, M; int textS[MAXN]; int tarS[MAXL]; ...

  6. autofac meta

    http://kevincuzner.com/2014/05/19/extreme-attributed-metadata-autofac/ http://stackoverflow.com/ques ...

  7. SQL Server索引怎么用

    什么是索引 拿汉语字典的目录页(索引)打比方:正如汉语字典中的汉字按页存放一样,SQL Server中的数据记录也是按页存放的,每页容量一般为4K .为了加快查找的速度,汉语字(词)典一般都有按拼音. ...

  8. 浅析Java中HashMap的实现

    概述 HashMap是一个散列表,是基于拉链法实现的.这个类继承了Map接口,Map接口提供了所有的哈希操作,比如set().put().remove()等,并且允许操作的键值对为null.HashM ...

  9. js spin 加载动画(loading)

    js spin 加载动画 最近做页面ajax加载是又用到loading动画,还好有一个spin.js 具体的包大家可以去http://fgnass.github.com/spin.js/下载, 如果想 ...

  10. Liunx 配置IDE

    如果你还没装编译环境或自己不确定装没装,不妨先执行 sudo apt-get install build-essential 如果你已经了解一些 vim 的知识,而且想用它来编辑源代码,那么我们不妨装 ...