[Poi06 Tet] Two-dimensional line segment tree

Source: Internet
Author: User

Finally, I know how to write a two-dimensional line segment tree!

I thought it was a bit funny. After so long, oi could not even write a two-dimensional line segment tree ~

In the past, the mark of the Two-dimensional line segment tree was not clearly written. This was the end of the knot"

This question requires two operations on a two-dimensional line segment tree.

1: change a child matrix to a value.

2: query the maximum value of a sub-matrix.

The values in the entire matrix are monotonous and non-downgrading. That is to say, we can use a line segment tree that does not need to be marked down to process one dimension.

One-dimensional situations:

Each node on a line segment tree records two values.

Cover -- value completely overwritten

Max -- maximum value of cover in the subtree (including its own)

Then

If the queried interval contains the interval of the node, the max value is returned. Otherwise, update the interval with the cover of the node and continue recursion.

You can modify it when you modify it.

It is increased to two-dimensional. Similarly, the first dimension does not change. At this time, we need to open two line segment trees, a covered line segment tree, And a max line segment tree.

If the queried range contains the node, the maximum value of the corresponding range on the second dimension is returned.

If the content is not included, use the cover value in the second dimension to update it and continue recursion.

The second-dimensional line segment tree is exactly the same as the one-dimensional line segment tree.

The total time complexity is O (qlognlogn ).

PS: The 0 field in my code indicates the cover value, and the 1 field indicates the max value.

Code:

{$ Inline ++} <br/> program syj; <br/> uses <br/> math; <br/> (* <br/> ask2 (1 ,...) === max <br/> ask2 (2 ,...) = label <br/> *) <br/> const <br/> maxd = 1005; <br/> var <br/> P, Q, N,, b, c, X, Y: longint; <br/> F: array [0 .. 1 SHL 11, 1 .. 2, 0 .. 1 SHL 11, 0 .. 1] of longint; <br/> function ask2 (p, U, I, L, R: longint): longint; inline; <br/> var M: longint; <br/> begin <br/> If (Y <= L) and (r <= B) Then exit (F [p, U, I, 1]); <br/> ask2: = f [p, U, I, 0]; <br/> M: = (L + r)> 1; <br/> if y <= m then ask2: = max (ask2, ask2 (p, U, I * 2, l, m )); <br/> If B> M then ask2: = max (ask2, ask2 (p, U, I * 2 + 1, m + 1, R )); <br/> end; <br/> function ask (I, L, R: longint): longint; inline; <br/> var M: longint; <br/> begin <br/> If (x <= L) and (r <= A) Then exit (ask2 (I, 1, 1, 1, q )); <br/> ask: = ask2 (I, 2, 1, 1, q); <br/> M: = (L + r)> 1; <br/> If x <= m then ask: = max (Ask, ask (I * 2, l, m); <br/> If a> M then ask: = max (Ask, ask (I * 2 + 1, m + 1, R); <br/> end; <br/> procedure cover2 (p, U, I, l, R: longint); inline; <br/> var M: longint; <br/> begin <br/> If (Y <= L) and (r <= B) then begin <br/> F [p, U, I, 0]: = max (F [p, U, I, 0], c); <br/> F [p, U, I, 1]: = max (F [p, U, I, 1], C ); <br/> exit; <br/> end; <br/> M: = (L + r)> 1; <br/> if y <= m then cover2 (p, U, I * 2, l, m); <br/> If B> M then cover2 (p, U, I * 2 + 1, m + 1, R); <br/> F [p, U, I, 1]: = max (F [p, U, I, 1], c); <br/> end; <br/> Procedure cover (I, L, R: longint); inline; <br/> var M: longint; <br/> begin <br/> cover2 (I, 1, 1, q); <br/> If (x <= L) and (r <=) then begin <br/> cover2 (I, 2, 1, 1, q); exit; <br/> end; <br/> M: = (L + r)> 1; <br/> If x <= m then cover (I * 2, l, m); <br/> If a> M then cover (I * 2 + 1, m + 1, R); <br/> end; <br/> begin <br/> assign (input, 'tet. in '); reset (input); <br/> assign (output, 'tet. out'); rewrite (output); <br/> readln (p, q, n); <br/> for N: = 1 to n do begin <br/> Read (a, B, c, x, y); <br/> Inc (A, x); Inc (B, y); Inc (x); Inc (y); <br/> Inc (C, ask (1, 1, p); <br/> cover (1, 1, P ); <br/> end; <br/> writeln (F [1, 1, 1]); <br/> close (input); close (output); <br/> end. <br/> 

 

Besides, I haven't written a blog for a long time. Recently, I am too lazy to post my blog. I will make it up later!

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.