Slide 1

Slide 1 text

CodeForces #162 Div1-E RodeSideTrees writer: snuke

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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).

Slide 4

Slide 4 text

Solution height position like this picture then.....

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

How should we process queries? Solution 1 1 1 2 2 3 4 y x

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

What data structures do we need? 2D segtree? -> too slow :( Solution 1 1 1 2 2 3 4 y x

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

O(N * 10 * log N) Time Complexity

Slide 14

Slide 14 text

Thank you for watching!