Segmented interval query and interval modification, interval query Modification

Source: Internet
Author: User

Segmented interval query and interval modification, interval query Modification

A series with a length of n and n operations are given. operations involve interval addition and interval summation.

 

The question is changed to the question on the interval, and incomplete blocks are still violent. to quickly calculate the answer to the complete block, you need to maintain the element and of each block, first preprocessing.

Consider the interval modification operation. If incomplete blocks are changed, update the block elements and the complete blocks are similar to the previously marked steps, calculate the element and increment directly based on the element of the block and the added value.

 

Interval addition after modification

1 void interval_add (LL ll LL, LL rr, LL v) 2 {3 for (ll I = ll; I <= min (where [LL] * m, rr ); I ++) 4 // The result is that where [ll] is not a complete block, that is, ll is on the rightmost side of the actual block, 5 // then facilitate ll-the end of the block/rr, and add 6 a [I] + = v, sum [where [ll] + = v; 7 // note that sum represents the element and in a block, and where represents the block position 8 if (where [ll]! = Where [rr]) 9 // note that if ll and rr are in the same block, the above side has been added, so do not add 10 {11 for (LL I = (where [rr]-1) * m + 1; I <= rr; I ++) 12 // here, the rr is located on the rightmost left side of the actual block. 13 // where [I] * m indicates the rightmost element of block I. 14/ /where [rr]-1 is the rightmost element of the block on the left of the rr Block 15 // until the rr violent increase of 16 a [I] + = v, sum [where [rr] + = v; 17} 18 for (LL I = where [ll] + 1; I <= where [rr]-1; I ++) 19 // here, where [ll] and where [rr] have been handled violently. Therefore, you can add 20 [I] + = v; 21} Only by enumerating the middle blocks}

 

Interval Query

1 void interval_ask (LL ll, LL rr) 2 {3 LL ans = 0; 4 for (LL I = ll; I <= min (where [ll] * m, rr); I ++) 5 ans + = a [I] + add [where [I]; 6 // brute force sum. Note that ll \ I should be written in where, because we are always repeating 7 // 8 if (where [ll] In the element ending with ll to its lifecycle! = Where [rr]) 9 for (LL I = (where [rr]-1) * m + 1; I <= rr; I ++) 10 // note that we need to add one here, because all for is <=. If you do not write + 1, we will add 11 ans + = a [I] + add [where [I] on both sides; 12 13 for (LL I = where [ll] + 1; I <= where [rr]-1; I ++) 14 ans + = sum [I] + add [I] * m; 15 // here the number of elements in * range is 16 17 printf ("% lld \ n ", ans); 18}

 

Complete code

 

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # define LL long 6 using namespace std; 7 const ll maxn = 100001; 8 LL n, q, m, how, l, r, v; 9 LL a [MAXN]; // The initial value is 10 LL add [MAXN]; // then add the 11 LL where [MAXN] value to each block; // record the 12 LL sum [MAXN] for each value; // record the element and 13 void interval_add (LL ll, LL rr, LL v) 14 {15 for (LL I = ll; I <= min (where [ll] * m, rr); I ++) 16 // The result is that where [ll] is an incomplete block, that is, ll is actually The rightmost side of the block is 17 // then the end/rr of the block where ll-is located is convenient. The violent increase is 18 a [I] + = v, sum [where [ll] + = v; 19 // note that sum represents the elements and, where indicates the block position 20 if (where [ll]! = Where [rr]) 21 // note that if ll and rr are in the same block, the above side has been added, so do not add 22 {23 for (LL I = (where [rr]-1) * m + 1; I <= rr; I ++) 24 // here, the rr is located on the rightmost left side of the actual block. 25 // where [I] * m indicates the rightmost element of block I. 26/ /where [rr]-1 is the rightmost element of the block on the left of the rr Block 27 // until the rr violent increase of 28 a [I] + = v, sum [where [rr] + = v; 29} 30 for (LL I = where [ll] + 1; I <= where [rr]-1; I ++) 31 // here, where [ll] and where [rr] are both handled in a brute force manner. Therefore, you can add [I] + = v by enumerating only the middle blocks; 33} 34 void interval_ask (LL ll, LL rr) 35 {36 LL ans = 0; 37 fo R (LL I = ll; I <= min (where [ll] * m, rr); I ++) 38 ans + = a [I] + add [where [I]; 39 // brute force sum. Note that ll must be written in where, because we always loop 40 41 if (where [ll]! = Where [rr]) 42 for (LL I = (where [rr]-1) * m + 1; I <= rr; I ++) 43 // note that we need to add one here, because all the for statements are <=. If not written, + 1 will add 44 ans + = a [I] + add [where [I] on both sides; 45 46 for (LL I = where [ll] + 1; I <= where [rr]-1; I ++) 47 ans + = sum [I] + add [I] * m; 48 49 printf ("% lld \ n", ans); 50} 51 int main () 52 {53 scanf ("% lld", & n); 54 scanf ("% lld", & q); 55 m = sqrt (n ); 56 for (LL I = 1; I <= n; I ++) 57 scanf ("% lld", & a [I]); 58 for (LL I = 1; I <= n; I ++) 59 where [I] = (I-1)/m + 1, sum [where [I] + = a [I]; // here I can be-1 (hzwer writes-1) or not. If I do not write, the number of elements in the first block will be 1-60 61 for (LL I = 1; I <= q; I ++) 62 {63 scanf ("% lld", & how); 64 if (how = 1) // Add 65 {66 scanf ("% lld", & l, & r, & v); 67 interval_add (l, r, v ); 68} 69 else // Single Point query 70 {71 scanf ("% lld", & l, & r); 72 interval_ask (l, r ); 73 // where stores the block to which the vertex belongs. add indicates that the added element 74 // a [v] of the block is the starting value of this vertex, answer 75} 76} 77 return 0; 78}

 

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.