This question should be said to be a simple application of line tree and tree array! They all use basic functions to update the interval lookup point!
When using a line segment tree, you do not want to look for a range from the original update point. here you need to add another lnc in the structure to record the changed value when the update interval is overwritten in this area! However, when the search interval is not a given interval, you need to update and change the sum value of the given interval! Also, update is called for the subinterval to maintain a line segment tree for the interval search!
When using a tree array, note that read (I) is to read the value of read (I) in the table below )!
Note that the array should be as big as possible! Avoid wa or re!
The following is the AC code for your reference:
Line Segment tree practices:
# Include <iostream> <br/> using namespace STD; <br/> struct node <br/> {<br/> int left, right; <br/> int sum, lnc; <br/>} tree [300000]; <br/> int num [100000]; <br/> void Init (int id, int L, int R) <br/>{< br/> If (L = r) <br/>{< br/> tree [ID]. sum = num [l]; <br/> tree [ID]. left = tree [ID]. right = L; <br/> tree [ID]. lnc = 0; <br/> return; <br/>}< br/> Init (ID * 2, L, (L + r)/2 ); <br/> Init (ID * 2 + 1, (L + r)/2 + 1, R); <br/> tree [ID]. sum = tree [2 * Id]. sum + tree [ID * 2 + 1]. SUM; <br/> tree [ID]. lnc = 0; <br/> tree [ID]. left = L; <br/> tree [ID]. right = r; <br/>}< br/> void Update (int id, int L, int R, int Val) <br/> {<br/> If (tree [ID]. left = L & tree [ID]. right = r) <br/>{< br/> tree [ID]. lnc + = val; <br/> return; <br/>}< br/> tree [ID]. sum + = Val * (R-l + 1); <br/> int mid = (tree [ID]. left + tree [ID]. right)/2; <br/> If (r <= mid) <br/> Update (2 * ID, L, R, Val ); <br/> else if (L> = Mid + 1) <br/> up Date (2 * ID + 1, L, R, Val); <br/> else <br/>{< br/> Update (2 * ID, L, mid, val); <br/> Update (2 * ID + 1, Mid + 1, R, Val ); <br/>}< br/> int read (int id, int L, int R) <br/> {<br/> If (tree [ID]. left = L & tree [ID]. right = r) <br/>{< br/> return tree [ID]. sum + tree [ID]. lnc * (R-l + 1); <br/>}< br/> tree [ID]. sum + = tree [ID]. lnc * (tree [ID]. right-tree [ID]. left + 1); <br/> int val = tree [ID]. lnc; <br/> int mid = (tree [ID]. left + tree [ID]. right)/2; <Br/> Update (2 * ID, tree [ID]. left, mid, Val); <br/> Update (2 * ID + 1, Mid + 1, tree [ID]. right, Val); <br/> tree [ID]. lnc = 0; <br/> If (r <= mid) <br/> return read (2 * ID, L, R ); <br/> else if (L> = Mid + 1) <br/> return read (2 * ID + 1, L, R ); <br/> else <br/> {<br/> return read (2 * ID, L, mid) + Read (2 * ID + 1, Mid + 1, r); <br/>}< br/> int main () <br/>{< br/> int N, I, A, B, temp; <br/> while (scanf ("% d", & N) <br/> {<br/> memset (Num, 0, sizeof (Num ); <Br/> memset (tree, 0, sizeof (tree); <br/> Init (1, 1, n); <br/> temp = N; </P> <p> while (temp --) <br/> {<br/> scanf ("% d", & A, & B ); <br/> Update (1, A, B, 1); <br/>}< br/> for (I = 1; I <= N; I ++) <br/>{< br/> printf ("% d", read (1, I, I); <br/> if (I! = N) <br/> printf (""); <br/> else <br/> printf ("/N "); <br/>}< br/> return 0; <br/>}
Tree array practices:
# Include <iostream> <br/> using namespace STD; <br/> int tree [1, 100100]; <br/> int maxn; <br/> void Update (int K, int v) <br/>{< br/> while (k <= maxn) <br/>{< br/> tree [k] + = V; <br/> K + = K &-K; <br/>}< br/> int read (int K) // calculate the sum value from 1 to k <br/>{< br/> int sum = 0; <br/> while (k> 0) <br/>{< br/> sum + = tree [k]; <br/> K-= K &-K; <br/>}< br/> return sum; <br/>}< br/> int main () <br/>{< br/> int I,, b, temp; <br/> while (scanf ("% d ",& Maxn) & maxn) <br/>{< br/> temp = maxn; <br/> memset (tree, 0, sizeof (tree )); <br/> while (temp --) <br/>{< br/> scanf ("% d", & A, & B ); <br/> Update (A, 1); <br/> Update (B + 1,-1); <br/>}< br/> for (I = 1; I <= maxn; I ++) <br/>{< br/> printf ("% d", read (I); <br/> if (I! = Maxn) <br/> printf (""); <br/> else <br/> printf ("/N "); <br/>}< br/> return 0; <br/>}< br/>
Haha, keep it up!