线段树— Billboard

来源:互联网 发布:日式发型知乎 编辑:程序博客网 时间:2024/06/05 19:24
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 524333
 

Sample Output
1213-1
 
题意:
给你一个黑板,给出你高和宽H,W,再给出你num个广告的宽(每个广告的高都是1),让你求出每一个广告所在的行是第几行(有个条件就是:每一个广告能靠坐上边的就靠左上边)
这属于线段树的应用题了,我们将每一个节点存的一个区间的所有行中的,最大的一行的剩余空间的值。
分析:
我们将某一个广告放到黑板上,如果最大的一行中的最大剩余空间小于这个广告的宽,就输出-1;否则从上向下找,找某一行的剩余空间大于这个广告的宽。
我们可以理解成更改某个点,即将某个点的值减少val,若点的值小于val,弄就输出-1,否则输出那个点所在的位置。

#include<iostream>using namespace std;#define lson l,m,id*2#define rson m+1,r,id*2+1const int MAXN=200100;int maxx[MAXN*4];int idp[MAXN*4];int cnt;void pushup(int id) {maxx[id]=max(maxx[id*2],maxx[id*2+1]);}void build(int l,int r,int id,int val) {if(l==r){idp[id]=++cnt;maxx[id]=val; return;}int m=(l+r)/2; build(lson,val);build(rson,val);pushup(id);} int update(int l,int r,int id,int val){if(maxx[id]<val)return -1;if(l==r){maxx[id]-=val;return idp[id];}int m=(l+r)/2;int ret=0;if(maxx[id*2]>=val){ret=update(lson,val);}else if(maxx[id*2+1]>=val){ret=update(rson,val);}pushup(id);return ret;}int main(){int h,w,num,val,n;while(~scanf("%d%d%d",&h,&w,&num)){cnt=0;n=min(h,num);build(1,n,1,w);while(num--){ scanf("%d",&val);printf("%d\n",update(1,n,1,val));}}}