比较水的入门题

记录矩形竖边的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. @vue/cli 3 打包文件读取绝对路径处理

    @vue/cli 3 封装了 webpack.config.js,一般都在 vue.config.js 里面配置,官网不推荐在 webpack 的 output 处理,这里踩了一下坑,希望可以帮到后面 ...

  2. 【C#复习总结】垃圾回收机制(GC)1

    摘要:今天我们漫谈C#中的垃圾回收机制,本文将从垃圾回收机制的原理讲起,希望对大家有所帮助. GC的前世与今生 虽然本文是以.NET作为目标来讲述GC,但是GC的概念并非才诞生不久.早在1958年,由 ...

  3. Angular刷新浏览器 404 问题

    最近在用angular写一个后台的项目,遇到一个小问题. 进入某个路由页面之后,手动触发浏览器的刷新,然后就404了... 翻看Angular的文档,发现Google早已经给我们想到了这个问题的处理方 ...

  4. Python-生成器_36

    #生成器函数 def generator(): print(1) return 'a' ret = generator() print(ret) #只要含有yield关键字的函数都是生成器函数 # y ...

  5. Mysql数据库中索引的概念总结

    1.索引的目的是什么 1.快速访问数据表中的特定信息,提高检索速度 2.创建唯一性索引,保证数据库表中每一行数据的唯一性. 3.加速表和表之间的连接 4.使用分组和排序子句进行数据检索时,可以显著减少 ...

  6. Iar8.1安装包破解

    Iar8.1安装包链接链接:https://pan.baidu.com/s/1F6sxEcatk3_YPq47lvc8Mw 密码:mnlz 破解链接  https://www.cnblogs.com/ ...

  7. MySQLl导入导出SQL文件

    window 1.导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u dbuser -p dbname > dbname.sql ...

  8. Rime 小狼毫 注意事项

    https://rime.im/https://github.com/rime/weasel/pulse 打不出中文可能是,没有五笔需要的文件: wubi_pinyin.schema.yamlCtrl ...

  9. 城市联动 - 自动生成SQL语句

    字段比较简单/  如果有需要可以自己定制字段和排序/ 一共二级城市联动, 本人业务需要, 所以就两层, 网上关于三层的挺多, 有需要可以借鉴/ 废话不多说, 先看效果图, 代码在下面 <?php ...

  10. [转帖]OS/2 兴 衰 史

    OS/2 兴 衰 史 https://zhidao.baidu.com/question/12076254.html 最近在看windows的版本 感觉自己接触电脑太晚 知道的也是很少 不明白 之前有 ...