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 ...
随机推荐
- loj1201(最大独立集)
传送门:A Perfect Murder 题意:有一群苍蝇,之间有一些是朋友关系,如果杀了一只苍蝇,那么它的朋友们都会有警惕性,再也杀不了这些朋友了,问最多能杀多少只苍蝇. 分析:根据朋友性连边,最多 ...
- 你的App为什么上不了TOP10?
App市场风起云涌.但是,却仅仅有少数几个App能成为"暴发户",很多其它的则沉淀在应用商店中无人问津. 在移动互联网时代.智能手机成为了中心. 手机之所以智能.就在于手机上 ...
- Ubuntu 8.04下安装DB2方法
參考文献: How-to: Ubuntu 7.10 Server x86 32-bit and DB2 Express-C v9.5 DB2 v9.7 Infomation Center 场景:在IB ...
- Android5.0L下因sensorservice crash导致systemserver重新启动的第二种场景分析
一.出问题的场景 1.Sensorservice线程正在处理compass sensor事件的过程中.检查了一次buffer的指针的有效性,并在稍后会传递到AKM获取数据的函数接口中使用 2.Sens ...
- Android之查看外部依赖jar的源代码_android private libralies does not allow modifications to source
在Eclipse中开发android的应用程序时,有时想查看函数的内部实现.可是当在函数上点击ctrl和鼠标左键的时候. 往往出现例如以下提示: Class File Editor Source no ...
- 关于使用X-UA-Compatible来设置IE浏览器兼容模式
原文地址:http://www.cnblogs.com/nidilzhang/archive/2010/01/09/1642887.html 前言 为了帮助确保你的网页在所有未来的IE版本都有一致的外 ...
- HDU2586 How far away ?(LCA模板题)
题目链接:传送门 题意: 给定一棵树,求两个点之间的距离. 分析: LCA 的模板题目 ans = dis[u]+dis[v] - 2*dis[lca(u,v)]; 在线算法:详细解说 传送门 代码例 ...
- hdu1532 (最大流入门,EK算法)
看着这个博客 然后敲了hdu1532这个入门题,算是对最大流有点理解了 #include <stdio.h> #include <string.h> #include < ...
- Mercurial简介
前言 目前所在的公司的版本控制使用的是Mercurial,它也有一个对应的客户端小乌龟,但是Mercurial跟我们之前使用的SVN有着本质的区别,对于其区别会在下一篇中介绍到,这次主要是带领 ...
- SQL Server :理解IAM 页
原文:SQL Server :理解IAM 页 在以前的文章里,我们讨论了数据页,GAM和SGAM,还有PFS页.今天我们一起来讨论下索引分配映射(Index Allocation Map:IAM)页. ...