比较水的入门题

记录矩形竖边的x坐标,离散化排序。以被标记的边建树。

扫描线段树,查询线段树内被标记的边。遇到矩形的右边就删除此边

每一段的面积是查询结果乘边的横坐标之差,求和就是答案

 #include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int maxn = ;
int N,num,kase;
double savey[maxn*]; struct line{
double x,y1,y2;
int flag;
bool operator < (const struct line &t) const {return x < t.x;}
}Line[maxn]; struct Node{
int l,r;
double dl,dr;
double len;
int flag;
}SegTree[maxn*]; void Build(int i,int l,int r)
{
SegTree[i].l = l;
SegTree[i].r = r;
SegTree[i].flag = SegTree[i].len = ;
SegTree[i].dl = savey[l];
SegTree[i].dr = savey[r];
if(l + == r) return;
int mid = (l+r)>>;
Build(i<<,l,mid);
Build(i<<|,mid,r);
} void getlen(int t)
{
if(SegTree[t].flag > )
{
SegTree[t].len = SegTree[t].dr - SegTree[t].dl;
return ;
}
if(SegTree[t].l+ == SegTree[t].r) SegTree[t].len = ;
else SegTree[t].len = SegTree[t<<].len + SegTree[t<<|].len;
} void Update(int i,line e)
{
if(e.y1 == SegTree[i].dl && e.y2 == SegTree[i].dr)
{
SegTree[i].flag += e.flag;
getlen(i);
return;
}
if(e.y2 <= SegTree[i<<].dr) Update(i<<,e);
else if(e.y1 >= SegTree[i<<|].dl) Update(i<<|,e);
else
{
line temp = e;
temp.y2 = SegTree[i<<].dr;
Update(i<<,temp);
temp = e;
temp.y1 = SegTree[i<<|].dl;
Update(i<<|,temp);
}
getlen(i);
} int main()
{
while(~scanf("%d",&N) && N)
{
kase++;
num=;
double x1,x2,y1,y2;
for(int i=;i<=N;i++)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
Line[num].x = x1;
Line[num].y1 = y1;
Line[num].y2 = y2;
Line[num].flag = ;
savey[num++]=y1;
Line[num].x = x2;
Line[num].y1 = y1;
Line[num].y2 = y2;
Line[num].flag = -;
savey[num++] = y2;
}
sort(Line+,Line+num);
sort(savey+,savey+num);
Build(,,num-);
Update(,Line[]);
double ans = ;
for(int i=;i<num;i++)
{
//printf("%f %f\n",SegTree[1].len,Line[i].x-Line[i-1].x);
ans += SegTree[].len * (Line[i].x - Line[i-].x);
Update(,Line[i]);
}
printf("Test case #%d\n",kase);
printf("Total explored area: %.2f\n\n",ans);
}
return ;
}

POJ1151-扫面线+线段树+离散化//入门题的更多相关文章

  1. hdu 1754:I Hate It(线段树,入门题,RMQ问题)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  2. 扫面线+线段树(hdu1542)

    之前写过这个算法,时间长了就忘掉了,,现在不看书自己努力回想起来,对算法的理解,对线段树的理解感觉也更深了一点(可能心理作用,哈哈哈) 思路简单说一下吧 从做到右遍历每一条矩阵的边(左右边),看该边对 ...

  3. hdu1542 矩形面积并(线段树+离散化+扫描线)

    题意: 给你n个矩形,输入每个矩形的左上角坐标和右下角坐标. 然后求矩形的总面积.(矩形可能相交). 题解: 前言: 先说说做这道题的感受: 刚看到这道题顿时就懵逼了,几何 烂的渣渣.后来从网上搜题解 ...

  4. 【POJ1151】Atlantis(线段树,扫描线)

    [POJ1151]Atlantis(线段树,扫描线) 题面 Vjudge 题解 学一学扫描线 其实很简单啦 这道题目要求的就是若干矩形的面积和 把扫描线平行于某个轴扫过去(我选的平行\(y\)轴扫) ...

  5. BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针

    BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针 Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间, ...

  6. poj-1151矩形面积并-线段树

    title: poj-1151矩形面积并-线段树 date: 2018-10-30 22:35:11 tags: acm 刷题 categoties: ACM-线段树 概述 线段树问题里的另一个问题, ...

  7. 【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)

    [POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  8. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  9. poj 2528 Mayor's posters(线段树+离散化)

    /* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...

随机推荐

  1. 分布式系统消息中间件——RabbitMQ的使用思考篇

    分布式系统消息中间件--RabbitMQ的使用思考篇 前言     前面的两篇文章分布式系统消息中间件--RabbitMQ的使用基础篇与分布式系统消息中间件--RabbitMQ的使用进阶篇,我们简单介 ...

  2. WCF系列教程之WCF服务配置工具

    本文参考自http://www.cnblogs.com/wangweimutou/p/4367905.html Visual studio 针对服务配置提供了一个可视化的配置界面(Microsoft ...

  3. Macaca初体验-Android端(Python)

    前言: Macaca 是一套面向用户端软件的测试解决方案,提供了自动化驱动,周边工具,集成方案.由阿里巴巴公司开源:http://macacajs.github.io/macaca/ 特点: 同时支持 ...

  4. C++ string中的find()函数

    1.string中find()返回值是字母在母串中的位置(下标记录),如果没有找到,那么会返回一个特别的标记npos.(返回值可以看成是一个int型的数) #include<cstring> ...

  5. PHP开发web应用安全总结

    XSS跨站脚本 概念:恶意攻击者往Web页面里插入恶意html代码,当用户浏览该页之时,嵌入其中Web里面的html代码会被执行,从而达到恶意用户的特殊目的. 危害: 盗取用户COOKIE信息. 跳转 ...

  6. Linux系统mysql使用(二)

    一.查看某数据库的表 # 假设此时数据库名为hiveuse hive; show tables;

  7. Spring.profile配合Jenkins发布War包,实现开发、测试和生产环境的按需切换

    前两篇不错 Spring.profile实现开发.测试和生产环境的配置和切换 - Strugglion - 博客园https://www.cnblogs.com/strugglion/p/709102 ...

  8. 对B+树,B树,红黑树的理解

    出处:https://www.jianshu.com/p/86a1fd2d7406 写在前面,好像不同的教材对b树,b-树的定义不一样.我就不纠结这个到底是叫b-树还是b-树了. 如图所示,区别有以下 ...

  9. python之路--前端CSS

    一.CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义了如何显示HTML元素,给HTML设置样式,让他更加美观. 当浏览器读到这个样式表, 他就会按照这个样式来对文档进行 ...

  10. Unable to handle kernel paging request at virtual address

    1.Unable to handle kernel paging request at virtual address 00000000 =====>越出内核地址空间范围,原因是由于使用空NUL ...