P28816 [洛谷] [IOI2008] Teleporters
省选
题目描述

Problem Description

You are participating in a race that traverses Egypt along a straight route from west to east. Initially, you are at the westernmost point of this route. According to the race rules, you must always move eastward along this route.

There are $N$ teleporters along this route. Each teleporter has two endpoints. Whenever you reach one of the endpoints of a teleporter, you are immediately transported to the other endpoint of that teleporter (note that depending on your current endpoint, the teleporter can transport you either eastward or westward). After being transported to the other endpoint, you must continue moving eastward along the route; you cannot avoid any teleporter endpoints on your path. No two endpoints are at the same location. All endpoints are strictly between the start and end points of the route.

Each time you are transported, you earn 1 point. The goal of the race is to earn as many points as possible. To maximize your score, you are allowed to add $M$ new teleporters to the route before the race starts. Using these new teleporters, you can also earn points.

You can place the endpoints of these new teleporters at any positions (even non-integer coordinates), as long as these positions do not coincide with any existing endpoints. In other words, all teleporter endpoints must be unique. Similarly, the endpoints of the new teleporters must be strictly between the start and end points of the route.

The problem guarantees that regardless of how you add these teleporters, you will always be able to reach the end of the race route.

Write a program that, given the positions of the $N$ teleporter endpoints and the number of new teleporters $M$ you can add, calculates the maximum score you can achieve.

Input Format

Your program must read the following data from standard input:
- The first line contains an integer $N$, the number of teleporters initially on the route.
- The second line contains an integer $M$, the maximum number of new teleporters you can add.
- The next $N$ lines describe each teleporter. The $i$-th line describes the $i$-th teleporter and contains two integers $W_i$ and $E_i$, which represent the distances from the route start to the two endpoints of the teleporter.

No two endpoints are at the same location. The start of the route is at position $0$, and the end is at position $2,000,001$.

Output Format

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

Sample Input and Output

Sample Input #1

3
1
10 11
1 4
2 3

Sample Output #1

6

Sample Input #2

3 3 5 7 6 10 1999999 2000000

Sample Output #2

12

Notes

Sample 1 Explanation

Diagram

The left diagram shows the initial race route with 3 teleporters. The right diagram shows the same route after adding a new teleporter with endpoints at $0.5$ and $1.5$.

After adding the new teleporter, your race journey is as follows:
- You start at position $0$ and move eastward.
- You reach the teleporter endpoint at $0.5$ and are transported to $1.5$ (earning 1 point).
- You continue eastward and reach the teleporter endpoint at $2$; you are transported to $3$ (earning 2 points).
- You reach the teleporter endpoint at $4$ and are transported to $1$ (earning 3 points).
- You reach the teleporter endpoint at $1.5$ and are transported to $0.5$ (earning 4 points).
- You reach the teleporter endpoint at $1$ and are transported to $4$ (earning 5 points).
- You reach the teleporter endpoint at $10$ and are transported to $11$ (earning 6 points).
- You continue moving until you reach the end of the race, finishing with a total score of 6 points.

Data Range

  • For $30\%$ of the data, $N \leq 500$ and $M \leq 500$.
  • For all data, $1 \leq N \leq 1,000,000$, $1 \leq M \leq 1,000,000$, $1 \leq W_i < E_i \leq 2,000,000$.
输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
3 1 10 11 1 4 2 3
样例输出
6