Shadow Line

시간 제한10초메모리 제한2048 MB

요약
x축 음의 방향으로 움직이는 점광원이 x = w 벽에 정확히 하나의 그림자 구간을 만드는 x 구간의 길이를 모두 더하고, 그 영역이 무한하면 -1을 출력한다.
난이도

어려움10점 중 8점

유형
기하, 정렬, 구현
정답자
아직 제출이 없습니다

문제

You have a point light source at the origin in the 2D plane. To the right there is an infinitely tall wall. There are some opaque vertical line segments between the light source and the wall. As a result, each line segment casts a shadow onto the wall. All of these shadows overlap to form one or more intervals on the wall.

Now imagine moving the light source along the xx-axis in the negative direction, effectively pulling the light further away from all the objects in a straight line. As the light source moves, the shadows move as well, potentially changing the number of shaded intervals on the wall. Your job is to compute the sum of the lengths of the intervals along the xx-axis for which the light source creates a single shaded interval on the wall.

입력

The first line of input contains two integers nn (1≤n≤3,0001 \leq n \leq 3\\,000) and ww (2≤w≤1062 \leq w \leq 10^6), where nn is the number of opaque vertical segments, and ww is the xx-coordinate of the infinitely tall wall.

Each of the next nn lines contains three integers xx (0<x<w0 < x < w), y_lowy\_{low} and y_highy\_{high} (−106≤y_low<y_high≤106-10^6 \leq y\_{low} < y\_{high} \leq 10^6). Each set of three integers describes a line segment from (x,y_low)(x,y\_{low}) to (x,y_high)(x,y\_{high}). All yy-coordinates will be unique. No two line segments will intersect or overlap.

출력

Output a single number, which is the sum of the lengths of the intervals along the xx-axis for which the light source creates a single shaded interval on the wall. If this sum includes an unbounded interval (i.e. there is a single shaded interval when the light source is infinitely far away), print −1-1 instead. Your answer will be accepted if the absolute or relative error is within 10−610^{-6} of the judge's answer.

예제1

  1. 예제 1

    입력
    3 20
    2 1 3
    6 -4 2
    12 -10 -5
    
    예상 출력
    16.0