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 ...
随机推荐
- LCA和RMQ
下面写提供几个学习LCA和RMQ的博客,都很通熟易懂 http://dongxicheng.org/structure/lca-rmq/ 这个应该是讲得最好的,且博主还有很多其他文章,可以读读,感觉认 ...
- Leetcode: Combination Sum IV && Summary: The Key to Solve DP
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- PHP 小谈静态用法
1.静态的表现形式,在PHP中定义一个静态变量需要加入一个关键字——static,静态对象是属于类的,非静态对象是属于对象的 class Fenbi { public $changdu;//属于对象的 ...
- 汉字转【pinyin】
引言 github地址:aizuyan/pinyin 无意中看到了overtrue/pinyin这个项目,感觉很有意思,这个项目做了这么一件事情: 将汉字转化为拼音 刚看到这里是不是觉得没什么难度,没 ...
- ruby的在ubuntu上的安装
apt (Debian or Ubuntu) Debian GNU/Linux and Ubuntu use the apt package manager. You can use it like ...
- Sqlserver列出所有数据库名,表名,字段名
Sqlserver列出所有数据库名,表名,字段名 1.获取所有数据库名: ? 1 SELECT Name FROM Master..SysDatabases ORDER BY Name 注 ...
- asp上传图片提示 ADODB.Stream 错误 '800a0bbc'的解决方法
asp上传图片提示 ADODB.Stream 错误 '800a0bbc' 有这个提示有很多问题导致.权限是常见一种.这个不多说,还有一个有点怪的就是 windows2008显示系统时间的格式竟然是:2 ...
- 夺命雷公狗---DEDECMS----10dedecms双标签
双标签基本语法如下: {dede:标签名 参数名=“值” 参数名2=“值”...} 内容...... {/dede} 我们先来查看下手册,如下所示: 我们先来用一个channel的标签来做实例,因为c ...
- Ado.net连接池 sp_reset_connection 概念
什么是连接池? 正常情况下,每次访问数据库都会打开和关闭,中断物理连接后需要再次进行物理连接.这样操作会浪费资源 使用连接池,主要的区别在于,不需要中断物理连接,即每次中断请求时spid还是存在! 原 ...
- 【《zw版·Halcon与delphi系列原创教程》Halcon图层与常用绘图函数
[<zw版·Halcon与delphi系列原创教程>Halcon图层与常用绘图函数 Halcon的绘图函数,与传统编程vb.c.delphi语言完全不同, 传统编程语言,甚至cad ...