Billboard

Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 23498    Accepted Submission(s): 9687

Problem Description
At
the entrance to the university, there is a huge rectangular billboard
of size h*w (h is its height and w is its width). The board is the place
where all possible announcements are posted: nearest programming
competitions, changes 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.

 
Input
There are multiple cases (no more than 40 cases).

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.

 
Output
For
each announcement (in the order they are given in the input file)
output one number - the number of the row in which this announcement is
placed. Rows are numbered from 1 to h, starting with the top row. If an
announcement can't be put on the billboard, output "-1" for this
announcement.
 
Sample Input
3 5 5
2
4
3
3
3
 
Sample Output
1
2
1
3
-1
 
再次温习一下线段树,这道题算是较简单的线段树了吧。
这个题目意思就是给出h个大小初始为w的数这些数大小一样但有各自的顺序,接着进行n次询问,每次要从这些数找到一个不小于询问值得数,且这个数尽量排在前面,如果没有找到输出-1,如果找到就让这个数更新为此数减去询问值后的值。
暴力寻找得话会T,考虑适当的数据结构来简化操作。
进一步的思考时候发现,如果一段区间分为两部分只要前面部分的最大值>=询问值,我们就不必考虑后面部分的数了,由此想到使用线段树,维护一个区间最大值即可。由于更新和查找都是log级别不会T。
还有一点就是这个最大的区间并不一定是[1,H],H最大1e9,不可能开出这个多空间保存节点,我们注意到N最大就是20w,由于要尽可能的将可以放置的数放在前面,
所以最大也就占用20w行,换句话说R最小的最大区间应是 [1,min(H,N)];这里的区间[l,r]指的是从l行到r行,所以maxn[i]就是第i个节点对应的所有行中剩余的最大宽度。
还是不太熟练犯了许多小错误,例如将结点值与L,R搞混。
 #include<bits/stdc++.h>
using namespace std;
int maxn[(<<)+];
int H,N,W,X;
void build()
{
int M=X<<;
for(int i=;i<=M;++i) maxn[i]=W;
}
void update(int L,int R,int id,int tar,int x)
{
int m=(L+R)>>,lc=id<<,rc=(id<<)|;
if(L==R&&L==tar) {maxn[id]=x;return;}
if(tar<=m){
update(L,m,lc,tar,x);
}
else{
update(m+,R,rc,tar,x);
}
maxn[id]=max(maxn[lc],maxn[rc]);
}
int ask(int L,int R,int id,int x)
{
if(maxn[id]<x) return -;
if(L==R) {update(,X,,L,maxn[id]-x);return L;}
int m=(L+R)>>,lc=id<<,rc=(id<<)|;
if(maxn[lc]>=x){
return ask(L,m,lc,x);
}
else{
return ask(m+,R,rc,x);
}
}
int main()
{
int i,j,k,wi;
while(cin>>H>>W>>N){X=min(H,N);
build();
for(i=;i<=N;++i){
scanf("%d",&wi);
printf("%d\n",ask(,X,,wi));
}
}
return ;
}
 

HDU 2795 线段树单点更新的更多相关文章

  1. HDU 2795 (线段树 单点更新) Billboard

    h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子. 每次找能放纸条而且是最上面的位置,询问完以后可以同时更新,所以可以把update和query写在同一个函数里. #include ...

  2. hdu 1166线段树 单点更新 区间求和

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  3. hdu 1166 线段树单点更新

    等线段树复习完再做个总结 1101 2 3 4 5 6 7 8 9 10Query 1 3Add 3 6Query 2 7Sub 10 2Add 6 3Query 3 10End Case 1:633 ...

  4. HDU 3308 线段树单点更新+区间查找最长连续子序列

    LCIS                                                              Time Limit: 6000/2000 MS (Java/Oth ...

  5. hdu 1754 线段树 单点更新 动态区间最大值

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. hdu 2795 Billboard 线段树单点更新

    Billboard Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=279 ...

  7. HDU 1166 敌兵布阵(线段树单点更新,板子题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  8. HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)

    HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...

  9. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

随机推荐

  1. inline-block布局错位问题

    如图, 两个display为 inline-block的元素,会出现情况 针对第三种情况: 需要添加 vertical-align: top; 参考代码 <!DOCTYPE html> & ...

  2. Mac下的Mysql无法登陆的问题

    以下是还原root权限和更改root密码的最便捷方法. 1:装mysql workbench .可视化界面直接操作. 2:苹果->系统偏好设置->最下边点mysql 在弹出页面中 关闭my ...

  3. VS和IE或者360兼容模式简单调试js方法

    首先IE(8.0版本以上)将脚本调试去掉,如下图 之后在vs里面的js要调试的地方添加代码debugger ,如下图所示 当程序运行到debugger处时,就会提示要调试,选择vs版本即可 之后会出现 ...

  4. React:快速上手(7)——使用中间件实现异步操作

    React:快速上手(7)——使用中间件实现异步操作 本文参考链接:Stack Overflow redux-thunk 我们使用store.dispath进行派发时,只能传递一个普通对象进去,如下: ...

  5. Django REST Framework 学习笔记

    前言: 基于一些不错的RESTful开发组件,可以快速的开发出不错的RESTful API,但如果不了解开发规范的.健壮的RESTful API的基本面,即便优秀的RESTful开发组件摆在面前,也无 ...

  6. python单元测试框架——pytest

    官网:https://docs.pytest.org/en/latest/ pytest帮你写出更好的程序 1.An example of a simple test:(一个简单的例子),命名为tes ...

  7. 获取 config文件的节点值

    System.Configuration.ConfigurationManager.AppSettings["followTemplate"];

  8. 20145302张薇 Java第一周学习总结

    20145302张薇 <Java程序设计>第一周学习总结 教材学习内容总结 第一章 1995年,java被公认诞生.java第一开始为了消费性数字产品(如手机)而设计,所以java本身有很 ...

  9. webservice的cxf和spring整合客户端开发

    1.新建一个java项目 2.导入cxf相关的jar包,并部署到项目中 3.用命令生成客户端使用说明文档 wsdl2java -p com.xiaostudy -d . http://127.0.0. ...

  10. UVa 11549 计算器谜题(Floyd判圈算法)

    https://vjudge.net/problem/UVA-11549 题意: 有一个老式计算器,只能显示n位数字,输入一个整数k,然后反复平方,如果溢出的话,计算器会显示结果的最高n位.如果一直这 ...