Margarite and the best present
Contest: Codeforces Round 524 (Div. 2)
Difficulty rating: 900
Tags: math

Little girl Margarita is a big fan of competitive programming. She especially loves problems about arrays and queries on them.

Recently, she was presented with an array $$$a$$$ of the size of $$$10^9$$$ elements that is filled as follows:

- $$$a_1 = -1$$$
- $$$a_2 = 2$$$
- $$$a_3 = -3$$$
- $$$a_4 = 4$$$
- $$$a_5 = -5$$$
- And so on ...

That is, the value of the $$$i$$$-th element of the array $$$a$$$ is calculated using the formula $$$a_i = i \cdot (-1)^i$$$.

She immediately came up with $$$q$$$ queries on this array. Each query is described with two numbers: $$$l$$$ and $$$r$$$. The answer to a query is the sum of all the elements of the array at positions from $$$l$$$ to $$$r$$$ inclusive.

Margarita really wants to know the answer to each of the requests. She doesn't want to count all this manually, but unfortunately, she couldn't write the program that solves the problem either. She has turned to you — the best programmer.

Help her find the answers!

Input format:
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^3$$$) — the number of the queries.

Each of the next $$$q$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 10^9$$$) — the descriptions of the queries.

Output format:
Print $$$q$$$ lines, each containing one number — the answer to the query.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first query, you need to find the sum of the elements of the array from position $$$1$$$ to position $$$3$$$. The sum is equal to $$$a_1 + a_2 + a_3 = -1 + 2 -3 = -2$$$.

In the second query, you need to find the sum of the elements of the array from position $$$2$$$ to position $$$5$$$. The sum is equal to $$$a_2 + a_3 + a_4 + a_5 = 2 -3 + 4 - 5 = -2$$$.

In the third query, you need to find the sum of the elements of the array from position $$$5$$$ to position $$$5$$$. The sum is equal to $$$a_5 = -5$$$.

In the fourth query, you need to find the sum of the elements of the array from position $$$4$$$ to position $$$4$$$. The sum is equal to $$$a_4 = 4$$$.

In the fifth query, you need to find the sum of the elements of the array from position $$$2$$$ to position $$$3$$$. The sum is equal to $$$a_2 + a_3 = 2 - 3 = -1$$$.

Example input:
5
1 3
2 5
5 5
4 4
2 3

Example output:
-2
-2
-5
4
-1

Editorial:
At first let's simplify the problem. Let's denote as $$$f(x)$$$ function that returns sum of the elements of the array that have indices from $$$1$$$ to $$$x$$$ inclusive.
In order to calculate the function in a fast and easy way let's split the task into two parts:
1. If $$$x$$$ is even, then the sum is equal to:$$$$$$f(x) = -1 + 2 - 3 + 4 - \cdots - (x - 1) + x$$$$$$$$$$$$f(x) = (-1 + 2) + (-3 + 4) + \cdots + (-(x - 1) + x)$$$$$$Since the number $$$x$$$ is even the number of such pairs is equal to $$$\frac{x}{2}$$$. Since the sum of the elements of each pair is equal to $$$1$$$, than $$$$$$f(x) = \frac{x}{2}$$$$$$
2. If $$$x$$$ is odd, than the sum is equal to:$$$$$$f(x) = -1 + 2 - 3 + 4 - \cdots - (x-2) + (x-1) - x$$$$$$$$$$$$f(x) = f(x-1) - x$$$$$$Since $$$x$$$ is an odd number than $$$x - 1$$$ is an even number. And we know how to solve the problem for even numbers.
But how do we calculate the sum of the elements on an arbitrary segment?
Let's show that the sum of the elements of the array with indices from $$$l$$$ to $$$r$$$ inclusive is equal to $$$f(r) - f(l-1)$$$.
$$$$$$f(r) = a_1 + a_2 + a_3 + \cdots + a_r$$$$$$ $$$$$$f(l-1) = a_1 + a_2 + a_3 + \cdots + a_{l-1}$$$$$$ $$$$$$f(r) - f(l-1) = a_1 - a_1 + a_2 - a_2 + a_3 - a_3 + \cdots + a_{l-1} - a_{l-1} + a{l} + a_{l+1} + \cdots + a_r$$$$$$ $$$$$$f(r) - f(l-1) = 0 + 0 + \cdots\ + 0 + a_l + a_{l+1} + \cdots + a_r$$$$$$ $$$$$$f(r) - f(l-1) = a_l + a_{l+1} + \cdots + a_r$$$$$$
Overall complexity $$$O(1)$$$.