HDU 2795 Billboard (bulletin board, line segment tree application), hdu2795
HDU 2795 Billboard)
ACM
Address: HDU 2795 Billboard
Question:
To post an announcement on the h * w publicity board, the height of each announcement is 1, and each announcement should be posted to the top and left as far as possible, give you the length of a series of announcements and ask if they can be pasted.
Analysis:
I don't really think about it, but I think it's easy to write.
You just need to put the billboards in the correct place. At this time, you can enter announcements in h positions. when filling out announcements, you can try to find the right position on the leftmost.
We can use a line segment tree. The search complexity is O (logn). Note that h has a very large range. If it is true, the Large Line Segment tree cannot be opened, but the range of n is only 20 w, and even if all announcements occupy a column, it will not exceed n, so the line segment tree can open min (h, n.
Code:
/** Author: illuz <iilluzen[at]gmail.com>* Blog: http://blog.csdn.net/hcbbt* File: 2795.cpp* Create Date: 2014-08-05 16:12:47* Descripton: segment tree */#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <map>using namespace std;#define repf(i,a,b) for(int i=(a);i<=(b);i++)#define lson(x) ((x) << 1)#define rson(x) ((x) << 1 | 1)typedef long long ll;const int N = 200010;const int ROOT = 1;int h, w, n, t;// below is sement point updated versionstruct seg { ll w;};struct segment_tree { seg node[N << 2]; void update(int pos) { node[pos].w = max(node[lson(pos)].w, node[rson(pos)].w); } void build(int l, int r, int pos) { if (l == r) { node[pos].w = w; return; } int m = (l + r) >> 1; build(l, m, lson(pos)); build(m + 1, r, rson(pos)); update(pos); } int queryandmodify(int l, int r, int pos, ll y) { if (y > node[pos].w) { return -1; } if (l == r) { node[pos].w -= y; return l; } int m = (l + r) >> 1; int res; if (y <= node[lson(pos)].w) res = queryandmodify(l, m, lson(pos), y); else res = queryandmodify(m + 1, r, rson(pos), y); update(pos); return res; }} sgm;int main() { while (~scanf("%d%d%d", &h, &w, &n)) { h = min(h, n); sgm.build(1, h, ROOT); repf (i, 1, n) { scanf("%d", &t); printf("%d\n", sgm.queryandmodify(1, h, ROOT, t)); } } return 0;}