Passports
Contest: Codeforces Round 500 (Div. 1) [based on EJOI]
Difficulty rating: 3400
Tags: dp, implementation

Gleb is a famous competitive programming teacher from Innopolis. He is planning a trip to N programming camps in the nearest future. Each camp will be held in a different country. For each of them, Gleb needs to apply for a visa.

For each of these trips Gleb knows three integers: the number of the first day of the trip si, the length of the trip in days leni, and the number of days ti this country's consulate will take to process a visa application and stick a visa in a passport. Gleb has P (1 ≤ P ≤ 2) valid passports and is able to decide which visa he wants to put in which passport.

For each trip, Gleb will have a flight to that country early in the morning of the day si and will return back late in the evening of the day si + leni - 1.

To apply for a visa on the day d, Gleb needs to be in Innopolis in the middle of this day. So he can't apply for a visa while he is on a trip, including the first and the last days. If a trip starts the next day after the end of the other one, Gleb can't apply for a visa between them as well. The earliest Gleb can apply for a visa is day 1.

After applying for a visa of country i on day d, Gleb will get his passport back in the middle of the day d + ti. Consulates use delivery services, so Gleb can get his passport back even if he is not in Innopolis on this day. Gleb can apply for another visa on the same day he received his passport back, if he is in Innopolis this day.

Gleb will not be able to start his trip on day si if he doesn't has a passport with a visa for the corresponding country in the morning of day si. In particular, the passport should not be in another country's consulate for visa processing.

Help Gleb to decide which visas he needs to receive in which passport, and when he should apply for each visa.

Input format:
In the first line of the input there are two integers N (1 ≤ N ≤ 22) and P (1 ≤ P ≤ 2)—the number of trips and the number of passports Gleb has, respectively.

The next N lines describe Gleb's trips. Each line contains three positive integers si, leni, ti (1 ≤ si, leni, ti ≤ 109)—the first day of the trip, the length of the trip and number of days the consulate of this country needs to process a visa application. It is guaranteed that no two trips intersect.

Output format:
If it is impossible to get all visas on time, just print "NO" (quotes for clarity). Otherwise, print "YES" and N lines describing trips. For each trip, first print number of the passport Gleb should put this country's visa in, and then print number of the day he should apply for it. Print trips in the same order as they appear in the input. Days are numbered from 1, starting with tomorrow—the first day you can apply for a visa. Passports are numbered from 1 to P.

If there are several possible answers, print any of them.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

Note:
Examples with answer "YES" are depicted below.

Each cell of the stripe represents a single day. Rectangles represent trips, each trip starts in the morning and ends in the evening. Rectangles with angled corners represent visa applications. Each application starts in the middle of a day and ends ti days after. The trip and the visa application for the same country have the same color.

In examples with two passports, visa applications and trips depicted above the time stripe are made using the first passport, visa applications and trips depicted below the time stripe are made using the second passport.

Example 1:

Example 2:

Example 3:

Example input:
2 1
3 1 1
6 1 1

Example output:
YES
1 1
1 4

Example input:
3 1
13 2 2
7 3 1
19 3 4

Example output:
YES
1 10
1 1
1 2

Editorial:
Let's solve the P = 1 case first. We'll use dynamic programming on subsets. Let's try to add visas to subset in order of application. Notice that if we only have one passport, every visa processing segment should lie between some two consecutive trips. For convenience, let's find all these segments beforehand.
Define dp[A] as the minimum day, before which all visas from set A can be acquired. Try all possible i as a visa which Gleb will apply for next. Now we have to find minimum possible day of application d, such that d ≥ dp[A], segment [d, d + ti] does not intersect with any trip, and d + ti < si. Such d can be found in O(n) by a linear search over precalculated free segment. Relax the value of $$dp\left[A \cup \{i\}\right]$$ with d + ti. If dp[{1..n}] <  + ∞ then there is a solution that can be restored using standard techniques. Total time complexity is O(2n n2), that can be too slow for n = 22.
Let's generalize this solution for P = 2. Still, Gleb can apply for a visa only when he is in Innopolis. However, the last day of visa processing can be during some trip, but only if all trips between the day of visa application and the day of visa acquisition use another passport. We will slightly change the definition of dp[A]: now this value is equal to the minimum possible day, by which it's possible to finish processing all visas from set A with one passport, assuming all trips not from A use another passport. By this definition, calculation of DP transition is a little bit different: when trying to add visa i we have to find minimum d, such that d ≥ dp[A], during day d Gleb is in Innopolis, and segment [d, d + ti] does not intersect with half-closed intervals [sj, sj + lenj) for all $$j \in A$$. This can be implemented similarly to the first part in O(n). Total running time is O(2n n2), that can pass system tests with some optimizations.
We'll optimize the solution down to O(2n n). To do that, we process all transition from set A in O(n) total time. Sort all visas in order of increasing ti. Then the optimal visa application day d will be increasing if visa processing time ti increases. Now we can apply two pointers technique to get O(n) total processing time for one set and O(2n n) for all sets.
After calculating dp[A] for all subsets, we have to try all partitions of {1..n} into two sets A and B and check if both A and B can be done with one passport each. This is equivalent to dp[A] <  + ∞. If there are two such sets that dp[A] <  + ∞ and dp[B] <  + ∞, then we have found the answer, otherwise there is no answer.