4927 line segment tree exercises 5, line segment tree exercises

Source: Internet
Author: User

4927 line segment tree exercises 5, line segment tree exercises
Time Limit: 1 s space limit: 128000 KB title level: GoldQuestionView running resultsDescriptionDescription

There are n numbers and 5 Operations

Add a B c: Increase the number of all numbers in the range [a, B] to c.

Set a B c: set all numbers in the range [a, B] to c.

Sum a B: The sum of the query interval [a, B]

Max a B: maximum value of the query interval [a, B]

Min a B: the minimum value of the query interval [a, B]

Input description Input Description

The first line has two integers n, m, and the second line has n Integers to indicate the initial values of the n numbers.

Next, perform the m operation, which is the same as the description of the question.

Output description Output Description

For all sum, max, and min queries, one answer is output in one row.

Sample Input Sample Input

10 6

3 9 2 8 1 7 5 0 4 6

Add 4 9 4

Set 2 6 2

Add 3 8 2

Sum 2 10

Max 1 7

Min 3 6

 

Sample output Sample Output

49

11

4

 

Data range and prompt Data Size & Hint

10%: 1 <n, m <= 10

30%: 1 <n, m <= 10000

100%: 1 <n, m <= 100000

Ensure that the intermediate results are in the range of long (C/C ++) and int64 (pascal ).

 

PS: Some people only scored 90 points due to data 6 errors, which was corrected on February 13.

The author expressed sincere apologies to the two users who scored 90 points.

 

A disgusting question

This question is specially operated in the set operation.

For this operation, we need to add two variables for maintenance, one for maintenance, and the other for maintenance.

Pay special attention not only to maintain the number of changes (there will be 0 cases)

 

When uploading a flag, you must first upload the set flag and then upload the add flag.

Because we have set the add Tag to 0 when setting the set tag.

If the add mark is not 0 at this time, it indicates that this mark must be set after the set mark is set.

 

Pay attention to the details.

 

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # define LL long 6 # define ls k <1 7 # define rs k <1 | 1 8 using namespace std; 9 const ll maxn = 400400; 10 const ll inf = 0x7fffff; 11 inline void read (LL & n) 12 {13 char c = getchar (); n = 0; bool flag = 0; 14 while (c <'0' | c> '9') c = '-'? Flag = 1, c = getchar (): c = getchar (); 15 while (c> = '0' & c <= '9 ') n = n * 10 + c-48, c = getchar (); flag = 1? N =-n: n = n; 16} 17 struct node 18 {19 LL l, r, w, Max, Min, Set, Add; 20 bool V; 21} tree [MAXN]; 22 LL n, m; 23 LL ans = 0; 24 inline void update (LL k) 25 {26 tree [k]. w = tree [ls]. w + tree [rs]. w; 27 tree [k]. max = max (tree [ls]. max, tree [rs]. max); 28 tree [k]. min = min (tree [ls]. min, tree [rs]. min); 29} 30 void Build_Tree (LL ll, LL rr, LL k) 31 {32 tree [k]. l = ll; tree [k]. r = rr; 33 if (tree [k]. l = tree [k]. r) 34 {35 read (tree [k]. w); 36 tree [k]. max = tree [k]. w; 37 tree [k]. min = tree [k]. w; 38 return; 39} 40 LL mid = tree [k]. l + tree [k]. r> 1; 41 Build_Tree (ll, mid, ls); 42 Build_Tree (mid + 1, rr, rs); 43 update (k ); 44} 45 void down (LL k) 46 {47 if (tree [k]. v) 48 {49 tree [ls]. add = 0; 50 tree [ls]. V = 1; 51 tree [ls]. w = (tree [ls]. r-tree [ls]. l + 1) * tree [k]. set; 52 tree [ls]. max = tree [ls]. min = tree [ls]. set = tree [k]. set; 53 54 tree [rs]. add = 0; 55 tree [rs]. V = 1; 56 tree [rs]. w = (tree [rs]. r-tree [rs]. l + 1) * tree [k]. set; 57 tree [rs]. max = tree [rs]. min = tree [rs]. set = tree [k]. set; 58 59 tree [k]. set = tree [k]. V = 0; 60} 61 if (tree [k]. add) 62 {63 tree [ls]. w + = (tree [ls]. r-tree [ls]. l + 1) * tree [k]. add; 64 tree [ls]. add + = tree [k]. add; 65 tree [ls]. max + = tree [k]. add; 66 tree [ls]. min + = tree [k]. add; 67 68 tree [rs]. w + = (tree [rs]. r-tree [rs]. l + 1) * tree [k]. add; 69 tree [rs]. add + = tree [k]. add; 70 tree [rs]. max + = tree [k]. add; 71 tree [rs]. min + = tree [k]. add; 72 tree [k]. add = 0; 73} 74 75} 76 void Interval_Add (LL k, LL ll, LL rr, LL val) 77 {78 if (ll <= tree [k]. l & tree [k]. r <= rr) 79 {80 tree [k]. w + = (tree [k]. r-tree [k]. l + 1) * val; 81 tree [k]. add + = val; 82 tree [k]. max + = val; 83 tree [k]. min + = val; 84 return; 85} 86 if (tree [k]. add | tree [k]. v) down (k); 87 LL mid = tree [k]. r + tree [k]. l> 1; 88 if (ll <= mid) Interval_Add (ls, ll, rr, val); 89 if (rr> mid) Interval_Add (rs, ll, rr, val); 90 update (k); 91} 92 void Interval_Change (LL k, LL ll, LL rr, LL val) 93 {94 if (ll <= tree [k]. l & tree [k]. r <= rr) 95 {96 tree [k]. max = tree [k]. min = val; 97 tree [k]. set = val; tree [k]. V = 1; 98 tree [k]. w = (tree [k]. r-tree [k]. l + 1) * val; 99 tree [k]. add = 0; 100 return; 101} 102 if (tree [k]. add | tree [k]. v) down (k); 103 LL mid = tree [k]. r + tree [k]. l> 1; 104 if (ll <= mid) Interval_Change (ls, ll, rr, val); 105 if (rr> mid) Interval_Change (rs, ll, rr, val); 106 update (k); 107} 108 void Interval_Sum (LL k, LL ll, LL rr) 109 {110 if (ll <= tree [k]. l & tree [k]. r <= rr) 111 {112 ans + = tree [k]. w; 113 return; 114} 115 if (tree [k]. add | tree [k]. v) down (k); 116 LL mid = tree [k]. r + tree [k]. l> 1; 117 if (ll <= mid) Interval_Sum (ls, ll, rr); 118 if (rr> mid) Interval_Sum (rs, ll, rr ); 119} 120 void Interval_Max (LL k, LL ll, LL rr) 121 {122 if (ll <= tree [k]. l & tree [k]. r <= rr) 123 {124 ans = max (ans, tree [k]. max); 125 return; 126} 127 if (tree [k]. add | tree [k]. v) down (k); 128 LL mid = tree [k]. r + tree [k]. l> 1; 129 if (ll <= mid) Interval_Max (ls, ll, rr); 130 if (rr> mid) Interval_Max (rs, ll, rr ); 131} 132 void Interval_Min (LL k, LL ll, LL rr) 133 {134 if (ll <= tree [k]. l & tree [k]. r <= rr) 135 {136 ans = min (ans, tree [k]. min); 137 return; 138} 139 if (tree [k]. add | tree [k]. v) down (k); 140 LL mid = tree [k]. r + tree [k]. l> 1; 141 if (ll <= mid) Interval_Min (ls, ll, rr); 142 if (rr> mid) Interval_Min (rs, ll, rr ); 143} 144 int main () 145 {146 read (n); read (m); 147 Build_Tree (1, n, 1); 148 for (LL I = 1; I <= m; I ++) 149 {150 char how [5]; 151 scanf ("% s", how ); 152 if (how [0] = 'A') // Add 153 {154 LL x, y, val; read (x); read (y ); read (val); 155 Interval_Add (1, x, y, val ); 156} 157 else if (how [0] = 's' & how [1] = 'E') // The interval value is 158 {159 LL x, y, val; read (x); read (y); read (val); 160 Interval_Change (1, x, y, val ); 161} 162 else if (how [0] = 's' & how [1] = 'U') // The sum of the intervals is 163 {164 LL x, y; read (x); read (y); ans = 0; 165 Interval_Sum (1, x, y); 166 printf ("% lld \ n", ans ); 167} 168 else if (how [0] = 'M' & how [1] = 'A') // the maximum value of the interval is 169 {170 LL x, y; read (x); read (y); ans = 0; 171 Interval_Max (1, x, y); 172 printf ("% lld \ n", ans ); 173} 174 else if (how [0] = 'M' & how [1] = 'I') // the minimum value of the interval is 175 {176 LL x, y; read (x); read (y); ans = INF; 177 Interval_Min (1, x, y); 178 printf ("% lld \ n", ans ); 179} 180} 181 return 0; 182}

 

Related Article

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.