题面

Poj

题解

下面内容引用自"李煜东 《算法竞赛进阶指南》"(对原文略有缩减,侵删):

因为矩形的大小固定,所以矩形可以由它的任意一个顶点唯一确定。我们可以考虑把矩形的右上角顶点放在什么位置,圈住的星星亮度总和最大。

所以,对于一颗星星,能够覆盖住这颗星星的右上角的位置在区间$[x,y]-[x+w,y+h]$之间,但是由于边界上的星星不算,所以不妨将星星变为$[x-0.5,y-0.5]$,于是右上角变为$[x+w-1,y+h-1]$。

问题就转化成了:平面上有若干个区域,每个区域都带有一个权值,求在哪个坐标上重叠的区域权值和最大。可以用扫描线算法来解决。

具体来说,将一颗星星抽象成一个矩形,取出左右边界(四元组):$(x,y,y+h-1,c)$和$(x+w,y,y+h-1,-c)$,然后按照横坐标排序。关于纵坐标建立一颗线段树,维护区间最大值$dat$,初始全为$0$,线段树上的一个值$y$表示区间$[y,y+1]$,注意扫描每个四元组$(x,y_1,y_2,c)$,执行区间修改,把$[y1,y2]$中的每一个数都加$c$,用根节点$dat$更新就好了。

#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using std::min; using std::max;
using std::swap; using std::sort;
using std::unique; using std::lower_bound;
typedef long long ll;
#define int ll template<typename T>
void read(T &x) {
int flag = 1; x = 0; char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') flag = -flag; ch = getchar(); }
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= flag;
} const int N = 2e4 + 10;
ll n, W, H;
ll cf[N << 1], tot;
struct star { ll x, y, h; } s[N];
struct raw { ll x, y1, y2, d; } cy[N << 1]; int cnt;
inline bool cmp (const raw &a ,const raw &b) { return a.x < b.x || (a.x == b.x && a.d < b.d); }
ll val[N << 4], add[N << 4]; void pushdown(int o, int lc, int rc) {
if(add[o]) {
add[lc] += add[o], add[rc] += add[o];
val[lc] += add[o], val[rc] += add[o], add[o] = 0;
}
} void modify (int ml, int mr, ll k, int o = 1, int l = 1, int r = tot) {
if(l >= ml && r <= mr) { val[o] += k, add[o] += k; return ; }
int mid = (l + r) >> 1, lc = o << 1, rc = lc | 1;
pushdown(o, lc, rc);
if(ml <= mid) modify(ml, mr, k, lc, l, mid);
if(mr > mid) modify(ml, mr, k, rc, mid + 1, r);
val[o] = max(val[lc], val[rc]);
} signed main () {
while(scanf("%lld%lld%lld", &n, &W, &H) != EOF) {
cnt = tot = 0; ll ans = 0;
for(int i = 1; i <= n; ++i) {
read(s[i].x), read(s[i].y), read(s[i].h);
cy[++cnt] = (raw){s[i].x, s[i].y, s[i].y + H - 1, s[i].h};
cy[++cnt] = (raw){s[i].x + W, s[i].y, s[i].y + H - 1, -s[i].h};
cf[++tot] = s[i].y, cf[++tot] = s[i].y + H - 1;
} sort(&cf[1], &cf[tot + 1]), tot = unique(&cf[1], &cf[tot + 1]) - cf - 1;
sort(&cy[1], &cy[cnt + 1], cmp);
memset(val, 0, sizeof val), memset(add, 0, sizeof add);
for(int i = 1; i <= cnt; ++i) {
cy[i].y1 = lower_bound(&cf[1], &cf[tot + 1], cy[i].y1) - cf;
cy[i].y2 = lower_bound(&cf[1], &cf[tot + 1], cy[i].y2) - cf;
modify(cy[i].y1, cy[i].y2, cy[i].d);
ans = max(ans, val[1]);
}
printf("%lld\n", ans);
}
return 0;
}

Poj2482 Stars in Your Window(扫描线)的更多相关文章

  1. 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 ...

  2. poj 2482 Stars in Your Window(扫描线)

    id=2482" target="_blank" style="">题目链接:poj 2482 Stars in Your Window 题目大 ...

  3. POJ2482 Stars in Your Window 和 test20180919 区间最大值

    Stars in Your Window Language:Default Stars in Your Window Time Limit: 1000MS Memory Limit: 65536K T ...

  4. 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 ...

  5. poj2482 Stars in Your Window

    此题可用线段树或静态二叉树来做. 考虑用线段树: 很容易想到先限定矩形横轴范围再考虑在此纵轴上矩形内物品总价值的最大值. 那么枚举矩形横轴的复杂度是O(n)的,考虑如何快速获取纵轴上的最大值. 我们不 ...

  6. 【POJ-2482】Stars in your window 线段树 + 扫描线

    Stars in Your Window Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11706   Accepted:  ...

  7. 【POJ2482】Stars in Your Window

    [POJ2482]Stars in Your Window 题面 vjudge 题解 第一眼还真没发现这题居然™是个扫描线 令点的坐标为\((x,y)\)权值为\(c\),则 若这个点能对结果有\(c ...

  8. 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 ...

  9. 【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 ...

随机推荐

  1. 11.nginx upload module + python django 后台 实现视频上传与切片

    1.需求:支持视频上传并切片,支持通过m3u8文件播放 2.视频切片的上一节已经谈过,这一节主要是视频上传的处理 第一步:upload-module模块安装 -----------首先下载upload ...

  2. http基础(1.了解web及网络基础,2.简单的http协议)

    第一章:了解web及网络基础 1.http:超文本传输协议 2.tcp/ip协议族:通常使用的网络是在tcp/ip协议族的基础上运作的,而http属于它内部的一个子集. 3.tcp/ip协议族按层次分 ...

  3. 搜索:N皇后

    N皇后问题是DFS的代表性问题,其最难的地方就是在判重这里,想明白了怎么判重的话问题就很显然了. 这里给出两份代码,其中第一份代码的效率更好,就是在判重上下了功夫.当然,我记得还有使用位运算进行判重的 ...

  4. 使用MyBatis查询 返回类型为int,但是当查询结果为空NULL,报异常的解决方法

    使用MyBatis查询 返回类型为int,但是当查询结果为空NULL,会报异常. 例如: <select id="getPersonRecordId" parameterTy ...

  5. c# socket select 模型代码(u3d)

    其实写过多次网络链接.但是因为换了工作,又没电脑在身边,所以以前的代码都没办法翻出来用. 所以从今天起,一些常用的代码只好放到网上. 公司有一个局域网的游戏.本来想用u3d的rpc就可以完成.但是后来 ...

  6. 【CODEVS】3546 矩阵链乘法

    [算法]区间DP [题解] 注意先输出右括号后输出左括号. f[i][i+x-1]=min(f[i][i+x-1],f[i][j]+f[j+1][i+x-1]+p[i]*p[j+1]*p[i+x]) ...

  7. 用vue快速开发app的脚手架工具

    前言 多页面应用于结构较于简单的页面,因为简答的页面使用router又过于麻烦.本脚手架出于这样的场景被开发出来. 使用脚手架搭配Hbuilder也同样可以快速使用vue开发安卓和IOS APP. 本 ...

  8. 【bzoj2006】NOI2010超级钢琴

    补了下前置技能…… 题意就是求一段区间的权值和前k大的子序列的和. 把段扔进优先队列 每次拿出来之后按照所选择的j进行分裂 #include<bits/stdc++.h> #define ...

  9. 使用 Visual Studio 部署 .NET Core 应用 ——.Net Core 部署到SUSE Linux Enterprise Server 12 SP2 64 位(GNOME 版本3.20.2)

    SUSE Linux安装 apache 命令:sudo zypper in apache 设置apache 服务可用 命令:sudo systemctl enable apache2.service启 ...

  10. linux命令(8):du命令

    du –ah:查看文件列表大小 du –sh:查看所有文件的大小总和