题目大意:有一个h*w的公告榜,可以依次在上面添加信息。每个信息的长度为x,高为1.

优先在最上面加入,如果空间足够的话,然后优先放在最左面。统计每条公告最终的位置,即它所在的行数。

这里是线段树来存储 当前区间(i,j)的所有位置,剩余的最大空间。 初始化即为w,公告榜的宽。

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
 
#include <iostream>
#include <stdio.h>
using namespace std; int tree[1000000]; // 记录当前区间所有位置,剩余的最大空间
int h,w,n,t; int max(int a,int b){
return a > b ? a : b;
}
void build(int l,int r,int k) {
tree[k] = w;
if (l == r) return ;
int m = (l + r) >> 1;
build(l,m,k*2);
build(m+1,r,k*2+1);
}
void update(int t,int l, int r, int k){
if(t > tree[k]){ // 如果当前无法加入,就直接返回
printf("-1\n");
return;
}
if(l == r){ //说明找到了合适位置可加入。
printf("%d\n", l);
tree[k] -= t;
return;
}
int m = (l+r)/2;
if(t <= tree[k*2])
update(t, l, m, k*2); //优先加入左子树,即上面
else if(t <= tree[k*2+1])
update(t, m+1, r, k*2+1);
tree[k] = max(tree[k*2], tree[2*k+1]);
} int main() {
//freopen("in.txt", "r" ,stdin);
while(scanf("%d %d %d", &h, &w, &n) != EOF){
if(h > n)
h = n;
build(1,h,1);
while(n--){ //更新
scanf("%d", &t);
update(t, 1, h ,1);
}
}
return 0;
}

线段树练习[单点更新] HDU 2795 Billboard的更多相关文章

  1. hdu1754线段树的单点更新区间查询

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

  2. HDUOJ---1754 I Hate It (线段树之单点更新查区间最大值)

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

  3. 线段树(单点更新) HDOJ 2795 Billboard

    题目传送门 /* 主要利用线段树求区间最值,sum[]代表位置可用空间 每次找到最大值的位置 功能:查询最靠前能容纳广告的位置 */ #include <cstdio> #include ...

  4. hdu 5475 An easy problem(暴力 || 线段树区间单点更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...

  5. [HDOJ2795]Billboard(线段树,单点更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:w*h的公告板要贴公告,公告是w*1的,每个公告有先后顺序,要使每个公告贴的位置尽可能地高 ...

  6. HDU 1394 Minimum Inversion Number(线段树的单点更新)

    点我看题目 题意 :给你一个数列,a1,a2,a3,a4.......an,然后可以求出逆序数,再把a1放到an后,可以得到一个新的逆序数,再把a2放到a1后边,,,,,,,依次下去,输出最小的那个逆 ...

  7. HDU 1754 I Hate It(线段树之单点更新 区间最值查询)

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

  8. POJ 2892 Tunnel Warfare || HDU 1540(树状数组+二分 || 线段树的单点更新+区间查询)

    点我看题目 题意 :N个村子连成一条线,相邻的村子都有直接的地道进行相连,不相连的都由地道间接相连,三个命令,D x,表示x村庄被摧毁,R  ,表示最后被摧毁的村庄已经重建了,Q x表示,与x直接或间 ...

  9. HDU 1754 I Hate It(线段树之单点更新,区间最值)

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

随机推荐

  1. Nagios 邮箱告警的方式太OUT了!

    一般来讲,在安装完 Nagios 后,我们做的第一件最正确的事,就是设置它的邮件通知,对吧.因为如果没有这一步骤的话,你怎么能够知道什么时候会出现问题呢? 伴随着成功的初始安装,你即将是你司唯一一个能 ...

  2. WINDOWS 2008 SERVER域用户自动登陆

    The user I wanted to auto-logon as didn’t have a password, this reg hack worked instead: HKEY_LOCAL_ ...

  3. 【HDOJ】4516 威威猫系列故事——因式分解

    可解的算法太多了,采用的算法是试x的值.注意题目的输入x^3-2x^2不会写成x^3+-2x^2.一直RE在这儿. /* 4516 */ #include <iostream> #incl ...

  4. WCF - Hosting WCF Service

    After creating a WCF service, the next step is to host it so that the client applications can consum ...

  5. BZOJ2741: 【FOTILE模拟赛】L

    2741: [FOTILE模拟赛]L Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 1170  Solved: 303[Submit][Status] ...

  6. MVC系统过滤器、自定义过滤器

    一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration:缓存的时间,以秒为 ...

  7. 解决Dagger2和butterknife冲突

    dagger2 和 RxJava butterknife 以及 Retrofit使用起来非常酸爽 代码非常干净清晰 动手尝试 配置编译 DaggerAppComponent的时候 出现问题 配置dag ...

  8. 多组 RadioButtonList 获取值

    <div class="row">    <table>        <thead><tr><th>操作</th ...

  9. Android UI -- 内容简介

    Android UI(User Interface) 是android学习的必要课程,在接下来的内容我们将主要介绍 Android UI 的基础知识.

  10. Tomcat启动时为什么要配置CATALINA_HOME环境变量??

    CATALINA_HOME的值被设为Tomcat的安装目录,如果环境变量CATALINA_HOME已经存在,则通过这个环境变量调用bin目录下的“catalina.bat start”命令 1.Tom ...