Can someone help me? In Discussion people said they solved in O(m*logn), is there O(n+m) solution too? If you can, give me ideas of both solutions. http://acm.timus.ru/problem.aspx?space=1&num=1987
Thanks
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 156 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
Can someone help me? In Discussion people said they solved in O(m*logn), is there O(n+m) solution too? If you can, give me ideas of both solutions. http://acm.timus.ru/problem.aspx?space=1&num=1987
Thanks
Название |
---|
I solved it in O(m*logn), the idea is to keep a set of active segments, sorted by lenght using a priority queue. Since queries are sorted we can also sort segments by their starting point. Now we can process queries one by one, each time we add segments that start before the point of the query and delete segments that end before the point of the query. Since we add and delete at most once each segment to the priority queue the running time is O(m*logn)
Actually, you even don't need priority queue, You can do it in O(n + m) with stack. You should notice that all right ends of active segments are sorted in ascending order. So all you need is just push right end of segment to stack when you face it, and pop once, so answer for query is O(1), totally O(n + m) (1 add, 1 delete for every segment).
I didn't think to this optimization...
Another way you can sovle it is segment tree. You can store in node index of segment covering current interval (last index, since they sorted). So if you want to get index for query X, at current node you first check appropriate son, whether there is some smaller segment, if not, you take answer from current node. For preventing ML you can create nodes only where you need it(because queries in [1, 109]), in that case you'll be needed O(n * log(n)) memory.