Can someone please help me on how to approach this Problem
Thanking you in anticipation.
# | User | Rating |
---|---|---|
1 | Benq | 3783 |
2 | jiangly | 3666 |
3 | tourist | 3611 |
4 | Um_nik | 3536 |
5 | inaFSTream | 3477 |
6 | fantasy | 3468 |
7 | maroonrk | 3464 |
8 | QAQAutoMaton | 3428 |
9 | ecnerwala | 3427 |
10 | Ormlis | 3396 |
# | User | Contrib. |
---|---|---|
1 | Um_nik | 184 |
2 | adamant | 178 |
3 | awoo | 177 |
4 | nor | 169 |
5 | maroonrk | 165 |
6 | -is-this-fft- | 163 |
7 | antontrygubO_o | 152 |
7 | ko_osaga | 152 |
9 | dario2994 | 150 |
10 | SecondThread | 149 |
Can someone please help me on how to approach this Problem
Thanking you in anticipation.
Name |
---|
for query you can use sparse table or segment tree
Use a range query data structure such as segment tree/sparse table.
The solution that I found most common after having solved the problem was using a range query data structure to answer queries on range [L, R] efficiently. I used a RQ DS too and so I'll explain a solution that uses segment tree.
For a fixed point L, find the left most right boundary R such that $$$gcd([L, ..., R])$$$ is 1. Once we have such a value R, we can now move the left boundary. For any index i in [L, R], the $$$gcd([i, ..., R])$$$ will either be 1 or something greater. When we encounter a left boundary that makes gcd to something not 1, we start searching for another right boundary keeping the left boundary fixed. Among all such [L, R] endpoints, the one with maximum length is your answer.
The solution using RQ DS is, imo, the most intuitive. The complexity you achieve this way is $$$O(n.log(n))$$$. You can use either segment tree or sparse table or any other logarithmic query associative DS to achieve this complexity. Note that the complexity actually seems like it's $$$O(n.log^2(n))$$$ because of gcd being in there but finding the gcd of an entire array is magically $$$O(n)$$$ approximately and not $$$O(n.({some \space log \space factor}))$$$.
There also is a solution that uses something called a gcdqueue which is similar in structure to this but I do not understand it and will not explain.
I solved it this way: https://github.com/actium/cf/blob/master/edu/09/2g.cpp
could you please explain your appoach
In tutorial, we can easily get max/min when adding an element, but it's hard to remove an element.
How does the tutorial solve it?
It uses two stacks to implement the queue, and get the result of max/min from two of the stack to get the result.
Can we just replace it with other merge function?