HDU2795Billboard(线段树,单点修改)

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,向布告板上贴布告,每张布告只占一行,长度为某个值,布告不能被覆盖

问你能不能把布告贴上,尽量向上贴。

线段树。每一行从上到下编号,

注意此题的输入占用太多时间,Java需要自己写一个输入类

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;

class InputReader2
{
    BufferedReader buf;
    StringTokenizer tok;
    InputReader2()
    {
        buf = new BufferedReader(new InputStreamReader(System.in));
    }
    boolean hasNext()
    {
        while(tok == null || !tok.hasMoreElements()) 
        {
            try
            {
                tok = new StringTokenizer(buf.readLine());
            } 
            catch(Exception e) 
            {
                return false;
            }
        }
        return true;
    }
    String next()
    {
        if(hasNext()) return tok.nextToken();
        return null;
    }
    int nextInt()
    {
        return Integer.parseInt(next());
    }
}

public class Main {
	static int max=200005;
	static int[] tree=new int[max<<2];
	
	static int h,w,n;
    //线段树,父节点存储的是子节点的最大值,即在这个子树中可以放入最大宽度的布告哦
	static void build_tree(int node,int start,int end){
		tree[node]=w;
		if(start==end){
			
			return;
		}
		int mid=(start+end)>>1;
		build_tree(node<<1,start,mid);
		build_tree(node<<1|1,mid+1,end);
	}
	static int update_tree(int node,int start,int end,int k){
		if(start==end){
			tree[node]-=k;
			return start;
		}
		int mid=(start+end)>>1;
		int res=0;
        //优先向上贴,即优先查找左子树(小节点)
		if(tree[node<<1]>=k){
			res=update_tree(node<<1,start,mid,k);
		}else{
			res=update_tree(node<<1|1,mid+1,end,k);
		}
		tree[node]=Math.max(tree[node<<1],tree[node<<1|1]);
		return res;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		InputReader2 sc=new InputReader2();
		
		while(sc.hasNext()){
			
			h=sc.nextInt();
			w=sc.nextInt();
			n=sc.nextInt();
			h=Math.min(h, n);//根据尽量向上贴的规则
			build_tree(1,1,h);//建立线段树,叶子节点赋初值
			Arrays.fill(tree,w);
			for(int i=1;i<=n;i++){
				int a=sc.nextInt();
				if(tree[1]<a){//如果这个树可存储的最大宽度小于a,则不可能贴上
					System.out.println("-1");
				}else{
					System.out.println(update_tree(1,1,h,a));
				}				
			}
		}
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值