B. Margarite and the best present
time limit per test
1 second
memory limit per test
256 megabytes
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 aa of the size of 109109 elements that is filled as follows:
![](/qrcode.jpg)
- a1=−1a1=−1
- a2=2a2=2
- a3=−3a3=−3
- a4=4a4=4
- a5=−5a5=−5
- And so on ...
That is, the value of the ii-th element of the array aa is calculated using the formula ai=i⋅(−1)iai=i⋅(−1)i.
She immediately came up with qq queries on this array. Each query is described with two numbers: ll and rr. The answer to a query is the sum of all the elements of the array at positions from ll to rr 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 qq (1≤q≤1031≤q≤103) — the number of the queries.
Each of the next qq lines contains two integers ll and rr (1≤l≤r≤1091≤l≤r≤109) — the descriptions of the queries.
Output
Print qq 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 11 to position 33. The sum is equal to a1+a2+a3=−1+2−3=−2a1+a2+a3=−1+2−3=−2.
In the second query, you need to find the sum of the elements of the array from position 22 to position 55. The sum is equal to a2+a3+a4+a5=2−3+4−5=−2a2+a3+a4+a5=2−3+4−5=−2.
By pxlsdz, contest: Codeforces Round #524 (Div. 2), problem: (B) Margarite and the best present, Accepted, #
#include<cstdio>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<numeric>
#include<cctype>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;
#define N 100000+5
#define rep(i,n) for(int i=0;i<n;i++)
#define sd(n) scanf("%d",&n)
#define sll(n) scanf("%I64d",&n)
#define pd(n) scanf("%d\n",n)
#define pll(n) scanf("%I64d\n",n)
#define MAX 26
typedef long long ll;
const ll mod=1e9+7;
ll n,m;
//ll a[N];
//ll b[N];
int main()
{
//string s;
//cin>>s;
//ll ans=0;
//ll sum=0;
scanf("%I64d",&n);
for(int i=1;i<=n;i++)
{
ll l,r;
scanf("%I64d%I64d",&l,&r);
//cout<<l<<" "<<r<<endl;
if(l%2==0&&r%2==0)
{
printf("%I64d\n",(l+r)/2);
}
else if(l%2==1&&r%2==0)
{
printf("%I64d\n",(r-l+1)/2);
}
else if(l%2==0&&r%2==1)
{
printf("%I64d\n",(-1)*(r-l+1)/2);
}
else if(l%2==1&&r%2==1)
{
ll ans=((r-1)+(l+1))/2;
//cout<<ans<<endl;
ans=ans-l-r;
printf("%I64d\n",ans);
}
}
return 0;
}
In the third query, you need to find the sum of the elements of the array from position 55 to position 55. The sum is equal to a5=−5a5=−5.
In the fourth query, you need to find the sum of the elements of the array from position 44 to position 44. The sum is equal to a4=4a4=4.
In the fifth query, you need to find the sum of the elements of the array from position 22 to position 33. The sum is equal to a2+a3=2−3=−1a2+a3=2−3=−1.