Geometry Rush

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

요약
한 점이 매초 (+1,+1) 또는 (+1,-1)로 움직이며 다각형 천장과 바닥 사이를 통과할 때, x=w에 도달할 수 있는 y의 최솟값과 최댓값을 구하거나 불가능을 판정한다.
난이도

어려움10점 중 8점

유형
그래프, BFS, 기하, 동적 계획법
정답자
아직 제출이 없습니다

문제

You are playing the summer's hottest rhythm-based action platformer---Geometry Rush! The game is played on a 2D plane. Your character begins at (0,0)(0,0) and every second must move at a 4545-degree angle either up-right or down-right, which takes your character from position (x,y)(x,y) to (x+1,y+1)(x+1,y+1) or (x+1,y−1)(x+1, y-1) respectively. You can change which direction you move every second, but not in between moves. There are obstacles protruding from the floor and ceiling that you must dodge. You win the game if, after ww seconds, you reach the line x=wx=w without having touched any obstacles on the way.

The play area extends vertically from y=−hy=-h to y=hy=h. Obstacles are two polygonal curves: one curve starts at (0,h)(0,h) and ends at (w,h)(w,h) and represents a ceiling of varying height. The xx values of the vertices of this curve are non-decreasing, and the yy values lie between −h-h and hh inclusive. A second polygonal curve starts at (0,−h)(0,-h) and ends at (w,−h)(w,-h) and represents the floor. Its vertices satisfy similar constraints.

Your character is a point of negligible extent: you can move from position (x,y)(x,y) to (x+1,y±1)(x+1,y\pm 1) so long as the line segment between your start and end position does not intersect either obstacle. (Exactly touching either polygonal curve counts as intersecting an obstacle, and loses the game.)

You have played a lot of games of Geometry Rush. To keep the game interesting, you have started to set challenges for yourself. For example: you win the game no matter where you cross the x=wx=w goal line. But for what maximum value of yy can you win the game by crossing at (w,y)(w,y) without touching any obstacles on the way? For what minimum value? Compute these numbers.

입력

The first line of the input contains four space-separated integers nn, mm, ww, and hh. The first two integers (3≤n,m≤1053 \leq n, m \leq 10^{5}) are the number of vertices in the ceiling and floor polygonal curves, respectively. The second two integers (3≤w,h≤1053 \leq w, h \leq 10^{5}) are the width and height of the play area, as described above.

The next nn lines each contain two space-separated integers xx and yy (0≤x≤w0 \leq x \leq w; −h≤y≤h-h \leq y \leq h): the coordinates of the vertices of the ceiling polygonal curve, in order from left to right. It is guaranteed that the first vertex is at (0,h)(0,h) and the last vertex is at (w,h)(w,h).

The next mm lines each contain two space-separated integers xx and yy (0≤x≤w0 \leq x \leq w; −h≤y≤h-h \leq y \leq h): the coordinates of the vertices of the floor polygonal curve, in order from left to right. It is guaranteed that the first vertex is at (0,−h)(0,-h) and the last vertex is at (w,−h)(w,-h).

For both polygonal curves: the xx coordinates are non-decreasing, all vertices are distinct, and the curve does not self-intersect. Neither curve intersects (0,0)(0,0). (The floor and ceiling curves might intersect each other, in which case the game is unwinnable.)

출력

If it is impossible to win the game, print impossible. Otherwise, print two space-separated integers: the minimum and maximum yy values that the player could reach at x=wx=w without losing the game by touching an obstacle along the way.

예제4

  1. 예제 1

    입력
    4 4 5 5
    0 5
    0 2
    5 2
    5 5
    0 -5
    0 -2
    5 -2
    5 -5
    
    예상 출력
    -1 1
    
  2. 예제 2

    입력
    4 4 6 5
    0 5
    0 2
    6 2
    6 5
    0 -5
    0 -2
    6 -2
    6 -5
    
    예상 출력
    0 0
    
  3. 예제 3

    입력
    3 3 7 5
    0 5
    5 -1
    7 5
    0 -5
    2 1
    7 -5
    
    예상 출력
    impossible
    
  4. 예제 4

    입력
    4 3 5 5
    0 5
    0 2
    5 2
    5 5
    0 -5
    3 -1
    5 -5
    
    예상 출력
    -1 1