Create a Data Structure which is collection of integers. Create methods to add an element, retrieve an element and addToAll method in O(1) time
# | 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 |
Create a Data Structure which is collection of integers. Create methods to add an element, retrieve an element and addToAll method in O(1) time
Name |
---|
You forgot to add "...delete an element, delete all elements, make segment tree faster than bit".
This comment only makes sense if "...add an element" means add an element at any random position.
I just need above three approach can you please explain little bit?
I am sorry you didn't get me. What I meant was it's not possible.
You can use a vector, and an int. You can add numbers to the vector, when you use addToAll, then you add it to the standalone integer, and when retrieving, you get the element from the vector, and add the standalone integer to it. When you add a number, then you have to deduct the integer from it. (-Morass- idea)
The standalone integer can be the 0th element of the vector also, for convenience.
but retriving complexity would not be constant in this case?
You can retrieve from a vector in O(1) and you can add two numbers in O(1). Why wouldn't it be constant?
Thats wrong. After adding 100 to all, I insert a new element. Now I add 1 to all.
According to you, the new element has been incremented by 101, but infact it is incremented by just 1.
Good day to you
imho you can add the element decreased by "100" [the new one] {so you would retrieve "Element-100+101" which is correct}
Have nice day ^_^
Use something like a DSU:
but dsu isn't exactly O(1) :P
It's not hard
Let's start with a plain vector of ints with O(1) amortized insertion at the back
Suppose we have right now N elements and we need to increment them by X, we can keep some separate variable delta indicating how much we added to the whole array without adding said value to each and every element in linear time
Retrieving ith element then is as easy as a[i] + delta
No problem so far as long as we don't insert more elements, because if we were to insert a number V at ith position, a[i] + delta would return a different value V + delta instead of just V, we can get around it by inserting V - delta instead of just V, this way a[i] + delta = (V - delta) + delta = V
It's worth mentioning that deleting an element won't be a problem, just make sure to choose a suitable underlying structure instead of plain vector if necessary to achieve required complexity
To support insertion at arbitrary positions, you may consider a tree-like structure at the cost of increased insertion complexity O(log2(n))
"methods to add an element, retrieve an element" is about as ambiguous as possible. And what even is addToAll?