time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Polycarp lives on the coordinate axis OxOx and travels from the point x=ax=a to x=bx=b. It moves uniformly rectilinearly at a speed of one unit of distance per minute.
On the axis OxOx at the point x=cx=c the base station of the mobile operator is placed. It is known that the radius of its coverage is rr. Thus, if Polycarp is at a distance less than or equal to rr from the point x=cx=c, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.
Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from x=ax=a to x=bx=b. His speed — one unit of distance per minute.
Input
The first line contains a positive integer tt (1≤t≤10001≤t≤1000) — the number of test cases. In the following lines are written tt test cases.
The description of each test case is one line, which contains four integers aa, bb, cc and rr (−108≤a,b,c≤108−108≤a,b,c≤108, 0≤r≤1080≤r≤108) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively.
Any of the numbers aa, bb and cc can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
Output
Print tt numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
Example
input
Copy
9 1 10 7 1 3 3 3 0 8 2 10 4 8 2 10 100 -10 20 -17 2 -3 2 2 0 -3 1 2 0 2 3 2 3 -1 3 -2 2
output
Copy
7 0 4 0 30 5 4 0 3
Note
The following picture illustrates the first test case.
Polycarp goes from 11 to 1010. The yellow area shows the coverage area of the station with a radius of coverage of 11, which is located at the point of 77. The green area shows a part of the path when Polycarp is out of coverage area.
解题说明:此题是一道模拟题,按照题目意思求解即可。
#include
#include
#include
#include
#include
using namespace std;
int main(void)
{
int t, a, b, c, r, sum, temp;
scanf("%d", &t);
while (t--)
{
scanf("%d %d %d %d", &a, &b, &c, &r);
if (a>b)
{
temp = a;
a = b;
b = temp;
}
if (c + r <= a)
{
sum = b - a;
}
else if (c + r >= a && c - r < a)
{
sum = b - c - r;
}
else if (c - r >= a && c <= b - r)
{
sum = b - a - 2 * r;
}
else if (c - r < b&&c + r >= b)
{
sum = c - r - a;
}
else
{
sum = b - a;
}
if (sum < 0)
{
sum = 0;
}
printf("%d\n", sum);
}
return 0;
}