Billboard

Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 15719    Accepted Submission(s): 6629
Problem Description
At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest programming competitions, changes
in the dining room menu, and other important information.



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
There are multiple cases (no more than 40 cases).



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
For each announcement (in the order they are given in the input file) 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
3 5 5
2
4
3
3
3
 
Sample Output
1
2
1
3
-1
 

题意:有一个高h宽w的公告栏,如今要在上边贴N个1*W的公告。要求1。要在可以全然放下的区域贴公告。2,贴公告时遵循最上最左的原则。问你每张公告分别贴在第几行,若公告无法插入输出-1。


思路:以min(h, N)为区间右边界建立线段树,初始化每一个区间最大值Max为w。
之后每次贴公告就是——找一个Max值最大的区间插入 + 维护区间最大值。若Max[1] < W说明不能插入。




AC代码:
#include <cstdio>
#include <algorithm>
#include <iostream>
#define ll o<<1
#define rr o<<1|1
using namespace std;
const int MAXN = 2 * 1e5 + 10;
struct Tree { int l, r, Max; }tree[MAXN<<2];
void PushUp(int o) {
tree[o].Max = max(tree[ll].Max, tree[rr].Max);
}
void Build(int o, int l, int r, int v) {
tree[o].l = l; tree[o].r = r; tree[o].Max = v;
if(l == r) { return ; }
int mid = (l + r) >> 1;
Build(ll, l, mid, v); Build(rr, mid+1, r, v);
}
void Update(int o, int pos, int v) {
if(tree[o].l == tree[o].r) {
tree[o].Max += v;
return ;
}
int mid = (tree[o].l + tree[o].r) >> 1;
if(pos <= mid) Update(ll, pos, v);
else Update(rr, pos, v);
PushUp(o);
}
int Query(int o, int v) {
if(tree[o].l == tree[o].r) {
return tree[o].l;
}
if(tree[ll].Max >= v) return Query(ll, v);
else return Query(rr, v);
}
int main()
{
int h, w, n;
while(scanf("%d%d%d", &h, &w, &n) != EOF) {
Build(1, 1, min(n, h), w);
for(int i = 1; i <= n; i++) {
int v; scanf("%d", &v);
if(tree[1].Max >= v) {
int id = Query(1, v);
printf("%d\n", id);
Update(1, id, -v);
}
else {
printf("-1\n");
}
}
}
return 0;
}

hdoj 2795 Billboard 【线段树 单点更新 + 维护区间最大值】的更多相关文章

  1. HDU 2795 Billboard (线段树单点更新 && 求区间最值位置)

    题意 : 有一块 h * w 的公告板,现在往上面贴 n 张长恒为 1 宽为 wi 的公告,每次贴的地方都是尽量靠左靠上,问你每一张公告将被贴在1~h的哪一行?按照输入顺序给出. 分析 : 这道题说明 ...

  2. hdu 2795 Billboard 线段树单点更新

    Billboard Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=279 ...

  3. HDU 1754 I Hate It 【线段树单点修改 维护区间最大值】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1754 I Hate It Time Limit: 9000/3000 MS (Java/Others ...

  4. hdu 1754 线段树 单点更新 动态区间最大值

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. poj 2892---Tunnel Warfare(线段树单点更新、区间合并)

    题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...

  6. HDU - 1754 线段树-单点修改+询问区间最大值

    这个也是线段树的经验问题,待修改的,动态询问区间的最大值,只需要每次更新的时候,去把利用子节点的信息进行修改即可以. 注意更新的时候区间的选择,需要对区间进行二分. #include<iostr ...

  7. nyoj 568——RMQ with Shifts——————【线段树单点更新、区间求最值】

    RMQ with Shifts 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述     In the traditional RMQ (Range Minimum Q ...

  8. HDU1754 —— I Hate It 线段树 单点修改及区间最大值

    题目链接:https://vjudge.net/problem/HDU-1754 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜 ...

  9. hdu1166(线段树单点更新&区间求和模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...

随机推荐

  1. ubuntu系统源的更新

    以前没有注意,ubuntu的系统的源原来每次在GUI操作后都会追加在后面而不是覆盖,比如先添加了上海交大的源那么后来如果发现很慢下载不了,而这个包已经在apt-cache里面了,下次如果要instal ...

  2. js设计模式-适配器模式

    说明:适配器模式表面上看起来像门面模式.它们都要对别的对象进行包装并改变其呈现的接口.但是两者的差别在于它们如何改变接口.门面元素展现的是一个简化的接口,它并不提供额外的选择,而且有时为了方便完成常见 ...

  3. First Day Python介绍

    前言:刚开通的博客,谢谢博客园平台,管理辛苦! Python介绍 Python是一门高级的.面向对象的.解释性.脚本语言. 高级语言:贴近开发者,对应底层语言,底层语言贴近机器:java.C#.php ...

  4. 用Webpack构建Vue项目

    开始之前,需要安装node环境.(安装过程在此就不啰嗦了)   1.创建基本结构 首先我们要创建一个空文件夹(我这里叫todos,你可以随便命名)作为项目的根目录. 创建一个没有任何依赖关系的pack ...

  5. 访问修饰符相关注意点(protected子类友好)

    注意:protected表示只有在子类和同包中可以访问. 需要注意的是,在其他包中,若是创建了父类的对象,但是父类对象访问不了自己类里面用protected修饰的属性,只能由子类访问父类的protec ...

  6. App Store兼容性问题

    app下载出现兼容性问题  项目支持9.0以上的系统 但是10.3的iphone5下载的一直是老版本app  下载时提示不兼容 导致无法正常使用 解决办法: 修改Build-Settings-> ...

  7. linux网络路由配置

    网卡配置文件介绍: # vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 (描述网卡对应的设备别名,例如ifcfg-eth0的文件中它为 ...

  8. vue1 到 vue2 v-el变成ref

    vue1的写法 div class="menu-wrapper" v-el="menu-wrapper"> <ul class="menu ...

  9. Kinect+OpenNI+OpenCV使用

    关于OpenNI,已经可以使用2.0,可以不再使用PrimeSense: 这里的是转载其他人的 OpenCV系列: 原文:http://blog.csdn.net/chenxin_130/articl ...

  10. Java判断字符串中是否含有英文

    实现代码: /* * 判断字符串中是否含有英文,包含返回true */ public boolean isENChar(String string) { boolean flag = false; P ...