给出一个高为h,宽为w的广告板,有n张广告需要贴,从第一行开始贴,尽量靠左,输出每个广告最后贴在哪一行的

先一直想不通这样建树是为什么

后来看到一篇题解里面的一句话“直到找到一个满足条件的叶子节点”

所以用min(h,n)建树,最后输出的为哪一行,即为一个单点(线段树的最末一层)

大概像这样

然后就是更新值,查询

 #include <cstdio>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std; #define getmid(l,r) ((l) + ((r) - (l)) / 2) typedef long long LL;
const double eps = 1e-;
const int INF = ( << ) - ;
const int maxn = ; int a[maxn];
struct node{
int l,r,maxx;
}t[*maxn]; int n,w,h,x; void Push_up(int p){
t[p].maxx = max(t[p<<].maxx,t[p<<|].maxx);
} void Build_tree(int p,int l,int r){
t[p].l = l;
t[p].r = r;
if(l == r) {
t[p].maxx = w;
return;
}
int mid = getmid(l,r);
Build_tree(p<<,l,mid);
Build_tree(p<<|,mid+,r);
Push_up(p);
} int Query(int p){
if(t[p].maxx < x) return -;
if(t[p].l == t[p].r){
t[p].maxx -= x;
return t[p].l;
}
int ans;
if(t[p<<].maxx >= x) ans = Query(p<<);
else ans = Query(p<<|);
Push_up(p);
return ans;
} int main(){
while(scanf("%d %d %d",&h,&w,&n) != EOF){
Build_tree(,,min(n,h));
for(int i = ;i <= n;i++){
scanf("%d",&x);
printf("%d\n",Query());
}
}
return ;
}

hdu 2795 Billboard 【线段树】的更多相关文章

  1. hdu 2795 Billboard 线段树单点更新

    Billboard Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=279 ...

  2. HDU 2795 Billboard (线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题目大意:有一块h*w的矩形广告板,要往上面贴广告;   然后给n个1*wi的广告,要求把广告贴 ...

  3. HDU 2795 Billboard (线段树+贪心)

    手动博客搬家:本文发表于20170822 21:30:17, 原地址https://blog.csdn.net/suncongbo/article/details/77488127 URL: http ...

  4. [HDU] 2795 Billboard [线段树区间求最值]

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. HDU 2795 Billboard 线段树,区间最大值,单点更新

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. ACM学习历程—HDU 2795 Billboard(线段树)

    Description At the entrance to the university, there is a huge rectangular billboard of size h*w (h ...

  7. HDU 2795 Billboard (线段树单点更新 && 求区间最值位置)

    题意 : 有一块 h * w 的公告板,现在往上面贴 n 张长恒为 1 宽为 wi 的公告,每次贴的地方都是尽量靠左靠上,问你每一张公告将被贴在1~h的哪一行?按照输入顺序给出. 分析 : 这道题说明 ...

  8. HDU 2795 Billboard 线段树活用

    题目大意:在h*w 高乘宽这样大小的 board上要贴广告,每个广告的高均为1,wi值就是数据另给,每组数组给了一个board和多个广告,要你求出,每个广告应该贴在board的哪一行,如果实在贴不上, ...

  9. hdu 2795 Billboard 线段树+二分

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Probl ...

  10. HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧)

    HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧) 题意分析 题目大意:一个h*w的公告牌,要在其上贴公告. 输入的是1*wi的w值,这些是公告的尺寸. 贴公告 ...

随机推荐

  1. Laravel5.6安裝:Warning: require(../vendor/autoload.php): failed to open stream: No such file or directory

    在phpstudy下使用composer+laravel安装器的方式安装了Laravel,但是访问的时候报错: Warning: require(D:\phpstudy\WWW\public\mybl ...

  2. Redmine 甘特图导出 PDF 和 PNG 中文乱码问题

    Redmine使用了RMagick来处理图片,fpdf处理PDF,并在调用时设定了字体PDF中文字体 redmine 中关于PDF字体设置的代码 case pdf_encoding           ...

  3. 数据结构(1) 第一天 算法时间复杂度、线性表介绍、动态数组搭建(仿Vector)、单向链表搭建、企业链表思路

    01 数据结构基本概念_大O表示法 无论n是多少都执行三个具体步骤 执行了12步 O(12)=>O(1) O(n) log 2 N = log c N / log c N (相当于两个对数进行了 ...

  4. ansible自动化部署

    ansible通过模块实现批量管理及部署服务器,功能有模块实现 ansible无需在被控端安装agent/client,因为ansible是通过ssh分发ad-hoc(单条指令)或者palybook( ...

  5. weex手机端安全键盘

    github地址:weexSafeKeyboard 效果图: 技术依赖:框架:weex+vue 弹出层:weex-ui 图标:iconfont 说明:1.如果不想用到weex-ui,可以把inputk ...

  6. Spring学习总结(18)——Spring整合Mysql数据库一主多从、多主多从配置

    一.新建jdbc.properties配置文件 master.jdbc.driverClassName=com.mysql.jdbc.Driver master.jdbc.url=jdbc:mysql ...

  7. 洛谷 1262 间谍网络 Tarjan 图论

    洛谷 1262 图论 tarjan 并不感觉把这道题目放在图的遍历中很合适,虽然思路比较简单但是代码还是有点多的,, 将可收买的间谍的cost值设为它的价格,不可购买的设为inf,按照控制关系连图,T ...

  8. linux下添加自定义脚本到开机自启动的方法

    原文链接:http://www.jb51.net/LINUXjishu/183462.html 我的机器有个coreseek服务,但是没加到开启启动中去,导致机房一旦重启了机器,我的服务便不能使用了. ...

  9. CCEditBox/CCEditBoxImpl

    #ifndef __CCEditBoxIMPL_H__ #define __CCEditBoxIMPL_H__ #include "cocos2d.h" #include &quo ...

  10. VMWare虚拟机下为Ubuntu 12.04.1网络设置(NAT方式)

    NAT方式: 虚拟机能够上外网,能够訪问宿主计算机所在网络的其它计算机(反之不行). 第一步:设置虚拟机vmware网络參数 (1)打开虚拟机,选择菜单"编辑">" ...