Due to the high mood of algorithm learning over the past few days, I reviewed the previously learned line tree, which further deepened my understanding of the line tree. By the way, I recorded my experiences:
1. To create a line segment tree, you must first consider the attributes of nodes in the line segment tree, such as left and right boundaries, intervals and values inserted in the range, latency tags, and so on.
2. Find out the influence of updating a child node on the father's day node, and the influence of updating the Father's Day node on the child node, so as to design the push_up () and push_down () functions.
3. build (), updata (), Quary (), and function are designed based on the previous two steps, when these three functions need to call the push_up and push_down functions is the focus of this part, and the order must not be disordered.
4. If the first three steps are properly resolved, the line segment tree problem will not be a problem...
The meaning of this question is to give you a piece of data, and then give you two kinds of operations. Q indicates the length of the maximum continuous increasing sequence of a range, and the u table shows changing the value of a certain position.
The following is an analysis based on the four steps above:
1. node attributes: L, R, lsum, rsum, and msum indicate the left and right boundary of the node, the left longest increasing length, and the right longest increasing length. The longest increasing length of the Interval
2. After analysis, this question only involves the impact of updating the child node on the parent node. Therefore, you only need to design the push_up function.
3. The build () design is very simple. The updata () design is similar to the update of the Line Segment Tree insertion point question line. The most difficult problem is Quary () it should be noted that the query results sometimes need to be merged.
4. The analysis is complete.
AC code:
[Cpp]
# Include <iostream>
# Include <string. h>
# Include <algorithm>
# Include <cstdio>
# Define CLR (arr, val) memset (arr, val, sizeof (arr ))
# Define N 100010
Using namespace std;
Void in (int &)
{
Char ch;
While (ch = getchar () <'0' | ch> '9 ');
For (a = 0; ch> = '0' & ch <= '9'; ch = getchar () a = a * 10 + ch-'0 ';
}
# Define lson l, m, rt <1
# Define rson m + 1, r, rt <1 | 1
Typedef struct {
Int L;
Int R;
Int lsum;
Int rsum;
Int msum;
} Tree;
Tree Node [N <2];
Int a [N];
Void push_up (int rt, int s ){
Node [rt]. lsum = Node [rt <1]. lsum;
Node [rt]. rsum = Node [rt <1 | 1]. rsum;
Node [rt]. msum = max (Node [rt <1]. msum, Node [rt <1 | 1]. msum );
If (a [Node [rt <1]. R] <a [Node [rt <1 | 1]. L]) {
If (Node [rt <1]. lsum = (s-(s> 1) Node [rt]. lsum + = Node [rt <1 | 1]. lsum;
If (Node [rt <1 | 1]. rsum = (s> 1) Node [rt]. rsum + = Node [rt <1]. rsum;
Node [rt]. msum = max (Node [rt]. msum, Node [rt <1]. rsum + Node [rt <1 | 1]. lsum );
}
}
Void build (int l, int r, int rt ){
Node [rt]. L = l;
Node [rt]. R = r;
If (l = r ){
In (a [r]);
Node [rt]. lsum = Node [rt]. rsum = Node [rt]. msum = 1;
Return;
}
Int m = (l + r)> 1;
Build (lson );
Build (rson );
Push_up (rt, r-l + 1 );
}
Void updata (int p, int l, int r, int rt)
{
If (r = l) return;
Int m = (l + r)> 1;
If (p <= m) updata (p, lson );
Else updata (p, rson );
Push_up (rt, r-l + 1 );
}
Int Quary (int L, int R, int l, int r, int rt ){
If (l = L & r = R) return Node [rt]. msum;
Int m = (l + r)> 1;
If (L> m) return Quary (L, R, rson );
Else if (R <= m) return Quary (L, R, lson );
Else {
Int ans = max (Quary (L, m, lson), Quary (m + 1, R, rson ));
If (a [Node [rt <1]. r] <a [Node [rt <1 | 1]. l]) ans = max (ans, min (m-L + 1, Node [rt <1]. rsum) + min (R-m, Node [rt <1 | 1]. lsum ));
Return ans;
}
}
Int main ()
{
Int T; in (T );
While (T --){
Int n, m;
In (n), in (m );
Build (1, n, 1 );
Char s [2];
For (int I = 0; I! = M; ++ I)
{
Scanf ("% s", s );
If (s [0] = 'q '){
Int B, c;
In (B), B ++;
In (c), c ++;
Printf ("% d \ n", Quary (B, c, 1, n, 1 ));
}
Else {
Int B, c;
In (B), B ++; in (c );
A [B] = c;
Updata (B, 1, n, 1 );
}
}
}
Return 0;
}
// Error: the relationship between the length of the interval and the left and right boundary is obfuscated during the build. Merging is not designed for the query.