POJ 1151 Atlantis 线段树+离散化+扫描线
这次是求矩形面积并
/*
Problem: 1151 User: 96655
Memory: 716K Time: 0MS
Language: G++ Result: Accepted
*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
using namespace std;
const int maxn=;
double y[maxn*];
struct Line
{
int co;
double x,y1,y2;
void fun(double a,double b,double c,int d)
{
x=a;
y1=b;
y2=c;
co=d;
}
bool operator<(const Line &e)const
{
return x<e.x;
}
}line[maxn*];
struct Node
{
double s,t,len;
int co;
void change(int o)
{
co+=o;
if(co==) len=;
else len=t-s;
}
};
struct Segtree
{
Node tree[maxn*];
void build(int l,int r,int o)
{
tree[o].s=y[l];tree[o].t=y[r];
tree[o].co=;tree[o].len=;
if(l+<r)
{
int m=(l+r)>>;
build(l,m,o*);
build(m,r,o*+);
}
}
void pushup(int o)
{
tree[o].len=tree[o*].len+tree[o*+].len;
}
void update(int l,int r,int o,Line e)
{
if(l+==r)
{
tree[o].change(e.co);
return;
}
int m=(l+r)>>;
if(e.y1<tree[o*].t)update(l,m,o*,e);
if(e.y2>tree[o*+].s)update(m,r,o*+,e);
pushup(o);
}
}seg;
int main()
{
int n,ncase=;
while(~scanf("%d",&n),n)
{
int cnt=;
for(int i=;i<n;i++)
{
double x1,x2,y1,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
line[cnt].fun(x1,y1,y2,);
y[cnt++]=y1;
line[cnt].fun(x2,y1,y2,-);
y[cnt++]=y2;
}
sort(line,line+cnt);
sort(y,y+cnt);
int d=;
for(int i=;i<cnt;i++)
if(y[i]!=y[i-])y[d++]=y[i];
double ans=;
seg.build(,d-,);
seg.update(,d-,,line[]);
for(int i=;i<cnt;i++)
{
ans+=(line[i].x-line[i-].x)*seg.tree[].len;
seg.update(,d-,,line[i]);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n",++ncase,ans);
}
return ;
}
POJ 1151 Atlantis 线段树+离散化+扫描线的更多相关文章
- POJ 1151 - Atlantis 线段树+扫描线..
离散化: 将所有的x轴坐标存在一个数组里..排序.当进入一条线段时..通过二分的方式确定其左右点对应的离散值... 扫描线..可以看成一根平行于x轴的直线..至y=0开始往上扫..直到扫出最后一条平行 ...
- POJ 1151 Atlantis 线段树求矩形面积并 方法详解
第一次做线段树扫描法的题,网搜各种讲解,发现大多数都讲得太过简洁,不是太容易理解.所以自己打算写一个详细的.看完必会o(∩_∩)o 顾名思义,扫描法就是用一根想象中的线扫过所有矩形,在写代码的过程中, ...
- hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- 【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)
[POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- hdu1542 矩形面积并(线段树+离散化+扫描线)
题意: 给你n个矩形,输入每个矩形的左上角坐标和右下角坐标. 然后求矩形的总面积.(矩形可能相交). 题解: 前言: 先说说做这道题的感受: 刚看到这道题顿时就懵逼了,几何 烂的渣渣.后来从网上搜题解 ...
- POJ 1151Atlantis 矩形面积并[线段树 离散化 扫描线]
Atlantis Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21734 Accepted: 8179 Descrip ...
- POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算
求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...
- Picture POJ - 1177 线段树+离散化+扫描线 求交叉图像周长
参考 https://www.cnblogs.com/null00/archive/2012/04/22/2464876.html #include <stdio.h> #include ...
- hdu 1542 Atlantis(线段树,扫描线)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
随机推荐
- hdu 4710 Balls Rearrangement()
http://acm.hdu.edu.cn/showproblem.php?pid=4710 [code]: #include <iostream> #include <cstdio ...
- wamp设置实现本机IP或者局域网访问 (转)
<Directory /> Options FollowSymLinks AllowOverride None Order deny,allow Allow from all #以前是De ...
- ANN中Precision-Recall权衡
如果想要得到较高的精度,则需要较长的编码. 编码长度m增长的话,则item碰撞的概率会成倍的减小,从而导致召回率下降. 为了得到较高的召回率,则需要多个哈希表. 参考http://yongyuan.n ...
- python检测文件是否更新
import os import time filename = "test.txt" info = os.stat(filename) if time.time()-info.s ...
- import,reload,__import__在python中的区别
import,reload,__import__在python中的区别 http://blog.csdn.net/five3/article/details/7762870 import作用:导入/引 ...
- 如何在CHROME里调试前端代码?
以前看前端们调得很神的, 刚看书到这里,作一个记录,演练了一下,确实有点神!!! :) <!DOCTYPE html> <html lang="en"> & ...
- poj 1870 Bee Breeding
思路:首先要建立坐标,具体作法见:http://www.cnblogs.com/xin-hua/p/3237096.html 然后将得到2坐标之差x,y:如果x,y同号,则相加,否则去最大.(要取绝对 ...
- linux ubuntu 11.04 samba 服务器设置
安装 SAMBA 组件 sudo apt-get install samba smbfs smbclient 配置相关参数 sudo gedit /etc/samba/smb.conf 文件中相关 ...
- hdu 4794 FIb求循环节
很容易看出来这道题是求模n意义下fib数列的最小循环节 对于fib数列的最小循环节的求法,我们可以这样: 1.令n=p1^m1 * p2^m2 * p3^m3…… 2.分别计算fib数列在模p1^m1 ...
- linux下使用yum安装mysql、tomcat、httpd
一.linux下使用yum安装mysql 1.安装 查看有没有安装过: yum list installed mysql* rpm -qa | grep m ...