Poj 3347 kadj squares (ry + line segment tree)

Source: Internet
Author: User

Question:

Place squares as shown in the figure. Output the squares from the top to the bottom.

Question: 1: because all the line segments have a 45/135-degree angle, all data is larger than SQRT (2) and can be processed as integers. 2: When we use a line segment tree to handle the entire point, we need to pay attention to the following situations because we can only process the entire point.

That is, 3rd can only be seen from the leftmost point. However, 2nd marks this point as 2, and the result is 3, which is invisible. To avoid this problem, we can multiply the left and right points of each square by 2, and then subtract 1 from the right vertex (similar to the processing of the open and closed intervals)

# Include <cmath> # include <cstdio> # include <cstdlib> # include <algorithm> # include <cstring> using namespace STD; # define L (u) <1) # define R (u) <1 | 1) struct square {int B, l, R, Len, ID, flag ;} s [500]; struct node {int L, R, flag;} node [10000]; bool mark [500]; void build (int u, int L, int R) {node [u]. L = L; node [u]. R = r; node [u]. flag = 0; If (L = r) return; int mid = (L + r)> 1; build (L (u ), L, mid); Build (R (u), Mid + 1, R);} void Update (int u, int L, int R, int F) {If (node [u]. L = L & node [u]. R = r) {node [u]. flag = f; return;} If (node [u]. flag! = 0) {node [L (u)]. flag = node [u]. flag; node [R (u)]. flag = node [u]. flag; node [u]. flag = 0;} int mid = (node [u]. L + node [u]. r)> 1; if (r <= mid) Update (L (u), L, r, f); else if (L> mid) update (R (u), L, r, f); else {Update (L (u), L, mid, f); Update (R (u ), mid + 1, r, f) ;}} void query (int u) {If (node [u]. flag! = 0) {mark [node [u]. flag] = true; return;} If (node [u]. L = node [u]. r) return; query (L (u); query (R (u);} bool CMP (const square & A, const square & B) {return. len <= B. len;} int main () {int N; while (scanf ("% d", & N) {int I, j, TMP, T = 0; for (I = 1; I <= N; I ++) {scanf ("% d", & S [I]. len); s [I]. id = I; TMP = 0; if (I = 1) s [I]. B = s [I]. len; else {for (j = I-1; j> = 1; j --)// Enumerate all the rectangles before I to compact the rectangle I (s [I]. len> S [J]. len) TMP = max (TMP, s [J]. B + s [J]. len * 2); else {TMP = max (TMP, s [J]. B + s [I]. len * 2); break;} If (j <1 & S [I]. len> = TMP) s [I]. B = s [I]. len; else s [I]. B = TMP;} s [I]. L = (s [I]. b-s [I]. len) * 2; // note !!! S [I]. r = (s [I]. B + s [I]. Len) * 2-1 ;//!!! T = max (s [I]. r, T);} Sort (S + 1, S + 1 + N, CMP); Build (1, 0, T); memset (mark, 0, sizeof (Mark); for (I = 1; I <= N; I ++) Update (1, s [I]. l, s [I]. r, s [I]. ID); query (1); for (I = 1; I <= N; I ++) if (MARK [I]) printf ("% d", I ); printf ("\ n");} 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.