Billboard

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

Total Submission(s): 10676    Accepted Submission(s): 4728

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的广告版。每个广告是1*w的,给出m个广告,要每张广告尽量在上层尽量靠左,输出它所在的高度。假设放不下。就输出-1
这里的问题就是h给的非常大,可是 一共仅仅有m个广告,所以即使是一条广告占一条,那么也就仅仅须要m的高度,多余的高度没实用,假设m的高度的不能放下m条广告,那么即使h再高,也放不下,所以n应该是m和h的小值。一段存储下它所控制的区间的最大值,仅仅要最大值大于广告wi,那么第i条广告就能够放到这个区间内。 先推断左子树。左子树不合适后推断右子树,除非是当前的wi大于整段的最大值。否则一定能够放到广告版内,注意,在放入一个广告之后,改动全部的父节点的最大值。
 
 
 
#include <cstdio>
#include <cstring>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
#define maxn 300000
#define lmin 1
#define rmax n
#define lson l,(l+r)/2,rt<<1
#define rson (r+l)/2+1,r,rt<<1|1
#define root lmin,rmax,1
#define now l,r,rt
#define int_now int l,int r,int rt
int cl[maxn<<2] , k[maxn<<2] , top ;
void push_up(int_now)
{
cl[rt] = max( cl[rt<<1], cl[rt<<1|1] ) ;
}
void creat(int w,int_now)
{
if( l != r )
{
creat(w,lson);
creat(w,rson);
push_up(now);
}
else
{
cl[rt] = w ;
k[rt] = ++top ;
}
}
int update(int w,int_now)
{
int ans ;
if( cl[rt] < w )
return -1 ;
if( l == r && cl[rt] >= w )
{
cl[rt] -= w ;
return k[rt] ;
}
else
{
if( cl[rt<<1] >= w )
ans = update(w,lson);
else
ans = update(w,rson);
push_up(now);
return ans;
}
}
int main()
{
int i , n , w , m ;
while(scanf("%d %d %d", &n, &w, &m)!=EOF)
{
top = 0 ;
n = min(n,m);
creat(w,root);
while(m--)
{
scanf("%d", &w);
printf("%d\n", update(w,root));
}
}
}

hdu2795Billboard(线段树,找第一个大于w的点)的更多相关文章

  1. W - Doom HDU - 5239 线段树 找取模的规律+求一个很大的数的平方对一个数取模的写法 特别的模数==2^63-2^31

    这个题目一开始感觉还是有点难的,这个模数这么大,根本就不知道怎么写,然后去搜了题解,知道了怎么去求当x很大的时候x的平方对一个数取模怎么样不会爆掉. 然后还顺便发现了一个规律就是当一个数更新一定次数之 ...

  2. hdu2795billboard线段树

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/2795/ 题目大意:有一块长方形木板,从上到下被分成h*w的区域,现要将n个长条放进这些区域中,要求从上到下只要后 ...

  3. 【Codeforces717F】Heroes of Making Magic III 线段树 + 找规律

    F. Heroes of Making Magic III time limit per test:3 seconds memory limit per test:256 megabytes inpu ...

  4. hdu2795Billboard(线段树)

    http://acm.hdu.edu.cn/showproblem.php?pid=2795 单点更新,树存储的为某一行内剩余的长度 // File Name: hdu2795.cpp // Auth ...

  5. hdu3436Queue-jumpers(线段树)

    链接 这题最不好求的一部分是rank部分 因为top每次都是把一个数放在队头 不会穿插在数组里 也就是说后面没有top过的一部分数 依旧保持有序 这样可以分为两部分来处理 比如 1 2 3 4 5 6 ...

  6. 『zkw线段树及其简单运用』

    阅读本文前,请确保已经阅读并理解了如下两篇文章: 『线段树 Segment Tree』 『线段树简单运用』 引入 这是一种由\(THU-zkw\)大佬发明的数据结构,本质上是经典的线段树区间划分思想, ...

  7. HDU5692 Snacks DFS序 线段树

    去博客园看该题解 题目 HDU5692 Snacks Problem Description 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的 ...

  8. 有趣的线段树模板合集(线段树,最短/长路,单调栈,线段树合并,线段树分裂,树上差分,Tarjan-LCA,势能线段树,李超线段树)

    线段树分裂 以某个键值为中点将线段树分裂成左右两部分,应该类似Treap的分裂吧(我菜不会Treap).一般应用于区间排序. 方法很简单,就是把分裂之后的两棵树的重复的\(\log\)个节点新建出来, ...

  9. Codeforces 527C Glass Carving (最长连续0变形+线段树)

    Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glas ...

随机推荐

  1. 关于python return 和 print 的区别

    概念上一个是 返回值   一个是打印输出 区别一:return是结束语一般放在函数的最后,当你在return 结束后面再写一些东西是不执行的如 下 def renshu(x,y): h=x+y pri ...

  2. man 7 glob

    GLOB(7) Linux Programmer's Manual GLOB(7) NAME glob - 形成路径名称 描述 (DESCRIPTION) 很久以前 在 UNIX V6 版 中 有一个 ...

  3. List Slider

    http://www.jssor.com/download-jssor-slider-development-kit.html

  4. 一 Storm 基础

     1  Storm 分布式计算结构称为 Topology (拓扑)         Topology 由 stream(数据流).spout(数据流的生成者).bolt(运算)组成.          ...

  5. Android 取得 ListView中每个Item项目的值

    首先我们须要创建 ListView .这里假定我们已经创建好了而且使用SimpleAdapter设置好了adapter数据,看一下我们的adapter ArrayList<HashMap< ...

  6. phpcms v9会员推荐位

    模版显示推荐会员代码 <div class="zhanxun2">             <div class="title">   ...

  7. zzulioj--1824--BOOM(模拟水)

    1824: BOOM Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 25  Solved: 5 SubmitStatusWeb Board Descr ...

  8. 35.angularJS的ng-repeat指令

    转自:https://www.cnblogs.com/best/tag/Angular/ 1. <html> <head> <meta charset="utf ...

  9. Traversing a list

    The most common way to traverse the elements of a list is with a for loop. The syntax is the same as ...

  10. [C++] upper_bound和lower_bound

    upper_bound 源码 template <class ForwardIterator, class T> ForwardIterator upper_bound (ForwardI ...