CodeForces162 Div1-E

65bd3434deeeb8afc00485cdfc3e260a?s=47 snuke
January 20, 2013

CodeForces162 Div1-E

Solution of CodeForces#162 Div1-E.

65bd3434deeeb8afc00485cdfc3e260a?s=128

snuke

January 20, 2013
Tweet

Transcript

  1. CodeForces #162 Div1-E RodeSideTrees writer: snuke

  2. Problem summary You should process queries: Type1: Plant a tree

    with height h.(h<=10) Type2: Cut the x-th tree from west.(x<=10) After each query, you should calculate the length of LIS of height of tree(from west to east). Before each query, trees grow 1 meter. N: The number of trees ≦ 10^5 M: The number of queries ≦ 2*10^5
  3. Solution When you want to plant a tree with height

    h at time t, you should plant a tree with height h-t instead. Then you can ignore the growth of the trees. And plot trees on a x-y plane: x-coordinate is the position and y-coordinate is the modified height (h-t).
  4. Solution height position like this picture then.....

  5. Solution LIS is the longest sequence of points P_1, P_2,

    ... such that x(P_1) < x(P_2) < ... y(P_1) < y(P_2) < ... y x
  6. Solution At each point write the length of the longest

    increasing sequence that starts from the point. The value written on point p = (the maximal value written in the rectangle whoselower-left corner is p) + 1. 1 1 1 2 2 3 4 y x
  7. How should we process queries? Solution 1 1 1 2

    2 3 4 y x
  8. Planting query -> plot a new point The new point

    will be one of the ten points that have the smallest y-coordinate. To process this query, erase all values written below the new point first and rewrite the values to those points from top to bottom. Solution 1 1 1 2 2 3 4 y x
  9. Cutting query -> remove a point Similarly the removed point

    will be one of the ten points that have the smallest x-coordinate. So you can erase all values written on those points and rewrite correct values from right to left. Solution 1 1 1 2 2 3 4 y x
  10. What data structures do we need? 2D segtree? -> too

    slow :( Solution 1 1 1 2 2 3 4 y x
  11. Make two segment trees: let's call them segx and segy.

    segx : x-directional segtree segy : y-directional segtree For planting queries use segx. For cutting queries use segy. Solution 1 1 1 2 2 3 4 y x
  12. The i-th leaf of segx contains the value written on

    the point whose x-coordinate is x and non-leaf nodes of the segment trees have the maximum of children of the node. Define segy similarly. Solution 1 1 1 2 2 3 4 y x
  13. O(N * 10 * log N) Time Complexity

  14. Thank you for watching!