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个数, ...
随机推荐
- MongoDB基本命令
1. 启动和停止MongoDB: 执行mongod命令启动MongoDB服务器.mongod有很多可配置的选项,我们通过mongod --help可以查看所有选项,这里仅介绍一些主要选项: - ...
- 攻城狮在路上(贰) Spring(二)--- Spring IoC概念介绍
一.IoC的概念: IoC(控制反转)是Spring容器的核心.另一种解释是DI(依赖注入),即让调用类对某一个接口的依赖关系由第三方注入,以移除调用类对某一个接口实现类的一览. 定义如此,由此可见, ...
- emc 郵件設置
1. 進入Data Domain管理界面后,在Administration--->Settings界面.點擊More mail Server--->Set Mail Server---&g ...
- long和int的区别
转自:http://blog.sina.com.cn/s/blog_6f62c9510101svjz.html 突然间就想到了long和int到底什么区别(发现有很多问题都是突然间想到的),然后百度. ...
- apache https配置步骤
转自:http://www.cnblogs.com/best-jobs/p/3298258.html 1. 确认是否安装ssl模块 是否有mod_ssl.so文件 2. 生成证书和密钥 linux ...
- 注解:【基于外键的】Hibernate单向1->1关联
Person与Address关联:单向1->1,[基于外键的] Person.java package org.crazyit.app.domain; import javax.persiste ...
- POJ 2114 Boatherds 树分治
Boatherds Description Boatherds Inc. is a sailing company operating in the country of Trabantust ...
- 关于解决haswell赛扬和奔腾 不能安装的问题
打开EFI\CLOVER\config.plist,并找到KernelAndKextPatches字段,在子集内插入下面代码. <key>FakeCPUID</key> < ...
- HDU 5652 India and China Origins(经典并查集)
特别经典的一个题,还有一种方法就是二分+bfs 题意:空间内n*m个点,每个点是0或者1,0代表此点可以走,1代表不能走.接着经过q年,每年一个坐标表示此点不能走.问哪年开始图上不能出现最上边不能到达 ...
- SSH无密码登录配置小结
ssh-keygen -t rsa //-t指定算法 将公钥复制到被管理机器上面 ssh-copy-id -i ~/.ssh/id_rsa.pub 172.29.0.89 ssh-copy-id -i ...