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.
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.
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.
3 5 5
2
4
3
3
3
1
2
1
3
-1
题目大意:
有一块h*w的矩形广告板,要往上面贴广告;
然后给n个1*wi的广告,要求把广告贴上去;
而且要求广告要尽量往上贴并且尽量靠左;
求第n个广告的所在的位置,不能贴则为-1;
算法思想:
利用线段树可以求区间的最大值;
将位置即h用来建树(h<=n,大了没有意义);
树中存储的为该位置还拥有的空间;
若左子树的最大值大于他,就查询左子树,否则查询右子树;
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#define N 200005
#define ll long long
using namespace std;
int h, w, n;
int width[N];
int tree[N<<2];
void build(int l, int r, int root)
{
if (l == r)
{
tree[root] = w;
return ;
}
int m = (l+r)>>1;
build(l, m, root<<1);
build(m+1, r, root<<1|1);
tree[root] = max(tree[root<<1], tree[root<<1|1]);
}
void update(int x, int val, int l, int r, int root)
{
if (l == r)
{
tree[root] -= val;
return ;
}
int m = (l+r)>>1;
if (x <= m) update(x, val, l, m, root<<1);
else update(x, val, m+1, r, root<<1|1);
tree[root] = max(tree[root<<1], tree[root<<1|1]);
}
int query(int x, int l, int r, int root)
{
if (tree[root] < x) return -1;
if (l == r) return l;
int m=(l+r)>>1;
int p1 = query(x, l, m, root<<1);
int p2 = query(x, m+1, r, root<<1|1);
if (p1 == -1) return p2;
return p1;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.txt", "r", stdin);
#endif
int i, t, tt, end;
while(~scanf("%d%d%d", &h, &w, &n))
{
end = min(n, h);
build(1, end, 1);
for (i = 0; i < n; i++)
{
scanf("%d", &t);
tt = query(t, 1, end, 1);
printf("%d\n", tt);
if (tt != -1) update(tt, t, 1, end, 1);
}
}
return 0;
}