Hi everyone I have been trying to solve this problem for a lot of time Timus 1846()..I know that this is a problem of segment tree .Can anyone please help me regarding the same. REGARDS..
# | 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 everyone I have been trying to solve this problem for a lot of time Timus 1846()..I know that this is a problem of segment tree .Can anyone please help me regarding the same. REGARDS..
Name |
---|
http://mirror.codeforces.com/blog/entry/3327
I had seen that but it did not focus on tree formation.. But Thanks anyway for sparing time kirjuri
No problem. Actually, I just skimmed through that post, that turns out to be very informative and wrote a solution in Java. I thought it could help you as well.
https://gist.github.com/ale64bit/324531e9de9e677c14800c22d81c32b8
Initially, we will just read the input without processing it in order to apply coordinate compression. Now for every number in the input we know its position if all numbers are sorted. For example, if we have number 9,7,1,1,8,7, the position of 1 is 1, the position of 7 is 2, the position of 8 is 3 and the position of 9 is 4.
Notice that it only matters whether number X is in the set, not how many times it is actually present. So at any moment we can keep the number of occurences for each number. When the query is '+', we increase X's count and if it becomes 1, then we need to insert X into our data structure. When the query is '-', we decrease X's count and if it becomes 0, then we need to erase X from our data structure.
Now let's see how we will build our data structure. Say that P[X] is the position of X if the array is sorted (the thing we talked about in the first paragraph) and M is the maximum value of P[X] for some X. Then we can build segment tree over those positions (indices from 1 to M). Each node in the segment tree will store the GCD of its children and initially everything is set to zero. It's easy to see that the answer after performing each query will be stored in the root of the tree. When inserting a number X into our structure, we simply assign value X to index P[X] and when erasing a number X, we assign value 0 to index P[X].
Here is my code — http://ideone.com/uJ8ndT :)
Thanks a lot P_Nyagolov :)
Instead of compressing coordinates you can just store in map the vector of positions where you added alement X, and when you need to add element, you just put it in any (number of query, for example) position of segment tree, and when you need to remove element, peek the last number from map[X] and set that position in segment tree to zero. Note as well that this approach is online (if there's not enough space for new element, you can just build a bigger segment tree, in the same manner that vector expands itself in C++).
//Treap also should work well for this problem, I think, though I don't know, would it fit in TL or not
Can It be done using square root decomposition also.. Regards.
Yes, it's pretty much the same idea, just store the gcd for each block :)