Академический Документы
Профессиональный Документы
Культура Документы
:
f (, , )
[i; j], f(ai, , aj).
(, , ): ai = newValue.
: a[i..j] = newValue (ai = newValue, ai+1 =
newValue, , aj = newValue).
: O(n), O(1).
, (
O(log2n) ) .
. a[0n 1].
, :
l r a[l] + a[l + 1] + + a[r 1] + a[r];
a[i] = x
O(log2n).
- , a[0n
1]. : a[0n / 2] a[n / 2 + 1n 1].
,
, , ,
1. [0n 1], (
), .
.
, , , :
, ( -,
1). " ".
. a {5, 2, 8, 4}.
.
, , .
1, 2 3. ,
i, 2i, 2i + 1.
t, ,
2n, 4n. n ,
, ( ,
n ).
build a,
v LeftPos RightPos, v.
. . 10 11
.
sum [Left; Right]. v, LeftPos
RightPos 1, 0 n 1.
int sum(int v, int LeftPos, int RightPos, int Left, int Right)
{
if (Left > Right) return 0;
if ((Left == LeftPos) && (Right == RightPos)) return t[v];
int Middle = (LeftPos + RightPos) / 2;
return sum (v*2, LeftPos, Middle, Left, min(Right,Middle)) +
sum (v*2+1, Middle+1, RightPos, max(Left,Middle+1), Right);
}
{8, 3, 7, 5, 9, 4},
, :
. Position NewValue.
v, LeftPos RightPos 1, 0 n 1.
void update(int v, int LeftPos, int RightPos, int Position, int NewValue)
{
if (LeftPos == RightPos) t[v] = NewValue;
else
{
int Middle = (LeftPos + RightPos) / 2;
if (Position <= Middle) update (v*2, LeftPos, Middle, Position, NewValue);
else update (v*2+1, Middle+1, RightPos, Position, NewValue);
t[v] = t[v*2] + t[v*2+1];
}
}
.
( ).
.
. .
,
, (
), : ,
.
.
:
summa add,
. add
summa ,
, add ,
add .
, .
. [0; 5].
[0; 2] [3; 5]. .
, value.
add
value, (, -
). ,
.
. .
t node MAX, MAX
, .
add v ,
. add v .
void Push(int v, int LeftPos, int Middle, int RightPos)
{
if (t[v].add)
{
t[2*v].add += t[v].add;
t[2*v].summa += (Middle - LeftPos + 1) * t[v].add;
t[2*v+1].add += t[v].add;
t[2*v+1].summa += (RightPos - Middle) * t[v].add;
t[v].add = 0;
}
}
, add .
Push(v,LeftPos,Middle,RightPos);
.
AddValue(2*v, LeftPos, Middle, L, min(Middle,R), Value);
AddValue(2*v+1, Middle+1, RightPos, max(L,Middle+1), R, Value);
v .
t[v].summa = t[2*v].summa + t[2*v+1].summa;
}
v [LeftPos; RightPos]. Summa
[L; R].
long long Summa(int v, int LeftPos, int RightPos, int L, int R)
{
if (L > R) return 0;
, add .
Push(v,LeftPos,Middle,RightPos);