Billboard(线段树) HDU - 2795
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.
题意:
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<cstring> #include<stdio.h> #include<algorithm> #include<map> #include<queue> #include<set> #include <sstream> #include<vector> #include<cmath> #include<stack> #include<time.h> #include<ctime> using namespace std; #define inf 1<<30 #define eps 1e-7 #define LD long double #define LL long long #define maxn 1000005 int s[maxn] = {}; int h, w, n, ans = -1; struct node { int L, R, len; }tree[maxn]; void up(int step) { tree[step].len = max(tree[step << 1].len, tree[step << 1 | 1].len); } void build(int step, int L, int R)//建树 { tree[step].L = L; tree[step].R = R; if (tree[step].L == tree[step].R) { tree[step].len = w;//L层的宽度为w return; } int mid = (L + R) >> 1; build(step << 1, L, mid); build(step << 1 | 1, mid + 1, R); up(step);//更新 } void treeInsert(int step, int k)//插入 { if (tree[step].L == tree[step].R) { tree[step].len -= k;//贴完广告后,这一层要减去广告占用的空间 ans = tree[step].L;//记录层数 return; } if (k <= tree[step << 1].len) treeInsert(step << 1, k); else treeInsert(step << 1 | 1, k); up(step);//更新 } int main() { while (~scanf("%d%d%d", &h, &w, &n)) { if (h > n) { h = n; } build(1, 1, h); int k; for (int i = 1; i <= n; i++) { scanf("%d", &k); ans = -1;//更新 if (k <= tree[1].len) { treeInsert(1, k); } printf("%d\n", ans); } } }