Margarite and the best present (Codeforces 1080B)
Tags: Math, 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: 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: Print $$$q$$$ lines, each containing one number — the answer to the query. 

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$$$.

Sample Inputs:
5
1 3
2 5
5 5
4 4
2 3

Sample Outputs:
-2
-2
-5
4
-1

Solution (Python):
q = input()
q = int(q)

def calculate(num):
    if num % 2 == 0:
        return num // 2
    else:
        return (num - 1) // 2 - num
for i in range(q):
    (l, r) = input().split()
    (l, r) = (int(l), int(r))
    print(calculate(r) - calculate(l - 1))

Solution (Python):
for _ in range(int(input())):
    (l, r) = [*map(int, input().split())]
    if l % 2 == r % 2 == 0:
        print(r * (-1) ** r + (l - r) // 2)
    elif l % 2 == r % 2 == 1:
        print(r * (-1) ** r + (r - l) // 2)
    elif l % 2 == 1 and r % 2 == 0:
        print((r - l + 1) // 2)
    else:
        print(-1 * (r - l + 1) // 2)

Solution (Python):
q = int(input())
for i in range(q):
    (l, r) = map(int, input().split())
    if l % 2 == 0:
        b = l
        a = l + 1
    else:
        a = l
        b = l + 1
    if r % 2 == 0:
        d = r
        c = r - 1
    else:
        c = r
        d = r - 1
    l1 = (c - a) // 2 + 1
    l2 = (d - b) // 2 + 1
    print(int((d + b) * l2 // 2 - (a + c) * l1 // 2))