I have an interesting problem on data structures.
We're given an array a[1..n]. Also we're given queries l,r,m and it means that min(a[l]..a[r])=m. We should restore array a.
How to solve? Thank you!
# | 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 | 160 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
8 | awoo | 154 |
10 | luogu_official | 151 |
I have an interesting problem on data structures.
We're given an array a[1..n]. Also we're given queries l,r,m and it means that min(a[l]..a[r])=m. We should restore array a.
How to solve? Thank you!
Name |
---|
Here is the main idea. For example, you have query with maximum answer from all queries. Now you can just make all numbers from L to R equal to M because all that you know about those numbers is that they are not less than M and maybe some less numbers. So you do with next query and so on (of course you change only those numbers that weren't changed before). That's a good solution but too slow. You can see that K-th number is maximum answer from all queries that cover this number. So now we use the scanline idea: sort queries by L and go from first element to last, taking answer from set of current query answers. This solution works in O(NlogQ + QlogQ), where Q is the number of queries.
Sometimes you also need to check if such array can exist. You can just try to build it and then check all queries again by building a segment tree on A array. Asymptotic is the same.
That's a good solution but too slow
.As I understand we should for i-th position use maximal value that was in queries. Is it correct to build segment tree with maximums on queries. And then get an array. It isn't slow
O(Q*logN))
and seems correct.taking answer from set of current query answers
How efficiently keep set of query answers?
Thanks!
This is what scanline does the best. Basically for each query you make two events: adding M into set on L position and deleting it from set on R + 1 position. Then you sort these events by their position and just track them from first to last, processing all events for position K before setting this number. (Set actually means multiset, at least for C++)
You can do it like you want, I just prefere this solution. You can also use treap and maybe there are other solutions that are as efficient as this one.
Yes, his idea is all correct. you can sort all the queries with start index then for each query follow the given procedure
for i in range(queries) :
basically the idea is to make all the segment disjoints and value for a given segment is the maximum of all queries value involved in that segment :)
This task has already been used in TCO14 Round 2C (and I am the writer).
Here you can find the solution:http://apps.topcoder.com/wiki/display/tc/TCO+2014+Round+2C
Pff, my idea seems to be wrong, sorry :)