Pagini recente » Coding contest trick: Meet in the middle | Diferente pentru blog/square-root-trick intre reviziile 38 si 39 | Monitorul de evaluare | Diferente pentru blog/square-root-trick intre reviziile 59 si 60
Nu exista diferente intre titluri.
Diferente intre continut:
The code looks like this:
== code(c) |
S[i/k] = S[i/k] - A[i] + x
A[i] = x
def update(S, A, i, k, 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) |
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
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
==
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.