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 | jiangly | 3976 |
2 | tourist | 3815 |
3 | jqdai0815 | 3682 |
4 | ksun48 | 3614 |
5 | orzdevinwang | 3526 |
6 | ecnerwala | 3514 |
7 | Benq | 3482 |
8 | hos.lyric | 3382 |
9 | gamegame | 3374 |
10 | heuristica | 3357 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | -is-this-fft- | 165 |
3 | Um_nik | 161 |
3 | atcoder_official | 161 |
5 | djm03178 | 157 |
6 | Dominater069 | 156 |
7 | adamant | 154 |
8 | luogu_official | 152 |
9 | awoo | 151 |
10 | TheScrasse | 147 |
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