您的位置:首页 > 其它

hdu 2795 Billboard 线段树

2015-08-01 16:30 316 查看
题解看代码。

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2795

#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 200005;

struct Node {
int x,y;
int maxlen;
//维护一个区间的最大长度;
}t[MAXN<<2];

int h,w,n;

void Push_Up(int rt) {
t[rt].maxlen = max(t[rt<<1].maxlen,t[rt<<1|1].maxlen);
}

void Build(int x,int y ,int rt) {
t[rt].x = x; t[rt].y = y; t[rt].maxlen = w;
if(x == y) {
return ;
}
int mid = (x + y) >> 1;
Build(x,mid,rt<<1);
Build(mid+1,y,rt<<1|1);
//Push_Up(rt);可以不用;
}

int Query(int rt,int len) {
//printf("%d %d %d\n",x,y,rt);
if(t[rt].x == t[rt].y) {
t[rt].maxlen -= len;
return t[rt].x;
}
int ans = -1;
if(t[rt<<1].maxlen >= len) {
ans = Query(rt<<1,len);
}
else {
ans = Query(rt<<1|1,len);
}
Push_Up(rt);
return ans;
}

void Debug(int rt) {
printf("x : %d y : %d maxlen : %d\n",t[rt].x,t[rt].y,t[rt].maxlen);
if(t[rt].x==t[rt].y) return ;
Debug(rt<<1);
Debug(rt<<1|1);
}

void Deal_with() {
while(~scanf("%d %d %d",&h,&w,&n)) {
//printf("%d %d %d\n",h,w,n);
h = min(h,n);
//为什么min(h,n)不可以,min(h,n+1)就可以;
//出现这种问题是因为Query()写丑了;
//n = 1 的时候就有可能出问题。
Build(1,h,1);
int len;
for(int i = 1; i <= n ; i++) {
scanf("%d",&len);
if(t[1].maxlen >= len)	printf("%d\n",Query(1,len));
else puts("-1");
//Debug(1);
}
}
}

int main(void) {
//freopen("a.in","r",stdin);
Deal_with();
return 0;
}
//h*w的木板,每次放下1*wi;
//思路:从上往下找到第一个区域放下1*wi即可,长度减掉wi;
//虽然h小于1e9,但是最多只有min(h,n)行;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: