Question: Needless to say ......
Idea: An entry line segment tree plug-in line, explained in the code
AC code:
[Cpp]
/*
Line Segment tree-insertion question line:
1: The line segment tree stores the sum of corresponding intervals.
2: When a value is updated, the parent node of the value is updated (in turn up until the root node ).
3: During the query, find the number of online segments that are divided into the corresponding cells and find sum.
*/
# Include <stdio. h>
# Include <string. h>
# Define Max 4*100000
# Define LL (a) a <1; // 2 *
# Define RR (a) a> 1 | 1; // 2 * a + 1
# Define Mid (x, y) (x + y)> 1 // (x + y)/2
Int A [Max], cnt, ans;
Struct hello
{
Int l;
Int r;
Int sum;
} Tree [Max];
Void Build_tree (int l, int r, int t) // l, r indicates the interval, and t indicates the interval Node
{
Int x;
Tree [t]. l = l;
Tree [t]. r = r;
Tree [t]. sum = 0;
If (l = r)
{
// Tree [t]. sum = A [l];
While (t! = 0)
{
Tree [t]. sum + = A [l];
T/= 2;
}
Return;
}
X = Mid (l, r );
Build_tree (l, x, 2 * t );
Build_tree (x + 1, r, 2 * t + 1 );
}
Void Updata_tree (int l, int r, int t) // l r indicates the interval to be updated, and t indicates the current node
{
If (tree [t]. l = l & tree [t]. r = r)
{
While (t! = 0)
{
Tree [t]. sum + = cnt;
T/= 2;
}
Return;
}
Int x = Mid (tree [t]. l, tree [t]. r );
If (x> = r)
Updata_tree (l, r, 2 * t );
Else if (x + 1 <= l)
Updata_tree (l, r, 2 * t + 1 );
Else
{
Updata_tree (l, x, 2 * t );
Updata_tree (x + 1, r, 2 * t + 1 );
}
}
Void Query_tree (int l, int r, int t) // l r indicates the interval t to be queried, indicating the current node
{
// Printf ("inin ------------- \ n ");
If (tree [t]. l = l & tree [t]. r = r)
{
// Printf ("outout ------ \ n ");
Ans + = tree [t]. sum;
Return;
}
Int x = Mid (tree [t]. l, tree [t]. r );
If (x> = r)
Query_tree (l, r, 2 * t );
Else if (x + 1 <= l)
Query_tree (l, r, 2 * t + 1 );
Else
{
Query_tree (l, x, 2 * t );
Query_tree (x + 1, r, 2 * t + 1 );
}
}
Int main ()
{
Int I, j, n, m, ncase;
Scanf ("% d", & ncase );
For (n = 1; n <= ncase; n ++)
{
Printf ("Case % d: \ n", n );
Scanf ("% d", & m );
For (I = 1; I <= m; I ++)
Scanf ("% d", & A [I]);
Build_tree (1, m, 1 );
Char str [100];
While (scanf ("% s", str) & str [0]! = 'E ')
{
Int x, y;
If (str [0] ='s ')
{
Scanf ("% d", & x, & cnt );
Cnt =-cnt;
Updata_tree (x, x, 1 );
}
Else if (str [0] = 'A ')
{
Scanf ("% d", & x, & cnt );
Updata_tree (x, x, 1 );
}
Else if (str [0] = 'q ')
{
Scanf ("% d", & x, & y );
Ans = 0;
Query_tree (x, y, 1 );
Printf ("% d \ n", ans );
}
}
}
}