当前位置: 代码迷 >> 综合 >> HDU 2795——Billboard【线段树 单点更新 维护区间最大值】
  详细解决方案

HDU 2795——Billboard【线段树 单点更新 维护区间最大值】

热度:95   发布时间:2023-12-16 23:01:21.0

题目传送门


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宽wi,尽量将广告向左上方贴,如果宽度大于w那么智能换行贴。
问你每个广告贴的高度是多少。
在这里插入图片描述

题解

  • 好题,模拟线段树
  • 每一行看作一个节点,维护每行的剩余容量,即维护区间最大值

AC-Code

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <fstream>
#include <utility>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);const int INF = 0x7fffffff;
const int MOD = 1e4;
const int MAXN = 2e5 + 7;struct node {
    int left;int right;int data;// 维护剩余空间最大值
}tree[MAXN << 2];
int n, w, h;
int ans;
void PushUp(int rt) {
    tree[rt].data = max(tree[rt << 1].data, tree[rt << 1 | 1].data);
}void build(int rt, int l, int r) {
    tree[rt].left = l;tree[rt].right = r;tree[rt].data = w;if (l == r) {
    return;}int mid = (l + r) >> 1;build(rt << 1, l, mid);build(rt << 1 | 1, mid + 1, r);//PushUp(rt);
}
void updata(int rt, int val) {
    if (tree[rt].left == tree[rt].right) {
    tree[rt].data -= val;	// 容量减少ans = tree[rt].left;	// 记录行标return;}if (val <= tree[rt << 1].data)	updata(rt << 1, val);	//如果左子树有剩余位置else 	updata(rt << 1 | 1, val);PushUp(rt);
}int main() {
    ios;while (cin >> h >> w >> n) {
    h = min(h, n);build(1, 1, h);for (int i = 1; i <= n; i++) {
    int a;cin >> a;if (tree[1].data < a)	// 仅需看整个树是否还有位置cout << -1 << endl;else {
    updata(1, a);cout << ans << endl;}}}return 0;
}