Line Segment tree (2): Segment update

Source: Internet
Author: User

Poj 2777

 /*  Color line segments and count the number of colors in the segments    Because there are not many colors, bitwise operations can be used, but I still use arrays to record    Note: C> and <operator ratio +-low. x <1 + 1 indicates that x * 2 + 1 is incorrect and must be enclosed in brackets.  That is, (x <1) + 1 has made this mistake more than once.    SimplifiedCode. But there are still hundreds of rows ......      */ # Include <stdio. h> # Define Maxn 1000000 Struct Node { Int L, R, C;} t [maxn]; Int C [32]; Void Build ( Int L, Int R, Int X) {T [X]. L = L; t [X]. r = r; t [X]. c = 1; If (L = r) Return ; Int M = (L + r)> 1; build (L, M, x <1); Build (m + 1, R, (x <1) + 1 );} Void Update ( Int L, Int R, Int C, Int X ){ If (L = T [X]. L) & (r = T [X]. R )) Return T [X]. C = C; // Else if (T [X]. L <t [X]. R) { Int M = (T [X]. L + T [X]. R)> 1; If (T [X]. c> = 1) {T [x <1]. C = T [(x <1) + 1]. C = T [X]. c; t [X]. C =-1 ;} If (R <= m) Update (L, R, C, x <1 ); Else   If (L> m) Update (L, R, C, (x <1) + 1 ); Else {Update (L, M, C, (x <1); Update (m + 1, R, C, (x <1) + 1 );}}} Void Query ( Int L, Int R, Int X ){ If (T [X]. c> 0) Return C [T [X]. C] = 1; // Else if (T [X]. c =-1 & T [X]. L <t [X]. R) { Int M = (T [X]. L + T [X]. R)> 1; If (R <= m) query (L, R, x <1 ); Else   If (L> m) query (L, R, (x <1) + 1 ); Else {Query (L, M, x <1); query (m + 1, R, (x <1) + 1 );}}} Int Count (){ Int I, sum = 0; For (I = 1; I <= 30; I ++) sum + = C [I]; Return SUM ;} Void Swap ( Int *, Int * B ){ Int Temp = * A; * A = * B; * B = temp ;} Int Main (){ Int N, T, O;While (Scanf ( "% D" , & N, & T, & O) = 3) {build (1, n, 1 ); While (O --){ Char Ch [2]; scanf ( "% S" , & Ch ); If (CH [0] = 'C' ){ Int A, B, C; scanf ( "% D" , & A, & B, & C ); If (A> B) Swap (& A, & B); Update (a, B, c, 1 );}Else { Int A, B; scanf ( "% D" , & A, & B ); If (A> B) Swap (& A, & B); memset (C, 0, Sizeof (C); query (A, B, 1); printf ( "% D \ n" , Count ());}}} Return 0 ;}

 

HDU 1698

 /*  Question: At the beginning, N skills with a weight of 1 are given, and the skill range [x .. y] of M is 1, 2, or 3. Finally, the total weight of the skill is obtained.    Algorithm: Segment update, interval summation, and line segment tree maintenance.   Experience: Sometimes the C language can only be handed in with G ++. Debugging for a long time, mainly because it was written according to the original line segment tree routine when the skill was changed. Different requirements should be used flexibly and cannot be applied.  */ # Include <stdio. h> # Define Maxn 400000 # Define Root 1 Struct { Int Left, right, cover;} t [maxn]; // The cover field indicates the skills in the control range. If it is-1, there are multiple skills.  Void Build ( Int P, Int Left, Int Right) {T [p]. Left = left; t [p]. Right = right; t [p]. Cover = 1; If (Left = right) Return ; Int M = (left + right)/2; build (p * 2, left, m); Build (p * 2 + 1, m + 1, right );} // Build a skill that is 1 in the initial stage  Void Change ( Int P, Int Left, Int Right, Int Color ){ If (T [p]. Left> = left) & (T [p]. Right <= right) {T [p]. Cover = color; Return ;} // If the interval controlled by the fruit tree needs to be colored, just change the cover domain.      If (T [p]. Cover! =-1) {T [p * 2]. cover = T [p * 2 + 1]. cover = T [p]. cover; t [p]. cover =-1 ;} // Otherwise, if the color of the control interval of the fruit tree is the same, the cover may change to-1, and the left and right subtree is not updated in the previous case, update now      Int M = (T [p]. Left + T [p]. Right)/2; If (Left <= m) Change (p * 2, left, right, color ); If (Right> m) Change (p * 2 + 1, left, right, color ); // An error occurs in this step. If the left subtree is involved, the left subtree is updated. If the right subtree is involved, the right subtree is updated. If the left subtree is updated within the left subtree range, the right subtree is updated within the range of the right subtree. Imitating what you see on the InternetProgramI thought it would be a little less, but it was wrong. } Int Count ( Int P, Int Left, Int Right ){ Int M = (T [p]. Left + T [p]. Right)/2; If (T [p]. Left = left & T [p]. Right = right ){ If (T [p]. Cover! =-1) Return T [p]. Cover * (T [p]. Right-T [p]. Left + 1 ); Else   Return Count (p * 2, left, m) + count (p * 2 + 1, m + 1, right );} // Statistics. If the tree is monotonous, the left and right Subtrees are counted. Otherwise, the left and right Subtrees are counted separately.     If (M> = right) Return Count (p * 2, left, M ); Else   If (M <left) Return Count (p * 2 + 1, m + 1, right ); Else   Return Count (p * 2, left, m) + count (p * 2 + 1, m + 1, right ); // If the entire tree is not included, the statistics are collected separately. } // In fact, I think this question is to count the entire tree, so the count process seems simpler, but I did not think about it carefully.  Int Main (){ Int I, J, K, M, N, t, x, y, z; scanf ( "% D" , & T ); For (K = 1; k <= T; k ++) {scanf ( "% D" , & N, & M); Build (root, 1, n ); For (I = 1; I <= m; I ++) {scanf ( "% D" , & X, & Y, & Z); change (root, x, y, z);} printf ( "Case % d: the total value of the hook is % d. \ n" , K, count (1, 1, n ));} // Note that there is an output.      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.