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 ...
随机推荐
- uboot之bootm以及go命令的实现
本文档简单介绍了uboot中用于引导内核的命令bootm的实现,同时分析了uImage文件的格式,还简单看了一下uboot下go命令的实现 作者: 彭东林 邮箱: pengdonglin137@163 ...
- SVN 等版本管理工具
程序猿团队开发代码,必须的程序版本管理工具 1.SVN使用教程总结 2.SVN如何切换用户 在使用svn更新或提交数据时需要输入用户名和密码,在输入框中可以选择是否记录,以便下次操作无需再次输入用户名 ...
- kaptcha的和springboot一起使用的简单例子
https://blog.csdn.net/xiaoyu19910321/article/details/79296030
- 【bootstrap】右侧sidebar不跟着内容滚动的异常
移动开发需要依赖于Web服务的接口,但是写这个接口文档实在是比较繁琐,所以今天我就写了个包解析程序自动生成接口文档. 内容显示我是借鉴Bootstrap的官方教程http://v3.bootcss.c ...
- Swift实战(一): 剪子包袱锤ios应用
来自十奶的大作业教学视频. http://www.swiftv.cn/course/ic2tqzob 主要了解了MVC模型. 首先是View,通过设计mainstoryboard构建UI界面,主要靠拖 ...
- SPOJ LCS2 - Longest Common Substring II 后缀自动机 多个串的LCS
LCS2 - Longest Common Substring II no tags A string is finite sequence of characters over a non-emp ...
- ES通过API调整设置
1.查询es的设置信息 2.查询单个索引的设置 3.设置复制集为0
- HttpClient 访问 https 出现peer can't
package util; import java.security.cert.CertificateException; import javax.net.ssl.SSLContext;import ...
- regularexpression_action
re.compile('"ssid":"[^"]*"}',re.MULTILINE) regex ,str_= re.compile('"s ...
- 点聚-weboffice 6.0 (二)
1.修订操作 //设置当前操作用户 function SetUserName() { try{ var webObj=document.getElementById("WebOffice1& ...