hdoj 2795 Billboard 【线段树 单点更新 + 维护区间最大值】
Billboard
in the dining room menu, and other important information.
On September 1, the billboard was empty. One by one, the announcements started being put on the billboard.
Each announcement is a stripe of paper of unit height. More specifically, the i-th announcement is a rectangle of size 1 * wi.
When someone puts a new announcement on the billboard, she would always choose the topmost possible position for the announcement. Among all possible topmost positions she would always choose the leftmost one.
If there is no valid location for a new announcement, it is not put on the billboard (that's why some programming contests have no participants from this university).
Given the sizes of the billboard and the announcements, your task is to find the numbers of rows in which the announcements are placed.
The first line of the input file contains three integer numbers, h, w, and n (1 <= h,w <= 10^9; 1 <= n <= 200,000) - the dimensions of the billboard and the number of announcements.
Each of the next n lines contains an integer number wi (1 <= wi <= 10^9) - the width of i-th announcement.
be put on the billboard, output "-1" for this announcement.
3 5 5
2
4
3
3
3
1
2
1
3
-1
#include <cstdio>
#include <algorithm>
#include <iostream>
#define ll o<<1
#define rr o<<1|1
using namespace std;
const int MAXN = 2 * 1e5 + 10;
struct Tree { int l, r, Max; }tree[MAXN<<2];
void PushUp(int o) {
tree[o].Max = max(tree[ll].Max, tree[rr].Max);
}
void Build(int o, int l, int r, int v) {
tree[o].l = l; tree[o].r = r; tree[o].Max = v;
if(l == r) { return ; }
int mid = (l + r) >> 1;
Build(ll, l, mid, v); Build(rr, mid+1, r, v);
}
void Update(int o, int pos, int v) {
if(tree[o].l == tree[o].r) {
tree[o].Max += v;
return ;
}
int mid = (tree[o].l + tree[o].r) >> 1;
if(pos <= mid) Update(ll, pos, v);
else Update(rr, pos, v);
PushUp(o);
}
int Query(int o, int v) {
if(tree[o].l == tree[o].r) {
return tree[o].l;
}
if(tree[ll].Max >= v) return Query(ll, v);
else return Query(rr, v);
}
int main()
{
int h, w, n;
while(scanf("%d%d%d", &h, &w, &n) != EOF) {
Build(1, 1, min(n, h), w);
for(int i = 1; i <= n; i++) {
int v; scanf("%d", &v);
if(tree[1].Max >= v) {
int id = Query(1, v);
printf("%d\n", id);
Update(1, id, -v);
}
else {
printf("-1\n");
}
}
}
return 0;
}
hdoj 2795 Billboard 【线段树 单点更新 + 维护区间最大值】的更多相关文章
- HDU 2795 Billboard (线段树单点更新 && 求区间最值位置)
题意 : 有一块 h * w 的公告板,现在往上面贴 n 张长恒为 1 宽为 wi 的公告,每次贴的地方都是尽量靠左靠上,问你每一张公告将被贴在1~h的哪一行?按照输入顺序给出. 分析 : 这道题说明 ...
- hdu 2795 Billboard 线段树单点更新
Billboard Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=279 ...
- HDU 1754 I Hate It 【线段树单点修改 维护区间最大值】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1754 I Hate It Time Limit: 9000/3000 MS (Java/Others ...
- hdu 1754 线段树 单点更新 动态区间最大值
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- poj 2892---Tunnel Warfare(线段树单点更新、区间合并)
题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...
- HDU - 1754 线段树-单点修改+询问区间最大值
这个也是线段树的经验问题,待修改的,动态询问区间的最大值,只需要每次更新的时候,去把利用子节点的信息进行修改即可以. 注意更新的时候区间的选择,需要对区间进行二分. #include<iostr ...
- nyoj 568——RMQ with Shifts——————【线段树单点更新、区间求最值】
RMQ with Shifts 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 In the traditional RMQ (Range Minimum Q ...
- HDU1754 —— I Hate It 线段树 单点修改及区间最大值
题目链接:https://vjudge.net/problem/HDU-1754 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜 ...
- hdu1166(线段树单点更新&区间求和模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...
随机推荐
- 第6章 Spring MVC的数据转换、格式化和数据校验
使用ConversionService转换数据 <%@ page language="java" contentType="text/html; charset=U ...
- js设计模式-命令模式
命令模式是一种组织型模式,主要用在把调用对象(用户界面.API和代理等)与实现操作的对象隔离开.也就是说 ,凡是两个对象间的互动方式需要更高的模块化程度时都可以用到这种模式. 命令模式的好处:1.提高 ...
- Polyfill 与 Shim
Polyfill 与 Shim polyfill 的概念是 Remy Sharp 在2010年提出的. polyfill,或 polyfiller ,表示为开发人员提供旧浏览器没有原生支持的较新功能的 ...
- Arduino-IIC-Wire.h
前言:Wire.h是Arduino的IIC库. 一.Wire库函数 Wire.begin() Wire.requestFrom() Wire.beginTransmission() Wire.endT ...
- Vue-router记录
一.嵌套路由默认选中第一个子路由 可以给主路由加一个重定向的属性,把路径指向相应的子路由. { path: '/home', name: 'Home', //重定向 redirect: '/home/ ...
- 豆瓣项目(用react+webpack)
用豆瓣电影api的项目 电影列表组件渲染 步骤: 1. 发送Ajax请求 1.1 在组件的componentWillMount这个生命周期钩子中发送请求 1.2 发送ajax XMLHttpReque ...
- angular基础入门
第一章 AngularJs入门 AngularJS是一款由Google公司开发维护的前端框架,其克服了HTML在构建应用上的诸多不足,从而降低了开发成本提升了开发效率. 1 特点 AngularJS与 ...
- 高德地图修改gps定位点样式
效果图 navi_map_gps_locked.png 图片1 图片2 修改 MyLocationStyle myLocationStyle = new MyLocationStyle();//初始化 ...
- VHDL之Aggregate
Definition A basic operation that combines one or more values into a composite value of a record or ...
- Java为什么需要基本类型
提问 首先抛出问题吧:Java为什么需要基本类型? 最开始遇到这个问题的场景不记得了,但是一查之下,发现一个问题:Java既然是面向对象的,宣称一切都是对象,为什么还有基础类型. 先上结论 为了性能+ ...