Advertisement
MinhNGUYEN2k4

Untitled

Jan 17th, 2022
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. Task NORMA Author: Ivan Katanić
  2. The goal of this task is, for each two integers A and B (1 <= A <= B <= N),
  3. calculate the price of the subsequence [A,B] of input array X and to sum up
  4. the given values. Let us imagine that we are moving the right bound B from
  5. left to right and maintaining the array of solutions for current B, so that at
  6. position A of that array, call it array T, there is the price of subsequence [A,B].
  7. A high-level algorithm would look like this:
  8. initialize array T to 0
  9. solution = 0
  10. for B from 1 to N
  11. subsequences are expanded from [A,B1]
  12. to [A,B] by adding
  13. X[B]
  14. therefore refresh values in array T at positions 1 to B
  15. add to the solution T[1] + T[2] + … + T[B]
  16. Let’s imagine that every member T[A] of array T internally contains values m,
  17. M and L, minimal number of sequence [A,B], maximal number of sequence
  18. [A,B] and length of subsequence [A,B], respectively.
  19. We need to have an efficient update of values (prices) in array T. Since the
  20. value of a member is the product of its internal values, let us observe how
  21. these are changing when incrementing B by 1 (moving to the right). Values L
  22. of each member with index smaller than or equal to B are incremented by 1.
  23. Let Pm be the position of the rightmost member of array X that is to the left of
  24. B such that it holds X[Pm] < X[B]. Value m of all members of array T at
  25. position from the interval [1,Pm] will be left unchanged while the value m of all
  26. members of array T on positions from the interval [Pm+1,B] will become
  27. exactly X[B].
  28. Similarly, PM be the position of the rightmost member of array X that is to the
  29. left of B such that it holds X[PM] > X[B]. Value M of all members of array T at
  30. position from the interval [1,PM] will be left unchanged while the value M of all
  31. members of array T on positions from the interval [PM+1,B] will become
  32. exactly X[B].
  33. Therefore, it is necessary to implement a data structure that will allow the
  34. following operations:
  35. increment_L(lo, hi, d_L) - increment value L by d_L to all members of the
  36. array at position from the interval [lo,hi].
  37. set_m(lo, hi, new_m) - set value m to new_m to all member of the array at
  38. position from the from the interval [lo,hi].
  39. set_M(lo, hi, new_M) - set value M to new_M to all member of the array at
  40. position from the from the interval [lo,hi].
  41. sum_mML(lo, hi) - return the sum of products of values m, M and L of all the
  42. members of the array at position from the interval [lo, hi].
  43. It turns out that the required operations can be efficiently achieved using a
  44. tournament tree. For this purpose, every node of the tree must contain the
  45. following values:
  46. len - length of the interval of members of the array that the node is covering,
  47. for example, for leaves it holds len=1
  48. sm - sum of values m of all members from the interval
  49. sM - sum of values M of all members from the interval
  50. sL - sum of values L of all members from the interval
  51. smM - sum of values m*M of all members from the interval
  52. smL - sum of values m*L of all members from the interval
  53. sML - sum of values M*L of all members from the interval
  54. smML - sum of values m*M*L of all members from the interval
  55. How would incrementing values L to all members from the interval belonging
  56. to the node of the tree look like?
  57. It is necessary to suitably change the value of each node in the following way:
  58. sL = sL + d_L * len
  59. smL = smL + sm * d_L
  60. sML = sML + sM * d_L
  61. smML = smML + smM * d_L.
  62. Let’s observe how setting values m to all members from the interval belonging
  63. to the node of the tree looks like:
  64. sm = new_m * len
  65. smM = new_m * sM
  66. smL = new_m * sL
  67. smML = new_m * sML.
  68. And, finally, setting values M to all members from the interval belonging to the
  69. node of the tree:
  70. sM = new_M * len
  71. smM = new_M * sm
  72. smL = new_M * sL
  73. smML = new_M * smL.
  74. Because of the fact that all values in the nodes are sums, merging two nodes
  75. of a child for the purpose of calculating values of the parent node is simply the
  76. summation of the corresponding values from both nodes.
  77. The nature of mentioned operations on the structure is such that it is
  78. necessary to use tournament tree with propagation. The details of this
  79. expansion are general and left out from this description, but can be looked up
  80. in the attached code.
  81. We are still left with efficiently finding the rightmost smaller or larger number
  82. than the current X[B]. This can be simply implemented using a stack. Here we
  83. will describe finding the rightmost smaller value that is to the left of X[B], and
  84. finding the rightmost larger value is done in a similar manner.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement