This page is still under construction.

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

Hurry Plotter

Time limit1sMemory limit128 MB

Summary
Maximize the number of horizontal segments a sweeping plotter draws within a time limit, where drawn moves cost double and the final row skips the return trip.
Level

Medium7 of 10

Topics
Dynamic programming, Sorting, Greedy
Solved
No attempts yet

Problem

A plotter is a vector graphics printing device that a computer drives to print plots. There are two kinds of plotters, pen plotters and electrostatic plotters. A pen plotter prints by moving a pen across the surface of the paper. It can draw complex line art, text included, but the mechanical movement of the pen makes it very slow. This problem is about that slowness.

A discrete horizontal pen plotter draws only horizontal segments whose endpoints have integer coordinates. Its drawing method is simple. The pen starts at the upper left corner of the page (x=y=0x = y = 0) and moves right, drawing the segments it was told to draw on that row. Then it moves all the way back to the left, goes down one row (y←y+1y \leftarrow y + 1), and repeats the same work on the next row. In other words, the pen can go down only while it sits at the far left (x=0x = 0), and on each row it makes at most one left to right pass and at most one right to left pass.

Moving the pen one unit of length to the left (x←x−1x \leftarrow x - 1) or to the right (x←x+1x \leftarrow x + 1) takes one unit of time. That time doubles while the pen is on the paper drawing a segment. Moving one row down at x=0x = 0 takes no time. The plotter stops the moment it finishes the last segment it draws, so on its final drawing row it does not pay for the trip back to x=0x = 0.

Drawing every segment can take a long time, so the plotter now accepts a drawing time limit. Within that limit it should draw as many segments as it can, still following the drawing method above. Given the time limit and the segments, find that maximum number.

Input

The input contains several test cases. Each test case starts with a line holding two integers nn and tt. The integer nn is the number of segments (n≤1000n \le 1000) and tt is the time limit (t≤106t \le 10^6). Each of the next nn lines describes one segment with three integers yy, xsx_s and xtx_t. The integer yy is the row of that segment (0≤y≤20000 \le y \le 2000), and xsx_s and xtx_t are the xx coordinates of its endpoints (0≤xs≤xt≤1060 \le x_s \le x_t \le 10^6). The segments are disjoint and never intersect. A line with n=t=0n = t = 0 ends the input and is not a test case.

Output

Write the answer for the iith test case on the iith line of the output. Each line holds one integer, the largest number of segments the plotter can draw within that test case's time limit.

Examples1

  1. Example 1

    Input
    1 3
    0 1 2
    3 5
    1 1 2
    3 1 3
    1 3 4
    3 6
    1 1 2
    3 1 3
    1 3 4
    4 11
    1 3 4
    1 1 2
    2 1 2
    2 3 4
    0 0
    
    Expected output
    1
    1
    2
    3