Billboard(线段树单点更新)

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

题意:给你一个布告板的高和宽,贴n个告示,每个高为1,宽度wi,尽可能向上和向左贴,问每个告示将被贴在哪一行。

题解:可直接贪心去做,用线段树去记录每一行余下的宽度,同时父节点为两个子节点的最大值,同时优先考虑子节点,查询时间为logn。

#include<stdio.h>
#include <algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}

int sum[200005<<2];
int h,w,n;
void pushup(int rt){sum[rt]=max(sum[rt<<1],sum[rt<<1|1]);}
void build(int l,int r,int rt) //建立线段树
{
    sum[rt]=w;
    if(l==r){return;}
    int m=(l+r)>>1;
    build(l,m,rt<<1);build(m+1,r,rt<<1|1);
}

int query(int x,int l,int r,int rt) //查询宽度为x的告示会放在哪一行
{
    if(l==r) return l;
    int ans=0;
    int m=(l+r)>>1;

    if(x<=sum[rt<<1])
        ans=query(x,l,m,rt<<1);
    else
        ans=query(x,m+1,r,rt<<1|1);

    return ans;
}

void update(int c,int l,int r,int rt) //每贴一个告示,进行点更新
{
    if(l==r){sum[rt]-=c; return;}
    int m=(l+r)>>1;

    if(c<=sum[rt<<1])
        update(c,l,m,rt<<1);
    else
        update(c,m+1,r,rt<<1|1);

    pushup(rt);
}

int main ()
{
    freopen("billboard.in", "r", stdin);
  	freopen("billboard.out", "w", stdout);

    cin>>h>>w>>n;
    h=min(h,n); //最多贴n行,将布告栏的高度缩小到n

    build(1,h,1);
    int an;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&an);
        if(an>sum[1]) //如果大于最大的节点,则该告示无法贴上
        {
            printf("-1\n");
            continue;
        }
        printf("%d\n",query(an,1,h,1));
        update(an,1,h,1);
    }

    return 0;


    fclose(stdin);
    fclose(stdout);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值