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 ...
随机推荐
- Swift动画编程指南-02 Swift动画是怎么炼成的
上一节我们看了几个很棒的例子,我们不禁会想.他们是怎么设计的,怎么从一个空白的画布变成一个完整的,美丽的动画.这些动画是如何产生的,是哪些属性被改变了.我们还要认真思考的是,每一个步骤到底发生了什么. ...
- Lintcode: Rotate String
Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given &qu ...
- Lintcode: Minimum Adjustment Cost
Given an integer array, adjust each integers so that the difference of every adjcent integers are no ...
- AJAX简单的数据增删改与分页应用
运行截图: PageBar.js: /* * 说明: * 整体思想,1.第一页时不显示:首页,上一页, * 2.最后一页时不显示:下一页,尾页 * 3.中间有 5 页导航, * 若:3.1.(总页数& ...
- php初探
1.php中的连接符.可以连接多个字符串,相当于java中的+ 2.echo必须与后面的输出内容有至少一个空格 3.php编程中每个结尾都需要添加分号
- springday03-go1
springday02项目下新建包annotation11.复制xml文件到包annotation1下,并添加组件扫描方式代码2.Waiter类实现序列化接口,构造函数,并使用特定注解标记waiter ...
- CORBA的简单介绍及HelloWorld(zhuan)
http://blog.csdn.net/drykilllogic/article/details/25971915
- 超链接点击后不显示hover
超链接访问过后 hover 样式就不出现了,被点击访问过的超链接样式不在具有 hover 和 active 了 解决方法:改变CSS属性的排列顺序 L-V-H-A a:link {} a:visite ...
- centos的网路配置文件的位置
在这个路径下:/etc/sysconfig/network-scripts/ 每个网卡有相应的配置文件
- thinkphp 一个页面使用2次分页的方法
thinkphp内置ORG.Util.Page方法分页,使分页变得非常简单快捷. 但是如果一个页面里需要使用2次分页,就会产生冲突,这里先记录下百度来的解决办法 可以说是毫无技术含量的办法: 将Pag ...