#CT0000066. Phân tầng theo nhiều khoảng - EP1 (Layer Selection with Multiple Intervals - EP1)
Phân tầng theo nhiều khoảng - EP1 (Layer Selection with Multiple Intervals - EP1)
Layer Selection with Multiple Intervals - EP1
Version: Phuoc Hung OJ Extended
Problem Statement
Positions are numbered by the positive integers .
For each positive integer , a position belongs to layer if and only if
Thus every position belongs to exactly one layer.
You are given a position interval and layer intervals . A position is selected if the index of its layer belongs to at least one of the given layer intervals. The intervals may overlap, contain one another, or be adjacent.
Count the selected positions in . Each position is counted once even if its layer is covered by several intervals.
Input
- The first line contains three integers .
- The next lines each contain two integers , describing the -th layer interval.
Output
Print one integer: the number of selected positions .
Subtasks
- Subtask 1 (25 points): and .
- Subtask 2 (30 points): .
- Subtask 3 (45 points): , , .
Examples
Example 1
Input
1 20 2
2 2
3 3
Output
12
Explanation
Layer 2 covers positions 4 through 8 and layer 3 covers positions 9 through 15. Together they form , which contains 12 positions.
Example 2
Input
10 30 2
4 4
2 2
Output
9
Explanation
Layer 2 is and does not intersect . Layer 4 is , so all 9 positions of that layer are counted.
Example 3
Input
1 100 3
2 4
4 6
8 8
Output
62
Explanation
The intervals and overlap, so together they cover layers 2 through 6. These layers correspond to positions , containing 45 positions. Layer 8 corresponds to , containing 17 more positions. The total is .
Example 4
Input
50 70 2
1 3
8 10
Output
7
Explanation
Layers 1 through 3 end before position 50. Layers 8 through 10 start at 64, so only contributes, which contains 7 positions.
Example 5
Input
1000000000000000000 1000000000000000000 1
1000000000 1000000000
Output
1
Explanation
, so the only position in the interval belongs to layer and is counted once.