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个数, ...
随机推荐
- Android5.0如何正确启用isLoggable(二) 理分析
转自:http://www.it165.net/pro/html/201506/43374.html 概要 在上文<Android 5.0 如何正确启用isLoggable(一)__使用详解&g ...
- Shell编程基础教程5--文本过滤、正则表达式、相关命令
5.文本过滤.正则表达式.相关命令 5.1.正则表达式(什么是正则表达式?正则表达式怎么进行匹配?常用命令) 简介: 一种用来描述文本模式的特殊语法 ...
- 【转载】Pyqt 添加右键菜单方法
转载地址: http://www.cnblogs.com/yogalau/p/3954042.html?utm_source=tuicool QListWidget 是继承 QWidget 的, 所以 ...
- 【Agorithm】一次一密加密解密算法
#include<iostream> #include<cstdio> #include<cstdlib> #include<ctime> #inclu ...
- golang channel basic
package mainimport ( "fmt" "math/rand" "time")func main() { rand.Seed( ...
- go-martini 简单分析之一
env.go 环境变量 const ( Dev string = "development" Prod string = "production" Test s ...
- iphone手机不同版本兼容、横竖屏
/* 兼容问题*/ @media screen and (device-width: 320px) and (device-height: 480px) and (-webkit-device-pix ...
- ereg/eregi报错处理办法
ereg()函数和eregi()函数用法相同,不同之处在与ereg()区分大小写,eregi()不区分大小写 在php5.3以上的版本将不再支持eregi()和ereg()函数 处理办法: 正则函数处 ...
- 网络模拟器WANem使用配置图文教程
转自:http://blog.csdn.net/zm_21/article/details/25810263 WANem简介 由于公司在一些场合需要模拟真实的网络环境,如时延,丢包,抖动等,虽然使用L ...
- 配置ogg异构oracle-mysql(1)基础环境配置
一.环境描述: 192.168.0.164 ( Oracle ) —> 192.168.0.165 (Mysql ) 版本: 操作系统:redhat5.8 Oracle: 11.2.0.3 M ...