HDU 3340 Rain in ACStar(线段树+几何)
HDU 3340 Rain in ACStar
pid=3340" target="_blank" style="">题目链接
题意:给定几个多边形(3-5边形),然后中间有一些询问。询问一个区间的总面积
思路:多边形切割为梯形,梯形的面积为上底d1 + 下底d2 乘上 高度 / 2。两个梯形面积累加的话,能够等价为上底下底累加,所以就能够用线段树搞了,然后给定的多边形点是按顺序的,能够利用容斥去方便把一个询问拆分成几个询问
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; const int N = 133333;
int t, n; struct Point {
int x, y, id;
Point() {}
Point(int x, int y) {
this->x = x;
this->y = y;
this->id = id;
}
void read() {
scanf("%d%d", &x, &y);
}
} p[6]; bool cmpp(Point a, Point b) {
return a.x < b.x;
} struct OP {
int tp, l, r;
double a, b;
OP() {}
OP(int l, int r, int tp, double a = 0.0, double b = 0.0) {
this->l = l; this->r = r; this->tp = tp;
this->a = a; this->b = b;
}
} op[N]; int hash[N * 2], hn, opn; int find(int x) {
return lower_bound(hash, hash + hn, x) - hash;
} void build_p(int m) {
p[m++] = p[0];
for (int i = 0; i < m - 1; i++) {
if (p[i].x < p[i + 1].x) op[opn++] = OP(p[i].x, p[i + 1].x, 1, -p[i].y, -p[i + 1].y);
else op[opn++] = OP(p[i + 1].x, p[i].x, 1, p[i + 1].y, p[i].y);
}
} #define lson(x) ((x<<1)+1)
#define rson(x) ((x<<1)+2) struct Node {
int l, r;
double d1, d2, s;
void gao(double a, double b) {
d1 += a;
d2 += b;
s += (a + b) * (hash[r + 1] - hash[l]) / 2;
}
} node[N * 8]; void build(int l, int r, int x = 0) {
node[x].l = l; node[x].r = r;
node[x].d1 = node[x].d2 = node[x].s = 0;
if (l == r) return;
int mid = (l + r) / 2;
build(l, mid, lson(x));
build(mid + 1, r, rson(x));
} double cal(double h1, double h2, double d1, double d2) {
return (d2 * h1 + d1 * h2) / (h1 + h2);
} void pushdown(int x) {
double h1 = hash[node[lson(x)].r + 1] - hash[node[lson(x)].l];
double h2 = hash[node[rson(x)].r + 1] - hash[node[rson(x)].l];
double d = cal(h1, h2, node[x].d1, node[x].d2);
node[lson(x)].gao(node[x].d1, d);
node[rson(x)].gao(d, node[x].d2);
node[x].d1 = node[x].d2 = 0;
} void pushup(int x) {
node[x].s = node[lson(x)].s + node[rson(x)].s;
} void add(int l, int r, double a, double b, int x = 0) {
if (node[x].l >= l && node[x].r <= r) {
double d1 = cal(hash[node[x].l] - hash[l], hash[r + 1] - hash[node[x].l], a, b);
double d2 = cal(hash[node[x].r + 1] - hash[l], hash[r + 1] - hash[node[x].r + 1], a, b);
node[x].gao(d1, d2);
return;
}
int mid = (node[x].l + node[x].r) / 2;
pushdown(x);
if (l <= mid) add(l, r, a, b, lson(x));
if (r > mid) add(l, r, a, b, rson(x));
pushup(x);
} double query(int l, int r, int x = 0) {
if (node[x].l >= l && node[x].r <= r) return node[x].s;
int mid = (node[x].l + node[x].r) / 2;
double ans = 0;
pushdown(x);
if (l <= mid) ans += query(l, r, lson(x));
if (r > mid) ans += query(l, r, rson(x));
pushup(x);
return ans;
} int main() {
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
char ss[15];
int l, r, m;
hn = opn = 0;
while (n--) {
scanf("%s", ss);
if (ss[0] == 'Q') {
scanf("%d%d", &l, &r);
hash[hn++] = l; hash[hn++] = r;
op[opn++] = OP(l, r, 0);
}
else {
scanf("%d", &m);
for (int i = 0; i < m; i++) {
p[i].read();
hash[hn++] = p[i].x;
}
build_p(m);
}
}
int tmp = 1;
sort(hash, hash + hn);
for (int i = 1; i < hn; i++)
if (hash[i] != hash[i - 1])
hash[tmp++] = hash[i];
hn = tmp;
build(0, hn - 2);
for (int i = 0; i < opn; i++) {
if (op[i].tp) add(find(op[i].l), find(op[i].r) - 1, op[i].a, op[i].b);
else printf("%.3lf\n", query(find(op[i].l), find(op[i].r) - 1));
}
}
return 0;
}
HDU 3340 Rain in ACStar(线段树+几何)的更多相关文章
- hdu 3340 Rain in ACStar 线段树区间等差数列更新
Rain in ACStar Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/problem/show/1385 ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU.5692 Snacks ( DFS序 线段树维护最大值 )
HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)
HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
- hdu 1754 I Hate It 线段树 点改动
// hdu 1754 I Hate It 线段树 点改动 // // 不多说,裸的点改动 // // 继续练 #include <algorithm> #include <bits ...
- hdu 1166 敌兵布阵 线段树 点更新
// hdu 1166 敌兵布阵 线段树 点更新 // // 这道题裸的线段树的点更新,直接写就能够了 // // 一直以来想要进线段树的坑,结果一直没有跳进去,今天算是跳进去吧, // 尽管十分简单 ...
随机推荐
- codevs1358棋盘游戏(状压dp)
1358 棋盘游戏 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 大师 Master 题目描述 Description 这个游戏在一个有10*10个格子的棋盘上进行,初 ...
- Gym - 101981M The 2018 ICPC Asia Nanjing Regional Contest M.Mediocre String Problem Manacher+扩增KMP
题面 题意:给你2个串(长度1e6),在第一个串里找“s1s2s3”,第二个串里找“s4”,拼接后,是一个回文串,求方案数 题解:知道s1和s4回文,s2和s3回文,所以我们枚举s1的右端点,s1的长 ...
- 爬虫框架Scrapy与Web框架Django结合
在做两者结合之前,需要先准备一个可以独立运行的Scrapy框架和一个可以独立运行的Django框架! 当准备好这两个框架之后,就可以做两者的结合了. 一. 把scrapy框架,移动到Django框架的 ...
- Asp.net跨域配置
<system.webServer> <httpProtocol> <customHeaders> <add name="Access-Contro ...
- My97DatePicker 动态设置有效/无效日期
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...
- eclipse中server 没有tomcat选项
eclipse集成Tomcat: 打开eclipse - 窗口 - 首选项 - 服务器 - 运行时环境 找到Tomcat然后添加. eclipse添加插件: 开发WEB项目时要集成Tomcat可以并不 ...
- 关于改变安卓Button样式,这里有一个好方法。
首先,在drawable下创建一个新的xml文件(例如我创建的为button.xml).然后在里面输入以下代码. <item> <shape> <gradient and ...
- SqlSever2005 一千万条以上记录分页数据库优化经验总结【索引优化 + 代码优化】
对普通开发人员来说经常能接触到上千万条数据优化的机会也不是很多,这里还是要感谢公司提供了这样的一个环境,而且公司让我来做优化工作.当数据库中的记录不超过10万条时,很难分辨出开发人员的水平有多高,当数 ...
- 【Oracle】解决oracle sqlplus 中上下左右backspace不能用
一. 解决输入 BACKSPACE 键变成 ^h 的问题 #su - oracle $stty erase ^h. 要永久生效,可以加入到用户环境配置文件.bash_profile中(vi .ba ...
- C++版的LLC代码
图像稀疏编码总结:LLC和SCSPM,文章对稀疏编码讲解非常详细. <Locality-constrained Linear Coding for Image Classification> ...