题目地址:HDU 1255

这题跟面积并的方法非常像,仅仅只是须要再加一个变量。

刚開始我以为直接用那个变量即可,仅仅只是推断是否大于0改成推断是否大于1。可是后来发现了个问题,由于这个没有下放,没延迟,比方,在父节点上加了一次1,在该父节点的子节点上又加了一次1,可是这时候全部的结点仍然没有达到2的,可是实际上子节点已经达到2了。这时候能够再加一个变量。那个变量用来保存覆盖数大于等于0的情况。这种话当计算大于1的覆盖节点的时候,当推断为1的时候就要加上子节点的全部情况,由于字节点是大于0的,加上子节点的说明该父节点也是大于1的。

代码例如以下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm> using namespace std;
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
int lazy[10000], cnt;
double sum[10000], c[10000], once[10000];
struct node
{
double l, r, h;
int f;
} edge[100000];
int cmp(node x, node y)
{
return x.h<y.h;
}
void add(double l, double r, double h, int f)
{
edge[cnt].l=l;
edge[cnt].r=r;
edge[cnt].h=h;
edge[cnt++].f=f;
}
void PushUp(int l, int r, int rt)
{
if(lazy[rt]>=2)
{
once[rt]=sum[rt]=c[r+1]-c[l];
}
else if(lazy[rt]==1)
{
once[rt]=c[r+1]-c[l];
if(l==r)
{
sum[rt]=0;
}
else
sum[rt]=once[rt<<1]+once[rt<<1|1];
}
else
{
if(l==r)
once[rt]=sum[rt]=0;
else
{
once[rt]=once[rt<<1]+once[rt<<1|1];
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
}
}
void update(int ll, int rr, int x, int l, int r,int rt)
{
if(ll<=l&&rr>=r)
{
lazy[rt]+=x;
PushUp(l,r,rt);
return ;
}
int mid=l+r>>1;
if(ll<=mid) update(ll,rr,x,lson);
if(rr>mid) update(ll,rr,x,rson);
PushUp(l,r,rt);
}
int erfen(double x, int high)
{
int low=0, mid;
while(low<=high)
{
mid=low+high>>1;
if(c[mid]==x)
return mid;
else if(c[mid]>x)
high=mid-1;
else
low=mid+1;
}
}
int main()
{
int t, n, i, j, k;
double x1, x2, y1, y2, ans;
scanf("%d",&t);
while(t--)
{
ans=0;
memset(sum,0,sizeof(sum));
memset(lazy,0,sizeof(lazy));
memset(once,0,sizeof(once));
scanf("%d",&n);
k=0;
cnt=0;
for(i=0; i<n; i++)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
c[k++]=x1;
c[k++]=x2;
add(x1,x2,y1,1);
add(x1,x2,y2,-1);
}
sort(edge,edge+2*n,cmp);
sort(c,c+k);
for(i=0; i<2*n-1; i++)
{
int l=erfen(edge[i].l,2*n-1);
int r=erfen(edge[i].r,2*n-1);
//printf("%d %d\n",l,r);
update(l,r-1,edge[i].f,0,2*n-1,1);
ans+=sum[1]*(edge[i+1].h-edge[i].h);
//printf("%.2lf %.2lf\n",sum[1],edge[i+1].h-edge[i].h);
}
printf("%.2lf\n",ans);
}
return 0;
}

HDU 1255 覆盖的面积(线段树+扫描线)的更多相关文章

  1. HDU 1255 覆盖的面积 (线段树+扫描线+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 题意很清楚,就是让你求矩阵之间叠加层数大于1的矩形块的面积和. 因为n只有1000,所以我离散化 ...

  2. HDU 1255 覆盖的面积 线段树+扫描线

    同 POJ1151 这次是两次 #include <iostream> #include <algorithm> #include <cstdio> #includ ...

  3. hdu 1255 覆盖的面积 (线段树处理面积覆盖问题(模板))

    http://acm.hdu.edu.cn/showproblem.php?pid=1255 覆盖的面积 Time Limit: 10000/5000 MS (Java/Others)    Memo ...

  4. HDU 1255 覆盖的面积(线段树面积并)

      描述 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input 输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正 ...

  5. HDU 1255 覆盖的面积(线段树:扫描线求面积并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 题目大意:给你若干个矩形,让你求这些矩形重叠两次及以上的部分的面积. 解题思路:模板题,跟HDU ...

  6. hdu1255 覆盖的面积 线段树-扫描线

    矩形面积并 线段树-扫描线裸题 #include<stdio.h> #include<string.h> #include<algorithm> #include& ...

  7. HDU - 1255 覆盖的面积(线段树求矩形面积交 扫描线+离散化)

    链接:线段树求矩形面积并 扫描线+离散化 1.给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. 2.看完线段树求矩形面积并 的方法后,再看这题,求的是矩形面积交,类同. 求面积时,用被覆 ...

  8. hdu 1255 覆盖的面积(线段树 面积 交) (待整理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 Description 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.   In ...

  9. hdu 1255 覆盖的面积(求覆盖至少两次以上的面积)

    了校赛,还有什么途径可以申请加入ACM校队?  覆盖的面积 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  10. HDU 1264 Counting Squares (线段树-扫描线-矩形面积并)

    版权声明:欢迎关注我的博客.本文为博主[炒饭君]原创文章,未经博主同意不得转载 https://blog.csdn.net/a1061747415/article/details/25471349 P ...

随机推荐

  1. 87. [NOIP2000] 乘积最大

    ★☆   输入文件:cjzd.in   输出文件:cjzd.out   简单对比 时间限制:1 s   内存限制:128 MB 问题描述 今年是国际数学联盟确定的“2000——世界数学年”,又恰逢我国 ...

  2. Android O 通知栏的"running in the background"

    Android O新增的一个特性,系统会在通知栏显示当前在后台运行的应用,其实际是显示启动了前台服务的应用,并且当前应用的Activity不在前台.具体我们看下源码是怎么实现的. 1 APP调用sta ...

  3. sqlserver如何查询一个表的主键都是哪些表的外键

    select object_name(a.parent_object_id) 'tables'  from sys.foreign_keys a  where a.referenced_object_ ...

  4. Thread stack overrun

    ERROR 1436 (HY000): Thread stack overrun:  6448 bytes used of a 131072 byte stac k, and 128000 bytes ...

  5. 十年后我不会log,还是活的很好啊

    混迹于互联网也一两年了,出于喜爱与生活压力依然会从事很久.迟迟不写博客,大概是觉得知识与经验积累在笔记上时不时看看就好了,而实际情况是笔记很少翻,遇到问题搜博客和百度依然是首选,故开通博客记录自己工作 ...

  6. vue城市三级联动组件 vue-area-linkage

    Install the pkg with npm: // v5之前的版本 npm i --save vue-area-linkage // v5及之后的版本 npm i --save vue-area ...

  7. 有关bash,我希望我能知晓的十件事

    简介 我之前的一篇文章比我预想的更受欢迎,因此我想再写一篇文章来介绍一些不太知名的bash功能 正如之前所言,由于我觉得bash是一种要经常使用(且需理解)的技术,所以我在研究bash时写了一本书.虽 ...

  8. spring aop 方法增加日志记录

    使用场景: 1:调用外部接口时需要记录出参和入参. 2:分布式系统之间,调用各个系统之间需要记录日志,一旦出现了问题也可以找得到元数据 一言不合,上代码: # 枚举类 package xxxxxxxx ...

  9. Spring资源访问接口Resource

    该接口拥有对不同资源类型的实现类 boolean exists() 资源是否存在 boolean isOpen() 资源是否打开 URL getURL() 如果底层资源可以表示成URL,则该方法返回对 ...

  10. The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_60\bin;C:\Windows\Sun\Jav

    启动项目自动结束,查看日志发现 [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache To ...