比较水的入门题

记录矩形竖边的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. 面试 5:手写 Java 的 pow() 实现

    我们在处理一道编程面试题的时候,通常除了注意代码规范以外,千万要记得自己心中模拟一个单元测试.主要通过三方面来处理. 功能性测试 边界值测试 负面性测试 不管如何,一定要保证自己代码考虑的全面,而不要 ...

  2. .net core jwt

    https://www.cnblogs.com/JacZhu/p/6837676.html

  3. 常见的web攻击手段总结

    xxs攻击(跨站脚本攻击) 攻击者在网页中嵌入恶意脚本程序,当用户打开该网页时脚本程序便在浏览器上执行,盗取客户端的cookie.用户名密码.下载执行病毒木马程 序 解决: 我们可以对用户输入的数据进 ...

  4. 1000/problem/A

    传送门: [http://codeforces.com/contest/1000/problem/A] 题意: 一个比赛颁奖,要准备T-Shirt给获奖者,但有的去年获奖过,衣服尺寸可以不改,有的需要 ...

  5. 同步和异步概念(由DZW前端框架引发的百度地图api无法加载问题总结)

    首先概念: 在计算机领域,同步就是指一个进程在执行某个请求的时候,若该请求需要一段时间才能返回信息,那么这个进程将会一直等待下去,直到收到返回信息才继续执行下去:异步是指进程不需要一直等下去,而是继续 ...

  6. PS滤镜给城市夜空照片添加满天星

    原图 一.新建空白图层. 二.填充黑色(编辑→填充). 三.转换为智能对象. 四.添加杂色(滤镜→杂色→添加杂色). 五.使用高斯模糊(滤镜→模糊→高斯模糊). 六.如果你想再次修改模糊效果,可双击该 ...

  7. springBoot项目启动类启动无法访问

    springBoot项目启动类启动无法访问. 网上也查了一些资料,我这里总结.下不来虚的,也不废话. 解决办法: 1.若是maven项目,则找到右边Maven Projects --->Plug ...

  8. c#+linux+mono+Redis集群(解决无法连接Redis的问题)

    在linux环境中使用mono来执行c#的程序, 在连接redis的时候遇到了无法连接数据库的错误.如下: Unhandled Exception:StackExchange.Redis.RedisC ...

  9. 将表单数据转换为json代码分享

    <body> <form action="#" method="post" id="form1"> <inpu ...

  10. jQuery 事件 - triggerHandler() 方法

    定义和用法 triggerHandler() 方法触发被选元素的指定事件类型.但不会执行浏览器默认动作,也不会产生事件冒泡. triggerHandler() 方法与 trigger() 方法类似.不 ...