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..
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
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..
Название |
---|
http://codeforces.me/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 :)