HDU-1255-覆盖的面积-线段树求面积并(模板)
2015-08-03 13:49
453 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255
好吧,这题和HDU1542几乎完全一样,链接:/article/2046926.html我有详细讲;
这个题目唯一的不同就是这是个求重合的面积,而HDU1542是求并面积,如果明白了1542题目的原理,我想很轻松的就可以搞定这个题目,只要将cover>0改成cover>1就可以求出覆盖面积了,我这里就不在赘述了;代码我再贴一下
好吧,这题和HDU1542几乎完全一样,链接:/article/2046926.html我有详细讲;
这个题目唯一的不同就是这是个求重合的面积,而HDU1542是求并面积,如果明白了1542题目的原理,我想很轻松的就可以搞定这个题目,只要将cover>0改成cover>1就可以求出覆盖面积了,我这里就不在赘述了;代码我再贴一下
#include<iostream> #include<string> #include<cstdio> #include<cstring> #include<queue> #include<map> #include<cmath> #include<stack> #include<set> #include<vector> #include<algorithm> #define LL long long #define inf 1<<30 using namespace std; const int N=2005; int n; double y ; struct LINE { double x; double y_down,y_up; int flag; }line ; struct node { double l,r; double x; int cover; bool flag; }node[N<<2]; bool cmp(LINE a,LINE b) { return a.x<b.x; } void build(int rt,int l,int r) { node[rt].l=y[l]; node[rt].r=y[r]; node[rt].x=-1; node[rt].flag=false; node[rt].cover=0; if(l+1==r){ node[rt].flag=true; return; } int mid=(l+r)>>1; build(rt<<1,l,mid); build(rt<<1|1,mid,r); } double Insert_query(int rt,double x,double l,double r,int flag) { if(l>=node[rt].r||r<=node[rt].l) return 0; if(node[rt].flag){ if(node[rt].cover>1){ double pre=node[rt].x; double ans=(x-pre)*(node[rt].r-node[rt].l); node[rt].x=x; node[rt].cover+=flag; return ans; }else{ node[rt].x=x; node[rt].cover+=flag; return 0; } } double ans1,ans2; ans1=Insert_query(rt<<1,x,l,r,flag); ans2=Insert_query(rt<<1|1,x,l,r,flag); return ans1+ans2; } int main() { int t; double x1,x2,y1,y2; scanf("%d",&t); while(t--){ scanf("%d",&n); int cnt=-1; for(int i=0;i<n;i++){ scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); y[++cnt]=y1; line[cnt].x=x1; line[cnt].y_down=y1; line[cnt].y_up=y2; line[cnt].flag=1; y[++cnt]=y2; line[cnt].x=x2; line[cnt].y_down=y1; line[cnt].y_up=y2; line[cnt].flag=-1; } sort(y,y+cnt+1); sort(line,line+cnt+1,cmp); build(1,0,cnt); double area=0; for(int i=0;i<=cnt;i++){ area+=Insert_query(1,line[i].x,line[i].y_down,line[i].y_up,line[i].flag); } printf("%.2lf\n",area); } return 0; }
相关文章推荐
- 红杏 插件 可以访问谷歌等
- C语言中全局变量、局部变量、静态全局变量、静态局部变量的区别
- HDU 1166 线段树模板题
- java---socket编程之服务器端
- 曾帮Facebook把用户做到7亿的那个人,告诉你如何实现用户增长
- 《关于互联网创业你不知道的事》,读完少奋斗十年
- HDU - 1237 - 简单计算器(栈)
- HDU 1969--Pie【二分】
- BoundService的基本使用
- Java 8之方法引用(Method References)
- MVC上传相关
- Web.xml配置中目前需要掌握的知识点
- Nginx详细配置的备注说明
- 海量数据总结
- 微信开发总结 六
- Oracle内存数据库Timesten的使用(2)
- python 使用元类
- iOS关于关于动态函数的操作
- 每天一个linux命令(28):tar命令
- Apache http 发送post请求,解析响应