您的位置:首页 > 理论基础 > 数据结构算法

Billboard(线段树,单点更新)

2015-10-12 22:39 429 查看
A - Billboard
Time Limit:8000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU
2795

Appoint description: 
bupt_admin_13  (2015-07-28)System Crawler  (2015-10-11)

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

 

思路:

    其实就是求1*W的板要从上到下最多的放到板。一开始根本没思路,看起来与之前做的题都不一样,最后又有了看题解的冲动。但是又想了想还是再想多一个钟吧,没想到竟然被我想到了,之后还1A。这题与求最值有点不同在,大多数题目是叫你求区间的最值,但是这题却是求点的。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
#define T 2000005
#define inf 0xffffffff
struct node
{
int v;
int l,r,mid;
}tree[T];
int val;
void PushUp(int rt)
{
tree[rt].v = max(tree[rt<<1].v,tree[rt<<1|1].v);
}
void Build(int rt,int L,int R)
{
tree[rt].l = L,tree[rt].r = R;
tree[rt].mid = (L+R)>>1;
if(L==R){
tree[rt].v=val;
return;
}
Build(rt<<1,L,tree[rt].mid);
Build(rt<<1|1,tree[rt].mid+1,R);
PushUp(rt);
}
void Update(int rt,int pos)
{
if(tree[rt].l==tree[rt].r){
tree[rt].v -= pos;
return;
}
if(pos<=tree[rt<<1].v){
Update(rt<<1,pos);
}
else
{
Update(rt<<1|1,pos);
}
PushUp(rt);
}
int query(int rt,int pos)
{
if(tree[rt].v<pos){
return 0;
}
if(tree[rt].v>=pos&&tree[rt].l==tree[rt].r){
return tree[rt].l;
}
if(tree[rt<<1].v>=pos){
query(rt<<1,pos);
}
else
{
query(rt<<1|1,pos);
}
}
int main()
{
/*freopen("input.txt","r",stdin);*/
int n,m,i,j,k,t;
while(~scanf("%d%d%d",&n,&val,&k))
{
if(n>T-5)
Build(1,1,k);
else
Build(1,1,n);
for(i=1;i<=k;++i){
scanf("%d",&j);
if(j>val)
printf("-1\n");
else
{
t=query(1,j);
if(!t){
printf("-1\n");
}
else
{
printf("%d\n",t);
Update(1,j);
}
}
}

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构 线段树