Painting the Fence
InterviewTime limit1sMemory limit1024 MB
Given two painted segments [A,B] and [C,D] on a fence, find how many boards are painted at least once.
- Level
Easy3 of 10
- Topics
- Intervals, Math, Implementation
- Solved
- No attempts yet
Problem
Petar painted part of a fence, starting from board number A through board number B inclusive. Then he took a new can of paint and painted part of the fence, starting from board number C through board number D inclusive. Some of the fence may be painted a second time. Petar can paint the fence from right to left as well as from left to right.
Write a program painting that finds the total number of painted boards on the fence.
Input
The first line of the standard input contains four integers A, B, C and D, separated by a single space. These are the numbers of the boards between which Petar painted parts of the fence.
Output
The first line of the standard output must contain a single integer: the total number of painted boards.
Constraints
- 1 ≤ A, B, C, D ≤ 1 000 000 000
Hint
Explanation of example 3: The number of painted boards between the first and the fourth is 4, between the second and the sixth it is 5, for a total of 9. But between the second and the fourth, three boards are painted twice. This means we must subtract 3 from the total. Thus the number of painted boards becomes 6.