覆盖的面积(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 题目大意:中文题目 具体思路:和上一篇的博客思路差不多,上一个题求的是面积,然后我们这个地方求的是啊覆盖两次及两 ...
随机推荐
- winform把图片存储到数据库
1.先在Form中放一个PictureBox控件,再放三个按钮. 2.双击打开按钮,在里面写如下代码: OpenFileDialog open1 = new OpenFileDialog(); Dia ...
- Oracle RAC的日志体系
Oracle Clusterware 不像数据库那样,有丰富的视图.工具可以用来辅助诊断,它的日志和trace文件是唯一的选择.但不像Oracle只有alert日志和几种trace文件,Oracle ...
- sql语句的各种模糊查询
一般模糊语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: 1.%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况 ...
- ffmpeg命令行
ubuntu下简单安装ffmpeg sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-nextsudo apt-get update sudo a ...
- P1073 最优贸易
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; int head1[maxn], head2[ ...
- mysql root 维护
修改root密码: mysql -uroot -paaamysql> use mysql;mysql> UPDATE user SET password=password("aa ...
- IOS 单例模式的写法
iOS的单例模式有两种官方写法,如下: 1)不使用GCD的方式 #import "Manager.h" static Manager *manager; @implementati ...
- 使用Entity Framework 自动产生的Sql语句
对于一个单独实体的通常操作有3种:添加新的实体.修改实体以及删除实体. 1.添加新的实体 Entity Framework Code First添加新的实体通过调用DbSet.Add()方法来实现. ...
- 执行带参数的sql字符串
--要传入的参数 declare @Rv NVARCHAR(40) --要执行的带参数的sql字符串 declare @sql nvarchar(max) set @sql='select * fr ...
- Android颜色资源文件
<?xml version="1.0" encoding="utf-8"?><resources> <color name=&qu ...