ACM学习历程—HDU 2795 Billboard(线段树)
Description
On September 1, the billboard was empty. One by one, the announcements started being put on the billboard.
Each announcement is a stripe of paper of unit height. More specifically, the i-th announcement is a rectangle of size 1 * wi.
When someone puts a new announcement on the billboard,
she would always choose the topmost possible position for the
announcement. Among all possible topmost positions she would always
choose the leftmost one.
If there is no valid location for a new announcement, it
is not put on the billboard (that's why some programming contests have
no participants from this university).
Given the sizes of the billboard and the announcements,
your task is to find the numbers of rows in which the announcements are
placed.
Input
The first line of the input file contains three integer
numbers, h, w, and n (1 <= h,w <= 10^9; 1 <= n <= 200,000) -
the dimensions of the billboard and the number of announcements.
Each of the next n lines contains an integer number wi (1 <= wi <= 10^9) - the width of i-th announcement.
Output
output one number - the number of the row in which this announcement is
placed. Rows are numbered from 1 to h, starting with the top row. If an
announcement can't be put on the billboard, output "-1" for this
announcement.
Sample Input
Sample Output
然后根据线段树查询,从左儿子到右儿子查询。
直到查到点对应的区间时,就修改该区间的val值,并返回lt或者rt值。
然而,这题数据规模有点BT,必须任意时候不满足就return -1,不能查到底部才返回。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
#define LL long long using namespace std; int h, w, n; //线段树
//区间某点增值,求区间最大值
const int maxn = ;
struct node
{
int lt, rt;
int val;
}tree[*maxn]; //向上更新
void pushUp(int id)
{
tree[id].val = max(tree[id<<].val, tree[id<<|].val);
} //建立线段树
void build(int lt, int rt, int id)
{
tree[id].lt = lt;
tree[id].rt = rt;
tree[id].val = ;//每段的初值,根据题目要求
if (lt == rt)
{
tree[id].val = w;
return;
}
int mid = (lt + rt) >> ;
build(lt, mid, id<<);
build(mid + , rt, id<<|);
pushUp(id);
} //查询某段区间内的符合p的
int query(int lt, int rt, int id, int p)
{
if (tree[id].val < p)
return -;
if (tree[id].lt == tree[id].rt)
{
tree[id].val -= p;
return tree[id].lt;
}
int tmp;
tmp = query(lt, rt, id<<, p);
if (tmp != -)
{
pushUp(id);
return tmp;
}
tmp = query(lt, rt, id<<|, p);
pushUp(id);
return tmp;
} void work()
{
int k, ans;
int len = min(h, n);
build(, len, );
for (int i = ; i < n; ++i)
{
scanf("%d", &k);
ans = query(, len, , k);
printf("%d\n", ans);
}
} int main()
{
//freopen("test.in", "r", stdin);
while (scanf("%d%d%d", &h, &w, &n) != EOF)
{
work();
}
return ;
}
ACM学习历程—HDU 2795 Billboard(线段树)的更多相关文章
- ACM学习历程—HDU 5289 Assignment(线段树 || RMQ || 单调队列)
Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fro ...
- hdu 2795 Billboard 线段树单点更新
Billboard Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=279 ...
- HDU 2795 Billboard (线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题目大意:有一块h*w的矩形广告板,要往上面贴广告; 然后给n个1*wi的广告,要求把广告贴 ...
- HDU 2795 Billboard (线段树+贪心)
手动博客搬家:本文发表于20170822 21:30:17, 原地址https://blog.csdn.net/suncongbo/article/details/77488127 URL: http ...
- [HDU] 2795 Billboard [线段树区间求最值]
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2795 Billboard 线段树,区间最大值,单点更新
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU 2795 Billboard (线段树单点更新 && 求区间最值位置)
题意 : 有一块 h * w 的公告板,现在往上面贴 n 张长恒为 1 宽为 wi 的公告,每次贴的地方都是尽量靠左靠上,问你每一张公告将被贴在1~h的哪一行?按照输入顺序给出. 分析 : 这道题说明 ...
- HDU 2795 Billboard 线段树活用
题目大意:在h*w 高乘宽这样大小的 board上要贴广告,每个广告的高均为1,wi值就是数据另给,每组数组给了一个board和多个广告,要你求出,每个广告应该贴在board的哪一行,如果实在贴不上, ...
- hdu 2795 Billboard 线段树+二分
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...
随机推荐
- Mode Standby
Modern Standby 1.Connected Standby和 Connected Standby是Windows 8全新的电源管理系统,即当系统进入休眠状态时,应用程式虽处於暂停(suspe ...
- ARM内核和架构
转:深入浅谈,CPU设计原理 CPU的内部架构和工作原理 推荐一本书:编码的奥秘 一.ARM内核和架构 ARM产品越来越丰富,命名也越来越多.很多朋友提问: ARM内核和架构都是什么 ...
- Codeforces Round #263 (Div. 2) proB
题目: B. Appleman and Card Game time limit per test 1 second memory limit per test 256 megabytes input ...
- windows下如何快速优雅的使用python的科学计算库?
Python是一种强大的编程语言,其提供了很多用于科学计算的模块,常见的包括numpy.scipy.pandas和matplotlib.要利用Python进行科学计算,就需要一一安装所需的模块,而这些 ...
- NuGet管理工具安装
安装完成后VS重启即可
- live555二次开发经验总结:RTSPClient客户端与RTSPServer服务器
live555介绍 安防领域的流媒体开发者估计没有谁不知道live555的,可能并不是因为其架构有多牛,代码有多好看,而是因为这玩意存在的年限实在是太长了,从changelog来看,live555从2 ...
- git查看某一次commit里面的内容,即本次commit相对于原来的版本进行了哪些修改
1 知道commit id的话 git show commit-id 2 想要查看某次commit的某个文件进行了哪些修改 git show commit-id filename
- ios导航栏问题
http://m.blog.csdn.net/article/details?id=47395605
- 有关svg的一些理解
SVG 是使用XML来描述二维图形和绘图程序的语言. SVG指可伸缩的矢量图形(Scalable Vector Graphics) SVG使用XML格式定义图形 SVG图形在放大或改变尺寸的情况下,图 ...
- uboot中添加自定义命令
uboot中可以通过修改源程序来添加自定义命令,进一步扩展uboot的功能. 我想在uboot下添加一条新的命令(名为varcpy),用来拷贝uboot中的环境变量. 修改方式如下: 创建新文件com ...