P3372 [TEMPLATE] Line Segment tree 1 query and interval modification, p3372 Line Segment
Description
For example, if you know a sequence, you need to perform the following two operations:
1. Add x to each number in a certain range
2. Obtain the sum of each number in a certain range.
Input/Output Format
Input Format:
The first line contains two integers N and M, indicating the number of numbers in the series and the total number of operations respectively.
The second row contains N integers separated by spaces. the I-th digit indicates the initial value of the I-th Column.
Each row in the next M row contains 3 or 4 integers, indicating an operation, as shown below:
Operation 1: Format: 1 x y k meaning: Add k to each number in the range [x, y]
Operation 2: Format: 2 x y Meaning: Sum of each number in the output range [x, y]
Output Format:
The output contains several integer rows, that is, the result of all operation 2.
Input and Output sample
Input example #1:
5 51 5 4 2 32 2 41 2 3 22 3 41 1 5 12 1 4
Output sample #1:
11820
Description
Time-Space limit: 1000 ms, 128 M
Data scale:
For 30% of data: N <= 8, M <= 10
For 70% of data: N <= 1000, M <= 10000
For 100% of data: N <= 100000, M <= 100000
(The data has been enhanced ^ _ ^ to ensure that it is within the range of int64/long data)
Example:
I would like to remind you that
If you are using Dev-c ++ version 5.92, using % lld may cause a runtime error.
At this time, if you make sure that your program is correct, you can directly submit
If you do not trust your program, you can change % lld to % I64d (I is capital I) for debugging, so that no errors will occur.
But remember
% Lld must be written when submitted to loogu !!!!!!
Otherwise, all WA instead of RE
Remember
(Ps: cena's evaluation system is also % lld)
My code is basically composed of functions.
Easy to understand
You can refer
Another tips:
If you want to change int to long int in large batches
# Define statement
Then use the search replacement function
Note that int + space is used for searching.
Otherwise, your printf will become very beautiful (manual funny)
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # define lglg long int 5 using namespace std; 6 const lglg MAXN = 200001; 7 lglg n, m; 8 lglg ans = 0; 9 struct node10 {11 lglg l, r, w, f; 12} tree [MAXN * 4]; 13 inline void updata (lglg k) 14 {15 tree [k]. w = tree [k * 2]. w + tree [k * 2 + 1]. w; 16} 17 inline void build (lglg k, lglg ll, lglg rr) 18 {19 tree [k]. l = ll; tree [k]. r = rr; 20 if (tree [k]. l = tree [k]. r) 21 {22 scanf ("% lld", & tree [k]. w); 23 return; 24} 25 lglg m = (ll + rr)/2; 26 build (k * 2, ll, m ); 27 build (k * 2 + 1, m + 1, rr); 28 updata (k); 29} 30 inline void down (lglg k) 31 {32 tree [k * 2]. f + = tree [k]. f; 33 tree [k * 2 + 1]. f + = tree [k]. f; 34 tree [k * 2]. w + = (tree [k * 2]. r-tree [k * 2]. l + 1) * tree [k]. f; 35 tree [k * 2 + 1]. w + = (tree [k * 2 + 1]. r-tree [k * 2 + 1]. l + 1) * tree [k]. f; 36 tree [k]. f = 0; 37} 38 inline void interval_change (lglg k, lglg ll, lglg rr, lglg v) 39 {40 if (tree [k]. l> = ll & tree [k]. r <= rr) 41 {42 tree [k]. w + = (tree [k]. r-tree [k]. l + 1) * v; 43 tree [k]. f + = v; 44 return; 45} 46 if (tree [k]. f) down (k); 47 lglg m = (tree [k]. l + tree [k]. r)/2; 48 if (ll <= m) interval_change (k * 2, ll, rr, v); 49 if (rr> m) interval_change (k * 2 + 1, ll, rr, v); 50 updata (k); 51} 52 inline void interval_ask (lglg k, lglg ll, lglg rr) 53 {54 if (tree [k]. l> = ll & tree [k]. r <= rr) 55 {56 ans + = tree [k]. w; 57 return; 58} 59 if (tree [k]. f) down (k); 60 lglg m = (tree [k]. l + tree [k]. r)/2; 61 if (ll <= m) 62 interval_ask (k * 2, ll, rr); 63 if (rr> m) 64 interval_ask (k * 2 + 1, ll, rr); 65} 66 int main () 67 {68 scanf ("% lld", & n ); 69 scanf ("% lld", & m); 70 build (1, 1, n); 71 while (m --) 72 {73 lglg how; 74 scanf ("% lld ", & how); 75 if (how = 1) // increase the interval by 76 {77 lglg x, y, v; 78 scanf ("% lld ", & x, & y, & v); 79 interval_change (1, x, y, v); 80} 81 else // sum of the range 82 {83 lglg x, y; 84 ans = 0; 85 scanf ("% lld", & x, & y); 86 interval_ask (1, x, y ); 87 printf ("% lld \ n", ans); 88} 89} 90 return 0; 91}