hdu3340 线段树+多边形
Rain in ACStar
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 788 Accepted Submission(s): 218
This is one of the ten biggest secrets of this world! And it is time to expose the truth!
Yes, Super Cow AC is from ACStar which is ten million light-year away from our earth. No one, even AC himself, knows how AC came to our home. The only memory in his head is the strange rain in ACStar.
Because of the special gravity of ACStar, the raindrops in ACStar have many funny features. They have arbitrary sizes, color and tastes. The most interesting parts of the raindrops are their shapes. When AC was very young, he found that all the drops he saw
in air were convex hull. Once the raindrops fell to the ground, they would be absorb by the soil.
![](http://acm.hdu.edu.cn/data/images/C235-1008-1.png)
This year is set to be AC-year. In recognition of Great General AC's contribution to our empire, the Emperor decided to build a huge AC park. Inside this park there is a laboratory to simulate the rain in ACStar. As a researcher of this lab, you are appointed
to measure the volume of rain absorbed by soil. To simplify this problem, scientists put the rain into two-dimensional plane in which the ground is represented as a straight line and the raindrops are convex polygon. So the area of the graphics stands for
the volume of raindrops.
You will receive two types of instructions:
1.R P (This type of instructions tell you sufficient information about the raindrops.)
2.Q A B (Ask you to report the volume of rain absorbed by soil of [A,B].)
Instructions are given in chronological order.
After T, the inputs will be each test case. The first line of each case will be N(no more than 25000), representing for the numbers of instructions. The following N lines will give instructions of the two types.
For each instruction of type 1, it will be followed by a line listing P (at least 3 and at most 5) points representing the convex polygon of the coming raindrop. The points are started by the leftmost point and are given in counterclockwise order. It's guaranteed
that no points of the same raindrop are in the same vertical line.
All numbers are positive integer no more than 1000000000.
It is guaranteed that the result is less than 1e8.
7
Q 1 100
R 4
10 10 11 10 13 11 12 11
Q 10 11
Q 1 100
R 3
100 20 120 20 110 30
Q 1 100
Q 12 120
0.250
1.000
1.000
100.250
/*
hdu3340 线段树+多边形
R x:表示在这有个x边形
Q l r:查询在[l,r]之间的多边形的面积 所以考虑用线段树解决,那么我们就要知道如何处理区间的面积,对于一个多边形,
它的每一条边都可以在两端作垂直于x轴的垂线然后形成一个梯形,于是用图形
上半部分的边形成的梯形和- 半部分的边形成的梯形和便能得到多边形面积。
而且两个同高的梯形的面积和等同于上底和下底的叠加 hhh-2016-03-02 20:54:04 Accepted
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn = 500200;
map<int,int>has;
vector<int> tp;
char op[10]; struct node
{
int l,r;
int mid()
{
return l+(r-l)/2;
}
double h1,h2,k,len;
double area;
} tree[maxn]; void push_up(int r)
{
tree[r].area = tree[r<<1].area+tree[r<<1|1].area;
} void build(int i,int l,int r)
{
tree[i].l = l,tree[i].r = r;
tree[i].h1 = tree[i].h2 = tree[i].k = tree[i].area = 0;
tree[i].len = tp[r]-tp[l];
if(l == r) return;
if(l+1 != r)
{
build(i<<1,l,tree[i].mid());
build(i<<1|1,tree[i].mid(),r);
push_up(i);
}
} void push_down(int i)
{
int lson = i<<1,rson =i<<1|1;
double h1=tree[i].h1,h2=tree[i].h2,k=tree[i].k;
double md = (tp[tree[i].mid()]-tp[tree[i].l])*k+h1; tree[lson].h1+=h1,tree[lson].h2+=md,tree[lson].k+=k;
tree[lson].area += (h1+md)*tree[lson].len/2; tree[rson].h1+=md,tree[rson].h2+=h2,tree[rson].k+=k;
tree[rson].area += (md+h2)*tree[rson].len/2; tree[i].h1 = tree[i].h2 = tree[i].k = 0;
} void update(int i,int l,int r,double h1,double h2,double k)
{
if(tree[i].l >= l && tree[i].r <= r)
{
tree[i].h1+=h1,tree[i].h2+=h2,tree[i].k+=k;
tree[i].area += (h1+h2)*tree[i].len/2;
return ;
}
push_down(i);
double md= (tp[tree[i].mid()]-tp[l])*k+h1;
if(r <= tree[i].mid())
update(i<<1,l,r,h1,h2,k);
else if(l >= tree[i].mid())
update(i<<1|1,l,r,h1,h2,k);
else
{
update(i<<1,l,tree[i].mid(),h1,md,k);
update(i<<1|1,tree[i].mid(),r,md,h2,k);
}
push_up(i);
} double query(int i,int l,int r)
{
if(tree[i].l >= l && tree[i].r <= r)
{
return tree[i].area;
}
push_down(i);
double ans = 0;
if(l < tree[i].mid())
ans += query(i<<1,l,r);
if(tree[i].mid() < r)
ans += query(i<<1|1,l,r);
push_up(i);
return ans;
} struct point
{
int x[6],y[6];
int num;
void get()
{
num = 0;
scanf("%s",op);
if(op[0] == 'R')
{
scanf("%d",&num);
for(int i = 0; i < num; i++)
{
scanf("%d%d",&x[i],&y[i]);
tp.push_back(x[i]);
}
}
else
{
scanf("%d%d",&x[1],&y[1]);
tp.push_back(x[1]);
tp.push_back(y[1]);
}
}
} Point[50010]; int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
tp.clear(),has.clear();
scanf("%d",&n);
for(int i =1;i <= n;i++)
{
Point[i].get();
} sort(tp.begin(),tp.end());
tp.erase(unique(tp.begin(),tp.end()),tp.end());
for(int i = 0;i < (int)tp.size();i++)
has[tp[i]] = i;
build(1,0,tp.size()-1);
for(int i =1;i <= n;i++)
{
if(Point[i].num)
{
for(int j = 0;j < Point[i].num;j++)
{
int num = Point[i].num;
int x1=Point[i].x[j],y1=Point[i].y[j];
int x2=Point[i].x[(j+1)%num],y2=Point[i].y[(j+1)%num];
if(x1 > x2)swap(x1,x2),swap(y1,y2);
else y1=-y1,y2=-y2;
double k = (1.0*y1-1.0*y2)/(1.0*x1-1.0*x2);
update(1,has[x1],has[x2],y1,y2,k);
// printf("%.3f\n",query(1,has[1],has[100]));
}
}
else
{
int x = Point[i].x[1];
int y = Point[i].y[1];
printf("%.3f\n",query(1,has[x],has[y]));
}
}
}
return 0;
}
hdu3340 线段树+多边形的更多相关文章
- 线段树总结 (转载 里面有扫描线类 还有NotOnlySuccess线段树大神的地址)
转载自:http://blog.csdn.net/shiqi_614/article/details/8228102 之前做了些线段树相关的题目,开学一段时间后,想着把它整理下,完成了大牛NotOnl ...
- [转载]完全版线段树 by notonlysuccess大牛
原文出处:http://www.notonlysuccess.com/ (好像现在这个博客已经挂掉了,在网上找到的全部都是转载) 今天在清北学堂听课,听到了一些很令人吃惊的消息.至于这消息具体是啥,等 ...
- 【转】线段树完全版~by NotOnlySuccess
线段树完全版 ~by NotOnlySuccess 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章了,觉 ...
- [IOI2018]排座位——线段树
题目链接: IOI2018seat 题目大意:给出一个$H*W$的矩阵,将$0 \sim W*H-1$分别填入矩阵的格子里(每个格子里一个数),定义一个子矩阵是美妙的当且仅当这个子矩阵包含且仅包含$0 ...
- HDU 1542 - Atlantis - [线段树+扫描线]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- 《完全版线段树》——notonlysuccess
转载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文 ...
- 【转】 线段树完全版 ~by NotOnlySuccess
载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章 ...
- 【转载】完全版线段树 by notonlysuccess大牛
原文出处:http://www.notonlysuccess.com/ 今晚上比赛就考到了 排兵布阵啊,难受. [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时 ...
- HDU 3340 Rain in ACStar(线段树+几何)
HDU 3340 Rain in ACStar pid=3340" target="_blank" style="">题目链接 题意:给定几个多 ...
随机推荐
- aws中的路由表
参考官方文档: 由表中包含一系列被称为路由的规则,可用于判断网络流量的导向目的地. 在您的 VPC 中的每个子网必须与一个路由表关联:路由表控制子网的路由.一个子网一次只能与一个路由表关联,但您可以将 ...
- 静态链表的C实现(基于数据结构 严蔚敏)
静态链表是利用一维数组实现逻辑上的单链表结构,结点的逻辑上相邻但物理位置上不一定相邻,因为内存分配上是一次性的,故称为静态. 特点: 预先需要一片连续的存储空间: 非随机存取: 无现成的"内 ...
- Java NIO之选择器
1.简介 前面的文章说了缓冲区,说了通道,本文就来说说 NIO 中另一个重要的实现,即选择器 Selector.在更早的文章中,我简述了几种 IO 模型.如果大家看过之前的文章,并动手写过代码的话.再 ...
- 使用pie.htc时Border-radius的兼容
如果一个图层中(navin)使用了pie.htc来对ie6,7,8进行兼容,如若上一层(navwrap)的样式中有背景的属性,则此层 (navin) 在ie6,7,8中背景颜色不显示.如下图:此部分的 ...
- Spring Security入门(1-13)Spring Security的投票机制和投票器
1.三种表决方式,默认是 一票制AffirmativeBased public interface AccessDecisionManager { /** * 通过传递的参数来决定用户是否有访问对应受 ...
- Archaius 原理
Archaius 原理 Archaius是什么? Archaius提供了动态修改配置的值的功能,在修改配置后,不需要重启应用服务.其核心思想就是轮询配置源,每一次迭代,检测配置是否更改,有更改重新更新 ...
- mysql中的视图、事务和索引
视图: 对于一个sql查询,如果发生了修改,就需要修改sql语句. 我们可以通过定义视图来解决问题.改变需求之后就改变视图. 视图是对查询的封装 定义视图: create view 视图名称 as s ...
- Hive&SqlServerql:inner join on条件中如果两边都是空值的情况下,关联结果中会把数据给过滤掉。
今天遇到的一个大坑,话不多少,看sql和下边的查询结果: --问题:恰好把buildingid is null的记录给过滤掉 ),buildingid ),)); ); ); ); ); ); ); ...
- SpringMVC(十):SpringMVC 处理输出模型数据之Map及Model
Spring MVC提供了以下几种途径输出模型数据: 1)ModelAndView:处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据: 2)Map及Model:处理方法 ...
- Ubuntu+Firefox总是打不开网页
参考链接 火狐的设置里面找: 首选项 -> 高级 -> 网络 -> 连接 -> 设置, 结果发现之前默认是选择了使用系统代理设置, 果断拒绝!... 勾选第一项, " ...