Algorithm


B. Margarite and the best present
time limit per test
1 second
memory limit per test
256 megabytes
Problem Link-https://codeforces.com/problemset/problem/1080/B
 
input
standard input
output
standard output

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">a

of the size of 109">109

elements that is filled as follows:

  • a1=−1">a1=1
  • a2=2">a2=2
  • a3=−3">a3=3
  • a4=4">a4=4
  • a5=−5">a5=5
  • And so on ...

That is, the value of the i">i

-th element of the array a">a is calculated using the formula ai=i⋅(−1)i">ai=i(1)i

.

She immediately came up with q">q

queries on this array. Each query is described with two numbers: l">l and r">r. The answer to a query is the sum of all the elements of the array at positions from l">l to r">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">q

(1≤q≤103">1q103

) — the number of the queries.

Each of the next q">q

lines contains two integers l">l and r">r (1≤l≤r≤109">1lr109

) — the descriptions of the queries.

Output

Print q">q

lines, each containing one number — the answer to the query.

Example
Input
Copy
5
1 3
2 5
5 5
4 4
2 3
Output
Copy
-2
-2
-5
4
-1
Note

In the first query, you need to find the sum of the elements of the array from position 1">1

to position 3">3. The sum is equal to a1+a2+a3=−1+2−3=−2">a1+a2+a3=1+23=2

.

In the second query, you need to find the sum of the elements of the array from position 2">2

to position 5">5. The sum is equal to a2+a3+a4+a5=2−3+4−5=−2">a2+a3+a4+a5=23+45=2

.

In the third query, you need to find the sum of the elements of the array from position 5">5

to position 5">5. The sum is equal to a5=−5">a5=5

.

In the fourth query, you need to find the sum of the elements of the array from position 4">4

to position 4">4. The sum is equal to a4=4">a4=4

.

In the fifth query, you need to find the sum of the elements of the array from position 2">2

to position 3">3. The sum is equal to a2+a3=2−3=−1">a2+a3=23=1.

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define endl '\n'
#define debug(n) cout<<(n)<<endl;
const ll INF = 2e18 + 99;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t;
  cin>>t;
  long long l, r;
  while(t--){

    cin>>l>>r;
    long long odd_l, odd_r;
    l--;

    if(l % 2 == 0) {
      odd_l = l/2 * l/2;
    } else{
      odd_l = (l/2 + 1) * (l/2 + 1);
    }

    long long even_l = l/2 * (l/2 + 1);
    if(r % 2 == 0) {
      odd_r = r/2 * r/2;
    } else {
      odd_r = (r/2 + 1) * (r/2 + 1);
    }
    long long even_r = r/2 * (r/2 + 1);

    long long even_s = even_r - even_l;
    long long odd_s = odd_r - odd_l;

    cout<<(even_s - odd_s)<<"\n";
  }
  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
5
1 3
2 5
5 5
4 4
2 3

Output

x
+
cmd
-2
-2
-5
4
-1
Advertisements

Demonstration


Codeforces solution 1080-B-B. Margarite and the best present codeforces solution,Codeforces 1080-B soloution, Codeforces Margarite and the best present

Previous
Codeforces solution 1030-A - In Search of an Easy Problem Codeforces solution
Next
Codeforces Solution-Number of Ways-Solution in C, C++, Java, Python