This page is still under construction.

Parts of this page are still being built. What you see may change.

Hill Walk

Time limit1sMemory limit128 MB

Summary
Given non-crossing slanted segments, simulate Bessie climbing each hill and falling straight down at its upper end, counting the distinct hills she touches.
Level

Hard8 of 10

Topics
Sorting, Binary search, Geometry, Simulation
Solved
No attempts yet

Problem

There are NN hills (1≤N≤100,0001 \le N \le 100{,}000). Each hill is a line segment from (x1,y1)(x_1, y_1) to (x2,y2)(x_2, y_2) with x1<x2x_1 < x_2 and y1<y2y_1 < y_2. No two segments intersect or even touch, not even at their endpoints, and the first hill satisfies (x1,y1)=(0,0)(x_1, y_1) = (0, 0).

Bessie the cow starts at (0,0)(0, 0) on the first hill. Whenever Bessie is on a hill, she climbs up until she reaches its upper end, then jumps off the edge. If she lands on another hill she keeps walking along that hill; otherwise she falls forever and lands safely on a cushion of pillows at y=−∞y = -\infty.

Treat each hill from (x1,y1)(x_1, y_1) to (x2,y2)(x_2, y_2) as containing the point (x1,y1)(x_1, y_1) but not the point (x2,y2)(x_2, y_2): if Bessie falls straight down at x=x1x = x_1 she lands on the hill, but if she falls at x=x2x = x_2 she does not.

Count the total number of hills Bessie touches at some point during her walk.

Input

  • Line 11: the number of hills, NN.
  • Lines 2…N+12 \ldots N+1: line i+1i+1 contains four integers x1 y1 x2 y2x_1\ y_1\ x_2\ y_2 describing hill ii. Every integer is in the range 0…1,000,000,0000 \ldots 1{,}000{,}000{,}000.

Output

  • Line 11: the number of hills Bessie touches during her journey.

Hint

In the example, there are four hills. The first hill runs from (0,0)(0, 0) to (5,6)(5, 6). Starting on it, Bessie walks along hills #1, #4, and finally #3, touching three hills in total.

Examples1

  1. Example 1

    Input
    4
    0 0 5 6
    1 0 2 1
    7 2 8 5
    3 0 7 7
    
    Expected output
    3