BillboardTimeLimit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9045 Accepted Submission(s): 4021 Problem At On Each When If Given Input There The Each Output For each announcement Sample 3 5 5 2 4 3 3 3 Sample 1 2 1 3 -1 Author [email protected] Source HDOJ |
题意:
知识点:区间范围取最大值。
叶子节点代表广告牌的宽度
#include<stdio.h>
#include <algorithm>
using
namespace std;
#define MAXN 200000
int
t[MAXN*4];
int h,w,n;
void PushUp(int index){
t[index]=max(t[index*2],t[index*2+1]);
}
void
build(int l,int r,int
index){
if(l==r){
t[index]=w;
return ;
}
int mid=(l+r)/2;
build(l,mid,index*2);
build(mid+1,r,index*2+1);
PushUp(index);
}
int
Query(int width,int l,int
r,int index){
if(l==r){
t[index]-=width;
return l;//返回所在层数
}
int mid=(l+r)/2;
int hh=0;
if(t[index*2]>=width)
hh=Query(width,l,mid,index*2);
else
hh=Query(width,mid+1,r,index*2+1);
PushUp(index);
return hh;
}
int main(){
while(~scanf("%d%d%d",&h,&w,&n)){
if(h>n)//这里如果广告牌的高度大于通告的个数的话
h=n;
build(1,h,1);//是以广告牌的高度为范围建树
int wi;
for(int
i=1;i<=n;i++){
scanf("%d",&wi);
if(t[1]<wi)
printf("-1\n");
else
printf("%d\n",Query(wi,1,h,1));
}
}
return 0;
}
【线段树四】HDU 2795 Billboard,码迷,mamicode.com