luogu#P4686. [IOI 2008] Teleporters

[IOI 2008] Teleporters

Problem Description

You are taking part in a race that goes from west to east along a straight route across Egypt. At the start, you are at the westernmost end of this straight route. According to the rules, you must always move east along this straight route.

There are NN teleporters on this route. Each teleporter has two endpoints. Whenever you reach one of the two endpoints of a teleporter, it immediately teleports you to the other endpoint (note that depending on which endpoint you are at, the teleporter may send you eastward or westward). After being teleported to the other endpoint, you must continue moving east along the route; you cannot avoid any teleporter endpoints that lie on your path. It is guaranteed that no teleporter has both endpoints at the same position. All endpoints lie strictly between the start and the finish of the route.

Each time you are teleported, you gain 11 point. The goal of the race is to gain as many points as possible. To maximize your score, you are allowed to add up to MM new teleporters on this route before the race starts. You can also gain points by using these new teleporters.

You may place the endpoints of these new teleporters at any positions (even at non-integer coordinates), as long as those positions are not already occupied by another endpoint. In other words, all teleporter endpoint positions must be unique. Likewise, the endpoints of the new teleporters must also lie strictly between the start and the finish of the route.

It is guaranteed that no matter how you add these teleporters, you will always be able to reach the finish of the race route.

Write a program that, given the endpoints of the NN existing teleporters and the number MM of new teleporters you may add, computes the maximum score you can obtain.

Input Format

Your program must read the following data from standard input:

  • Line 11 contains an integer NN, the number of teleporters initially on the route.
  • Line 22 contains an integer MM, the maximum number of new teleporters you may add.
  • The next NN lines each describe one teleporter. Line ii describes the ii-th teleporter and contains two integers WiW_i and EiE_i, which give the distances from the start of the route to the two endpoints of that teleporter.

For these teleporters, no two endpoints are at the same position. The start of the race route is at position 00, and the finish is at position 20000012\,000\,001.

Output Format

Your program must write one line to standard output containing a single integer, the maximum score you can achieve.

3
1
10 11
1 4
2 3
6
3
3
5 7
6 10
1999999 2000000
12

Hint

Explanation for Sample 1

The left figure above shows a race route with 33 teleporters initially. The right figure shows the same route after adding one new teleporter with endpoints at 0.50.5 and 1.51.5.

After adding the new teleporter shown above, your journey is as follows:

  • You start at position 00 and move east.
  • You reach the teleporter endpoint at 0.50.5 and are teleported to the other endpoint at 1.51.5 (you gain 11 point).
  • You continue moving east and reach the teleporter endpoint at 22; you are teleported to the other endpoint at 33 (you now have 22 points in total).
  • You reach the teleporter endpoint at 44 and are teleported to the other endpoint at 11 (you now have 33 points in total).
  • You reach the teleporter endpoint at 1.51.5 and are teleported to the other endpoint at 0.50.5 (you now have 44 points in total).
  • You reach the teleporter endpoint at 11 and are teleported to the other endpoint at 44 (you now have 55 points in total).
  • You reach the teleporter endpoint at 1010 and are teleported to the other endpoint at 1111 (you now have 66 points in total).
  • You continue until you reach the finish of the race, ending with a total score of 66 points.

Constraints

  • For 30%30\% of the testdata, N500N \leq 500 and M500M \leq 500.
  • For all testdata, 1N1,000,0001 \leq N \leq 1,000,000, 1M1,000,0001 \leq M \leq 1,000,000, 1WX<EX2,000,0001 \leq W_X < E_X \leq 2,000,000.

Translated by ChatGPT 5