HDOJ-2795 Billboard

这道题别被h <= 1*10^9骗了,其实h<=200000,用1-h构造线段树,每个节点存储其中每行能存储的宽度的最大值.

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

#define maxn 200005
struct Str{
    int l, r, s;
}str[maxn<<2];
int num[maxn], h, w;

void Build(int n, int l, int r)
{
    str[n].l = l;
    str[n].r = r;
    str[n].s = w;
    if(l == r)return ;

    int mid = (l + r) / 2;
    Build(n<<1, l, mid);
    Build(n<<1|1, mid+1, r);
}
void Update(int n, int m, int &h)
{
      if(str[n].r == str[n].l){
        h = str[n].l;
        str[n].s -= m;
        return ;
      }
     if(m <= str[n<<1].s)
        Update(n<<1, m, h);
     else 
        Update(n<<1|1, m, h);
    str[n].s = max(str[n<<1].s, str[n<<1|1].s);
}
int main()
{
// freopen("in.txt", "r", stdin);
    int n;

    while(cin >> h >> w >> n)
    {
        if(h > maxn)
          h = maxn;
        Build(1, 1, h);
        for(int i = 1; i <= n; i++)
        {
            int s, k = -1;

            scanf("%d", &s);
            if(s <= str[1].s)
              Update(1, s, k);
            cout << k << endl;
        }
    }
    return 0;
}

你可能感兴趣的:(HDOJ-2795 Billboard)