Hi!
Is there a data structure that can perform the following queries (in logaritmic time)?:
(a) for (i = 1; i <= n; i++) A[i] += B[i]
(b) given l and r perform for (i = l; i <= r; i++) B[i] = C
(c) given i, return the value of A[i]
Thanks!
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
Hi!
Is there a data structure that can perform the following queries (in logaritmic time)?:
(a) for (i = 1; i <= n; i++) A[i] += B[i]
(b) given l and r perform for (i = l; i <= r; i++) B[i] = C
(c) given i, return the value of A[i]
Thanks!
Name |
---|
Input for query (a) is O(n), so does it really matter to achieve logarithmic time?
The OP likely assumes A and B are some global arrays that you perform the operations upon (and that the input to (a) is O(1) in size)
Suppose that you are given A and B and then you are asked a lot of queries of type (a), (b), and (c).
You can try to keep D s.t. A[i] = B[i] * t + D[i] at all times for all i, where t is the number of operations of type a) so far. Then an operation of type b does for each i:
let delta = C — B[i] where B[i] is the old value of B at i; then D[i] -= delta * t
This seems difficult at first but a good observation is that you can rewrite operations of type b) as operations of type B[i] += delta. How so? It's kind of segtree beats, but much more specific: you can consider the potential of B as the number of adjacent positions of different values. When you have an update, you split it in maximal subarrays of equal value and perform operations of B[i] += delta on [x, y]. You'll have an amortized overall of N+M such operations. But they add a constant value to B, so you can make use of that to subtract a constant delta * t from A. You can clearly do this by a segment tree with lazy update (and via a segtree-like implementation, you can divide the range [x, y] into ranges of equal values; alternatively you can keep a set of the pairs where B[i] != B[i+1]). This is a very particular solution. I would be interested in hearing more general or simpler ideas