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 ...
随机推荐
- setChecked方法触发onCheckedChanged监听器问题
有时须要在程序初始化界面时,讲有些比如toggleButton等控件依照需求勾选,此时会发现,当我setChecked时会触发onCheckedChanged监听器,导致这部分代码被调用两次.解决方法 ...
- [非官方]ArcGIS10.2 for Desktop扩展工具包——XTools Pro
XTools Pro 是一套为ArcGIS平台设计的矢量空间分析. 形状转换和表管理扩展工具,大大增强了 ArcGIS 的功能,使用该工具能够提高 ArcGIS 用户的效率和性能. XTools Pr ...
- ON、WHERE、HAVING的差别
ON .WHERE.HAVING都能通过限制条件筛选数据,但他们的使用及其不同.以下我们来分析三者之间的差别. 1. ON 和WHERE 全部的查询都回产生一个中间暂时报表,查询结果就是从 ...
- HDU 4916 树分治
Mart Master II Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- CrossBridge介绍
CrossBridge介绍 作者:chszs,转载需注明.博客主页: http://blog.csdn.net/chszs CrossBridge是Adobe FlasCC的开源版本,它提供了一个完整 ...
- Python使用MySQLdb操作MySQL
import MySQLdb,sys try: conn=MySQLdb.connect(host=,user=',db='db1') except Exception,e: print e sys. ...
- Yii学习笔记之三(在windows 上安装 advanced )
首先说一下下载地址: http://www.yiiframework.com/download/ 然后将下载下来的文件进行解压到 你指定的文件夹 解压过程中假设报什么错误 直接忽略掉 我的解压文件夹是 ...
- Spring MVC中一般 普通类调用service
在Spring MVC中,Controller中使用service只需使用注解@Resource就行,但是一般类(即不使用@Controller注解的类)要用到service时,可用如下方法: 1.S ...
- openstack之nova-api服务流程分析
nova-api公布api服务没实用到一个些框架,基本都是从头写的.在不了解它时,以为它很复杂,难以掌握.花了两三天的时间把它分析一遍后,发现它本身的结构比較简单,主要难点在于对它所使用的一些类库不了 ...
- Vs2012于Linux应用程序开发(4):公共财产的定义
在嵌入式开发流程.有些参数基本上不改变,比如编译主机IP,username,password等参数.我们用VS提供的属性管理器来保存这些參数. 打开属性管理器: watermark/2/text/aH ...