hdu 5091 Beam Cannon(扫描线段树)
题目大意:给定N个点,如今要有一个W∗H的矩形,问说最多能圈住多少个点。
解题思路:线段的扫描线,如果有点(x,y),那么(x,y)~(x+W,y+H)形成的矩形,以框的右下角落的位置是能够圈住(x,y)
点,所以N个点即为N个矩形,求覆盖的最大次数,扫描线裸题。
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 40005;
const int bw = 20000;
#define lson(x) ((x)<<1)
#define rson(x) (((x)<<1)|1)
int lc[maxn << 2], rc[maxn << 2], ad[maxn << 2], nd[maxn << 2];
inline void pushup(int u) {
nd[u] = max(nd[lson(u)], + nd[rson(u)]) + ad[u];
}
inline void maintain(int u, int v) {
ad[u] += v;
nd[u] += v;
}
void build(int u, int l, int r) {
lc[u] = l;
rc[u] = r;
ad[u] = nd[u] = 0;
if (l == r)
return;
int mid = (l + r) >> 1;
build(lson(u), l, mid);
build(rson(u), mid + 1, r);
pushup(u);
}
void modify(int u, int l, int r, int v) {
if (l <= lc[u] && rc[u] <= r) {
maintain(u, v);
return;
}
int mid = (lc[u] + rc[u]) >> 1;
if (l <= mid)
modify(lson(u), l, r, v);
if (r > mid)
modify(rson(u), l, r, v);
pushup(u);
}
int query(int u, int l, int r) {
if (l <= lc[u] && rc[u] <= r)
return nd[u];
int mid = (lc[u] + rc[u]) >> 1, ret = 0;
if (l <= mid)
ret = max(ret, query(lson(u), l, r));
if (r > mid)
ret = max(ret, query(rson(u), l, r));
pushup(u);
return ret;
}
struct Seg {
int x, l, r, w;
Seg(int x = 0, int l = 0, int r = 0, int w = 0) {
this->x = x;
this->l = l;
this->r = r;
this->w = w;
}
};
vector<Seg> G;
inline bool cmp (const Seg& a, const Seg& b) {
if (a.x != b.x)
return a.x < b.x;
return a.w < b.w;
}
int N, W, H;
void init () {
int x, y;
G.clear();
scanf("%d%d", &W, &H);
for (int i = 0; i < N; i++) {
scanf("%d%d", &x, &y);
G.push_back(Seg(x, y + bw, min(bw, y + H) + bw, 1));
G.push_back(Seg(x + W + 1, y + bw, min(bw, y + H) + bw, -1));
}
build(1, 0, bw * 2);
sort(G.begin(), G.end(), cmp);
}
int main () {
while (scanf("%d", &N) == 1 && N != -1) {
init();
int ans = 0;
for (int i = 0; i < G.size(); i++) {
modify(1, G[i].l, G[i].r, G[i].w);
ans = max(ans, nd[1]);
}
printf("%d\n", ans);
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
hdu 5091 Beam Cannon(扫描线段树)的更多相关文章
- hdu 5091 Beam Cannon
题目大意: 有n个点(n<=10000),点的坐标绝对值不超过20000,然后问你用一个w*h(1<=w,h<=40000)的矩形,矩形的边平行于坐标轴,最多能盖住多少个点. 刘汝佳 ...
- HDU 5091 Beam Cannon (扫描线思想)
题意:移动一个矩形,使矩形内包含的点尽量多. 思路:把一个点拆成两个事件,一个进(权值为1)一个出(权值为-1),将所有点按照x排序,然后扫描,对于每个x,用一个滑窗计算一下最大值,再移动扫描线.树状 ...
- 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 ...
随机推荐
- red hat Linux 使用CentOS yum源更新
red hat linux是商业版软件,没有经过注册是无法使用红帽 yum源更新软件的,使用CentOS源更新操作如下: 1.删除red hat linux 原有的yum 源 rpm -aq | gr ...
- adm下载器
netdisk;5.2.7;PC;PC-Windows;6.2.9200;WindowsBaiduYunGuanJia
- poj3311(状压dp)
题目连接:http://poj.org/problem?id=3311 题意:一个送披萨的,每次送外卖不超过10个地方,给你这些地方之间的时间,求送完外卖回到店里的总时间最小. 分析:跑一遍Floyd ...
- 使用jquery+一般处理程序异步载入信息
需求:有时候.web界面对性能要求比較高.我们就不考虑使用asp.net控件.而是使用html标签+jquery+一般处理程序来进行异步处理. watermark/2/text/aHR0cDovL2J ...
- hdu4126(最小生成树+dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4126 题意:给出一幅3000个点的图,有10000次操作: 求将某条边的权值变大后的最小生成树,最后输 ...
- 如何收集 EBS 各种相关业务的表的数据
1. Receiving 相关 參照 Note: 402245.1, 跑 rcv11i_sa.sql 就能够, 输入 po number, 其余默认. 參照 Note: 1294177.1, 假设上面 ...
- Codeforces 549G. Happy Line 馋
非常有趣的贪婪: Let's reformulate the condition in terms of a certain height the towers, which will be on t ...
- android最近心得整理
activity中OnAttachedWindow生命周期在OnResume之后,所以对长宽获取在推荐在OnAttachedWindow中进行. onDetachedWindow是在OnDestroy ...
- HttpClient 网络优化
HttpClient 网络优化 尽管Android官网推荐在2.3及后续版本中使用HttpURLConnection作为网络开发首选类,但在连接管理和线程安全方面,HttpClient还是具有很大优势 ...
- ok6410 u-boot-2012.04.01移植七完善u-boot移植(u-boot移植结束)
继ok6410 u-boot-2012.04.01移植六后,开发板已支持MLC NAND.DM9000等.但还需要完善比如环境变量.mtdpart分区.裁剪.制作补丁等.下面的工作就是完善移植的u-b ...