【hdu2795】Billboard



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
 
Author
hhanger@zju
 
Source
题目大意:给定h*w的广告牌,让你每次把给定长度1*wi的广告尽量往高再尽量往左贴,不能分割,输出位置(h),不能贴就输出-1。
思路:储存最大值,若左儿子大于长度就到左节点,否则就到右节点,确定为哪一行时减去输出即可。
attention:注意数据范围,以w为节点,h为节点数建树,但因为”1 <= h,w <= 10^9“h一大必然boom shakalaka,所以h>n时取n即可,因为即使每个wi都等于w也可以满足,wi比w大,h再多也没用,因为不能劈成两半,且1<=n<=200000。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #define MAXN 222222
 6 using namespace std;
 7 int segtree[MAXN*4];
 8 int n,h,t,x,ans;
 9 void adddata(int now)
10 {
11     segtree[now]=max(segtree[(now<<1)],segtree[(now<<1)+1]);
12 }
13 void buildtree(int now,int l,int r)
14 {
15     if (l==r)    {segtree[now]=t;    return;}
16     int mid=(l+r)>>1;
17     buildtree((now<<1),l,mid);
18     buildtree((now<<1)+1,mid+1,r);
19     adddata(now);
20 }
21 int query(int now,int l,int r,int v)
22 {
23     if (l==r)    {segtree[now]-=v;return l;}
24     int mid=(l+r)>>1;
25     if (segtree[(now<<1)]>=v)    ans=query((now<<1),l,mid,v);
26     else    ans=query((now<<1)+1,mid+1,r,v);
27     adddata(now);
28     return ans;
29 }
30 int main()
31 {
32     int i,p;
33     while (~scanf("%d%d%d",&h,&t,&n))
34     {
35         h=min(h,n);
36         buildtree(1,1,h);
37         for (i=1;i<=n;i++)
38         {
39             scanf("%d",&x);
40             if (x>segtree[1])    printf("-1
");
41             else{    p=query(1,1,h,x);    printf("%d
",p);}
42         }
43     }
44     return 0;
45 }
—Anime Otaku Save The World.
原文地址:https://www.cnblogs.com/DMoon/p/5096771.html