ACM: Billboard 解题报告-线段树
Time Limit:8000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
Input
下面n行每行一个整数 wi (1 <= wi <= 10^9) - 第i张广告的宽度.
Output
Sample Input
3 5 5
2
4
3
3
3
Sample Output
1
2
1
3
-1
//线段树专题
//用父节点保存子节点剩余的最大的广告长度。
3 //AC代码如下:
#include"iostream"
#include"algorithm"
#include"cstdio"
#include"cstring"
#include"cmath"
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std; const int MX=+;
int sum[MX<<];
int h,w; void PushUp(int rt) {
sum[rt]=max(sum[rt<<],sum[rt<<|]); //更新节点 父节点为子节点里面广告牌剩余的最大的长度
} void Build(int l,int r,int rt) {
sum[rt]=w; //每个节点标记为广告纸的最大宽度
if(r==l) return ;
int m=(r+l)>>;
Build(lson);
Build(rson);
} int Query(int x,int l,int r,int rt) {
if(l==r) {
sum[rt]-=x; //查询到满足条件,更新此节点广告位置的长度
return l;
}
int m=(r+l)>>;
int ret=;
if(sum[rt<<]>=x) ret = Query(x,lson); //从满足条件的广告剩余宽度开始粘贴
else ret = Query(x,rson); //总是从左节点开始贴广告纸
PushUp(rt);
return ret;
}
int main() {
int n;
int x;
while(~scanf("%d%d%d",&h,&w,&n)) {
h=min(h,n); //【这样可以减少内存】多余的广告位不会使用到。
Build(,h,); //总共的子节点数为广告牌的高度
for(int qq=; qq<=n; qq++) {
scanf("%d",&x);
if(sum[]<x) printf("-1\n");
else printf("%d\n",Query(x,,h,));
}
}
return ;
}
ACM: Billboard 解题报告-线段树的更多相关文章
- ACM: Hotel 解题报告 - 线段树-区间合并
Hotel Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description The ...
- ACM Minimum Inversion Number 解题报告 -线段树
C - Minimum Inversion Number Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &a ...
- ACM: 敌兵布阵 解题报告 -线段树
敌兵布阵 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description Li ...
- ACM: Just a Hook 解题报告 -线段树
E - Just a Hook Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u D ...
- [jzoj 5662] 尺树寸泓 解题报告 (线段树+中序遍历)
interlinkage: https://jzoj.net/senior/#contest/show/2703/1 description: solution: 发现$dfs$序不好维护 注意到这是 ...
- [P3097] [USACO13DEC] [BZOJ4094] 最优挤奶Optimal Milking 解题报告(线段树+DP)
题目链接:https://www.luogu.org/problemnew/show/P3097#sub 题目描述 Farmer John has recently purchased a new b ...
- ACM: A Simple Problem with Integers 解题报告-线段树
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...
- ACM: I Hate It 解题报告 - 线段树
I Hate It Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Des ...
- [BZOJ1858] [SCOI2010] 序列操作 解题报告 (线段树)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1858 Description lxhgww最近收到了一个01序列,序列里面包含了n个数, ...
随机推荐
- Big Data, MapReduce, Hadoop, and Spark with Python
此书不错,很短,且想打通PYTHON和大数据架构的关系. 先看一次,计划把这个文档作个翻译. 先来一个模拟MAPREDUCE的东东... mapper.py class Mapper: def map ...
- poj 2524:Ubiquitous Religions(并查集,入门题)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23997 Accepted: ...
- POJ2778 DNA Sequence(AC自动机 矩阵)
先使用AC自动机求得状态转移关系,再建立矩阵,mat[i][j]表示一步可从i到j且i,j节点均非终止字符的方案数,则此矩阵的n次方表示n步从i,到j的方法数. #include<cstdio& ...
- phpcms v9 黄页实现手机访问手机版,电脑访问电脑版(双模板)
第一步.模板文件夹下,yp复制一份,改名字 ypwap 第二步.修改phpcms/modules/yp/index.php和phpcms/modules/ypwap/index.php //判断客户端 ...
- UML 类关系及画法
1 泛化 [泛化关系]:是一种继承关系,表示一般与特殊的关系,它指定了子类如何特化父类的所有特征和行为.例如:老虎是动物的一种,即有老虎的特性也有动物的共性. [箭头指向]:带三角箭头的实线,箭头指向 ...
- Java Server returned HTTP response code: 401
今天写一个小功能需要通过http请求获取一些返回数据,但是在登陆时是需要进行用户名和密码的校验的.写好之后请求,返回异常Java Server returned HTTP response code: ...
- apache服务器安装
下载地址:http://www.apachehaus.com/cgi-bin/download.plx 全程按这篇来的,很顺利 http://www.cnblogs.com/yerenyuan/p/5 ...
- WINDOWS页式内存管理解析
jpg 改 rar
- 获取内核当前执行模块和当前发生异常地址和线程异常Handler调用步骤
循环每个内核模块 统计模块大小 判断触发异常的地址属于那个模块.来确定模块 获取发生异常地址 nt!_EXCEPTION_RECORD +0x000 ExceptionCode : -21391605 ...
- Android自动化压力测试之Monkey Test Android常见的错误类型及黑白名单的使用方法(四)
Android常见的错误类型有两种 1.ANR类型 1)在5秒内没有响应输入的事件(例如,按键按下,屏幕触摸) 2)BroadcastReceiver在10秒内没有执行完毕 2.Crash类型 1)异 ...