Advertisement
Guest User

Untitled

a guest
May 3rd, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. Spring is interesting season of year. Chef is thinking about different things, but last time he thinks about interesting game - "Strange Matrix".
  2.  
  3. Chef has a matrix that consists of n rows, each contains m elements. Initially, the element aij of matrix equals j. (1 ≤ i ≤ n, 1 ≤ j ≤ m).
  4.  
  5. Then p times some element aij is increased by 1.
  6.  
  7. Then Chef needs to calculate the following:
  8.  
  9. For each row he tries to move from the last element (with number m) to the first one (with the number 1).
  10. While staying in aij Chef can only move to aij - 1 only if aij - 1 ≤ aij.
  11. The cost of such a movement is aij - aij - 1.
  12. Otherwise Chef can't move and lose (in this row).
  13. If Chef can move from the last element of the row to the first one, then the answer is the total cost of all the movements.
  14. If Chef can't move from the last element of the row to the first one, then the answer is -1.
  15.  
  16. Help Chef to find answers for all the rows after P commands of increasing.
  17. Input
  18.  
  19.  
  20.  
  21. The first line contains three integers n, m and p denoting the number of rows, the number of elements a single row and the number of increasing commands.
  22. Each of next p lines contains two integers i and j denoting that the element aij is increased by one.
  23.  
  24.  
  25. Output
  26.  
  27. For each row in a single line print the answer after the P increasing commands.
  28.  
  29.  
  30. Constraints
  31.  
  32. 1 ≤ n, m, p ≤ 10 ^ 5
  33. 1 ≤ i ≤ n
  34. 1 ≤ j ≤ m
  35.  
  36.  
  37. Example
  38.  
  39. Input:
  40. 4 4 6
  41. 2 2
  42. 3 2
  43. 3 2
  44. 4 3
  45. 4 4
  46. 4 3
  47.  
  48. Output:
  49. 3
  50. 3
  51. -1
  52. 4
  53.  
  54.  
  55. Explanation
  56.  
  57. Here is the whole matrix after P commands:
  58.  
  59. 1 2 3 4
  60. 1 3 3 4
  61. 1 4 3 4
  62. 1 2 5 5
  63.  
  64. Explanations to the answer:
  65.  
  66. The first line is without changes: 4-3=1, 3-2=1, 2-1=1. answer = 3.
  67. The second line: 4-3=1, 3-3=0, 3-1=2. The answer is 3.
  68. The third line: 4-3=1, 3-4=-1, Chef can't move to the first number here. Therefore, the answer is -1.
  69. The fourth line: 5-5=0, 5-2=3, 2-1=1. The answer is 4.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement