[HDU2795]billboard(线段树)

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

从题目能看出来,这道题处理的是区间,所以用线段树。

一开始纠结于如何建一棵10^9的线段树,后来发现最多能用到的区间只有10^6,也就是广告的总数量。为了优化代码,我们树的区间大小取H和Q的最小值。

于是题目变成了简单的找区间第一个比M大的数。

我们用线段树维护区间最大值,如果输入的M比根结点的最大值还大,那一定在区间里放不下,如果能在rt结点里放下,那我们优先考虑rt的左子结点rt<<1,因为我们要找最靠前的那个比M大的数。如果rt<<1的最大值比M小,那么我们就只能把M放到rt<<1|1里了,最后搜到叶子结点,返回叶子结点的l或r就能确定这个数的具体位置。

解题思路就这样,我就贴代码了。

#include<iostream>
#include<cstring>
#define maxn 1000000
using namespace std;
struct node{
	int l,r,max;
}tree[maxn<<2];
int H,W,Q;
void read(int &x)
{
    int f=1;x=0;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    x*=f;
}
void build(int l,int r,int rt)
{
	tree[rt].l=l;
	tree[rt].r=r;
	if(l==r)
	{
		tree[rt].max=W;
		return ;
	}
	int mid=(l+r)/2;
	build(l,mid,rt<<1);
	build(mid+1,r,rt<<1|1);
	tree[rt].max=max(tree[rt<<1].max,tree[rt<<1|1].max);
}
void sub(int adr,int x,int rt)
{
	if(tree[rt].l==tree[rt].r)
	{
		tree[rt].max-=x;
		return ; 
	}
	int mid=(tree[rt].l+tree[rt].r)/2;
	if(adr<=mid)
	{
		sub(adr,x,rt<<1);
	}
	else
	{
		sub(adr,x,rt<<1|1);
	}
	tree[rt].max=max(tree[rt<<1].max,tree[rt<<1|1].max);
}
int query(int m,int rt)
{
	if(tree[rt].l==tree[rt].r)
	{
		sub(tree[rt].l,m,1);
		return tree[rt].l;
	}
	if(m<=tree[rt<<1].max)
	{
		return query(m,rt<<1);
	}
	else
	{
		return query(m,rt<<1|1);
	}
}
int main() 
{
	while(cin>>H>>W>>Q)
	{
		build(1,min(H,Q),1);
		while(Q--)
		{
			int N;
			read(N); 
			if(N<=tree[1].max)
				cout<<query(N,1)<<endl;
			else
				cout<<"-1"<<endl;
		}
	}
}


转载于:https://www.cnblogs.com/sherrlock/p/9525785.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值