Editorial for Yet Another Contest 6 P6 - No More Snakes and Ladders
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
We can solve each query separately using dynamic programming. The DP states are the current position in the grid, and the number of remaining times we may ignore the contribution of a cell's value to the total sum. The transitions are straightforward.
Time complexity:
Subtask 2
We can use divide and conquer to answer the queries offline. Consider a recursive function which solves for all queries inside a particular subgrid. Imagine a vertical line of cells down the middle of the subgrid. Every query inside this subgrid must fall into one of three categories:
- Both the starting and ending cells of the query are completely on the left of the vertical line.
- Both the starting and ending cells of the query are completely on the right of the vertical line.
- The starting cell is on the left or on the splitting line, and the ending cell is on the right or on the splitting line.
The first two categories are handled by the 'divide' part of the divide and conquer. So, we will only focus on the third category of queries.
Each of the queries' paths must pass through at least one of the cells in the vertical line. So, for each cell on the vertical line, we can utilise the subtask 1 solution to precompute two DPs per cell; the first DP involves paths going down and right, starting at the cell, whilst the other DP involves paths going up and left, starting at the cell. If we fix the cell which a particular query's path must pass through, we can combine these two DPs to find the answer. So it suffices to precompute these DPs for each cell on the vertical line, and to iterate through every cell on the vertical line for each query.
Note that we can do exactly the same thing using a horizontal line of cells instead. It is optimal to choose between a vertical and horizontal line such that the two new subgrids are as close to squares as possible.
Time complexity: , assuming
Comments
How do we prove the time complexity for the full solution? I got AC while splitting only on vertical lines, which should run in but is fast enough to pass comfortably because the constant factor is relatively small. However, I don't immediately see how to show that adding the option of splitting over horizontal lines eliminates the log factor.
I tried implementing it but I am having trouble with the constant factor; even subtask 1 ran in 452 ms in the worst case, and I think I ran into the bottleneck in the full task
edit: Got it in 494 ms after optimizing memory usage to , which incidentally reduced indirection
Here's how I reason about it. We will ignore the queries for the complexity analysis and instead focus on the DP calculations. Also, let's ignore entirely for now.
For simplicity, assume that the grid is a square. For every splitting line, let's say that every cell in the current subgrid is related to the splitting line. Consider any particular cell, and calculate the sum of the lengths of all splitting lines related to it. If we sum this value over all cells, we get our final complexity.
But each cell is related to a length vertical splitting line, a length horizontal splitting line, a length vertical splitting line, and so on. Then, the sum of these lengths is approximately , so the total complexity is .
Alternatively, let be the total time to solve for a by grid. Consider calculating the DPs for a vertical splitting line of length and two horizontal splitting lines of length , then recursing on each of the four smaller subgrids. We have a recurrence in the form . Applying case of the master theorem, we see that as desired.
I typed this on my phone and it was very painful :(
Thanks for the explanation!