poj2482 Stars in Your Window
此题可用线段树或静态二叉树来做。
考虑用线段树:
很容易想到先限定矩形横轴范围再考虑在此纵轴上矩形内物品总价值的最大值。
那么枚举矩形横轴的复杂度是O(n)的,考虑如何快速获取纵轴上的最大值。
我们不可能再次枚举纵轴,依次统计,这样做事多余的。
考虑窗口在纵轴上的滑动,每上升到一个新的高度,在加入新元素的同时只需将最底层的那些值弹出队列即可。
这样我们需要考虑队列上元素和的最大值。
我们从反面考虑每个元素对特定队列(矩形纵轴位置)的贡献。
枚举窗口的上面一条边,那么元素对窗口贡献正值当且仅当H(element) < H(window_top) - b。
否则对窗口贡献0。
注意到变化是连续的,考虑统计所有高度不超过枚举高度的物品总价值减去那些高度低于窗口下端的物品总价值。
这样我们可以对每个点辅助构造一个对应的虚拟点,位置恰在该点上方b处,权值为是原点的相反数。
于是队列和的最大值变成统计前缀最大值。
y轴上离散化用线段是维护即可,x轴控制进出队。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + ;
typedef __int64 LL;
struct Seg{
LL l, r;
LL sum, maxl;
}seg[maxn << ];
LL n, a, b;
LL k;
LL base, len;
struct Point{
LL x, y, z;
Point(LL x = , LL y = , LL z = ) : x(x), y(y), z(z) {}
}t[maxn << ]; bool cmp1(Point a, Point b){
return a.y < b.y;
} bool cmp2(Point a, Point b){
return a.x < b.x;
} void build(LL u, LL l, LL r){
seg[u].l = l, seg[u].r = r;
seg[u].maxl = seg[u].sum = ;
if(r - l < ) return;
LL mid = (l + r) >> ;
build(u << , l, mid);
build(u << | , mid, r);
} void push_up(LL u){
seg[u].sum = seg[u << ].sum + seg[u << | ].sum;
seg[u].maxl = max(seg[u << ].maxl, seg[u << ].sum + seg[u << | ].maxl);
} void add(LL u, LL l, LL r, LL L, LL R, LL from, LL d){
if(l == L && r == R){
seg[u].maxl += d * t[from].z;
seg[u].sum += d * t[from].z;
return;
}
LL mid = (l + r) >> ;
if(R <= mid) add(u << , l, mid, L, R, from, d);
else if(L >= mid) add(u << | , mid, r, L, R, from, d);
push_up(u);
} int main(){
//freopen("in.txt", "r", stdin);
while(~scanf("%I64d%I64d%I64d", &n, &a, &b)){
for(LL i = , x, y, z; i <= n; i++){
scanf("%I64d%I64d%I64d", &x, &y, &z);
t[i] = Point(x, y, z);
t[i + n] = Point(x, y + b, -z);
}
len = n << ;
sort(t + , t + len + , cmp1);
base = t[].y;
t[].y = ;
for(LL i = ; i <= len; i++){
if(t[i].y == base) t[i].y = t[i - ].y;
else{
base = t[i].y;
t[i].y = t[i - ].y + ;
}
}
LL high = t[len].y;
sort(t + , t + len + , cmp2);
base = ;
while(base <= len && t[base].x - t[].x < a) ++base;
build(, , high + );
for(LL i = ; i < base; i++) add(, , high + , t[i].y, t[i].y + , i, );
LL ans = seg[].maxl;
LL pre = ;
for(LL i = base; i < len + ; i++){
LL tp = pre;
while(tp < len + && t[i].x - t[tp].x >= a) ++tp;
for(LL j = pre; j < tp; j++) add(, , high + , t[j].y, t[j].y + , j, -);
LL tem = i;
while(i < len && t[i + ].x == t[i].x) ++i;
for(LL j = tem; j <= i; j++) add(, , high + , t[j].y, t[j].y + , j, );
ans = max(ans, seg[].maxl);
pre = tp;
}
printf("%I64d\n", ans);
}
return ;
}
poj2482 Stars in Your Window的更多相关文章
- POJ2482 Stars in Your Window(扫描线+区间最大+区间更新)
Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I stil ...
- POJ2482 Stars in Your Window 和 test20180919 区间最大值
Stars in Your Window Language:Default Stars in Your Window Time Limit: 1000MS Memory Limit: 65536K T ...
- POJ2482 Stars in Your Window 题解
Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I stil ...
- Poj2482 Stars in Your Window(扫描线)
题面 Poj 题解 下面内容引用自"李煜东 <算法竞赛进阶指南>"(对原文略有缩减,侵删): 因为矩形的大小固定,所以矩形可以由它的任意一个顶点唯一确定.我们可以考虑把 ...
- 【POJ2482】Stars in Your Window
[POJ2482]Stars in Your Window 题面 vjudge 题解 第一眼还真没发现这题居然™是个扫描线 令点的坐标为\((x,y)\)权值为\(c\),则 若这个点能对结果有\(c ...
- 【POJ-2482】Stars in your window 线段树 + 扫描线
Stars in Your Window Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11706 Accepted: ...
- 【POJ2482】【线段树】Stars in Your Window
Description Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw ...
- 51nod 1208 && POJ 2482:Stars in Your Window
1208 Stars in Your Window 题目来源: Poj 基准时间限制:2 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 收藏 取消关注 整点上有N颗星星,每颗 ...
- POJ 2482 Stars in Your Window 线段树扫描线
Stars in Your Window Description Fleeting time does not blur my memory of you. Can it really be 4 ...
随机推荐
- css 字数超过一行显示省略号
display:block;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;
- 从零开始攻略PHP(6)——代码重用与函数编写的一些注意事项
一个新的项目是这样创建的:它将已有的可重新利用的组件进行组合,并将新的开发难度降低到最小. 代码重用的好处:降低成本.提升可靠性和一致性. 1.使用require()和include()函数 使用一条 ...
- 关于GridView只显示一样的问题
如果GridView不管怎么改都只能显示一行的话,就重写GridView,自定义GridView: public class MyGridView extends GridView { public ...
- 封装mysqli类
类: <?phpheader('content-type:text/html;charset=utf-8');/*掌握满足单例模式的必要条件(1)私有的构造方法-为了防止在类外使用new关键字实 ...
- session 和 cookie 的区别与联系
1.创建一个新的Cookie Cookie cookie = new Cookie("username",name); 2.设置cookie在客户端上存活多久 cookie.set ...
- MYSQL数据库自动本地/异地双备份/MYSQL增量备份
构建高安全电子商务网站之(网站文件及数据库自动本地/异地双备份)架构图 继续介绍Linux服务器文件备份,数据库备份,数据安全存储相关的电子商务系统架构.针对安全性有多种多样的解决方案,其中数据备份是 ...
- paper 25 :SVM支持向量机是什么意思?
转载来源:https://www.zhihu.com/question/21094489 作者:余洋链接:https://www.zhihu.com/question/21094489/answer/ ...
- Vim篇
Vim编辑器中的一些常用命令: 1:shift+* , 选取光标所在处的整个字符,并查找.(十分方便),快捷键gd 2:set nu , 显示各行行号,使得基于行的命令更方便. 3:shift+% , ...
- 对linux的根目录执行强制递归移除
开始开始时使用: #rm -f -r / 提示对根目录使用递归操作很危险,然后就没执行成功,让使用 --no-preserve-root 这个参数. 好吧,反正是虚拟机 于是执行: #rm -f -r ...
- windows下nginx和php环境的配置
至于php的配置,与之前博文中使用apache服务器时一样. 对于nginx的配置,来看看如何修改配置文件: #user nobody; worker_processes ; #error_log l ...