首页 > 其他 > 详细

【hdu】Billboard(线段树)

时间:2014-09-13 22:51:26      阅读:357      评论:0      收藏:0      [点我收藏+]

线段树的区间最大值问题,边界特殊处理一下。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
#define maxn 222222
int tree[maxn << 2];
int w,h,m,ok;
void BuildTree(int L,int R,int pos){
    tree[pos] = w;
    if(L == R)
        return ;
    int m = (L + R) >> 1;
    BuildTree(L,m,pos << 1);
    BuildTree(m + 1 , R , (pos << 1)|1);
    return ;
}
void UpDate(int len,int L,int R,int pos){
    if(L == R){
        if(tree[pos] >= len){
            printf("%d\n",L);
            tree[pos] -= len;
        }
        else
           printf("-1\n");
        return ;
    }
    int m = (L + R) >> 1;
    int e1 = tree[pos << 1];     //左边最大值
    int e2 = tree[(pos << 1)|1]; //右边最大值
    if(e1 >= len)
        UpDate(len,L,m,pos << 1);
    else if(e2 >= len)
        UpDate(len,m + 1,R,(pos << 1)|1);
    else{
        printf("-1\n");
        return ;
    }
    tree[pos] = max(tree[pos << 1],tree[(pos <<1)|1]);
}
int main(){
    int x;
    while(scanf("%d%d%d",&h,&w,&m) != EOF){
        if(h > m) h = m;
        BuildTree(1,h,1);
        for(int i = 0 ; i < m ; i++){
            scanf("%d",&x);
            UpDate(x,1,h,1);
        }
    }
    return 0;
}

【hdu】Billboard(线段树)

原文:http://blog.csdn.net/u013451221/article/details/39255909

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!