Poj 2482 stars in your window line segment tree

Source: Internet
Author: User

Each star has a certain brightness value. Now there are many stars in the coordinate system. The coordinates and brightness values of each star have been given, and the maximum value that can be framed by a rectangle is obtained. Lazy flag.

Question: Draw a rectangle in the lower left corner of each star. This rectangle is its influence range. To facilitate processing, we divide each star into two,
Frist_star (X, Y, Val), second_star (x + W, Y,-Val), so that when we insert stars continuously, in fact, it is equivalent to moving a rectangle frame from left to right, because the total brightness and Val indicate that the stars are framed when it comes to first_star, and-Val is added when it comes to second_star, so Val + (-Val) = 0 is equivalent to no stars. Then, every time we insert a star, we will get a rectangle with a fixed X axis range but an indefinite Y axis range (that is, two scanning lines, because the upper and lower sides of the rectangle are not fixed ). So you only need to maintain y
The coordinate variable adds Val to the range [y, Y + H] and returns the maximum value of the moment using the line segment tree. When all the stars are inserted, the maximum value is what we need.

Note that the vertex on the side of the rectangle cannot be added. (I think the original intention is to add the stars themselves, but not the stars on the other sides of the rectangle in the lower left corner of the star ). Therefore, you should pay attention to the sorting of nodes. when X is the same, remember to put the Val smaller ones in front! Otherwise, the maximum value may contain vertices on the edge.

# Include <iostream> using namespace STD; # define lint _ int64 # define n 1000000 lint y [N]; struct item {lint X, Y, V ;} point [N]; struct line {lint L, R, sum, add;} node [N]; int ycmp (const void * a, const void * B) {return * (lint *) A-* (lint *) B;} int pcmp (const void * a, const void * B) {item * Ta = (item *) a; item * TB = (item *) B; If (Ta-> X = Tb-> X) return ta-> V-TB-> V; /* Note that if Val is negative, it is placed in front; otherwise, the boundary vertex */return TA will be added. -> X-TB-> X;} lint max (lint A, lint B) {return A> B? A: B;} void build_tree (lint left, lint right, lint U) {node [u]. L = left; node [u]. R = right; node [u]. sum = node [u]. add = 0; If (Left = right) return; lint mid = (left + right)/2; build_tree (left, mid, u * 2 ); build_tree (Mid + 1, right, u * 2 + 1);} int bfind (lint L, lint R, lint key) {While (L <= r) {int mid = (L + r)/2; If (Y [Mid] = Key) return mid; If (Key <Y [Mid]) r = mid-1; el Se if (Key> Y [Mid]) L = Mid + 1;} return 0;} void Update (lint L, lint R, lint Val, lint U) {If (L <= node [u]. L & node [u]. r <= r) {node [u]. add + = val; node [u]. sum + = val; return;} If (node [u]. add! = 0) {node [u * 2]. add + = node [u]. add; node [u * 2]. sum + = node [u]. add; node [u * 2 + 1]. add + = node [u]. add; node [u * 2 + 1]. sum + = node [u]. add; node [u]. add = 0;} lint mid = (node [u]. L + node [u]. r)/2; If (r <= mid) Update (L, R, Val, u * 2); else if (L> mid) Update (L, R, Val, u * 2 + 1); else {Update (L, mid, Val, u * 2); Update (Mid + 1, R, Val, u * 2 + 1 );} node [u]. sum = max (node [u * 2]. sum, node [u * 2 + 1 ]. Sum);} int main () {lint N, W, H, I, CNT, ans; // freopen ("a.txt", "r", stdin ); while (scanf ("% i64d % i64d % i64d", & N, & W, & H )! = EOF) {for (I = 1; I <= N; ++ I) {scanf ("% i64d % i64d % i64d", & point [I]. x, & point [I]. y, & point [I]. v); y [I] = point [I]. y; y [n + I] = point [I]. Y + H; point [n + I]. X = point [I]. X + W; point [n + I]. y = point [I]. y; point [n + I]. V =-point [I]. v;} qsort (Y + 1, 2 * n, sizeof (Y [1]), ycmp); qsort (point + 1, 2 * n, sizeof (point [1]), pcmp); for (I = CNT = 1; I <= 2 * n; ++ I) {If (Y [CNT]! = Y [I]) y [++ CNT] = Y [I];} build_tree (1, CNT, 1); ans = 0; for (I = 1; I <= 2 * n; ++ I) {lint left = bfind (1, CNT, point [I]. y); lint right = bfind (1, CNT, point [I]. Y + H)-1; if (left> right) Swap (left, right); Update (left, right, point [I]. v, 1); ans = max (ANS, node [1]. sum);} printf ("% i64d \ n", ANS);} 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.