The values , and lie in the range from to . We'll iterate over all possible unit segments from to and count how many of them lie completely within the interval or the interval .
Example
Consider the fence painting shown in the example below.
The paint covers units of the fence, from x = 4 to x = 10.
Algorithm implementation
Read the input data.
scanf("%d %d", &a, &b);
scanf("%d %d", &c, &d);Iterate over the coordinates from to . In the variable , count the number of coordinates that belong to at least one of the intervals and .
res = 0;
for (i = 0; i <= 100; i++)
if ((a <= i && i < b) || (c <= i && i < d)) res++;Print the answer.
printf("%d\n", res);