覆盖的面积(HDU 1255 线段树)
覆盖的面积
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

注意:本题的输入数据较多,推荐使用scanf读入数据.
2
5
1 1 4 2
1 3 3 7
2 1.5 5 4.5
3.5 1.25 7.5 4
6 3 10 7
3
0 0 1 1
1 0 2 1
2 0 3 1
7.63
0.00 线段树计算被覆盖部分的面积#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#define LL long long using namespace std; const int Max = 1000; typedef struct Tree
{
int num;
double OneL,TwoL;//记录区间被覆盖多于一次和两次的长度
}Tree; Tree Tr[Max*10]; typedef struct Point
{
double x;
double y1;
double y2;
int op;
bool operator < (const struct Point a)const
{
return x<a.x;
} }Point; Point P[Max*3]; vector <double>y; int T,n; void Pushup(int L,int R,int st)
{
if(Tr[st].num>=2)
{
Tr[st].TwoL=Tr[st].OneL=y[R-1]-y[L-1];
return ;
}
if(Tr[st].num==1)
{
if(L+1==R)
{
Tr[st].TwoL=0;
Tr[st].OneL=y[R-1]-y[L-1];
}
else
{
Tr[st].OneL=y[R-1]-y[L-1];
Tr[st].TwoL=Tr[st<<1].OneL+Tr[st<<1|1].OneL;
}
return ;
}
if(Tr[st].num==0)
{
if(L+1==R)
{
Tr[st].OneL=Tr[st].TwoL=0;
}
else
{
Tr[st].OneL=Tr[st<<1].OneL+Tr[st<<1|1].OneL;
Tr[st].TwoL=Tr[st<<1].TwoL+Tr[st<<1|1].TwoL;
}
return ;
}
}
void Build(int L,int R,int st)
{
Tr[st].num=0;
Tr[st].OneL=Tr[st].TwoL=0;
if(L+1==R)
{
return ;
}
int mid = (L+R)>>1; Build(L,mid,st<<1); Build(mid,R,st<<1|1); Pushup(L,R,st);
} void Update(int L,int R,int st,int l,int r,int d)
{
if(l>=R||r<=L)
{
return ;
}
if(L>=l&&R<=r)
{
Tr[st].num+=d;
Pushup(L,R,st);
return ;
}
int mid = (L+R)>>1;
if(l<=mid)
{
Update(L,mid,st<<1,l,r,d);
}
if(r>mid)
{
Update(mid,R,st<<1|1,l,r,d);
}
Pushup(L,R,st); } int main()
{
double x1,x2,y1,y2; scanf("%d",&T); while(T--)
{
scanf("%d",&n); y.clear(); map<double ,int >M; int m=0; for(int i=0;i<n;i++)
{
scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2); P[m].x=x1; P[m].y1=y1; P[m].op=1; P[m++].y2=y2; P[m].x=x2; P[m].y1=y1; P[m].op=-1; P[m++].y2=y2; y.push_back(y1); y.push_back(y2);
} sort(P,P+m); sort(y.begin(),y.end()); y.erase(unique(y.begin(),y.end()),y.end());//去重 int sum = y.size(); for(int i=0;i<sum;i++)//重新编号
{
M[y[i]]=i+1;
} double Area =0 ; double h; Build(1,sum,1); for(int i=0;i<m-1;i++)
{
Update(1,sum,1,M[P[i].y1],M[P[i].y2],P[i].op);
h=P[i+1].x-P[i].x;
Area += (h*Tr[1].TwoL);//计算面积
}
printf("%.2f\n",Area);
}
return 0;
}
覆盖的面积(HDU 1255 线段树)的更多相关文章
- 覆盖的面积 HDU - 1255 线段树+扫描线+离散化 求特定交叉面积
#include<cstdio> #include<map> #include<algorithm> using namespace std; ; struct N ...
- 线段树扫描线(一、Atlantis HDU - 1542(覆盖面积) 二、覆盖的面积 HDU - 1255(重叠两次的面积))
扫描线求周长: hdu1828 Picture(线段树+扫描线+矩形周长) 参考链接:https://blog.csdn.net/konghhhhh/java/article/details/7823 ...
- hdu 1255(线段树 扫描线) 覆盖的面积
http://acm.hdu.edu.cn/showproblem.php?pid=1255 典型线段树辅助扫描线,顾名思义扫描线就是相当于yy出一条直线从左到右(也可以从上到下)扫描过去,此时先将所 ...
- 覆盖的面积 HDU - 1255 (线段树-扫描线)模板提
给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1& ...
- O - 覆盖的面积 - hdu 1255(求面积)
分析:求一层的面积覆盖是非常简单的事情,不过多层面积覆盖应该怎么搞???也是比较简单的事情,只需要用两个变量记录就好了,一个记录覆盖一次的,一个记录覆盖两次的,就很容易解决了 ************ ...
- 覆盖的面积 HDU - 1255(扫描线求面积交)
题意: 就是扫描线求面积交 解析: 参考求面积并.... 就是把down的判断条件改了一下..由w > 0 改为 w > 1 同时要讨论一下 == 1 时 的情况, 所以就要用到一个临时 ...
- HDU 1255 覆盖的面积 (扫描线 线段树 离散化 矩形面积并)
题目链接 题意:中文题意. 分析:纯手敲,与上一道题目很相似,但是刚开始我以为只是把cnt>=0改成cnt>=2就行了,. 但是后来发现当当前加入的线段的范围之前 还有线段的时候就不行了, ...
- 覆盖的面积 HDU - 1255 (扫描线, 面积交)
求n个矩阵面积相交的部分,和求面积并一样,不过这里需要开两个数组保存覆盖一次和覆盖两次以上的次数的部分,还是模板,主要注意点就是pushup部分,如果我已经被两次覆盖,那我的两个数组在这个root点的 ...
- 面积并+扫描线 覆盖的面积 HDU - 1255
题目链接:https://cn.vjudge.net/problem/HDU-1255 题目大意:中文题目 具体思路:和上一篇的博客思路差不多,上一个题求的是面积,然后我们这个地方求的是啊覆盖两次及两 ...
随机推荐
- app 尺寸
web app 手机桌面logo尺寸大小(三种 ):114 72 57
- 讨论一下js获取响应中后台传回来的BigInteger类型的数字时,后几位会自动变为0的问题
后台返回的json:{"data":12345678912345678912} 在js中获取该data得到的值为:12345678912345680000 后经过实验发现,只有数字 ...
- php读取指定结束指针文件内容
fopen操作时文件读取开始指针位于文件开始部分, fseek 以指定文件大小以及开始指针位置确定结束指针位置 具体案例: <?php//打开文件流,fopen不会把文件整个加载到内存$f = ...
- 【iCore3 双核心板】例程二十六:MODBUS TCP实验——电源监控
实验指导书及代码包下载: http://pan.baidu.com/s/1pKhxKd9 iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- spring-session整合
如果项目之前没有整合过spring-data-redis的话,这一步需要先做,在maven中添加这两个依赖: <dependency> <groupId>org.spr ...
- 多个git账号的配置
问题描述: 作为开发人员,普遍有多个git账号,例如,公司邮箱对应的公司仓储账号和私人邮箱对应的github账号.在一台电脑上使用两个账号基于ssh协议拉代码,如果不进行额外设置,往往只有一个账号可以 ...
- .htaccess 重写去index.php
.htaccess 重写去index.php <IfModule mod_rewrite.c>RewriteEngine onRewriteCond %{REQUEST_FILENAME} ...
- Population Mean
Probability and Statistics > Moments > History and Terminology > Disciplinary Terminology & ...
- 一种javascript链式多重继承的方式(__proto__原型链)
var a=function(){this.foo='bar';} a.prototype={b:1}; var aa=function(){} aa.prototype={c:2,__proto__ ...
- Python之路-python数据类型(列表、字典、字符串、元祖)操作
一.列表: 列表的语法,以中括号开通和结尾,元素以逗号隔开.例如:name = [] 列表是以下标取值,第一个元素下标是0,第二个元素下标是1,最后一个元素下标是-1. 1.增加 #name = ...