Pagini recente » Cod sursa (job #2609091) | Diferente pentru blog/square-root-trick intre reviziile 53 si 54 | Pachetul de instalare | Diferente pentru utilizator/simon2712 intre reviziile 35 si 168 | Diferente pentru blog/square-root-trick intre reviziile 60 si 59
Nu exista diferente intre titluri.
Diferente intre continut:
The code looks like this:
== code(c) |
def update(S, A, i, k, x):
S[i/k] = S[i/k] - A[i] + x
A[i] = x
S[i/k] = S[i/k] - A[i] + x
A[i] = x
==
The query is interesting. Slices completely contained in our range are summed up fast. The elements of the first and last slice (partially contained in the queried range) have to be traversed one by one.
The code looks like this:
== code(c) |
def query(S, A, lo, hi, k):
s = 0
i = lo
while (i + 1) % k != 0 and i <= hi:
s += A[i]
i += 1
while i + k <= hi:
s += S[i/k]
i += k
while i <= hi:
s += A[i]
i += 1
return s
i = lo
while (i + 1) % k != 0 and i <= hi:
sum += A[i]
i += 1
while i + k <= hi:
sum += S[i/k]
i += k
while i <= hi:
sum += A[i]
i += 1
==
The query takes less than $k + n/k + k = 2k + n/k$ time. $2k + n/k$ is minimized when $k$ is $O(sqrt(n))$. For $k = sqrt(n)$ the query takes $O(sqrt(n))$ time.
Nu exista diferente intre securitate.
Topicul de forum nu a fost schimbat.