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 | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
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