HDU 1698 just a hook (lazy-tag idea updated by segment tree)

Source: Internet
Author: User

Question Link

Question: N hooks, Q queries, each hook may have a value of 1 2 3 and an initial value of 1.

Change the value from X to Y to Z. Calculates the total value at the end.

Analysis: Use Val to record the value of this interval. Val =-1 indicates that the value of this interval is inconsistent and has been updated down,

Val! =-1 indicates that the value of this interval is unified. When updating a certain interval, you only need to divide this interval into several interval updates,

That is, update only to the interval to be updated. You do not need to update each interval down until it reaches the end. If the update is not updated down before,

It is necessary to update down, because the values of this interval are not uniform.

In fact, this is the idea of lazy:

Introduce lazy idea: Lazy-tag idea, record the change value of each line segment Tree node. When the consistency of this part of the line segment is damaged, we will pass this change value to the subinterval, this greatly increases the efficiency of the Line Segment tree.
 
In this example, the lazy meaning that I understand is as follows. For example, if you need to add the value of the [a, B] range to the C operation, you can add the value from the root node [1, n] Start to call the UPDATE function for operations. If a child node is executed, its node is marked as RT, and tree [RT]. L = A & tree [RT]. R = B at this time, we can update the sum [RT] value of the RT node, sum [RT] + = C * (tree [RT]. r-tree [RT]. L + 1) Pay attention to the critical moment. If the update operation is performed based on the regular line segment tree, the sum [] value of the RT subnode should be updated at this time, the lazy thought is that the sum [] value of the RT subnode is not updated for the time being. Return is returned until the value of the RT subnode is used next time, this avoids many useless operations and saves time.

1 # include <iostream> 2 # include <cstdio> 3 # include <vector> 4 # include <cstring> 5 # include <cstdlib> 6 # include <algorithm> 7 # define LL _ int64 8 const int maxn = 100000 + 10; 9 Using namespace STD; 10 int N; 11 struct line12 {13 int L, R, Val; // Val indicates the value of this interval, =-1 indicates that the range value is inconsistent, and 14} tr [4 * maxn] has been updated down; 15 16 void build (int o, int L, int R) 17 {18 tr [O]. L = L; tr [O]. R = r; 19 tr [O]. val = 1; 20 if (L = r) retur N; 21 int mid = (L + r)/2; 22 build (2 * o, L, mid); 23 build (2 * O + 1, Mid + 1, r); 24} 25 void Update (int o, int L, int R, int v) 26 {27 if (TR [O]. L = L & tr [O]. R = r) // after finding the interval, only the Val of this interval can be updated. You do not need to go down to 28 {29 tr [O]. val = V; 30 return; 31} 32 If (TR [O]. val! =-1) // If the interval before the target interval is not updated downward during the search process, update it downward because the values below are different. 33 {34 tr [2 * o]. val = tr [O]. val; 35 tr [2 * O + 1]. val = tr [O]. val; 36 tr [O]. val =-1; // After the downward update, mark the Val of this interval as updated downward. 37} 38 int mid = (TR [O]. L + Tr [O]. r)/2; 39 if (r <= mid) update (2 * o, L, R, V); 40 else if (L> mid) update (2 * O + 1, L, R, V); 41 else42 {43 update (2 * o, L, mid, V ); 44 update (2 * O + 1, Mid + 1, R, V); 45} 46} 47 int query (int o, int L, int R) 48 {49 If (TR [O]. val! =-1) // indicates that this section has not been updated downward, and the values are uniform. You can add this section and return it. 50 return tr [O]. val * (R-l + 1); 51 int mid = (TR [O]. L + Tr [O]. r)/2; 52 If (r <= mid) return query (2 * o, L, mid); 53 else if (L> mid) return query (2 * O + 1, Mid + 1, R); 54 else55 {56 return query (2 * o, L, mid) + query (2 * O + 1, mid + 1, R); 57} 58} 59 int main () 60 {61 int t, q, CA = 1; 62 int x, y, z; 63 scanf ("% d", & T); 64 While (t --) 65 {66 scanf ("% d", & N, & Q ); 67 build (1, 1, n); 68 while (Q --) 69 {70 scanf ("% d", & X, & Y, & Z ); 71 update (1, x, y, z); 72} 73 printf ("case % d: the total value of the hook is % d. \ n ", CA ++, query (1, 1, n); 74} 75 return 0; 76}

 

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.