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个数, ...
随机推荐
- 安装oracle 10g RAC执行的几个脚本说明
1,/u01/app/oracle/oraInventory/orainstRoot.sh 脚本 #!/bin/sh if [ -d "/etc" ]; then /etc; fi ...
- Analysis Services OLAP 概述2
在DW/BI系统中,关系型数据库是存储和管理数据的最佳场所.但是关系数据库本身的智能化程度不够.关系型数据库缺乏如下功能: 丰富的元数据,帮助用户浏览数据和创建查询. 强大的分析计算和函数,在对上下文 ...
- Zero Copy 简介
转自:http://blog.csdn.net/zzz_781111/article/details/7534649 许多web应用都会向用户提供大量的静态内容,这意味着有很多data从硬盘读出之后, ...
- 咱就入个门之NHibernate映射文件配置(一)
之前写了数据库连接配置,这次说说映射文件的配置,即表映射[ORM的核心就是此啦!]. 下面我们使用最原始的手动配置hbm.xml文件. 步骤: 1.添加People类 namespace NHiber ...
- 建造者模式与原型模式/builder模式与prototype模式/创建型模式
建造者模式 定义 用于简化复杂对象的创建 JDK中的建造者模式 java.lang.StringBuilder中的append()方法,每次调用后返回修改后的对象本身. public StringBu ...
- 在Salesforce中处理Xml的生成与解析
在Salesforce中处理Xml的生成与解析 1): Generate Xml private String ConvertAccountToXmlInfo(Account acc){ Dom.Do ...
- r0遍历系统进程方法总结
方法1: ZwQuerySystemInformation 这个方法网上一搜一大堆,不举例了 方法2:暴力枚举PID枚举进程,代码: NTSTATUS DriverEntry(PDRIVER_OBJE ...
- Understanding, Operating and Monitoring Apache Kafka
Apache Kafka is an attractive service because it's conceptually simple and powerful. It's easy to un ...
- AJAX案例二:简单表单验证
案例:如果用户名输入为张三,那么在失去焦点时后面会显示该用户名已被注册,否则显示可以注册! <%@ page language="java" import="jav ...
- 【POI xls】解析xls遇到的问题
问题1:Package should contain a content type part org.apache.poi.POIXMLException: org.apache.poi.openxm ...