HDU2795 (Billboard) Line Segment tree

Source: Internet
Author: User
/*************************************** * ********* The subject is as follows: there is a rectangle advertisement board with H * W, and advertisements should be placed on the top; then n 1 * WI advertisements should be pasted on; and advertisements should be pasted on the left as far as possible; locate the position of the n-th advertisement. If it cannot be pasted, it is-1. algorithm idea: Use the line segment tree to calculate the maximum value of the interval; H is used to build a tree (H <= N, which is meaningless). The space stored in the tree is the space that the tree still has. If the maximum value of the Left subtree is greater than that of the tree, query the left subtree; otherwise, query the right subtree; **************************************** * ********/# include <iostream> # include <cstdio> # include <algorithm> using namespace STD; # define l, m, u <1 # define r m + 1, R, u <1 | 1 // U * 2 + 1 const Int n = 200000 + 10; int H, W, N; int A [n <2]; void build (int l, int R, int U) // U is the root node {A [u] = W; If (L = r) return; int M = (L + r)> 1; build (L ); build (r);} int query (int x, int L, int R, int U) {If (L = r) {A [u]-= X; return L;} int M = (L + r)> 1; int res = (a [U <1]> = x )? Query (x, L): Query (x, R); A [u] = max (A [U <1], a [U <1 | 1]); return res;} int main () {freopen ("C: \ Users \ Administrator \ Desktop \ kd.txt", "r", stdin); While (~ Scanf ("% d", & H, & W, & N) {If (h> N) H = N; build (1, h, 1); While (n --) {int X; scanf ("% d", & X); if (a [1] <X) printf ("-1 \ n"); else printf ("% d \ n", query (x, 1, h, 1) ;}} 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.