[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.
3 5 5 2 4 3 3 3Sample 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;
}
}
}
分类:
解题报告
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人