This page is still under construction.

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

Call a Cab

Time limit5sMemory limit256 MB

Summary
Partition the ordered points into the fewest rides where each ride meets one type's minimum total distance and heading range limit.
Level

Hard8 of 10

Topics
Dynamic programming, Segment tree, Sliding window, Prefix sum
Solved
No attempts yet

Problem

A group of tourists visits nn points p1,p2,…,pnp_1, p_2, \dots, p_n in a valley, in exactly that order. Transportation comes in several types (car, rickshaw, donkey cart), and every type is available on call only. Deep in the valley the phone works at the points and nowhere else, so the type of transportation can change only at a point.

Segment ii runs from pip_i to pi+1p_{i+1}. Its length is did_i, and it turns the heading by hih_i from the previous segment. The first segment counts as a turn of h1h_1 as well, so the heading of segment ii is Hi=h1+h2+⋯+hiH_i = h_1 + h_2 + \cdots + h_i.

A driver of type jj takes the group from pxp_x to pyp_y (x<yx < y) in one ride only when both conditions hold.

  • Minimum distance: the total distance dx+dx+1+⋯+dy−1d_x + d_{x+1} + \cdots + d_{y-1} is at least dminjdmin_j. A shorter ride is not worth the driver's time.
  • Maximum heading range: the largest of the headings Hx,Hx+1,…,Hy−1H_x, H_{x+1}, \dots, H_{y-1} minus the smallest one is at most aja_j. Drivers find anything other than going straight annoying.

Split the whole itinerary into consecutive groups of segments and call one driver for each group. The same type may be used again, but once a driver has had enough you hail a new one, and that adds another call even when the type is the same. Find the minimum number of calls needed to start at p1p_1 and visit every point up to pnp_n in the given order.

Input

The first line has the number of transportation types tt (1≤t≤2001 \le t \le 200) and the number of points nn (1≤n≤5×1041 \le n \le 5 \times 10^4), separated by a space.

Each of the next tt lines describes one type with two non-negative integers. The first integer dminjdmin_j (0≤dminj≤1060 \le dmin_j \le 10^6) is the minimum distance that type requires, and the second integer aja_j (0≤aj≤3.6×1050 \le a_j \le 3.6 \times 10^5) is the maximum heading range it allows.

Each of the next n−1n - 1 lines has the length did_i (0≤di≤1060 \le d_i \le 10^6) and the turn hih_i (−1.8×105<hi<1.8×105-1.8 \times 10^5 < h_i < 1.8 \times 10^5) of segment ii. There are no such lines when n=1n = 1.

All angles are given in thousandths of a degree.

Output

Print one line with the minimum number of calls needed to visit p1p_1 through pnp_n in the given order. Print IMPOSSIBLE when no such split exists. Print 0 when n=1n = 1, because no travel is needed.

Examples2

  1. Example 1

    Input
    4 4
    100 30000
    200 20000
    300 10000
    400 0
    50 10000
    75 20000
    400 -40000
    
    Expected output
    2
    
  2. Example 2

    Input
    1 3
    20 50000
    100 10000
    10 -60000
    
    Expected output
    IMPOSSIBLE