hdu 2795 Billboard(线段树单点更新)

来源:互联网 发布:mac抓图工具怎么用 编辑:程序博客网 时间:2024/04/27 23:01

Billboard

Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14337    Accepted Submission(s): 6148



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
 

Author
hhanger@zju
 

Source
HDOJ 2009 Summer Exercise(5)

题目大意:在一块h*w的黑板上贴广告,尽量往左上角贴,每张广告高度为1,对每次贴一张广告询问广告能贴在第几行(如果不能贴就输出-1)。
思路:数据很大,但不用离散化。因为广告高度为1,最多200000张广告,最差就是每行贴一张。所以当h>n的时候,可以令h=n。
方法是线段树。首先建树,每个点代表每行的剩余长度,对于一个区间可以找出其最大的剩余长度。
查询的时候如果左子树可以插入就往左子树插,左子树不行就看右子树,如果两者都不行就表示不能贴这张广告。
 
#include<stdio.h>#include<string.h>#define M 220005struct tree{int l,r,len;}tree[M<<2];int h,W,n,w[M];int max(int a,int b){if(a>b)return a;else return b;}void build(int l,int r,int root){tree[root].l=l;tree[root].r=r;tree[root].len=W;if(l==r){return;}int mid=l+r>>1;build(l,mid,root<<1);build(mid+1,r,root<<1|1);}void pushup(int root){if(tree[root].l==tree[root].r)return;tree[root].len=max(tree[root<<1].len,tree[root<<1|1].len);}void update(int root,int z){   if(tree[root].l==tree[root].r){   tree[root].len=tree[root].len-z;   printf("%d\n",tree[root].l);      return ;   }   if(tree[root<<1].len>=z)update(root<<1,z);   //如果左边可以,就往左插。else if(tree[root<<1|1].len>=z)update(root<<1|1,z);    //否则往右插。pushup(root);}int main(){int i,j,k;while(scanf("%d%d%d",&h,&W,&n)!=EOF)    {    if(h>n)h=n;    build(1,h,1);    for(i=1;i<=n;i++)    {    scanf("%d",&w[i]);      if(tree[1].len<w[i])printf("-1\n");    //如果1-h行里面都没有一行可以让该海报贴,就输出-1      else    update(1,w[i]);    }    }    return 0;}

0 0
原创粉丝点击