HDU 2795 Billboard (bulletin board, line segment tree application), hdu2795

Source: Internet
Author: User

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;}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.