覆盖的面积(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 题目大意:中文题目 具体思路:和上一篇的博客思路差不多,上一个题求的是面积,然后我们这个地方求的是啊覆盖两次及两 ...
随机推荐
- Runtime Error---Description: An application error occurred on the server....
[原]Runtime Error---Description: An application error occurred on the server.... 2010-1-7阅读2010 评论3 D ...
- 关于在VS 上发布网站
在vs 上建立的网站只能用 localhost 进行访问,想要自己本机上的网站发布到IIS上面,这样就可以直接用 IP 地址来访问了,那么照如下的方式做: 1. 首先需要使用vs 发布自己的网站 1. ...
- ckeditor简单的演示
先把ckeditor文件添加到项目中 然后在页面上引用 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml& ...
- bzoj3991: [SDOI2015]寻宝游戏--DFS序+LCA+set动态维护
之前貌似在hdu还是poj上写过这道题. #include<stdio.h> #include<string.h> #include<algorithm> #inc ...
- centos install zookeeper cluster
1.apache官方下载, 2.新版本需要jdk环境,然后配置好jdk环境 3.解压zookeeper,进入解压后的conf,新建zoo.cfg (删掉自带的cfg)内容如下 tickTime=200 ...
- 将公网IP自动发到Twitter上
[Twitter] 1. 在https://apps.twitter.com/创建新的应用 2. 在https://dev.twitter.com/rest/reference/post/status ...
- 20145209&20145309信息安全系统设计基础实验报告 (4)
实验步骤 阅读和理解源代码 demo_read,demo_write 函数完成驱动的读写接口功能,do_write 函数实现将用户写入的数据逆序排列,通过读取函数读取转换后的数据.这里只是演示接口的实 ...
- Objective-C 编码建议
Objective-C 是 C 语言的扩展,增加了动态类型和面对对象的特性.它被设计成具有易读易用的,支持复杂的面向对象设计的编程语言.它是 Mac OS X 以及 iPhone 的主要开发语言. C ...
- LeetCode Range Sum Query 2D - Mutable
原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...
- Trace-跟踪高消耗的语句需添加哪些事件
通常接手一台数据库服务器后,我们会开启Profiler跟踪来了解SQL Server的繁忙情况.我们首先会想到的是监控CPU或Duration超过某一阈值的语句/过程.那么所创建的Trace添加哪些事 ...