题目链接: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(扫描线段树)的更多相关文章

  1. hdu 5091 Beam Cannon

    题目大意: 有n个点(n<=10000),点的坐标绝对值不超过20000,然后问你用一个w*h(1<=w,h<=40000)的矩形,矩形的边平行于坐标轴,最多能盖住多少个点. 刘汝佳 ...

  2. HDU 5091 Beam Cannon (扫描线思想)

    题意:移动一个矩形,使矩形内包含的点尽量多. 思路:把一个点拆成两个事件,一个进(权值为1)一个出(权值为-1),将所有点按照x排序,然后扫描,对于每个x,用一个滑窗计算一下最大值,再移动扫描线.树状 ...

  3. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  4. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  5. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  6. HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)

    HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...

  7. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  8. HDU.1689 Just a Hook (线段树 区间替换 区间总和)

    HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...

  9. hdu 1754 I Hate It 线段树 点改动

    // hdu 1754 I Hate It 线段树 点改动 // // 不多说,裸的点改动 // // 继续练 #include <algorithm> #include <bits ...

随机推荐

  1. hdu5176(并查集)

    传送门:The Experience of Love 题意:一个叫Gorwin的女孩和一个叫Vivin的男孩是一对情侣.他们来到一个叫爱情的国家,这个国家由N个城市组成而且只有N−1条小道(像一棵树) ...

  2. [置顶] 一步一步学android之事件篇——下拉列表事件

    上一篇RadioGroup比较简单,所以再学习个spinner的OnItemSelectedListener事件,前面说过spinner的主要功能就是提供列表显示的选择,比如我们在选择城市的时候就会用 ...

  3. WPF案例(二)模拟Apple OS 界面前后180度反转

    原文:WPF案例(二)模拟Apple OS 界面前后180度反转 我们在设计应用程序界面的时候,为了充分利用界面空间,住住需要灵活的界面布局方式,比如可以在界面正面空间上定义一个Chart,背面空间上 ...

  4. Restlet+Fastjson 高速构建轻量级 Java RESTful Webservice

    自己入门Java时做过一个小型RESTful Web Service的项目,这里总结一下. 服务的数据交换格式主要採用JSON,服务为REST风格.连接採用Http协议,数据库使用MySQL,OR M ...

  5. Android 网络编程 Socket Http

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...

  6. 单片机实验: 三轴磁场模块 GY-271

    最近买了一块三轴磁场模块进行实验 名称:HMC5883L模块(三轴磁场模块) 型号:GY-271 使用芯片:HMC5883L 供电电源:3-5v 通信方式:IIC通信协议 测量范围:±1.3-8 高斯 ...

  7. Hello World 之 控制台版本(Console Application)

    原文:Hello World 之 控制台版本(Console Application) 先来介绍下Hello, World   "Hello, World"程序指的是只在计算机屏幕 ...

  8. Cocos2D &amp; SpriteBuilder Developer Guide

    https://www.makegameswith.us/docs/#!/cocos2d/1.0/overview

  9. Oracle自增列创建方法

    最近在做Oracle的项目,由于以前没有接触过Oracle的开发,遇到了不少的问题,比如给Oracle表添加自增列,与SQL Server就不同. Oracle没有自增字段这样的功能,但是通过触发器( ...

  10. oracle数据库恢复与备份

    一.oracle数据库恢复 1.恢复刚才删除的一条数据 delete from emp e where e.empname='SMITH' select * from flashback_transa ...