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


每张广告都是高度为1宽度为wi的细长的矩形纸条。贴广告的人总是会优先选择最上面的位置来帖,而且在所有最上面的可能位置中,他会选择最左面的位置,而且不能把已经贴好的广告盖住。 如果没有合适的位置了,那么这张广告就不会被贴了。 现在已知广告牌的尺寸和每张广告的尺寸,求每张广告被贴在的行编号。

Input

多组样例,不超过40个。对每组样例,第一行包含3个整数h,w,n(1 <= h,w <= 10^9; 1 <= n <= 200,000) -广告牌的尺寸和广告的个数。 下面n行每行一个整数 wi (1 <= wi <= 10^9) -  第i张广告的宽度.

Output

对每张广告,输出它被贴在的行编号(是1到h之间的数),顶部是第一行。如果某广告不能被贴上,则输出-1。
 


思路:

建立min(h,n)叶子结点,每个结点代表这一行的剩余长度。由于n可能小于h,这时候没有必要给出h个叶子节点,而且这里不做处理的话,会RE。h到了10^9,n回到200000。建立的一维数组是不允许有10^9这么大的。

pushup的时候,每个父节点代表子节点的最大值。为什么选择最大值呢?。最大值,表示了这个区间内,能否允许贴这一次的广告。广告长度<最大值,则允许贴。

因为要求优先选择最上面的位置来贴,其次是最左边的位置。每当输入一个长度,先查询左子树,一直查询到可能的叶子节点(left==right)的时候,更新为tree[root]-=length

虽然想到了用最大值,但是却迷惑在了:如何从叶子节点的下标,找到对应的原数组的下标。然而,实际上这个问题很简单。在判断叶子节点时,利用的left==right,left和right都是原数组的下标,只要返回left就可以了。被自己蠢哭。


//HDU 2795 
#include<iostream>
#include<algorithm>
#include<set>
#include<cstring>
#include<queue>
#include<stack>
#include<vector>
#include<stdio.h>
using namespace std;
#define ll long long
const int maxn=2e5+5;
int h,w;
int tree[maxn<<2];
int ans=1;

void pushup(int root)
{
    tree[root]=max(tree[root*2],tree[root*2+1]);
    return;
}

void build(int root,int left,int right)
{
    if(left==right)
    {
        tree[root]=w;
        return;
    }
    int mid=(left+right)/2;
    build(2*root,left,mid);
    build(2*root+1,mid+1,right);
    pushup(root);
}

void update(int root,int left,int right,int value)
{
    if(left==right)
    {
        tree[root]-=value;
        ans=left;
        return;
    }
    int mid=(left+right)/2;
    if(tree[2*root]>=value)
    {
    //    cout<<'l';
        update(2*root,left,mid,value);
    }
    else if(tree[2*root+1]>=value)
    {
    //    cout<<'r';
        update(2*root+1,mid+1,right,value);
    }
    pushup(root);
    
}

int main()
{
	int n;
    while(scanf("%d%d%d",&h,&w,&n)!=EOF)
    {
    	if(n<h) h=n;//没有这一句,会导致 RE runtime error
    	
        build(1,1,h);
        
//        for(int i=1;i<=4*h;i++)
//            cout<<tree[i]<<" ";
//        cout<<endl;
        
        for(int i=0;i<n;i++)
        {
            int t;
            scanf("%d",&t);
            if(t<=tree[1])
            {
                update(1,1,h,t);//需要t个大小 
                cout<<ans<<endl;    
            }
            
            else
                cout<<-1<<endl;
            
//            for(int i=1;i<=4*h;i++)
//                cout<<tree[i]<<" ";
//            cout<<endl;
        }
    }
    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值