jayanto's blog

By jayanto, history, 8 years ago, In English

I stuck in two problems more than one week but I don't know how to solve them. Do I leave them or spend more time on them.

problem link: Discovering Paths Just Some Permutation 5

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
8 years ago, # |
  Vote: I like it +7 Vote: I do not like it

For problem Discovering Paths you first have to solve it regularly (aka no queries), what I did is store two values on each position, how many ways you can reach this position from the start and how many ways you can reach the end from this position, that way if you multiply both values you obtain how many ways you can reach the end using that particular position (now you can use that to your advantage to block the squares).

First it's important to note that if you block a position every square to its right and bottom has less ways of being reached, and its not that hard to notice that instead of blocking the entire rectangle you can only block the topmost and leftmost squares, therefore making unreachable everything inside the rectangle. Lets suppose you have a 4x4 (there are 20 ways to start in the beginning and reach the end)
....
....
....
....

Now suppuse you block the center, so it's now like this.


....
.XX.
.XX.
....

So in order to get the answer you say how many ways can I reach the end using (1,1), and that's 12, in how many ways can I reach the end going through square (1,2) knowing that I can't reach it from (1,1), 3, same logic applies to (1,2), and you get 3, therefore you take away 12+3+3=18 options.

So answer is 2.

Doing this would require worst case O(R+C) per query, which is pretty slow, in order to optimize you can use partial sums for each row and column, therefore each query is O(1)

You can look at my code and test IO here http://pastebin.com/eY1VRVva

»
8 years ago, # |
  Vote: I like it +5 Vote: I do not like it

thanks Diego1149 :)