HDU 2795-Billboard (单点更新,成段查询)

来源:互联网 发布:windows xp 原版 编辑:程序博客网 时间:2024/06/05 21:37

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 524333
 

Sample Output

1213-1

思路 :线段树,单点更新,成段查询。

            注意:有可能存在海报长度超过宽度的~

CODE:

#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <string>#include <cstring>#include <queue>#include <stack>#include <vector>#include <set>#include <map>const int inf=0xfffffff;typedef long long ll;using namespace std;const int M=200005;int maxv[M<<2];int W,H,N;void build(int pos,int l,int r){    if(l==r) maxv[pos]=W;    else{        int mid=(l+r)/2;        build(pos*2,l,mid);        build(pos*2+1,mid+1,r);        maxv[pos]=max(maxv[pos*2],maxv[pos*2+1]);    }}void update(int pos,int l,int r,int k,int v){    if(l==r) maxv[pos] -= v;    else{        int mid=(l+r)/2;        if(k<=mid) update(pos*2,l,mid,k,v);        if(k>mid) update(pos*2+1,mid+1,r,k,v);        maxv[pos]=max(maxv[pos*2],maxv[pos*2+1]);    }}int query(int pos,int l,int r,int v){    int ans=inf;    if(l==r) return l;    else{        int mid=(l+r)/2;        if(maxv[pos*2]>=v)            ans=query(pos*2,l,mid,v);        else if(maxv[pos*2+1]>=v)            ans=query(pos*2+1,mid+1,r,v);        return ans;    }}int main(){    //freopen("in.in","r",stdin);    while(~scanf("%d%d%d",&H,&W,&N)){        build(1,1,N);        for(int p=1;p<=N;p++){            int n;            scanf("%d",&n);            if(n>W) {                puts("-1");                continue;            }            int ans=query(1,1,N,n);            if(ans <= H){                update(1,1,N,ans,n);                printf("%d\n",ans);            }            else puts("-1");            }    }    return 0;}


0 0
原创粉丝点击