Hdu oj 1754 I Hate It [calculate the maximum range of a line segment tree]

Source: Internet
Author: User

Question: It's very clear. You don't have to explain it too much ......
Idea: finding the maximum value of a line segment tree ...... Explanation in the code
AC code:
[Cpp]
/*
Line Segment tree-change point for finding the greatest value of the Interval
1: The greatest value of the interval is stored in the line segment tree.
2: when creating a line segment tree, go back to the single point to update the maximum value of the parent node (always up to the root node ).
3: When a value is changed, locate the Inter-region node of the value and go back to it to update the maximum value of the parent node.
4: When querying the maximum value of a range, find all corresponding cells and find the most value.
*/
# Include <stdio. h>
# Include <string. h>
# Define Max 4*1000000
# Define LL (a) a <1; // 2 *
# Define RR (a) a> 1 | 1; // 2 * a + 1
# Define Mid (x, y) (x + y)> 1 // (x + y)/2
Int A [Max], ans, cnt;
Struct hello
{
Int l;
Int r;
Int max; // The maximum value in the interval
} Tree [Max];
Void Build_tree (int l, int r, int t) // l, r indicates the interval, and t indicates the interval Node
{
Int x;
Tree [t]. l = l;
Tree [t]. r = r;
Tree [t]. max = 0;
If (l = r)
{
While (t)
{
If (tree [t]. max <A [l])
Tree [t]. max = A [l];
T/= 2;
}
Return;
}
X = Mid (l, r );
Build_tree (l, x, 2 * t );
Build_tree (x + 1, r, 2 * t + 1 );
}
Void Updata_tree (int l, int r, int t)
{
If (tree [t]. l = l & tree [t]. r = r)
{
Tree [t]. max = cnt;
While (t)
{
T/= 2;
Tree [t]. max = tree [2 * t]. max> tree [2 * t + 1]. max? Tree [2 * t]. max: tree [2 * t + 1]. max;
}
Return;
}
Int x = Mid (tree [t]. l, tree [t]. r );
If (x> = r)
Updata_tree (l, r, 2 * t );
Else if (x + 1 <= l)
Updata_tree (l, r, 2 * t + 1 );
Else
{
Updata_tree (l, x, 2 * t );
Updata_tree (x + 1, r, 2 * t + 1 );
}
}
Void Query_tree (int l, int r, int t)
{
If (tree [t]. l = l & tree [t]. r = r)
{
If (ans <tree [t]. max)
Ans = tree [t]. max;
Return;
}
Int x = Mid (tree [t]. l, tree [t]. r );
If (x> = r)
Query_tree (l, r, 2 * t );
Else if (x + 1 <= l)
Query_tree (l, r, 2 * t + 1 );
Else
{
Query_tree (l, x, 2 * t );
Query_tree (x + 1, r, 2 * t + 1 );
}
}
Int main ()
{
Int I, j, n, m, ncase;
While (~ Scanf ("% d", & n, & m ))
{
For (I = 1; I <= n; I ++)
Scanf ("% d", & A [I]);
Build_tree (1, n, 1 );
While (m --)
{
Char str [10];
Int x, y;
Scanf ("% s % d", str, & x, & y );
If (str [0] = 'q ')
{
Ans = 0;
Query_tree (x, y, 1 );
Printf ("% d \ n", ans );
}
Else
{
Cnt = y;
Updata_tree (x, x, 1 );
}
}
}
}

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.