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 );
}
}
}
}