Description
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
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
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
Sample Output
然后根据线段树查询,从左儿子到右儿子查询。
直到查到点对应的区间时,就修改该区间的val值,并返回lt或者rt值。
然而,这题数据规模有点BT,必须任意时候不满足就return -1,不能查到底部才返回。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
#define LL long long using namespace std; int h, w, n; //线段树
//区间某点增值,求区间最大值
const int maxn = ;
struct node
{
int lt, rt;
int val;
}tree[*maxn]; //向上更新
void pushUp(int id)
{
tree[id].val = max(tree[id<<].val, tree[id<<|].val);
} //建立线段树
void build(int lt, int rt, int id)
{
tree[id].lt = lt;
tree[id].rt = rt;
tree[id].val = ;//每段的初值,根据题目要求
if (lt == rt)
{
tree[id].val = w;
return;
}
int mid = (lt + rt) >> ;
build(lt, mid, id<<);
build(mid + , rt, id<<|);
pushUp(id);
} //查询某段区间内的符合p的
int query(int lt, int rt, int id, int p)
{
if (tree[id].val < p)
return -;
if (tree[id].lt == tree[id].rt)
{
tree[id].val -= p;
return tree[id].lt;
}
int tmp;
tmp = query(lt, rt, id<<, p);
if (tmp != -)
{
pushUp(id);
return tmp;
}
tmp = query(lt, rt, id<<|, p);
pushUp(id);
return tmp;
} void work()
{
int k, ans;
int len = min(h, n);
build(, len, );
for (int i = ; i < n; ++i)
{
scanf("%d", &k);
ans = query(, len, , k);
printf("%d\n", ans);
}
} int main()
{
//freopen("test.in", "r", stdin);
while (scanf("%d%d%d", &h, &w, &n) != EOF)
{
work();
}
return ;
}