Algorithm
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!
The first line contains a single integer q">q
(1≤q≤103">1≤q≤103
) — the number of the queries.
Each of the next q">q
lines contains two integers l">l and r">r (1≤l≤r≤109">1≤l≤r≤109
) — the descriptions of the queries.
Print q">q
lines, each containing one number — the answer to the query.
5 1 3 2 5 5 5 4 4 2 3
-2 -2 -5 4 -1
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+2−3=−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=2−3+4−5=−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=2−3=−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
1 3
2 5
5 5
4 4
2 3
Output
-2
-5
4
-1
Demonstration
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution,Codeforces 1080-B soloution, Codeforces Margarite and the best present