给出一个高为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. (转)基于MVC4+EasyUI的Web开发框架形成之旅--附件上传组件uploadify的使用

    http://www.cnblogs.com/wuhuacong/p/3343967.html 大概一年前,我还在用Asp.NET开发一些行业管理系统的时候,就曾经使用这个组件作为文件的上传操作,在随 ...

  2. 前端手机验证码cookie存储

    注册的时候经常会有手机验证码的输入这个环节,在第一次点击发送了验证码只后,比如倒计时只走了10秒钟,然后刷新的话,倒计时要还是存在的,这个时候就要有一个cookie的存在了. html的代码 < ...

  3. parent() parents() parentsUntil()三者之间的对比

    $(document).ready(function(){ $("span").parent(); });只拿到span的父级标签 $(document).ready(functi ...

  4. vc++创建Win32 Application窗体过程

    #include<windows.h>#include<stdio.h>LRESULT CALLBACK WinSunProc( HWND hwnd, UINT uMsg, W ...

  5. 安装oracle执行runInstaller文件时报错:“……/install/.oui:Permission denied”

    一:问题描述 二:出错原因 将windows下未解压的Oracle安装软件上传到了linux服务器,导致有三个文件的执行权限丢失. 三:解决方法 为其赋予相应权限即可. 1: [root@MyPc ~ ...

  6. mysql备份脚本二(带日志)

    #!/bin/bashdb_PATH='/shareSAN/mysqldata/ocs/mysql_back_wjf' #备份文件保存的路径#db_DBNAME='sz_ocs' #备份的数据库名db ...

  7. C++进阶 STL(2) 第二天 一元/二元函数对象、一元/二元谓词、stack容器、queue容器、list容器(双向链表)、set容器、对组、map容器

    01 上次课程回顾 昨天讲了三个容器 string  string是对char*进行的封装 vector 单口容器 动态数组 deque(双端队列) 函数对象/谓词: 一元函数对象: for_each ...

  8. class一些内置方法

    一. __getattribute__ class Foo: def __init__(self,x): self.x=x def __getattr__(self, item): print('执行 ...

  9. 【JavaScript框架封装】JavaScript中的文本字符串的转义和反转义的实现

    如果是想把本地的一个字符串串存到服务器,再次取出来的还是文本的话,就需要这个文本字符串的转义: /** * 对一个字符串的转义 * @param str * @return {*} */ functi ...

  10. [SDOI2018]战略游戏(圆方树+虚树)

    喜闻乐见的圆方树+虚树 图上不好做,先建出圆方树. 然后答案就是没被选到的且至少有两条边可以走到被选中的点的圆点的数量. 语文不好,但结论画画图即可得出. 然后套路建出虚树. 发现在虚树上DP可以得出 ...